From 1ca4e9c4859a23112684138c78608ddc0b8f1770 Mon Sep 17 00:00:00 2001 From: Carlos O'Ryan Date: Sun, 7 May 2023 09:22:27 -0700 Subject: [PATCH 1/4] fix: missing -DPROTOBUF_USE_DLLS in pkg-config (#12700) When the protobuf libraries have been compiled as shared libraries the users of the library need to add `-DPROTOBUF_USE_DLLS` to their build line. Otherwise some symbols are missing. Fixes #12699 FWIW, I am not sure this is an ideal fix. It may be better to fix the headers such that no macros change the ABI. Closes #12700 COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/12700 from coryan:fix-define-protobuf-use-dlls-in-pkg-config-file 13c792eebd3d070fa25eff68cfca6ae1493e9503 PiperOrigin-RevId: 530116678 --- cmake/install.cmake | 4 ++++ cmake/protobuf-lite.pc.cmake | 2 +- cmake/protobuf.pc.cmake | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/cmake/install.cmake b/cmake/install.cmake index 9d837ac536..8c96981da8 100644 --- a/cmake/install.cmake +++ b/cmake/install.cmake @@ -12,6 +12,10 @@ foreach (_target IN LISTS _pc_target_list) string(CONCAT _protobuf_PC_REQUIRES "${_protobuf_PC_REQUIRES}" "${_sep}" "${_target}") set(_sep " ") endforeach () +set(_protobuf_PC_CFLAGS) +if (protobuf_BUILD_SHARED_LIBS) + set(_protobuf_PC_CFLAGS -DPROTOBUF_USE_DLLS) +endif () configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/protobuf.pc.cmake ${CMAKE_CURRENT_BINARY_DIR}/protobuf.pc @ONLY) diff --git a/cmake/protobuf-lite.pc.cmake b/cmake/protobuf-lite.pc.cmake index 7f742c0258..eb5869e588 100644 --- a/cmake/protobuf-lite.pc.cmake +++ b/cmake/protobuf-lite.pc.cmake @@ -8,5 +8,5 @@ Description: Google's Data Interchange Format Version: @protobuf_VERSION@ Requires: @_protobuf_PC_REQUIRES@ Libs: -L${libdir} -lprotobuf-lite @CMAKE_THREAD_LIBS_INIT@ -Cflags: -I${includedir} +Cflags: -I${includedir} @_protobuf_PC_CFLAGS@ Conflicts: protobuf diff --git a/cmake/protobuf.pc.cmake b/cmake/protobuf.pc.cmake index 21bbf47fb1..81d87c1f70 100644 --- a/cmake/protobuf.pc.cmake +++ b/cmake/protobuf.pc.cmake @@ -8,5 +8,5 @@ Description: Google's Data Interchange Format Version: @protobuf_VERSION@ Requires: @_protobuf_PC_REQUIRES@ Libs: -L${libdir} -lprotobuf @CMAKE_THREAD_LIBS_INIT@ -Cflags: -I${includedir} +Cflags: -I${includedir} @_protobuf_PC_CFLAGS@ Conflicts: protobuf-lite From 28c990508f398e6af9c26263dad600025cacb332 Mon Sep 17 00:00:00 2001 From: Carlos O'Ryan Date: Sun, 7 May 2023 09:23:49 -0700 Subject: [PATCH 2/4] fix: typo in `string(JOIN)` workaround (#12698) My sketch to fix #12672 was wrong. This works for realsies. Closes #12698 COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/12698 from coryan:fix-cmake-typo-in-string-join-workaround 182d2e248ee10403541c5ddd07857846a66ae57d PiperOrigin-RevId: 530116824 --- cmake/install.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/install.cmake b/cmake/install.cmake index 8c96981da8..e7eb2103be 100644 --- a/cmake/install.cmake +++ b/cmake/install.cmake @@ -8,7 +8,7 @@ list(APPEND _pc_targets "utf8_range") set(_protobuf_PC_REQUIRES "") set(_sep "") -foreach (_target IN LISTS _pc_target_list) +foreach (_target IN LISTS _pc_targets) string(CONCAT _protobuf_PC_REQUIRES "${_protobuf_PC_REQUIRES}" "${_sep}" "${_target}") set(_sep " ") endforeach () From fe1277f9a835d50285cc8e82df5a77daaee42d77 Mon Sep 17 00:00:00 2001 From: Carlos O'Ryan Date: Sun, 7 May 2023 12:43:50 -0700 Subject: [PATCH 3/4] fix: avoid warnings on Windows (#12701) On Wndows, `size_t` is 64-bits, and `int` is 32-bits. That makes conversions from `size_t` to `int` potentially lossy, and they generate warnings. In this case an `int` variable was assigned to `size_t` and then passed to functions consuming `int`. Seems simpler to use `auto` and avoid these problems altogether. Closes #12701 COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/12701 from coryan:fix-warnings-repeated-field-warnings-in-msvc b1ec34de77cbd31c914b810c87cbe16f1ce51a7d PiperOrigin-RevId: 530134611 --- src/google/protobuf/repeated_field.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/google/protobuf/repeated_field.h b/src/google/protobuf/repeated_field.h index bfcb93833d..057565a416 100644 --- a/src/google/protobuf/repeated_field.h +++ b/src/google/protobuf/repeated_field.h @@ -494,7 +494,7 @@ template inline RepeatedField::RepeatedField(const RepeatedField& rhs) : current_size_(0), total_size_(0), arena_or_elements_(nullptr) { StaticValidityCheck(); - if (size_t size = rhs.current_size_) { + if (auto size = rhs.current_size_) { Grow(0, size); ExchangeCurrentSize(size); UninitializedCopyN(rhs.elements(), size, unsafe_elements()); @@ -775,7 +775,7 @@ inline void RepeatedField::Clear() { template inline void RepeatedField::MergeFrom(const RepeatedField& rhs) { ABSL_DCHECK_NE(&rhs, this); - if (size_t size = rhs.current_size_) { + if (auto size = rhs.current_size_) { Reserve(current_size_ + size); Element* dst = elements() + ExchangeCurrentSize(current_size_ + size); UninitializedCopyN(rhs.elements(), size, dst); From b880933ed39a3218176aa36a0fa109093b3b8e96 Mon Sep 17 00:00:00 2001 From: Carlos O'Ryan Date: Sun, 7 May 2023 13:12:01 -0700 Subject: [PATCH 4/4] fix: avoid warnings on MSVC (#12697) Warnings in header files can be a problem for consumers that enable `/WX` (or `-Werror`). In this case, using `... & -align` produces a warning (C4146) with MSVC. The fix is to use equivalent expression `... & ~(align - 1)`, which was already used in the same file. Fixes #12675 Closes #12697 COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/12697 from coryan:fix-msvc-warnings-in-arena-align 835f3b489a85e1b50281ac2943bd74d159eb3ead PiperOrigin-RevId: 530137165 --- src/google/protobuf/arena_align.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/google/protobuf/arena_align.h b/src/google/protobuf/arena_align.h index 958bb9d072..d63393c843 100644 --- a/src/google/protobuf/arena_align.h +++ b/src/google/protobuf/arena_align.h @@ -99,7 +99,7 @@ struct ArenaAlignDefault { } static inline PROTOBUF_ALWAYS_INLINE constexpr size_t Ceil(size_t n) { - return (n + align - 1) & -align; + return (n + align - 1) & ~(align - 1); } static inline PROTOBUF_ALWAYS_INLINE constexpr size_t Floor(size_t n) { return (n & ~(align - 1)); @@ -113,7 +113,7 @@ struct ArenaAlignDefault { template static inline PROTOBUF_ALWAYS_INLINE T* Ceil(T* ptr) { uintptr_t intptr = reinterpret_cast(ptr); - return reinterpret_cast((intptr + align - 1) & -align); + return reinterpret_cast((intptr + align - 1) & ~(align - 1)); } template @@ -142,7 +142,9 @@ struct ArenaAlign { return (reinterpret_cast(ptr) & (align - 1)) == 0U; } - constexpr size_t Ceil(size_t n) const { return (n + align - 1) & -align; } + constexpr size_t Ceil(size_t n) const { + return (n + align - 1) & ~(align - 1); + } constexpr size_t Floor(size_t n) const { return (n & ~(align - 1)); } constexpr size_t Padded(size_t n) const { @@ -156,7 +158,7 @@ struct ArenaAlign { template T* Ceil(T* ptr) const { uintptr_t intptr = reinterpret_cast(ptr); - return reinterpret_cast((intptr + align - 1) & -align); + return reinterpret_cast((intptr + align - 1) & ~(align - 1)); } template