From a387ffd86208a456d31fdbf0e98d009ed5d7932f Mon Sep 17 00:00:00 2001 From: Jean-Simon Lapointe Date: Tue, 23 Apr 2024 13:15:19 -0400 Subject: [PATCH] Enable Unity(Jumbo) builds Signed-off-by: Jean-Simon Lapointe --- CMakeLists.txt | 1 + cmake/libprotobuf-lite.cmake | 1 + cmake/protobuf-configure-target.cmake | 17 +++++++++++++++++ cmake/tests.cmake | 1 + src/google/protobuf/arenaz_sampler.cc | 1 + src/google/protobuf/generated_enum_util.cc | 1 + .../protobuf/generated_message_tctable_lite.cc | 2 ++ src/google/protobuf/generated_message_util.cc | 3 +++ src/google/protobuf/implicit_weak_message.cc | 5 +++++ src/google/protobuf/inlined_string_field.cc | 1 + src/google/protobuf/io/zero_copy_stream.cc | 1 + .../protobuf/io/zero_copy_stream_impl_lite.cc | 1 + src/google/protobuf/port.cc | 1 + src/google/protobuf/raw_ptr.cc | 1 + 14 files changed, 37 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 717f569c7a..7f38c6a174 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,7 @@ option(protobuf_BUILD_LIBUPB "Build libupb" ON) option(protobuf_DISABLE_RTTI "Remove runtime type information in the binaries" OFF) option(protobuf_TEST_XML_OUTDIR "Output directory for XML logs from tests." "") option(protobuf_ALLOW_CCACHE "Adjust build flags to allow for ccache support." OFF) +option(protobuf_USE_UNITY_BUILD "Enable Unity (Jumbo) build for" OFF) if (BUILD_SHARED_LIBS) set(protobuf_BUILD_SHARED_LIBS_DEFAULT ON) else (BUILD_SHARED_LIBS) diff --git a/cmake/libprotobuf-lite.cmake b/cmake/libprotobuf-lite.cmake index dfccbc8077..9aa81fb88c 100644 --- a/cmake/libprotobuf-lite.cmake +++ b/cmake/libprotobuf-lite.cmake @@ -29,6 +29,7 @@ target_include_directories(libprotobuf-lite PUBLIC ) target_link_libraries(libprotobuf-lite PUBLIC ${protobuf_ABSL_USED_TARGETS}) protobuf_configure_target(libprotobuf-lite) +protobuf_configure_unity_target(libprotobuf-lite) if(protobuf_BUILD_SHARED_LIBS) target_compile_definitions(libprotobuf-lite PUBLIC PROTOBUF_USE_DLLS diff --git a/cmake/protobuf-configure-target.cmake b/cmake/protobuf-configure-target.cmake index f57397f54e..aa519f7caa 100644 --- a/cmake/protobuf-configure-target.cmake +++ b/cmake/protobuf-configure-target.cmake @@ -52,4 +52,21 @@ function(protobuf_configure_target target) if (HAVE_ZLIB) target_compile_definitions("${target}" PRIVATE -DHAVE_ZLIB) endif () + + +endfunction () + +function(protobuf_configure_unity_target target) + if(protobuf_USE_UNITY_BUILD) + # UNITY_BUILD available in CMake >=3.18. + if(${CMAKE_VERSION} VERSION_GREATER 3.18 OR ${CMAKE_VERSION} VERSION_EQUAL 3.18) + # If protobuf_USE_UNITY_BUILD is set to ON, set target to use Unity builds. + set_target_properties("${target}" + PROPERTIES + UNITY_BUILD ON + UNITY_BUILD_MODE BATCH + UNITY_BUILD_BATCH_SIZE 50 + ) + endif() + endif() endfunction () diff --git a/cmake/tests.cmake b/cmake/tests.cmake index 64a0d00449..c8ae1140a9 100644 --- a/cmake/tests.cmake +++ b/cmake/tests.cmake @@ -103,6 +103,7 @@ add_executable(tests ${tests_files} ${common_test_files}) if (MSVC) target_compile_options(tests PRIVATE /wd4146 # unary minus operator applied to unsigned type, result still unsigned + /bigobj ) endif() target_link_libraries(tests diff --git a/src/google/protobuf/arenaz_sampler.cc b/src/google/protobuf/arenaz_sampler.cc index 4cac577962..65e9777a37 100644 --- a/src/google/protobuf/arenaz_sampler.cc +++ b/src/google/protobuf/arenaz_sampler.cc @@ -221,3 +221,4 @@ void SetThreadSafeArenazGlobalNextSample(int64_t next_sample) {} } // namespace internal } // namespace protobuf } // namespace google +#include "google/protobuf/port_undef.inc" diff --git a/src/google/protobuf/generated_enum_util.cc b/src/google/protobuf/generated_enum_util.cc index 372899e160..e770db5013 100644 --- a/src/google/protobuf/generated_enum_util.cc +++ b/src/google/protobuf/generated_enum_util.cc @@ -219,3 +219,4 @@ std::vector GenerateEnumData(absl::Span values) { } // namespace internal } // namespace protobuf } // namespace google +#include "google/protobuf/port_undef.inc" diff --git a/src/google/protobuf/generated_message_tctable_lite.cc b/src/google/protobuf/generated_message_tctable_lite.cc index 9db970e23f..96efd97440 100644 --- a/src/google/protobuf/generated_message_tctable_lite.cc +++ b/src/google/protobuf/generated_message_tctable_lite.cc @@ -2932,3 +2932,5 @@ const char* TcParser::DiscardEverythingFallback(PROTOBUF_TC_PARAM_DECL) { } // namespace internal } // namespace protobuf } // namespace google + +#include diff --git a/src/google/protobuf/generated_message_util.cc b/src/google/protobuf/generated_message_util.cc index 0c79714560..fd5cbbce9a 100644 --- a/src/google/protobuf/generated_message_util.cc +++ b/src/google/protobuf/generated_message_util.cc @@ -28,7 +28,10 @@ // Must be included last #include "google/protobuf/port_def.inc" +#ifndef PROTOBUF_PRAGMA_INIT_SEG_DONE PROTOBUF_PRAGMA_INIT_SEG +#define PROTOBUF_PRAGMA_INIT_SEG_DONE +#endif namespace google { diff --git a/src/google/protobuf/implicit_weak_message.cc b/src/google/protobuf/implicit_weak_message.cc index e4182864a3..1bca268d9b 100644 --- a/src/google/protobuf/implicit_weak_message.cc +++ b/src/google/protobuf/implicit_weak_message.cc @@ -14,7 +14,12 @@ // Must be included last. #include "google/protobuf/port_def.inc" +// Since we could be merging Translation units, we must check if this was done +// before. +#ifndef PROTOBUF_PRAGMA_INIT_SEG_DONE PROTOBUF_PRAGMA_INIT_SEG +#define PROTOBUF_PRAGMA_INIT_SEG_DONE +#endif namespace google { namespace protobuf { diff --git a/src/google/protobuf/inlined_string_field.cc b/src/google/protobuf/inlined_string_field.cc index ec0a9de10f..c404c8bdad 100644 --- a/src/google/protobuf/inlined_string_field.cc +++ b/src/google/protobuf/inlined_string_field.cc @@ -116,3 +116,4 @@ void InlinedStringField::ClearToDefault(const LazyString& default_value, } // namespace internal } // namespace protobuf } // namespace google +#include "google/protobuf/port_undef.inc" diff --git a/src/google/protobuf/io/zero_copy_stream.cc b/src/google/protobuf/io/zero_copy_stream.cc index 9bf728a07f..3631f370c8 100644 --- a/src/google/protobuf/io/zero_copy_stream.cc +++ b/src/google/protobuf/io/zero_copy_stream.cc @@ -119,3 +119,4 @@ bool ZeroCopyOutputStream::WriteAliasedRaw(const void* /* data */, } // namespace io } // namespace protobuf } // namespace google +#include "google/protobuf/port_undef.inc" diff --git a/src/google/protobuf/io/zero_copy_stream_impl_lite.cc b/src/google/protobuf/io/zero_copy_stream_impl_lite.cc index 8ea000774e..ab33887926 100644 --- a/src/google/protobuf/io/zero_copy_stream_impl_lite.cc +++ b/src/google/protobuf/io/zero_copy_stream_impl_lite.cc @@ -683,3 +683,4 @@ absl::Cord CordOutputStream::Consume() { } // namespace io } // namespace protobuf } // namespace google +#include "google/protobuf/port_undef.inc" diff --git a/src/google/protobuf/port.cc b/src/google/protobuf/port.cc index 30356f94f2..a13f008628 100644 --- a/src/google/protobuf/port.cc +++ b/src/google/protobuf/port.cc @@ -27,3 +27,4 @@ void protobuf_assumption_failed(const char* pred, const char* file, int line) { } // namespace internal } // namespace protobuf } // namespace google +#include "google/protobuf/port_undef.inc" diff --git a/src/google/protobuf/raw_ptr.cc b/src/google/protobuf/raw_ptr.cc index 01f4c36558..2e35890e84 100644 --- a/src/google/protobuf/raw_ptr.cc +++ b/src/google/protobuf/raw_ptr.cc @@ -23,3 +23,4 @@ ABSL_CONST_INIT PROTOBUF_EXPORT } // namespace internal } // namespace protobuf } // namespace google +#include "google/protobuf/port_undef.inc"