# Proto compilation for smartbotic-database # Get the protobuf compiler and gRPC plugin find_program(PROTOC protoc REQUIRED) find_program(GRPC_CPP_PLUGIN grpc_cpp_plugin REQUIRED) # Get the include directory for well-known types get_target_property(PROTOBUF_INCLUDE_DIR protobuf::libprotobuf INTERFACE_INCLUDE_DIRECTORIES) if(NOT PROTOBUF_INCLUDE_DIR) # Fallback for system protobuf set(PROTOBUF_INCLUDE_DIR "/usr/include") endif() # Create output directory set(PROTO_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") file(MAKE_DIRECTORY ${PROTO_OUTPUT_DIR}) # Proto file set(PROTO_FILE "${CMAKE_CURRENT_SOURCE_DIR}/database.proto") # Generated files set(PROTO_SRCS "${PROTO_OUTPUT_DIR}/database.pb.cc") set(PROTO_HDRS "${PROTO_OUTPUT_DIR}/database.pb.h") set(GRPC_SRCS "${PROTO_OUTPUT_DIR}/database.grpc.pb.cc") set(GRPC_HDRS "${PROTO_OUTPUT_DIR}/database.grpc.pb.h") # Generate proto and gRPC files add_custom_command( OUTPUT ${PROTO_SRCS} ${PROTO_HDRS} ${GRPC_SRCS} ${GRPC_HDRS} COMMAND ${PROTOC} --proto_path=${CMAKE_CURRENT_SOURCE_DIR} --proto_path=${PROTOBUF_INCLUDE_DIR} --cpp_out=${PROTO_OUTPUT_DIR} --grpc_out=${PROTO_OUTPUT_DIR} --plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN} ${PROTO_FILE} DEPENDS ${PROTO_FILE} COMMENT "Generating protobuf/gRPC code for database.proto" VERBATIM ) # Create proto library if(BUILD_SHARED_LIBS) add_library(smartbotic_db_proto OBJECT ${PROTO_SRCS} ${GRPC_SRCS}) set_target_properties(smartbotic_db_proto PROPERTIES POSITION_INDEPENDENT_CODE ON) else() add_library(smartbotic_db_proto STATIC ${PROTO_SRCS} ${GRPC_SRCS}) endif() target_include_directories(smartbotic_db_proto PUBLIC $ $ ) # Suppress warnings in generated code target_compile_options(smartbotic_db_proto PRIVATE -Wno-unused-parameter -Wno-array-bounds ) # Link dependencies if(TARGET gRPC::grpc++) target_link_libraries(smartbotic_db_proto PUBLIC protobuf::libprotobuf gRPC::grpc++ absl::log_internal_check_op ) else() target_link_libraries(smartbotic_db_proto PUBLIC protobuf ${GRPC_LIBRARIES} ) target_include_directories(smartbotic_db_proto PUBLIC ${GRPC_INCLUDE_DIRS}) endif() # Export for other targets add_library(smartbotic::db_proto ALIAS smartbotic_db_proto)