From ca5a29f0326f3f67adcdca607ef06dc12f4e12be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Tich=C3=A1k?= Date: Mon, 25 Mar 2024 18:52:55 +0100 Subject: [PATCH] [occ] switched to cmake functions officially provided by libproto --- occ/CMakeLists.txt | 97 ++------------------------------------- occ/protos/CMakeLists.txt | 47 +++++++++++++++++++ 2 files changed, 50 insertions(+), 94 deletions(-) create mode 100644 occ/protos/CMakeLists.txt diff --git a/occ/CMakeLists.txt b/occ/CMakeLists.txt index 99a72b211..cf2c38294 100644 --- a/occ/CMakeLists.txt +++ b/occ/CMakeLists.txt @@ -200,94 +200,8 @@ endif() ### ### Protobuf + gRPC ### -set(PROTOFILES - protos/occ.proto) - -# NOTE: we want the files to end up in a subdirectory "protos" in the build dir, -# but the Protobuf and gRPC generator functions don't support custom output -# directories. -# As a workaround, we rewrite CMAKE_CURRENT_BINARY_DIR for Protobuf/gRPC -# and then restore it to its real CMake-managed value. -file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/protos) -set(CMAKE_CURRENT_BINARY_DIR_OLD ${CMAKE_CURRENT_BINARY_DIR} ) -set(CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/protos) - -# Protobuf+gRPC generator wrapper -function(PROTOBUF_GENERATE_GRPC_CPP SRCS HDRS) - if (NOT ARGN) - message(SEND_ERROR "Error: PROTOBUF_GENERATE_GRPC_CPP() called without any proto files") - return() - endif () - - if (PROTOBUF_GENERATE_CPP_APPEND_PATH) # This variable is common for all types of output. - # Create an include path for each file specified - foreach (FIL ${ARGN}) - get_filename_component(ABS_FIL ${FIL} ABSOLUTE) - get_filename_component(ABS_PATH ${ABS_FIL} PATH) - list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) - if (${_contains_already} EQUAL -1) - list(APPEND _protobuf_include_path -I ${ABS_PATH}) - endif () - endforeach () - else () - set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR}) - endif () - - if (DEFINED PROTOBUF_IMPORT_DIRS) - foreach (DIR ${PROTOBUF_IMPORT_DIRS}) - get_filename_component(ABS_PATH ${DIR} ABSOLUTE) - list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) - if (${_contains_already} EQUAL -1) - list(APPEND _protobuf_include_path -I ${ABS_PATH}) - endif () - endforeach () - endif () - set(${SRCS}) - set(${HDRS}) - foreach (FIL ${ARGN}) - get_filename_component(ABS_FIL ${FIL} ABSOLUTE) - get_filename_component(FIL_WE ${FIL} NAME_WE) - - list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc") - list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h") - list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.cc") - list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.h") - - # protoc cpp generator - add_custom_command( - OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc" - "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h" - COMMAND protobuf::protoc - ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL} - DEPENDS ${ABS_FIL} - COMMENT "Running C++ protocol buffer compiler on ${FIL}" - VERBATIM) - - if(APPLE) - set(libenv DYLD_LIBRARY_PATH=$:$) - endif() - # protoc grpc cpp generator - add_custom_command( - OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.cc" - "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.h" - COMMAND ${libenv} $ - ARGS --grpc_out=${CMAKE_CURRENT_BINARY_DIR} - --plugin=protoc-gen-grpc=${gRPC_CPP_PLUGIN_EXECUTABLE} - ${_protobuf_include_path} ${ABS_FIL} - DEPENDS ${ABS_FIL} protobuf::protoc - COMMENT "Running gRPC C++ protocol buffer compiler on ${FIL}" - VERBATIM) - endforeach () - - set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE) - set(${SRCS} ${${SRCS}} PARENT_SCOPE) - set(${HDRS} ${${HDRS}} PARENT_SCOPE) -endfunction() - -protobuf_generate_grpc_cpp(GRPC_SOURCES GRPC_HEADERS ${PROTOFILES}) - -set(CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR_OLD}) +add_subdirectory(protos) ### ### Build target OCCLIBRARY @@ -332,12 +246,9 @@ if (UNIX AND NOT APPLE) "LINKER:--no-as-needed") endif() - target_link_libraries(${OCCLIBRARY} PUBLIC - gRPC::grpc++ - gRPC::grpc++_reflection - protobuf::libprotobuf + Occ-proto Boost::program_options) generate_export_header(${OCCLIBRARY}) @@ -430,9 +341,7 @@ add_library(${OCCPLUGIN} SHARED target_link_libraries(${OCCPLUGIN} PUBLIC FairMQ::FairMQ - gRPC::grpc++ - gRPC::grpc++_reflection - protobuf::libprotobuf + Occ-proto Boost::program_options) target_include_directories(${OCCPLUGIN} diff --git a/occ/protos/CMakeLists.txt b/occ/protos/CMakeLists.txt new file mode 100644 index 000000000..6ecea3e58 --- /dev/null +++ b/occ/protos/CMakeLists.txt @@ -0,0 +1,47 @@ +# === This file is part of ALICE O² === +# +# Copyright 2024 CERN and copyright holders of ALICE O². +# Author: Michal Tichak +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# In applying this license CERN does not waive the privileges and +# immunities granted to it by virtue of its status as an +# Intergovernmental Organization or submit itself to any jurisdiction. + +set(PROTO_GENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/protos") +set(PROTO_IMPORT_DIR "${CMAKE_CURRENT_LIST_DIR}") + +add_library(Occ-proto OBJECT "${CMAKE_CURRENT_LIST_DIR}/occ.proto") +target_link_libraries(Occ-proto PUBLIC + protobuf::libprotobuf + gRPC::grpc++ + gRPC::grpc++_reflection +) +target_include_directories(Occ-proto PUBLIC "$") + +protobuf_generate( + TARGET Occ-proto + IMPORT_DIRS ${PROTO_IMPORT_DIR} + PROTOC_OUT_DIR "${PROTO_GENERATED_DIR}") + +protobuf_generate( + TARGET Occ-proto + LANGUAGE grpc + GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc + PLUGIN "protoc-gen-grpc=\$" + IMPORT_DIRS ${PROTO_IMPORT_DIRS} + PROTOC_OUT_DIR "${PROTO_GENERATED_DIR}") + +install(TARGETS Occ-proto EXPORT OccTargets)