2222
2323cmake_minimum_required (VERSION 3.9 )
2424cmake_policy (SET CMP0028 NEW )
25+ cmake_policy (SET CMP0074 NEW )
26+ cmake_policy (SET CMP0077 NEW )
2527
2628### HACK
2729# lib is lib64 on CC7, but we want lib to be lib.
@@ -150,9 +152,18 @@ if (${FairMQ_FOUND})
150152 endif ()
151153 find_package (Boost ${FairMQ_Boost_VERSION} REQUIRED COMPONENTS ${FairMQ_Boost_COMPONENTS} )
152154endif ()
153- find_package (Protobuf 3.5.0 REQUIRED )
154- find_package (GRPC 1.9.1 REQUIRED )
155155
156+ # Protobuf
157+ set (protobuf_MODULE_COMPATIBLE TRUE )
158+ find_package (protobuf 3.7.1 CONFIG REQUIRED )
159+ message (STATUS "Using protobuf ${protobuf_VERSION} " )
160+
161+ # gRPC
162+ find_package (gRPC 1.19.1 CONFIG REQUIRED )
163+ message (STATUS "Using gRPC ${gRPC_VERSION} " )
164+
165+ # gRPC C++ plugin
166+ set (gRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE :gRPC ::grpc_cpp_plugin >)
156167
157168###
158169### Status messages for build options
@@ -164,6 +175,7 @@ else()
164175 message (STATUS "Code examples will not be built (BUILD_EXAMPLES=OFF)" )
165176endif ()
166177
178+
167179###
168180### Protobuf + gRPC
169181###
@@ -179,7 +191,76 @@ file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/protos)
179191set (CMAKE_CURRENT_BINARY_DIR_OLD ${CMAKE_CURRENT_BINARY_DIR} )
180192set (CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} /protos)
181193
182- protobuf_generate_cpp (PROTO_SOURCES PROTO_HEADERS ${PROTOFILES} )
194+ # Protobuf+gRPC generator wrapper
195+ function (PROTOBUF_GENERATE_GRPC_CPP SRCS HDRS )
196+ if (NOT ARGN)
197+ message (SEND_ERROR "Error: PROTOBUF_GENERATE_GRPC_CPP() called without any proto files" )
198+ return ()
199+ endif ()
200+
201+ if (PROTOBUF_GENERATE_CPP_APPEND_PATH) # This variable is common for all types of output.
202+ # Create an include path for each file specified
203+ foreach (FIL ${ARGN} )
204+ get_filename_component (ABS_FIL ${FIL} ABSOLUTE )
205+ get_filename_component (ABS_PATH ${ABS_FIL} PATH )
206+ list (FIND _protobuf_include_path ${ABS_PATH} _contains_already)
207+ if (${_contains_already} EQUAL -1)
208+ list (APPEND _protobuf_include_path -I ${ABS_PATH} )
209+ endif ()
210+ endforeach ()
211+ else ()
212+ set (_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR} )
213+ endif ()
214+
215+ if (DEFINED PROTOBUF_IMPORT_DIRS)
216+ foreach (DIR ${PROTOBUF_IMPORT_DIRS} )
217+ get_filename_component (ABS_PATH ${DIR} ABSOLUTE )
218+ list (FIND _protobuf_include_path ${ABS_PATH} _contains_already)
219+ if (${_contains_already} EQUAL -1)
220+ list (APPEND _protobuf_include_path -I ${ABS_PATH} )
221+ endif ()
222+ endforeach ()
223+ endif ()
224+
225+ set (${SRCS} )
226+ set (${HDRS} )
227+ foreach (FIL ${ARGN} )
228+ get_filename_component (ABS_FIL ${FIL} ABSOLUTE )
229+ get_filename_component (FIL_WE ${FIL} NAME_WE )
230+
231+ list (APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR} /${FIL_WE} .pb.cc" )
232+ list (APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR} /${FIL_WE} .pb.h" )
233+ list (APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR} /${FIL_WE} .grpc.pb.cc" )
234+ list (APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR} /${FIL_WE} .grpc.pb.h" )
235+
236+ # protoc cpp generator
237+ add_custom_command (
238+ OUTPUT "${CMAKE_CURRENT_BINARY_DIR} /${FIL_WE} .pb.cc"
239+ "${CMAKE_CURRENT_BINARY_DIR} /${FIL_WE} .pb.h"
240+ COMMAND protobuf::protoc
241+ ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL}
242+ DEPENDS ${ABS_FIL}
243+ COMMENT "Running C++ protocol buffer compiler on ${FIL} "
244+ VERBATIM )
245+
246+ # protoc grpc cpp generator
247+ add_custom_command (
248+ OUTPUT "${CMAKE_CURRENT_BINARY_DIR} /${FIL_WE} .grpc.pb.cc"
249+ "${CMAKE_CURRENT_BINARY_DIR} /${FIL_WE} .grpc.pb.h"
250+ COMMAND protobuf::protoc
251+ ARGS --grpc_out=${CMAKE_CURRENT_BINARY_DIR}
252+ --plugin=protoc-gen-grpc=${gRPC_CPP_PLUGIN_EXECUTABLE}
253+ ${_protobuf_include_path} ${ABS_FIL}
254+ DEPENDS ${ABS_FIL} protobuf::protoc
255+ COMMENT "Running gRPC C++ protocol buffer compiler on ${FIL} "
256+ VERBATIM )
257+ endforeach ()
258+
259+ set_source_files_properties (${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE )
260+ set (${SRCS} ${${SRCS} } PARENT_SCOPE )
261+ set (${HDRS} ${${HDRS} } PARENT_SCOPE )
262+ endfunction ()
263+
183264protobuf_generate_grpc_cpp (GRPC_SOURCES GRPC_HEADERS ${PROTOFILES} )
184265
185266set (CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR_OLD} )
@@ -211,7 +292,6 @@ set(OCCLIBRARY_PUBLIC_HEADERS
211292
212293add_library (${OCCLIBRARY} SHARED
213294 ${OCCLIBRARY_SOURCES}
214- ${PROTO_SOURCES}
215295 ${GRPC_SOURCES} )
216296
217297target_include_directories (${OCCLIBRARY}
@@ -225,7 +305,7 @@ target_include_directories(${OCCLIBRARY}
225305
226306target_link_libraries (${OCCLIBRARY}
227307 PUBLIC
228- grpc ::grpc++
308+ gRPC ::grpc++
229309 protobuf::libprotobuf )
230310
231311generate_export_header (${OCCLIBRARY} )
@@ -310,12 +390,11 @@ set(OCCPLUGIN_SOURCES
310390
311391add_library (${OCCPLUGIN} SHARED
312392 ${OCCPLUGIN_SOURCES}
313- ${PROTO_SOURCES}
314393 ${GRPC_SOURCES} )
315394
316395target_link_libraries (${OCCPLUGIN}
317396 FairMQ::FairMQ
318- grpc ::grpc++
397+ gRPC ::grpc++
319398 protobuf::libprotobuf
320399 Boost::program_options )
321400
0 commit comments