diff --git a/native/cpp/CommonCpp/grpc/CMakeLists.txt b/native/cpp/CommonCpp/grpc/CMakeLists.txt --- a/native/cpp/CommonCpp/grpc/CMakeLists.txt +++ b/native/cpp/CommonCpp/grpc/CMakeLists.txt @@ -4,27 +4,61 @@ include(GNUInstallDirs) find_package(protobuf REQUIRED) find_package(gRPC REQUIRED) - +# TODO: remove once native modules can be +# imported in CommonCpp/CMakeLists.txt +find_package(Folly REQUIRED) + +set(CMAKE_CXX_STANDARD 14) +# Allow for tools on PATH to be found +find_program(_PROTOBUF_PROTOC protoc + HINTS "${_PROTOBUF_PROTOC}") +find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin + HINTS "${_GRPC_CPP_PLUGIN_EXECUTABLE}") + +set(_proto_dir "${CMAKE_CURRENT_SOURCE_DIR}/protos") set(_components backup blob tunnelbroker) set(TARGETS) + # Iterate through each protobuf file # and create headers, sources, and export as component library foreach(component ${_components}) set(LIB_NAME comm-${component}-grpc) set(TARGETS ${TARGETS} ${LIB_NAME}) - set(PROTO_HDRS - ${CMAKE_CURRENT_SOURCE_DIR}/_generated/${component}.pb.h - ${CMAKE_CURRENT_SOURCE_DIR}/_generated/${component}.grpc.pb.h + set(BIN_PROTO_HDRS + ${component}.pb.h + ${component}.grpc.pb.h + ) + + set(BIN_PROTO_SRCS + ${component}.pb.cc + ${component}.grpc.pb.cc ) - set(PROTO_SRCS - ${CMAKE_CURRENT_SOURCE_DIR}/_generated/${component}.pb.cc - ${CMAKE_CURRENT_SOURCE_DIR}/_generated/${component}.grpc.pb.cc + set(_proto_file "${_proto_dir}/${component}.proto") + # Generate headers from protobuf files, and copy them to _generated dir + add_custom_command( + OUTPUT ${BIN_PROTO_HDRS} ${BIN_PROTO_SRCS} + COMMAND ${_PROTOBUF_PROTOC} --grpc_out "${CMAKE_CURRENT_BINARY_DIR}" + --cpp_out "${CMAKE_CURRENT_BINARY_DIR}" + -I "${_proto_dir}" + --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}" + "${_proto_file}" + COMMAND sed 's|^// Genera|// @genera|g' ${component}.grpc.pb.h + > ${CMAKE_CURRENT_SOURCE_DIR}/_generated/${component}.grpc.pb.h + COMMAND sed 's|^// Genera|// @genera|g' ${component}.grpc.pb.cc + > ${CMAKE_CURRENT_SOURCE_DIR}/_generated/${component}.grpc.pb.cc + COMMAND sed 's|^// Genera|// @genera|g' ${component}.pb.h + > ${CMAKE_CURRENT_SOURCE_DIR}/_generated/${component}.pb.h + COMMAND sed 's|^// Genera|// @genera|g' ${component}.pb.cc + > ${CMAKE_CURRENT_SOURCE_DIR}/_generated/${component}.pb.cc + DEPENDS "${_proto_file}" + MAIN_DEPENDENCY "${_proto_file}" + COMMENT "Generate protobuf files for ${component}" ) add_library(${LIB_NAME} - ${PROTO_HDRS} ${PROTO_SRCS} + ${BIN_PROTO_HDRS} ${BIN_PROTO_SRCS} ) target_link_libraries(${LIB_NAME} @@ -36,7 +70,7 @@ # use installation path when installing target_include_directories(${LIB_NAME} PUBLIC - $ + $ $ $ ) @@ -72,6 +106,9 @@ gRPC::grpc++ protobuf::libprotobuf comm-tunnelbroker-grpc + # TODO: remove once native modules can be + # imported in CommonCpp/CMakeLists.txt + Folly::folly ) target_include_directories(comm-client