gRPC-demo/buffers/CMakeLists.txt
2025-01-14 14:50:41 +01:00

77 lines
2.3 KiB
CMake

# Generate the source files from the protobuf files.
# These will be bundled into a statically linked library and
# linked into the applications.
# library name
set(PROTOBUFFER_LIB "grpc_proto_buffers")
set(PROTOBUFFER_LIB ${PROTOBUFFER_LIB} PARENT_SCOPE)
# protoc needs the folder in which all the buffer files reside
# in case there is an import
set(protobuf_src_folder ".")
cmake_path(ABSOLUTE_PATH protobuf_src_folder)
# Grab all the protobuffer files
file(
GLOB BUFFER_SRCS
"*.proto"
)
# Initialize the lists of generated files
set(proto_srcs)
set(proto_hdrs)
set(grpc_srcs)
set(grpc_hdrs)
# Generate all headers and buffer sources
foreach(buffer_src ${BUFFER_SRCS})
# Get the name stem of the file
cmake_path(GET buffer_src STEM buffer_name)
# Set the names for all generated files
set(buffer_proto_src "${CMAKE_CURRENT_BINARY_DIR}/${buffer_name}.pb.cc")
set(buffer_proto_hdr "${CMAKE_CURRENT_BINARY_DIR}/${buffer_name}.pb.h")
set(buffer_grpc_src "${CMAKE_CURRENT_BINARY_DIR}/${buffer_name}.grpc.pb.cc")
set(buffer_grpc_hdr "${CMAKE_CURRENT_BINARY_DIR}/${buffer_name}.grpc.pb.h")
# Generate
add_custom_command(
OUTPUT "${buffer_proto_src}" "${buffer_proto_hdr}" "${buffer_grpc_src}" "${buffer_grpc_hdr}"
COMMAND ${_PROTOBUF_PROTOC}
ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
--cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
-I "${protobuf_src_folder}"
--plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
"${buffer_src}"
DEPENDS "${buffer_src}"
)
# Add the files to the list of generated files
list(APPEND proto_srcs ${buffer_proto_src})
list(APPEND proto_hdrs ${buffer_proto_hdr})
list(APPEND grpc_srcs ${buffer_grpc_src})
list(APPEND grpc_hdrs ${buffer_grpc_hdr})
endforeach(buffer_src)
# Include the generated headers
include_directories(${CMAKE_CURRENT_BINARY_DIR})
# Build the library
add_library(
${PROTOBUFFER_LIB}
${proto_srcs}
${proto_hdrs}
${grpc_srcs}
${grpc_hdrs}
)
target_link_libraries(
${PROTOBUFFER_LIB}
absl::check
${_GRPC_GRPCPP_REFLECTION}
${_GRPC_GRPCPP}
${_PROTOBUF_LIBPROTOBUF}
)
# Make the headers available to other parts of the program
set(PROTOBUFFER_HDRS_DIR "${CMAKE_CURRENT_BINARY_DIR}" PARENT_SCOPE)