From 3836fd347493e75681c19c0241713eff26046715 Mon Sep 17 00:00:00 2001 From: jslap-ubi <151566283+jslap-ubi@users.noreply.github.com> Date: Tue, 25 Jun 2024 13:10:42 -0700 Subject: [PATCH] Enable Unity(Jumbo) builds (#16454) **Description** Added a cmake option to enable Unity builds for libprotobuf-lite. Added missing symmetric include for port_def.inc Added preproc guards for PROTOBUF_PRAGMA_INIT_SEG so it does not get included multiple times in unity build. **Motivation and Context** For large code base, especially in the game dev industry, Unity builds (https://en.wikipedia.org/wiki/Unity_build) are used to speed up the compilations. Special care must be taken to enable Unity builds for a project, since many translation units will be merged togheter. Closes #16454 COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/16454 from jslap-ubi:enable-unity-build a387ffd86208a456d31fdbf0e98d009ed5d7932f PiperOrigin-RevId: 646581094 --- CMakeLists.txt | 3 +++ cmake/libprotobuf-lite.cmake | 1 + cmake/protobuf-configure-target.cmake | 17 +++++++++++++++++ cmake/tests.cmake | 1 + src/google/protobuf/arenaz_sampler.cc | 2 ++ src/google/protobuf/generated_enum_util.cc | 2 ++ .../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 | 2 ++ src/google/protobuf/io/zero_copy_stream.cc | 2 ++ .../protobuf/io/zero_copy_stream_impl_lite.cc | 2 ++ src/google/protobuf/port.cc | 2 ++ src/google/protobuf/raw_ptr.cc | 2 ++ 14 files changed, 46 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 717f569c7a..68ce1a52e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,9 @@ 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) + +# We support Unity (Jumbo) builds best-effort. +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..c5b126bb4a 100644 --- a/src/google/protobuf/arenaz_sampler.cc +++ b/src/google/protobuf/arenaz_sampler.cc @@ -221,3 +221,5 @@ 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..eb6b08fcb8 100644 --- a/src/google/protobuf/generated_enum_util.cc +++ b/src/google/protobuf/generated_enum_util.cc @@ -219,3 +219,5 @@ 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 985abeae47..55df4a76a9 100644 --- a/src/google/protobuf/generated_message_tctable_lite.cc +++ b/src/google/protobuf/generated_message_tctable_lite.cc @@ -2938,3 +2938,5 @@ const char* TcParser::DiscardEverythingFallback(PROTOBUF_TC_PARAM_DECL) { } // namespace internal } // namespace protobuf } // namespace google + +#include "google/protobuf/port_undef.inc" diff --git a/src/google/protobuf/generated_message_util.cc b/src/google/protobuf/generated_message_util.cc index 37ff006308..09b7db5d66 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 92e0ce75f6..a3336a01f0 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..a17f922b6c 100644 --- a/src/google/protobuf/inlined_string_field.cc +++ b/src/google/protobuf/inlined_string_field.cc @@ -116,3 +116,5 @@ 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..e66e41977a 100644 --- a/src/google/protobuf/io/zero_copy_stream.cc +++ b/src/google/protobuf/io/zero_copy_stream.cc @@ -119,3 +119,5 @@ 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..15baebe3fa 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,5 @@ 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..5308f37177 100644 --- a/src/google/protobuf/port.cc +++ b/src/google/protobuf/port.cc @@ -27,3 +27,5 @@ 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..cffe79c502 100644 --- a/src/google/protobuf/raw_ptr.cc +++ b/src/google/protobuf/raw_ptr.cc @@ -23,3 +23,5 @@ ABSL_CONST_INIT PROTOBUF_EXPORT } // namespace internal } // namespace protobuf } // namespace google + +#include "google/protobuf/port_undef.inc"