Skip to content

Commit

Permalink
Add a real dependency on Abseil (#10416)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mkruskal-google authored Aug 16, 2022
1 parent 2b673bb commit bc1b310
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
20 changes: 19 additions & 1 deletion cmake/abseil-cpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
12 changes: 0 additions & 12 deletions cmake/install.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down
1 change: 1 addition & 0 deletions cmake/libprotobuf-lite.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmake/libprotobuf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmake/libprotoc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/compiler/cpp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ cc_library(
deps = [
"//:protobuf",
"//src/google/protobuf/compiler:code_generator",
"@com_google_absl//absl/container:flat_hash_set",
],
)

Expand Down
32 changes: 17 additions & 15 deletions src/google/protobuf/compiler/cpp/helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
#include <map>
#include <memory>
#include <queue>
#include <unordered_set>
#include <vector>

#include "absl/container/flat_hash_set.h"
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/logging.h>
#include <google/protobuf/descriptor.h>
Expand Down Expand Up @@ -176,15 +176,17 @@ static const char* const kKeywordList[] = {
#endif // !PROTOBUF_FUTURE_BREAKING_CHANGES
};

static std::unordered_set<std::string>* MakeKeywordsMap() {
auto* result = new std::unordered_set<std::string>();
for (const auto keyword : kKeywordList) {
result->emplace(keyword);
}
return result;
}
const absl::flat_hash_set<std::string>& Keywords() {
static const auto* keywords = []{
auto* keywords = new absl::flat_hash_set<std::string>();

static std::unordered_set<std::string>& 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";
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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("_");
Expand Down Expand Up @@ -1116,7 +1118,7 @@ bool IsAnyMessage(const Descriptor* descriptor, const Options& options) {
}

bool IsWellKnownMessage(const FileDescriptor* file) {
static const std::unordered_set<std::string> well_known_files{
static const auto* well_known_files = new absl::flat_hash_set<std::string>{
"google/protobuf/any.proto",
"google/protobuf/api.proto",
"google/protobuf/compiler/plugin.proto",
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion third_party/abseil-cpp
Submodule abseil-cpp updated 138 files

0 comments on commit bc1b310

Please sign in to comment.