Skip to content

Commit ca5a29f

Browse files
Michal Tichákteo
authored andcommitted
[occ] switched to cmake functions officially provided by libproto
1 parent e779661 commit ca5a29f

File tree

2 files changed

+50
-94
lines changed

2 files changed

+50
-94
lines changed

occ/CMakeLists.txt

Lines changed: 3 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -200,94 +200,8 @@ endif()
200200
###
201201
### Protobuf + gRPC
202202
###
203-
set(PROTOFILES
204-
protos/occ.proto)
205-
206-
# NOTE: we want the files to end up in a subdirectory "protos" in the build dir,
207-
# but the Protobuf and gRPC generator functions don't support custom output
208-
# directories.
209-
# As a workaround, we rewrite CMAKE_CURRENT_BINARY_DIR for Protobuf/gRPC
210-
# and then restore it to its real CMake-managed value.
211-
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/protos)
212-
set(CMAKE_CURRENT_BINARY_DIR_OLD ${CMAKE_CURRENT_BINARY_DIR} )
213-
set(CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/protos)
214-
215-
# Protobuf+gRPC generator wrapper
216-
function(PROTOBUF_GENERATE_GRPC_CPP SRCS HDRS)
217-
if (NOT ARGN)
218-
message(SEND_ERROR "Error: PROTOBUF_GENERATE_GRPC_CPP() called without any proto files")
219-
return()
220-
endif ()
221-
222-
if (PROTOBUF_GENERATE_CPP_APPEND_PATH) # This variable is common for all types of output.
223-
# Create an include path for each file specified
224-
foreach (FIL ${ARGN})
225-
get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
226-
get_filename_component(ABS_PATH ${ABS_FIL} PATH)
227-
list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
228-
if (${_contains_already} EQUAL -1)
229-
list(APPEND _protobuf_include_path -I ${ABS_PATH})
230-
endif ()
231-
endforeach ()
232-
else ()
233-
set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
234-
endif ()
235-
236-
if (DEFINED PROTOBUF_IMPORT_DIRS)
237-
foreach (DIR ${PROTOBUF_IMPORT_DIRS})
238-
get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
239-
list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
240-
if (${_contains_already} EQUAL -1)
241-
list(APPEND _protobuf_include_path -I ${ABS_PATH})
242-
endif ()
243-
endforeach ()
244-
endif ()
245203

246-
set(${SRCS})
247-
set(${HDRS})
248-
foreach (FIL ${ARGN})
249-
get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
250-
get_filename_component(FIL_WE ${FIL} NAME_WE)
251-
252-
list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc")
253-
list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")
254-
list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.cc")
255-
list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.h")
256-
257-
# protoc cpp generator
258-
add_custom_command(
259-
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc"
260-
"${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
261-
COMMAND protobuf::protoc
262-
ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL}
263-
DEPENDS ${ABS_FIL}
264-
COMMENT "Running C++ protocol buffer compiler on ${FIL}"
265-
VERBATIM)
266-
267-
if(APPLE)
268-
set(libenv DYLD_LIBRARY_PATH=$<TARGET_FILE_DIR:gRPC::grpc++>:$<TARGET_FILE_DIR:protobuf::libprotobuf>)
269-
endif()
270-
# protoc grpc cpp generator
271-
add_custom_command(
272-
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.cc"
273-
"${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.h"
274-
COMMAND ${libenv} $<TARGET_FILE:protobuf::protoc>
275-
ARGS --grpc_out=${CMAKE_CURRENT_BINARY_DIR}
276-
--plugin=protoc-gen-grpc=${gRPC_CPP_PLUGIN_EXECUTABLE}
277-
${_protobuf_include_path} ${ABS_FIL}
278-
DEPENDS ${ABS_FIL} protobuf::protoc
279-
COMMENT "Running gRPC C++ protocol buffer compiler on ${FIL}"
280-
VERBATIM)
281-
endforeach ()
282-
283-
set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
284-
set(${SRCS} ${${SRCS}} PARENT_SCOPE)
285-
set(${HDRS} ${${HDRS}} PARENT_SCOPE)
286-
endfunction()
287-
288-
protobuf_generate_grpc_cpp(GRPC_SOURCES GRPC_HEADERS ${PROTOFILES})
289-
290-
set(CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR_OLD})
204+
add_subdirectory(protos)
291205

292206
###
293207
### Build target OCCLIBRARY
@@ -332,12 +246,9 @@ if (UNIX AND NOT APPLE)
332246
"LINKER:--no-as-needed")
333247
endif()
334248

335-
336249
target_link_libraries(${OCCLIBRARY}
337250
PUBLIC
338-
gRPC::grpc++
339-
gRPC::grpc++_reflection
340-
protobuf::libprotobuf
251+
Occ-proto
341252
Boost::program_options)
342253

343254
generate_export_header(${OCCLIBRARY})
@@ -430,9 +341,7 @@ add_library(${OCCPLUGIN} SHARED
430341

431342
target_link_libraries(${OCCPLUGIN} PUBLIC
432343
FairMQ::FairMQ
433-
gRPC::grpc++
434-
gRPC::grpc++_reflection
435-
protobuf::libprotobuf
344+
Occ-proto
436345
Boost::program_options)
437346

438347
target_include_directories(${OCCPLUGIN}

occ/protos/CMakeLists.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# === This file is part of ALICE O² ===
2+
#
3+
# Copyright 2024 CERN and copyright holders of ALICE O².
4+
# Author: Michal Tichak <michal.tichak@cern.ch>
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
#
19+
# In applying this license CERN does not waive the privileges and
20+
# immunities granted to it by virtue of its status as an
21+
# Intergovernmental Organization or submit itself to any jurisdiction.
22+
23+
set(PROTO_GENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/protos")
24+
set(PROTO_IMPORT_DIR "${CMAKE_CURRENT_LIST_DIR}")
25+
26+
add_library(Occ-proto OBJECT "${CMAKE_CURRENT_LIST_DIR}/occ.proto")
27+
target_link_libraries(Occ-proto PUBLIC
28+
protobuf::libprotobuf
29+
gRPC::grpc++
30+
gRPC::grpc++_reflection
31+
)
32+
target_include_directories(Occ-proto PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>")
33+
34+
protobuf_generate(
35+
TARGET Occ-proto
36+
IMPORT_DIRS ${PROTO_IMPORT_DIR}
37+
PROTOC_OUT_DIR "${PROTO_GENERATED_DIR}")
38+
39+
protobuf_generate(
40+
TARGET Occ-proto
41+
LANGUAGE grpc
42+
GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc
43+
PLUGIN "protoc-gen-grpc=\$<TARGET_FILE:gRPC::grpc_cpp_plugin>"
44+
IMPORT_DIRS ${PROTO_IMPORT_DIRS}
45+
PROTOC_OUT_DIR "${PROTO_GENERATED_DIR}")
46+
47+
install(TARGETS Occ-proto EXPORT OccTargets)

0 commit comments

Comments
 (0)