Skip to content

Commit

Permalink
build: Workaround FindProtobuf lacking support for Protobuf >= 22
Browse files Browse the repository at this point in the history
1. If we can find a Protobuf with a CMake pkg, we are good.
2. Otherwise fall back to FindProtobuf.
3. If we find Protobuf >= 22 and do not detect the missing `absl::`
   target (this is a guess that `absl::` targets will exist once
   FindProtobuf supports Protobuf >= 22), we set Protobuf as NOTFOUND.

Side notes:
Old CMake versions (< 3.27) see Protobuf >= 21 as major version 4.21+.

See also:
* https://github.com/protocolbuffers/protobuf/releases/tag/v22.0
* https://protobuf.dev/news/2022-05-06/#versioning
* protocolbuffers/protobuf#12292
* https://gitlab.kitware.com/cmake/cmake/-/issues/24847

(cherry picked from commit 0c09823)
  • Loading branch information
dennisklein authored and ChristianTackeGSI committed Jul 24, 2023
1 parent a073cf8 commit 9f1b365
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
16 changes: 15 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,21 @@ endforeach()

find_package2(PUBLIC Pythia6)
find_package2(PUBLIC Pythia8)
find_package2(PUBLIC Protobuf)
# Protobuf https://github.com/protocolbuffers/protobuf/releases/tag/v22.0
# added a new dependency to abseil which is not handled yet by CMake's
# FindProtobuf.cmake.
# https://github.com/protocolbuffers/protobuf/issues/12292 - Let's try to
# find a Protobuf CMake pkg first and fall back to find module if not found.
find_package2(PUBLIC Protobuf CONFIG)
if(NOT Protobuf_FOUND)
unset(Protobuf_FOUND CACHE)
find_package2(PUBLIC Protobuf)
if( Protobuf_FOUND
AND Protobuf_VERSION VERSION_GREATER_EQUAL 4.22
AND NOT TARGET absl::raw_logging_internal)
set(Protobuf_FOUND NOTFOUND CACHE FORCE)
endif()
endif()
find_package2(PUBLIC msgpack)
find_package2(PUBLIC Flatbuffers)

Expand Down
11 changes: 4 additions & 7 deletions examples/advanced/Tutorial3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,20 @@ set(sources
timeBasedSimulation/FairTestDetectorTimeDigiTask.cxx
timeBasedSimulation/FairTestDetectorTimeRecoTask.cxx

$<$<BOOL:${Protobuf_FOUND}>:${CMAKE_CURRENT_BINARY_DIR}/FairTestDetectorPayload.pb.cc>
$<$<TARGET_EXISTS:protobuf::libprotobuf>:${CMAKE_CURRENT_BINARY_DIR}/FairTestDetectorPayload.pb.cc>
$<$<BOOL:${Flatbuffers_FOUND}>:${CMAKE_CURRENT_BINARY_DIR}/FairTestDetectorPayloadDigi_generated.h>
$<$<BOOL:${Flatbuffers_FOUND}>:${CMAKE_CURRENT_BINARY_DIR}/FairTestDetectorPayloadHit_generated.h>
)

fair_change_extensions_if_exists(.cxx .h FILES "${sources}" OUTVAR headers)

if(Protobuf_FOUND)
set(PROTOBUF_USED true)
if(TARGET protobuf::libprotobuf)
list(APPEND defs PROTOBUF)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/FairTestDetectorPayload.pb.h ${CMAKE_CURRENT_BINARY_DIR}/FairTestDetectorPayload.pb.cc
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} -I=. --cpp_out=${CMAKE_CURRENT_BINARY_DIR} FairTestDetectorPayload.proto
COMMAND $<TARGET_FILE:protobuf::protoc> -I=. --cpp_out=${CMAKE_CURRENT_BINARY_DIR} FairTestDetectorPayload.proto
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/MQ/data
)
else()
set(PROTOBUF_USED false)
endif()

if(Flatbuffers_FOUND)
Expand Down Expand Up @@ -129,7 +126,7 @@ target_link_libraries(${target} PUBLIC
FairRoot::FairMQ

Boost::serialization
$<$<BOOL:${Protobuf_FOUND}>:protobuf::libprotobuf>
$<$<TARGET_EXISTS:protobuf::libprotobuf>:protobuf::libprotobuf>
$<$<BOOL:${Flatbuffers_FOUND}>:${flatbuffers_lib}>
$<$<BOOL:${msgpack_FOUND}>:msgpackc>

Expand Down

0 comments on commit 9f1b365

Please sign in to comment.