Skip to content

Commit e612a84

Browse files
committed
[build] Port to CMake-built Protobuf and gRPC and bump both deps
1 parent 4db8c74 commit e612a84

2 files changed

Lines changed: 90 additions & 9 deletions

File tree

occ/CMakeLists.txt

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
cmake_minimum_required(VERSION 3.9)
2424
cmake_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})
152154
endif()
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)")
165176
endif()
166177

178+
167179
###
168180
### Protobuf + gRPC
169181
###
@@ -179,7 +191,76 @@ file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/protos)
179191
set(CMAKE_CURRENT_BINARY_DIR_OLD ${CMAKE_CURRENT_BINARY_DIR} )
180192
set(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+
183264
protobuf_generate_grpc_cpp(GRPC_SOURCES GRPC_HEADERS ${PROTOFILES})
184265

185266
set(CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR_OLD})
@@ -211,7 +292,6 @@ set(OCCLIBRARY_PUBLIC_HEADERS
211292

212293
add_library(${OCCLIBRARY} SHARED
213294
${OCCLIBRARY_SOURCES}
214-
${PROTO_SOURCES}
215295
${GRPC_SOURCES})
216296

217297
target_include_directories(${OCCLIBRARY}
@@ -225,7 +305,7 @@ target_include_directories(${OCCLIBRARY}
225305

226306
target_link_libraries(${OCCLIBRARY}
227307
PUBLIC
228-
grpc::grpc++
308+
gRPC::grpc++
229309
protobuf::libprotobuf)
230310

231311
generate_export_header(${OCCLIBRARY})
@@ -310,12 +390,11 @@ set(OCCPLUGIN_SOURCES
310390

311391
add_library(${OCCPLUGIN} SHARED
312392
${OCCPLUGIN_SOURCES}
313-
${PROTO_SOURCES}
314393
${GRPC_SOURCES})
315394

316395
target_link_libraries(${OCCPLUGIN}
317396
FairMQ::FairMQ
318-
grpc::grpc++
397+
gRPC::grpc++
319398
protobuf::libprotobuf
320399
Boost::program_options)
321400

occ/cmake/OccConfig.cmake.in

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@
2323
@PACKAGE_INIT@
2424

2525
set(Occ_VERSION @PROJECT_VERSION@)
26+
cmake_policy(SET CMP0077 NEW)
2627

2728
get_filename_component(Occ_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
2829
include(CMakeFindDependencyMacro)
2930

3031
list(APPEND CMAKE_MODULE_PATH ${Occ_CMAKE_DIR})
3132
find_dependency(Boost COMPONENTS program_options)
32-
find_dependency(Protobuf 3.5.0 REQUIRED)
33-
find_dependency(GRPC 1.9.1 REQUIRED)
33+
set(protobuf_MODULE_COMPATIBLE TRUE)
34+
find_dependency(protobuf 3.7.1 CONFIG REQUIRED)
35+
find_dependency(gRPC 1.19.1 CONFIG REQUIRED)
3436

3537
list(REMOVE_AT CMAKE_MODULE_PATH -1)
3638

0 commit comments

Comments
 (0)