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 a387ffd862
PiperOrigin-RevId: 646581094
pull/17235/head
jslap-ubi 9 months ago committed by Copybara-Service
parent f4b57b98b0
commit 3836fd3474
  1. 3
      CMakeLists.txt
  2. 1
      cmake/libprotobuf-lite.cmake
  3. 17
      cmake/protobuf-configure-target.cmake
  4. 1
      cmake/tests.cmake
  5. 2
      src/google/protobuf/arenaz_sampler.cc
  6. 2
      src/google/protobuf/generated_enum_util.cc
  7. 2
      src/google/protobuf/generated_message_tctable_lite.cc
  8. 3
      src/google/protobuf/generated_message_util.cc
  9. 5
      src/google/protobuf/implicit_weak_message.cc
  10. 2
      src/google/protobuf/inlined_string_field.cc
  11. 2
      src/google/protobuf/io/zero_copy_stream.cc
  12. 2
      src/google/protobuf/io/zero_copy_stream_impl_lite.cc
  13. 2
      src/google/protobuf/port.cc
  14. 2
      src/google/protobuf/raw_ptr.cc

@ -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)

@ -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

@ -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 ()

@ -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

@ -221,3 +221,5 @@ void SetThreadSafeArenazGlobalNextSample(int64_t next_sample) {}
} // namespace internal
} // namespace protobuf
} // namespace google
#include "google/protobuf/port_undef.inc"

@ -219,3 +219,5 @@ std::vector<uint32_t> GenerateEnumData(absl::Span<const int32_t> values) {
} // namespace internal
} // namespace protobuf
} // namespace google
#include "google/protobuf/port_undef.inc"

@ -2938,3 +2938,5 @@ const char* TcParser::DiscardEverythingFallback(PROTOBUF_TC_PARAM_DECL) {
} // namespace internal
} // namespace protobuf
} // namespace google
#include "google/protobuf/port_undef.inc"

@ -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 {

@ -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 {

@ -116,3 +116,5 @@ void InlinedStringField::ClearToDefault(const LazyString& default_value,
} // namespace internal
} // namespace protobuf
} // namespace google
#include "google/protobuf/port_undef.inc"

@ -119,3 +119,5 @@ bool ZeroCopyOutputStream::WriteAliasedRaw(const void* /* data */,
} // namespace io
} // namespace protobuf
} // namespace google
#include "google/protobuf/port_undef.inc"

@ -683,3 +683,5 @@ absl::Cord CordOutputStream::Consume() {
} // namespace io
} // namespace protobuf
} // namespace google
#include "google/protobuf/port_undef.inc"

@ -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"

@ -23,3 +23,5 @@ ABSL_CONST_INIT PROTOBUF_EXPORT
} // namespace internal
} // namespace protobuf
} // namespace google
#include "google/protobuf/port_undef.inc"

Loading…
Cancel
Save