You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.7 KiB
CMake
84 lines
2.7 KiB
CMake
cmake_minimum_required(VERSION 3.10)
|
|
project(SimpleRPCExample CXX)
|
|
|
|
set(CMAKE_CXX_STANDARD 17)
|
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|
|
|
# include source includes
|
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
|
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
|
|
|
|
# find python interpreter
|
|
find_package(Python3 COMPONENTS Interpreter REQUIRED)
|
|
|
|
set(GENERATED_DIR ${CMAKE_BINARY_DIR}/generated)
|
|
file(MAKE_DIRECTORY ${GENERATED_DIR})
|
|
|
|
set(RPC_GENERATOR ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_rpc.py)
|
|
set(RPC_TEMPLATES ${CMAKE_CURRENT_SOURCE_DIR}/tools/templates)
|
|
|
|
# helper to generate RPC code for an annotated header/source pair
|
|
function(autocode_annotated_file OUT_BASENAME HEADER SOURCE)
|
|
set(out_proxy_h "${GENERATED_DIR}/${OUT_BASENAME}.proxy.h")
|
|
set(out_proxy_cpp "${GENERATED_DIR}/${OUT_BASENAME}.proxy.cpp")
|
|
set(out_skeleton_h "${GENERATED_DIR}/${OUT_BASENAME}.skeleton.h")
|
|
set(out_skeleton_cpp "${GENERATED_DIR}/${OUT_BASENAME}.skeleton.cpp")
|
|
|
|
add_custom_command(
|
|
OUTPUT
|
|
${out_proxy_h}
|
|
${out_proxy_cpp}
|
|
${out_skeleton_h}
|
|
${out_skeleton_cpp}
|
|
COMMAND ${CMAKE_COMMAND} -E echo "Generating RPC stubs for ${OUT_BASENAME}..."
|
|
COMMAND ${Python3_EXECUTABLE} ${RPC_GENERATOR} --out-dir ${GENERATED_DIR}
|
|
--compile-commands ${CMAKE_BINARY_DIR}/compile_commands.json
|
|
--templates ${RPC_TEMPLATES}
|
|
--header ${HEADER}
|
|
--source ${SOURCE}
|
|
--out-base ${OUT_BASENAME}
|
|
DEPENDS ${RPC_GENERATOR} ${RPC_TEMPLATES}/* ${HEADER} ${SOURCE}
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
|
COMMENT "Running RPC code generator for ${OUT_BASENAME}"
|
|
VERBATIM
|
|
)
|
|
|
|
# accumulate all generated files into a global property so we can
|
|
# have one umbrella target that depends on all of them
|
|
set_property(GLOBAL APPEND PROPERTY AUTOCODE_GENERATED_FILES
|
|
${out_proxy_h}
|
|
${out_proxy_cpp}
|
|
${out_skeleton_h}
|
|
${out_skeleton_cpp}
|
|
)
|
|
endfunction()
|
|
|
|
include_directories(${GENERATED_DIR})
|
|
|
|
# declare all annotated files here
|
|
autocode_annotated_file(MyService
|
|
${CMAKE_CURRENT_SOURCE_DIR}/src/MyService.h
|
|
${CMAKE_CURRENT_SOURCE_DIR}/src/MyService.cpp
|
|
)
|
|
|
|
# umbrella target that depends on all generated RPC files
|
|
get_property(_all_generated GLOBAL PROPERTY AUTOCODE_GENERATED_FILES)
|
|
add_custom_target(rpc_generated DEPENDS ${_all_generated})
|
|
|
|
# Server
|
|
add_executable(server
|
|
src/server.cpp
|
|
src/MyService.cpp
|
|
${GENERATED_DIR}/MyService.skeleton.cpp
|
|
)
|
|
|
|
add_dependencies(server rpc_generated)
|
|
|
|
# Client
|
|
add_executable(client
|
|
src/client.cpp
|
|
${GENERATED_DIR}/MyService.proxy.cpp
|
|
)
|
|
|
|
add_dependencies(client rpc_generated)
|