Skip to content

Commit

Permalink
Fix for finding new versions of protobuf (#3331)
Browse files Browse the repository at this point in the history
Support for protobuf >= 22 by using
find_package(Protobuf CONFIG)

Signed-off-by: Steve Peters <scpeters@openrobotics.org>
Co-authored-by: Steve Peters <computersthatmove@gmail.com>
  • Loading branch information
traversaro and scpeters authored Jun 15, 2023
1 parent fbb3d12 commit 17e09f5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
48 changes: 34 additions & 14 deletions cmake/SearchForStuff.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,43 @@ endif()

########################################
# The Google Protobuf library for message generation + serialization
find_package(Protobuf REQUIRED)
if (NOT PROTOBUF_FOUND)
BUILD_ERROR ("Missing: Google Protobuf (libprotobuf-dev)")
endif()
if (NOT PROTOBUF_PROTOC_EXECUTABLE)
BUILD_ERROR ("Missing: Google Protobuf Compiler (protobuf-compiler)")
endif()
if (NOT PROTOBUF_PROTOC_LIBRARY)
BUILD_ERROR ("Missing: Google Protobuf Compiler Library (libprotoc-dev)")

# Protobuf >= 22 requires to link abseil, so we are constrained to use
# find_package(Protobuf) and link to protobuf::libprotobuf,
# see https://github.com/conda-forge/conda-forge-pinning-feedstock/issues/4075#issuecomment-1569242816
if (DEFINED PROTOBUF_VERSION AND PROTOBUF_VERSION GREATER_EQUAL 22.0)
set(GZ_PROTOBUF_USE_CMAKE_CONFIG_DEFAULT ON)
else()
set(GZ_PROTOBUF_USE_CMAKE_CONFIG_DEFAULT OFF)
endif()
option(GZ_PROTOBUF_USE_CMAKE_CONFIG "If true use protobuf-config.cmake to find protobuf" ${GZ_PROTOBUF_USE_CMAKE_CONFIG_DEFAULT})
mark_as_advanced(GZ_PROTOBUF_USE_CMAKE_CONFIG)

if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
set (GZ_PROTOBUF_LIBRARY ${PROTOBUF_LIBRARY_DEBUG})
set (GZ_PROTOBUF_PROTOC_LIBRARY ${PROTOBUF_PROTOC_LIBRARY_DEBUG})
if(NOT GZ_PROTOBUF_USE_CMAKE_CONFIG)
find_package(Protobuf REQUIRED)
if (NOT PROTOBUF_FOUND)
BUILD_ERROR ("Missing: Google Protobuf (libprotobuf-dev)")
endif()
if (NOT PROTOBUF_PROTOC_EXECUTABLE)
BUILD_ERROR ("Missing: Google Protobuf Compiler (protobuf-compiler)")
endif()
if (NOT PROTOBUF_PROTOC_LIBRARY)
BUILD_ERROR ("Missing: Google Protobuf Compiler Library (libprotoc-dev)")
endif()
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
set (GZ_PROTOBUF_LIBRARY ${PROTOBUF_LIBRARY_DEBUG})
set (GZ_PROTOBUF_PROTOC_LIBRARY ${PROTOBUF_PROTOC_LIBRARY_DEBUG})
else()
set (GZ_PROTOBUF_LIBRARY ${PROTOBUF_LIBRARY})
set (GZ_PROTOBUF_PROTOC_LIBRARY ${PROTOBUF_PROTOC_LIBRARY})
endif()
else()
set (GZ_PROTOBUF_LIBRARY ${PROTOBUF_LIBRARY})
set (GZ_PROTOBUF_PROTOC_LIBRARY ${PROTOBUF_PROTOC_LIBRARY})
find_package(Protobuf CONFIG REQUIRED)
set (GZ_PROTOBUF_LIBRARY protobuf::libprotobuf)
set (GZ_PROTOBUF_PROTOC_LIBRARY protobuf::libprotoc)
if(NOT DEFINED PROTOBUF_PROTOC_EXECUTABLE)
get_target_property(PROTOBUF_PROTOC_EXECUTABLE protobuf::protoc LOCATION)
endif()
endif()

########################################
Expand Down
15 changes: 12 additions & 3 deletions cmake/gazebo-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,18 @@ list(APPEND @PKG_NAME@_INCLUDE_DIRS ${Boost_INCLUDE_DIRS})
list(APPEND @PKG_NAME@_LIBRARIES ${Boost_LIBRARIES})

# Find protobuf
find_package(Protobuf REQUIRED)
list(APPEND @PKG_NAME@_INCLUDE_DIRS ${PROTOBUF_INCLUDE_DIRS})
list(APPEND @PKG_NAME@_LIBRARIES ${PROTOBUF_LIBRARIES})
set(GZ_PROTOBUF_USE_CMAKE_CONFIG @GZ_PROTOBUF_USE_CMAKE_CONFIG@)

if(NOT GZ_PROTOBUF_USE_CMAKE_CONFIG)
find_package(Protobuf REQUIRED)
list(APPEND @PKG_NAME@_INCLUDE_DIRS ${PROTOBUF_INCLUDE_DIRS})
list(APPEND @PKG_NAME@_LIBRARIES ${PROTOBUF_LIBRARIES})
else()
find_package(Protobuf CONFIG REQUIRED)
list(APPEND @PKG_NAME@_INCLUDE_DIRS ${PROTOBUF_INCLUDE_DIRS})
list(APPEND @PKG_NAME@_LIBRARIES protobuf::libprotoc)
list(APPEND @PKG_NAME@_LIBRARIES protobuf::libprotobuf)
endif()

# Find SDFormat
find_package(sdformat9 REQUIRED VERSION 9.8)
Expand Down

0 comments on commit 17e09f5

Please sign in to comment.