From bc1b31001d197cf3d9064c7a7aa462c740bbafcd Mon Sep 17 00:00:00 2001 From: Mike Kruskal <62662355+mkruskal-google@users.noreply.github.com> Date: Tue, 16 Aug 2022 16:31:35 -0700 Subject: [PATCH] Add a real dependency on Abseil (#10416) * Proof of concept for Abseil dependency * Adding most common Abseil libraries * Fixing shared library breakages * Switching to quotes over angled brackets * Disable install target by default * Fixing abseil to LTS commit * Upgrade to latest Abseil LTS * Turning install back on by default, removing unnecessary export statements * Add note to future self * Fixing unsafe globals --- .gitmodules | 2 +- CMakeLists.txt | 5 +++ cmake/abseil-cpp.cmake | 20 +++++++++++- cmake/install.cmake | 12 -------- cmake/libprotobuf-lite.cmake | 1 + cmake/libprotobuf.cmake | 1 + cmake/libprotoc.cmake | 1 + src/google/protobuf/compiler/cpp/BUILD.bazel | 1 + src/google/protobuf/compiler/cpp/helpers.cc | 32 +++++++++++--------- third_party/abseil-cpp | 2 +- 10 files changed, 47 insertions(+), 30 deletions(-) diff --git a/.gitmodules b/.gitmodules index 4aec90bc56..594203110e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -9,4 +9,4 @@ [submodule "third_party/abseil-cpp"] path = third_party/abseil-cpp url = https://github.com/abseil/abseil-cpp - branch = lts_2021_11_02 + branch = lts_2022_06_23 diff --git a/CMakeLists.txt b/CMakeLists.txt index e38e237a5c..6f9b7517f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,6 +79,11 @@ mark_as_advanced(protobuf_DEBUG_POSTFIX) # User options include(${protobuf_SOURCE_DIR}/cmake/protobuf-options.cmake) +if (protobuf_BUILD_SHARED_LIBS) + # This is necessary for linking in Abseil. + set(CMAKE_POSITION_INDEPENDENT_CODE ON) +endif () + # Version metadata set(protobuf_VERSION_STRING "3.21.4") set(protobuf_DESCRIPTION "Protocol Buffers") diff --git a/cmake/abseil-cpp.cmake b/cmake/abseil-cpp.cmake index 24d13f5991..746673245c 100644 --- a/cmake/abseil-cpp.cmake +++ b/cmake/abseil-cpp.cmake @@ -27,6 +27,24 @@ endif() set(_protobuf_FIND_ABSL "if(NOT TARGET absl::strings)\n find_package(absl CONFIG)\nendif()") set(protobuf_ABSL_USED_TARGETS + absl::algorithm + absl::base + absl::bind_front + absl::cleanup + absl::debugging + absl::flags + absl::flat_hash_map + absl::flat_hash_set + absl::function_ref + absl::hash + absl::memory + absl::optional + absl::span + absl::status + absl::statusor absl::strings - absl::strings_internal + absl::synchronization + absl::time + absl::utility + absl::variant ) diff --git a/cmake/install.cmake b/cmake/install.cmake index 9379aa77a6..533c64ff29 100644 --- a/cmake/install.cmake +++ b/cmake/install.cmake @@ -93,18 +93,6 @@ configure_file(${protobuf_SOURCE_DIR}/cmake/protobuf-options.cmake # Allows the build directory to be used as a find directory. -if (protobuf_BUILD_PROTOC_BINARIES) - export(TARGETS libprotobuf-lite libprotobuf libprotoc protoc - NAMESPACE protobuf:: - FILE ${CMAKE_BUILD_CMAKEDIR}/protobuf-targets.cmake - ) -else (protobuf_BUILD_PROTOC_BINARIES) - export(TARGETS libprotobuf-lite libprotobuf - NAMESPACE protobuf:: - FILE ${CMAKE_BUILD_CMAKEDIR}/protobuf-targets.cmake - ) -endif (protobuf_BUILD_PROTOC_BINARIES) - install(EXPORT protobuf-targets DESTINATION "${CMAKE_INSTALL_CMAKEDIR}" NAMESPACE protobuf:: diff --git a/cmake/libprotobuf-lite.cmake b/cmake/libprotobuf-lite.cmake index 36ed3df64f..8047969a3a 100644 --- a/cmake/libprotobuf-lite.cmake +++ b/cmake/libprotobuf-lite.cmake @@ -23,6 +23,7 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Android") target_link_libraries(libprotobuf-lite PRIVATE log) endif() target_include_directories(libprotobuf-lite PUBLIC ${protobuf_SOURCE_DIR}/src) +target_link_libraries(libprotobuf-lite PRIVATE ${protobuf_ABSL_USED_TARGETS}) target_include_directories(libprotobuf-lite PRIVATE ${ABSL_ROOT_DIR}) if(protobuf_BUILD_SHARED_LIBS) target_compile_definitions(libprotobuf-lite diff --git a/cmake/libprotobuf.cmake b/cmake/libprotobuf.cmake index 2607374b5c..7fae976c17 100644 --- a/cmake/libprotobuf.cmake +++ b/cmake/libprotobuf.cmake @@ -26,6 +26,7 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Android") target_link_libraries(libprotobuf PRIVATE log) endif() target_include_directories(libprotobuf PUBLIC ${protobuf_SOURCE_DIR}/src) +target_link_libraries(libprotobuf PRIVATE ${protobuf_ABSL_USED_TARGETS}) target_include_directories(libprotobuf PRIVATE ${ABSL_ROOT_DIR}) if(protobuf_BUILD_SHARED_LIBS) target_compile_definitions(libprotobuf diff --git a/cmake/libprotoc.cmake b/cmake/libprotoc.cmake index 509d87be28..b3a907ff3b 100644 --- a/cmake/libprotoc.cmake +++ b/cmake/libprotoc.cmake @@ -16,6 +16,7 @@ if(protobuf_HAVE_LD_VERSION_SCRIPT) LINK_DEPENDS ${protobuf_SOURCE_DIR}/src/libprotoc.map) endif() target_link_libraries(libprotoc PRIVATE libprotobuf) +target_link_libraries(libprotoc PRIVATE ${protobuf_ABSL_USED_TARGETS}) target_include_directories(libprotoc PRIVATE ${ABSL_ROOT_DIR}) if(protobuf_BUILD_SHARED_LIBS) target_compile_definitions(libprotoc diff --git a/src/google/protobuf/compiler/cpp/BUILD.bazel b/src/google/protobuf/compiler/cpp/BUILD.bazel index 098fd63e8e..44f08fec90 100644 --- a/src/google/protobuf/compiler/cpp/BUILD.bazel +++ b/src/google/protobuf/compiler/cpp/BUILD.bazel @@ -55,6 +55,7 @@ cc_library( deps = [ "//:protobuf", "//src/google/protobuf/compiler:code_generator", + "@com_google_absl//absl/container:flat_hash_set", ], ) diff --git a/src/google/protobuf/compiler/cpp/helpers.cc b/src/google/protobuf/compiler/cpp/helpers.cc index b3ceafb5fa..91351596da 100644 --- a/src/google/protobuf/compiler/cpp/helpers.cc +++ b/src/google/protobuf/compiler/cpp/helpers.cc @@ -41,9 +41,9 @@ #include #include #include -#include #include +#include "absl/container/flat_hash_set.h" #include #include #include @@ -176,15 +176,17 @@ static const char* const kKeywordList[] = { #endif // !PROTOBUF_FUTURE_BREAKING_CHANGES }; -static std::unordered_set* MakeKeywordsMap() { - auto* result = new std::unordered_set(); - for (const auto keyword : kKeywordList) { - result->emplace(keyword); - } - return result; -} +const absl::flat_hash_set& Keywords() { + static const auto* keywords = []{ + auto* keywords = new absl::flat_hash_set(); -static std::unordered_set& kKeywords = *MakeKeywordsMap(); + for (const auto keyword : kKeywordList) { + keywords->emplace(keyword); + } + return keywords; + }(); + return *keywords; +} std::string IntTypeName(const Options& options, const std::string& type) { return type + "_t"; @@ -509,7 +511,7 @@ std::string SuperClassName(const Descriptor* descriptor, } std::string ResolveKeyword(const std::string& name) { - if (kKeywords.count(name) > 0) { + if (Keywords().count(name) > 0) { return name + "_"; } return name; @@ -518,7 +520,7 @@ std::string ResolveKeyword(const std::string& name) { std::string FieldName(const FieldDescriptor* field) { std::string result = field->name(); LowerString(&result); - if (kKeywords.count(result) > 0) { + if (Keywords().count(result) > 0) { result.append("_"); } return result; @@ -552,7 +554,7 @@ std::string QualifiedOneofCaseConstantName(const FieldDescriptor* field) { std::string EnumValueName(const EnumValueDescriptor* enum_value) { std::string result = enum_value->name(); - if (kKeywords.count(result) > 0) { + if (Keywords().count(result) > 0) { result.append("_"); } return result; @@ -863,7 +865,7 @@ std::string SafeFunctionName(const Descriptor* descriptor, // Single underscore will also make it conflicting with the private data // member. We use double underscore to escape function names. function_name.append("__"); - } else if (kKeywords.count(name) > 0) { + } else if (Keywords().count(name) > 0) { // If the field name is a keyword, we append the underscore back to keep it // consistent with other function names. function_name.append("_"); @@ -1116,7 +1118,7 @@ bool IsAnyMessage(const Descriptor* descriptor, const Options& options) { } bool IsWellKnownMessage(const FileDescriptor* file) { - static const std::unordered_set well_known_files{ + static const auto* well_known_files = new absl::flat_hash_set{ "google/protobuf/any.proto", "google/protobuf/api.proto", "google/protobuf/compiler/plugin.proto", @@ -1130,7 +1132,7 @@ bool IsWellKnownMessage(const FileDescriptor* file) { "google/protobuf/type.proto", "google/protobuf/wrappers.proto", }; - return well_known_files.find(file->name()) != well_known_files.end(); + return well_known_files->find(file->name()) != well_known_files->end(); } static void GenerateUtf8CheckCode(const FieldDescriptor* field, diff --git a/third_party/abseil-cpp b/third_party/abseil-cpp index 8c6e53ef3a..273292d1cf 160000 --- a/third_party/abseil-cpp +++ b/third_party/abseil-cpp @@ -1 +1 @@ -Subproject commit 8c6e53ef3adb1227fffa442c50349dab134a54bc +Subproject commit 273292d1cfc0a94a65082ee350509af1d113344d