Skip to content

Commit

Permalink
[occ] switched to cmake functions officially provided by libproto
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Tichák authored and teo committed Mar 26, 2024
1 parent e779661 commit ca5a29f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 94 deletions.
97 changes: 3 additions & 94 deletions occ/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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=$<TARGET_FILE_DIR:gRPC::grpc++>:$<TARGET_FILE_DIR:protobuf::libprotobuf>)
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} $<TARGET_FILE:protobuf::protoc>
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
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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}
Expand Down
47 changes: 47 additions & 0 deletions occ/protos/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# === This file is part of ALICE O² ===
#
# Copyright 2024 CERN and copyright holders of ALICE O².
# Author: Michal Tichak <[email protected]>
#
# 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 <http://www.gnu.org/licenses/>.
#
# 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 "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>")

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=\$<TARGET_FILE:gRPC::grpc_cpp_plugin>"
IMPORT_DIRS ${PROTO_IMPORT_DIRS}
PROTOC_OUT_DIR "${PROTO_GENERATED_DIR}")

install(TARGETS Occ-proto EXPORT OccTargets)

0 comments on commit ca5a29f

Please sign in to comment.