From dee788d83f6fe6e8cec08fcaddb5a2cb7f7383dc Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Tue, 16 Aug 2022 11:01:24 -0700 Subject: [PATCH 01/10] Proof of concept for Abseil dependency --- src/google/protobuf/compiler/cpp/BUILD.bazel | 1 + src/google/protobuf/compiler/cpp/helpers.cc | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/google/protobuf/compiler/cpp/BUILD.bazel b/src/google/protobuf/compiler/cpp/BUILD.bazel index 098fd63e8ed0..44f08fec90e5 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 b3ceafb5fa1e..63cb095866f8 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 #include #include #include @@ -176,15 +176,15 @@ static const char* const kKeywordList[] = { #endif // !PROTOBUF_FUTURE_BREAKING_CHANGES }; -static std::unordered_set* MakeKeywordsMap() { - auto* result = new std::unordered_set(); +static absl::flat_hash_set* MakeKeywordsMap() { + auto* result = new absl::flat_hash_set(); for (const auto keyword : kKeywordList) { result->emplace(keyword); } return result; } -static std::unordered_set& kKeywords = *MakeKeywordsMap(); +static absl::flat_hash_set& kKeywords = *MakeKeywordsMap(); std::string IntTypeName(const Options& options, const std::string& type) { return type + "_t"; @@ -1116,7 +1116,7 @@ bool IsAnyMessage(const Descriptor* descriptor, const Options& options) { } bool IsWellKnownMessage(const FileDescriptor* file) { - static const std::unordered_set well_known_files{ + static const absl::flat_hash_set well_known_files{ "google/protobuf/any.proto", "google/protobuf/api.proto", "google/protobuf/compiler/plugin.proto", From 85ec84aacef5bbcb5610f7912128b4ac284b40ec Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Tue, 16 Aug 2022 11:02:03 -0700 Subject: [PATCH 02/10] Adding most common Abseil libraries --- cmake/abseil-cpp.cmake | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/cmake/abseil-cpp.cmake b/cmake/abseil-cpp.cmake index 24d13f59917f..746673245cb2 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 ) From 1569b4af936d86f56ab6e94ed24b9d81c283ceb9 Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Tue, 16 Aug 2022 11:02:16 -0700 Subject: [PATCH 03/10] Fixing shared library breakages --- CMakeLists.txt | 4 ++++ cmake/libprotobuf-lite.cmake | 1 + cmake/libprotobuf.cmake | 1 + cmake/libprotoc.cmake | 1 + 4 files changed, 7 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index e38e237a5c48..2e5ee8f71b00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,6 +79,10 @@ mark_as_advanced(protobuf_DEBUG_POSTFIX) # User options include(${protobuf_SOURCE_DIR}/cmake/protobuf-options.cmake) +if (protobuf_BUILD_SHARED_LIBS) + 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/libprotobuf-lite.cmake b/cmake/libprotobuf-lite.cmake index 36ed3df64f34..8047969a3a46 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 2607374b5cc7..7fae976c1742 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 509d87be2802..b3a907ff3b86 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 From 33ddc498aabb6b7a8cc2dabac3d08239874c4c3e Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Tue, 16 Aug 2022 11:13:56 -0700 Subject: [PATCH 04/10] Switching to quotes over angled brackets --- src/google/protobuf/compiler/cpp/helpers.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/google/protobuf/compiler/cpp/helpers.cc b/src/google/protobuf/compiler/cpp/helpers.cc index 63cb095866f8..d36967f7dcb6 100644 --- a/src/google/protobuf/compiler/cpp/helpers.cc +++ b/src/google/protobuf/compiler/cpp/helpers.cc @@ -43,7 +43,7 @@ #include #include -#include +#include "absl/container/flat_hash_set.h" #include #include #include From 0f8c05bead3812c45089395b3f0cb7d541e63270 Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Tue, 16 Aug 2022 14:45:01 -0700 Subject: [PATCH 05/10] Disable install target by default --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2e5ee8f71b00..eae7fbfdf597 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,7 +50,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES Intel) endif() # Options -option(protobuf_INSTALL "Install protobuf binaries and files" ON) +option(protobuf_INSTALL "Install protobuf binaries and files" OFF) if(WITH_PROTOC) set(protobuf_PROTOC_EXE ${WITH_PROTOC} CACHE FILEPATH "Protocol Buffer Compiler executable" FORCE) endif() From 7509ffd9d599fc881de0f2ffaccfd5ad37b02e96 Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Tue, 16 Aug 2022 15:13:13 -0700 Subject: [PATCH 06/10] Fixing abseil to LTS commit --- third_party/abseil-cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/third_party/abseil-cpp b/third_party/abseil-cpp index 8c6e53ef3adb..215105818dfd 160000 --- a/third_party/abseil-cpp +++ b/third_party/abseil-cpp @@ -1 +1 @@ -Subproject commit 8c6e53ef3adb1227fffa442c50349dab134a54bc +Subproject commit 215105818dfde3174fe799600bb0f3cae233d0bf From 1a02ce2f9c2e12fb5c0f9f00af462237d9b542b6 Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Tue, 16 Aug 2022 15:15:56 -0700 Subject: [PATCH 07/10] Upgrade to latest Abseil LTS --- .gitmodules | 2 +- third_party/abseil-cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index 4aec90bc5607..594203110e5d 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/third_party/abseil-cpp b/third_party/abseil-cpp index 215105818dfd..273292d1cfc0 160000 --- a/third_party/abseil-cpp +++ b/third_party/abseil-cpp @@ -1 +1 @@ -Subproject commit 215105818dfde3174fe799600bb0f3cae233d0bf +Subproject commit 273292d1cfc0a94a65082ee350509af1d113344d From 22efb1070bece4038fc36ad101057b9bdba7c66d Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Tue, 16 Aug 2022 15:34:49 -0700 Subject: [PATCH 08/10] Turning install back on by default, removing unnecessary export statements --- CMakeLists.txt | 2 +- cmake/install.cmake | 12 ------------ 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index eae7fbfdf597..2e5ee8f71b00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,7 +50,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES Intel) endif() # Options -option(protobuf_INSTALL "Install protobuf binaries and files" OFF) +option(protobuf_INSTALL "Install protobuf binaries and files" ON) if(WITH_PROTOC) set(protobuf_PROTOC_EXE ${WITH_PROTOC} CACHE FILEPATH "Protocol Buffer Compiler executable" FORCE) endif() diff --git a/cmake/install.cmake b/cmake/install.cmake index 9379aa77a615..533c64ff292a 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:: From e5d88e7b2986543a0340c5166ec4ee0d0a5a1873 Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Tue, 16 Aug 2022 15:37:24 -0700 Subject: [PATCH 09/10] Add note to future self --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2e5ee8f71b00..6f9b7517f396 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,6 +80,7 @@ mark_as_advanced(protobuf_DEBUG_POSTFIX) 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 () From 49564d0c7e51758b0d452a56e2f32c12522420fa Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Tue, 16 Aug 2022 15:42:24 -0700 Subject: [PATCH 10/10] Fixing unsafe globals --- src/google/protobuf/compiler/cpp/helpers.cc | 30 +++++++++++---------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/google/protobuf/compiler/cpp/helpers.cc b/src/google/protobuf/compiler/cpp/helpers.cc index d36967f7dcb6..91351596dac8 100644 --- a/src/google/protobuf/compiler/cpp/helpers.cc +++ b/src/google/protobuf/compiler/cpp/helpers.cc @@ -176,15 +176,17 @@ static const char* const kKeywordList[] = { #endif // !PROTOBUF_FUTURE_BREAKING_CHANGES }; -static absl::flat_hash_set* MakeKeywordsMap() { - auto* result = new absl::flat_hash_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 absl::flat_hash_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 absl::flat_hash_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,