From ce29cb74f790187247254abeddb4d5c2332049fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20M=2E=20Basto?= Date: Sat, 20 Jan 2024 22:43:58 +0000 Subject: [PATCH 1/2] Add support to protobuf 3.25.1 new-protobuf-cmake-logic.patch --- cpp/CMakeLists.txt | 90 ++++++++++++++++++++++++++------------- cpp/cmake/config.cmake.in | 4 +- 2 files changed, 63 insertions(+), 31 deletions(-) diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 96def5c8f9..e076796365 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -100,7 +100,8 @@ if (USE_ALTERNATE_FORMATS) endif () # Find all the required libraries and programs. -find_package(absl) +# Use "CONFIG" as there is no built-in cmake module for absl. +find_package(absl CONFIG REQUIRED) if(NOT absl_FOUND) # Overide abseil install rules for subprojects @@ -169,14 +170,24 @@ if (USE_RE2) find_required_library (RE2 re2/re2.h re2 "Google RE2") endif () -if (USE_PROTOBUF_LITE) - find_required_library (PROTOBUF google/protobuf/message_lite.h protobuf-lite - "Google Protocol Buffers") - check_library_version (PC_PROTOBUF protobuf-lite>=2.4) +find_package(Protobuf CONFIG) +if(NOT Protobuf_FOUND) + find_package(Protobuf REQUIRED) +endif() + +if (${Protobuf_VERSION} VERSION_LESS "3.21.0.0") + if (USE_PROTOBUF_LITE) + set (PROTOBUF_LIB ${Protobuf_LITE_LIBRARIES}) + else () + set (PROTOBUF_LIB ${Protobuf_LIBRARIES}) + endif () +# find_required_program (PROTOC protoc "Google Protocol Buffers compiler (protoc)") else () - find_required_library (PROTOBUF google/protobuf/message_lite.h protobuf - "Google Protocol Buffers") - check_library_version (PC_PROTOBUF protobuf>=2.4) + if (USE_PROTOBUF_LITE) + set (PROTOBUF_LIB protobuf::libprotobuf-lite) + else () + set (PROTOBUF_LIB protobuf::libprotobuf) + endif () endif () find_required_library (ICU_UC unicode/uchar.h icuuc "ICU") @@ -192,9 +203,6 @@ if (USE_ICU_REGEXP OR BUILD_GEOCODER) list (APPEND ICU_LIB ${ICU_I18N_LIB}) endif () -find_required_program (PROTOC protoc - "Google Protocol Buffers compiler (protoc)") - if (REGENERATE_METADATA) find_required_program (JAVA java "Java Runtime Environment") @@ -220,24 +228,39 @@ endif () set (RESOURCES_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../resources") set ( - PROTOBUF_SOURCES "${RESOURCES_DIR}/phonemetadata.proto" - "${RESOURCES_DIR}/phonenumber.proto" + PROTO_FILES "${RESOURCES_DIR}/phonemetadata.proto" + "${RESOURCES_DIR}/phonenumber.proto" ) -set ( - PROTOBUF_OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers/phonemetadata.pb.cc" - "${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers/phonemetadata.pb.h" - "${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers/phonenumber.pb.cc" - "${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers/phonenumber.pb.h" -) +if (${Protobuf_VERSION} VERSION_LESS "3.21.0.0") + set ( + PROTOBUF_OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers/phonemetadata.pb.cc" + "${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers/phonemetadata.pb.h" + "${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers/phonenumber.pb.cc" + "${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers/phonenumber.pb.h" + ) -add_custom_command ( - COMMAND ${PROTOC_BIN} --cpp_out=${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers/ - --proto_path=${RESOURCES_DIR} ${PROTOBUF_SOURCES} +# COMMAND ${PROTOC_BIN} + add_custom_command ( + COMMAND ${Protobuf_PROTOC_EXECUTABLE} + ARGS --cpp_out=${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers/ --proto_path=${RESOURCES_DIR} ${PROTO_FILES} + VERBATIM - OUTPUT ${PROTOBUF_OUTPUT} - DEPENDS ${PROTOBUF_SOURCES} -) + OUTPUT ${PROTOBUF_OUTPUT} + DEPENDS ${PROTO_FILES} + ) +else () + set (PROTOBUF_OUTPUT "") + add_library (proto-objects OBJECT ${PROTO_FILES}) + target_link_libraries (proto-objects PUBLIC protobuf::libprotobuf) + set (PROTO_BINARY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src") + target_include_directories (proto-objects PUBLIC "$") + protobuf_generate ( + TARGET proto-objects + IMPORT_DIRS "${RESOURCES_DIR}" + PROTOC_OUT_DIR "${PROTO_BINARY_DIR}/phonenumbers" + ) +endif () if (BUILD_GEOCODER) # Geocoding data cpp file generation @@ -267,9 +290,7 @@ set ( "src/phonenumbers/base/strings/string_piece.cc" "src/phonenumbers/default_logger.cc" "src/phonenumbers/logger.cc" - "src/phonenumbers/phonemetadata.pb.cc" # Generated by Protocol Buffers. "src/phonenumbers/phonenumber.cc" - "src/phonenumbers/phonenumber.pb.cc" # Generated by Protocol Buffers. "src/phonenumbers/phonenumberutil.cc" "src/phonenumbers/regex_based_matcher.cc" "src/phonenumbers/regexp_cache.cc" @@ -282,6 +303,10 @@ set ( "src/phonenumbers/utf/unilib.cc" ) +if (${Protobuf_VERSION} VERSION_LESS "3.21.0.0") + list (APPEND SOURCES ${PROTOBUF_OUTPUT}) +endif () + if (BUILD_GEOCODER) set ( GEOCODING_SOURCES @@ -290,7 +315,6 @@ if (BUILD_GEOCODER) "src/phonenumbers/geocoding/geocoding_data.cc" "src/phonenumbers/geocoding/mapping_file_provider.cc" "src/phonenumbers/geocoding/phonenumber_offline_geocoder.cc" - "src/phonenumbers/phonenumber.pb.h" # Forces proto buffer generation. ) endif () @@ -450,6 +474,10 @@ if (APPLE) list (APPEND LIBRARY_DEPS ${COREFOUNDATION_LIB} ${FOUNDATION_LIB}) endif () +if (${Protobuf_VERSION} VERSION_GREATER_EQUAL "3.21.0.0") + list (APPEND LIBRARY_DEPS proto-objects) +endif () + #---------------------------------------------------------------- # Build libraries #---------------------------------------------------------------- @@ -601,7 +629,11 @@ endif() # Install built libraries #---------------------------------------------------------------- -set (BUILT_LIBS) +if (${Protobuf_VERSION} VERSION_GREATER_EQUAL "3.21.0.0") + set (BUILT_LIBS proto-objects) +else () + set (BUILT_LIBS) +endif () set(targets_export_name "${PROJECT_NAME}-targets") if (BUILD_STATIC_LIB) diff --git a/cpp/cmake/config.cmake.in b/cpp/cmake/config.cmake.in index 05f915659e..b91ce98369 100644 --- a/cpp/cmake/config.cmake.in +++ b/cpp/cmake/config.cmake.in @@ -2,8 +2,8 @@ include(CMakeFindDependencyMacro) -find_dependency(absl) -find_dependency(Protobuf) +find_dependency(absl CONFIG) +find_dependency(Protobuf CONFIG) include("${CMAKE_CURRENT_LIST_DIR}/@targets_export_name@.cmake") check_required_components("@PROJECT_NAME@") From 8eab7e10cf1cd6e5f45df9483a2e5f1fdef2640d Mon Sep 17 00:00:00 2001 From: Fabian Vogt Date: Thu, 22 Feb 2024 13:51:45 +0100 Subject: [PATCH 2/2] Avoid intermediate proto-object library The use of proto-object breaks building shared libs and it doesn't make sense to install it. Instead of TARGET, use generate_protobuf with OUT_VAR. --- cpp/CMakeLists.txt | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index e076796365..f37b292422 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -250,13 +250,10 @@ if (${Protobuf_VERSION} VERSION_LESS "3.21.0.0") DEPENDS ${PROTO_FILES} ) else () - set (PROTOBUF_OUTPUT "") - add_library (proto-objects OBJECT ${PROTO_FILES}) - target_link_libraries (proto-objects PUBLIC protobuf::libprotobuf) set (PROTO_BINARY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src") - target_include_directories (proto-objects PUBLIC "$") protobuf_generate ( - TARGET proto-objects + PROTOS ${PROTO_FILES} + OUT_VAR PROTOBUF_OUTPUT IMPORT_DIRS "${RESOURCES_DIR}" PROTOC_OUT_DIR "${PROTO_BINARY_DIR}/phonenumbers" ) @@ -303,9 +300,7 @@ set ( "src/phonenumbers/utf/unilib.cc" ) -if (${Protobuf_VERSION} VERSION_LESS "3.21.0.0") - list (APPEND SOURCES ${PROTOBUF_OUTPUT}) -endif () +list (APPEND SOURCES ${PROTOBUF_OUTPUT}) if (BUILD_GEOCODER) set ( @@ -474,10 +469,6 @@ if (APPLE) list (APPEND LIBRARY_DEPS ${COREFOUNDATION_LIB} ${FOUNDATION_LIB}) endif () -if (${Protobuf_VERSION} VERSION_GREATER_EQUAL "3.21.0.0") - list (APPEND LIBRARY_DEPS proto-objects) -endif () - #---------------------------------------------------------------- # Build libraries #---------------------------------------------------------------- @@ -629,11 +620,7 @@ endif() # Install built libraries #---------------------------------------------------------------- -if (${Protobuf_VERSION} VERSION_GREATER_EQUAL "3.21.0.0") - set (BUILT_LIBS proto-objects) -else () - set (BUILT_LIBS) -endif () +set (BUILT_LIBS) set(targets_export_name "${PROJECT_NAME}-targets") if (BUILD_STATIC_LIB)