###############################################################################
# RPC library
#
# Provides interfaces to some RPC services.
###############################################################################
add_library(rpc_objs OBJECT
    src/helpers/Send.cpp
    src/rpc/Dispensary.cpp
    # file io
    src/file/Connection.cpp
    src/file/Open.cpp
    src/file/Read.cpp
    # task creation
    src/task/Connection.cpp
    src/task/Create.cpp
)

# specify dependent libraries
target_include_directories(rpc_objs PRIVATE rpc)
target_link_libraries(rpc_objs PRIVATE mpack)
# allow the library to have link time optimization
target_compile_options(rpc_objs PRIVATE -fPIC -flto -fno-exceptions -fno-rtti -fstack-protector-all)
# specify the include directories
target_include_directories(rpc_objs PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src)
target_include_directories(rpc_objs PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)

# set up the static and dynamic targets
add_library(rpc_static STATIC $<TARGET_OBJECTS:rpc_objs>)
set_target_properties(rpc_static PROPERTIES OUTPUT_NAME "rpc")
target_include_directories(rpc_static PUBLIC include)
target_link_libraries(rpc_static PUBLIC system_static mpack_static)

add_library(rpc_dynamic SHARED $<TARGET_OBJECTS:rpc_objs>)
set_target_properties(rpc_dynamic PROPERTIES OUTPUT_NAME "rpc")
target_include_directories(rpc_dynamic PUBLIC include)
target_link_libraries(rpc_dynamic PUBLIC system mpack_dynamic)

# install the library to the sysroot
install(TARGETS rpc_dynamic LIBRARY)
#add_library(rpc ALIAS rpc_dynamic)

add_custom_command(TARGET rpc_dynamic POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/librpc.so ${SYSROOT_DIR}/boot/lib/librpc.so)

# copy the rpc headers
FILE(GLOB LIBRPC_RPC_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/include/rpc/*.h*")
install(FILES ${LIBRPC_RPC_HEADERS} DESTINATION ${SYSROOT_DIR}/usr/include/rpc)
# and the runtime support (for autogenerated RPC)
FILE(GLOB LIBRPC_RPC_RT_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/include/rpc/rt/*.h*")
install(FILES ${LIBRPC_RPC_RT_HEADERS} DESTINATION ${SYSROOT_DIR}/usr/include/rpc/rt)

