Last active
May 12, 2019 23:15
-
-
Save baskeboler/4b236243a6583471a5eece96e8d880ea to your computer and use it in GitHub Desktop.
generate c++ grpc stubs and servers from protobuf v3 definitions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
find_package(Protobuf 3.2 REQUIRED) | |
if ( NOT (PROTOBUF_FOUND)) | |
message(FATAL_ERROR "Please Compile and Install ProtoBuffer -version 3 using GRPC C++: CMake will Exit") | |
endif() | |
MESSAGE(${PROTOBUF_PROTOC_EXECUTABLE}) | |
include_directories(${Protobuf_INCLUDE}) | |
enable_testing() | |
get_filename_component(PROTO_PATH ${CMAKE_SOURCE_DIR} DIRECTORY) | |
set(PROTO_PATH ${PROTO_PATH}/karaoke-midipp/interfaces) | |
message(${CMAKE_SOURCE_DIR}) | |
set(GENERATED_PROTOBUF_PATH ${CMAKE_SOURCE_DIR}/generated) | |
set(GENERATED_PROTOBUF_PATH "${CMAKE_SOURCE_DIR}/generated") | |
execute_process(COMMAND which grpc_cpp_plugin OUTPUT_VARIABLE GRPC_CPP_PLUGIN) | |
string(STRIP ${GRPC_CPP_PLUGIN} GRPC_CPP_PLUGIN) | |
set(PROTO_FILE_LIST ${PROTO_PATH} ) | |
foreach(loop_var RANGE 0) | |
list(GET PROTO_LIST ${loop_var} PROTO_FILE_NAME) | |
list(GET PROTO_FILE_LIST ${loop_var} PROTO_PATH) | |
message("file name " ${PROTO_FILE_NAME}) | |
message("file path " ${PROTO_PATH}) | |
add_custom_command( | |
OUTPUT "${PROTO_FILE_NAME}.pb.cc" | |
"${PROTO_FILE_NAME}.pb.h" | |
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} | |
ARGS "--proto_path=${PROTO_PATH}" | |
"--cpp_out=${GENERATED_PROTOBUF_PATH}" | |
"${PROTO_PATH}/${PROTO_FILE_NAME}.proto" | |
) | |
list(APPEND GENERATED_FILES | |
"${GENERATED_PROTOBUF_PATH}/${PROTO_FILE_NAME}.pb.cc" | |
) | |
list(APPEND OUT_FILES ${PROTO_FILE_NAME}.pb.cc ${PROTO_FILE_NAME}.pb.h) | |
endforeach(loop_var) | |
foreach(loop_var RANGE 0) | |
list(GET PROTO_LIST ${loop_var} PROTO_FILE_NAME) | |
list(GET PROTO_FILE_LIST ${loop_var} PROTO_PATH) | |
message("file name " ${PROTO_FILE_NAME}) | |
message("file path " ${PROTO_PATH}) | |
add_custom_command( | |
OUTPUT "${PROTO_FILE_NAME}.grpc.pb.h" | |
"${PROTO_FILE_NAME}.grpc.pb.cc" | |
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} | |
ARGS "--proto_path=${PROTO_PATH}" | |
"--grpc_out=${GENERATED_PROTOBUF_PATH}" | |
"--plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN}" | |
"${PROTO_PATH}/${PROTO_FILE_NAME}.proto" | |
) | |
list(APPEND GENERATED_FILES | |
"${GENERATED_PROTOBUF_PATH}/${PROTO_FILE_NAME}.grpc.pb.cc" | |
) | |
list(APPEND OUT_FILES ${PROTO_FILE_NAME}.grpc.pb.cc ${PROTO_FILE_NAME}.grpc.pb.h) | |
endforeach(loop_var) | |
add_custom_target(grpc_generated_services ALL DEPENDS ${OUT_FILES}) | |
include_directories(${GENERATED_PROTOBUF_PATH}) | |
include_directories(${Protobuf_INCLUDE}) | |
link_libraries(grpc++_unsecure grpc protobuf pthread ) | |
set_source_files_properties(${GENERATED_FILES} | |
PROPERTIES GENERATED TRUE) | |
link_libraries(grpc++_unsecure grpc protobuf pthread ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment