This is an automated email from the ASF dual-hosted git repository.

xiaofeng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new 37814d2d Support Protobuf 22 (#2546)
37814d2d is described below

commit 37814d2da8f4a08da642eca28f139c293321d519
Author: Xiaofeng Wang <wasp...@gmail.com>
AuthorDate: Wed Mar 13 13:31:41 2024 +0800

    Support Protobuf 22 (#2546)
    
    * Support Protobuf 22
    
    * config_brpc.sh: support to build with protobuf 22
    
    * ci-macos: compatible with Apple silicon
    
    * ci: compile with protobuf 22+ using macos
---
 .github/workflows/ci-macos.yml | 47 +++++++++++++++++++++--
 CMakeLists.txt                 | 50 +++++++++++++++++++++++--
 Makefile                       |  6 +--
 config_brpc.sh                 | 85 +++++++++++++++++++++++++++++++++++++++++-
 src/brpc/esp_message.h         |  2 +-
 src/brpc/global.cpp            |  4 ++
 src/brpc/memcache.h            |  8 ++--
 src/brpc/nshead_message.h      |  2 +-
 src/brpc/pb_compat.h           |  6 +++
 src/brpc/redis.h               |  8 ++--
 src/brpc/serialized_request.h  |  4 +-
 src/json2pb/json_to_pb.cpp     |  8 +++-
 src/json2pb/pb_to_json.cpp     |  8 +++-
 13 files changed, 210 insertions(+), 28 deletions(-)

diff --git a/.github/workflows/ci-macos.yml b/.github/workflows/ci-macos.yml
index 4e98aa31..c23885d5 100644
--- a/.github/workflows/ci-macos.yml
+++ b/.github/workflows/ci-macos.yml
@@ -27,9 +27,9 @@ jobs:
 
     - name: config_brpc
       run: |
-           GETOPT_PATH=$(find "/usr/local/Cellar/" -name "getopt" -type f 
-perm +111 -exec dirname {} \;)
+           GETOPT_PATH=$(brew --prefix gnu-getopt)/bin
            export PATH=$GETOPT_PATH:$PATH
-           ./config_brpc.sh --header="/usr/local/include" 
--libs="/usr/local/lib"
+           ./config_brpc.sh --header="$(brew --prefix)/include" --libs="$(brew 
--prefix)/lib"
 
     - name: compile
       run: |
@@ -44,7 +44,7 @@ jobs:
     - name: install dependences
       run: |
            brew install ./homebrew-formula/protobuf.rb 
-           brew install openssl gnu-getopt coreutils gflags leveldb
+           brew install openssl gflags leveldb
 
     - name: cmake
       run: |
@@ -56,3 +56,44 @@ jobs:
       run: |
            cd build
            make -j ${{env.proc_num}}
+
+  compile-with-make-protobuf22:
+    runs-on: macos-latest # https://github.com/actions/runner-images
+
+    steps:
+      - uses: actions/checkout@v2
+
+      - name: install dependences
+        run: |
+          brew install protobuf openssl gnu-getopt coreutils gflags leveldb
+
+      - name: config_brpc
+        run: |
+          GETOPT_PATH=$(brew --prefix gnu-getopt)/bin
+          export PATH=$GETOPT_PATH:$PATH
+           ./config_brpc.sh --header="$(brew --prefix)/include" --libs="$(brew 
--prefix)/lib"
+
+      - name: compile
+        run: |
+          make -j ${{env.proc_num}}
+
+  compile-with-cmake-protobuf22:
+    runs-on: macos-latest
+
+    steps:
+      - uses: actions/checkout@v2
+
+      - name: install dependences
+        run: |
+          brew install protobuf openssl gflags leveldb
+
+      - name: cmake
+        run: |
+          mkdir build
+          cd build
+          cmake ..
+
+      - name: compile
+        run: |
+          cd build
+          make -j ${{env.proc_num}}
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2eeec049..9df7f1ef 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -15,7 +15,7 @@
 # specific language governing permissions and limitations
 # under the License.
 
-cmake_minimum_required(VERSION 2.8.10)
+cmake_minimum_required(VERSION 2.8.12)
 project(brpc C CXX)
 
 option(WITH_GLOG "With glog" OFF)
@@ -140,8 +140,6 @@ else()
 endif()
 endmacro(use_cxx11)
 
-use_cxx11()
-
 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
     #required by butil/crc32.cc to boost performance for 10x
     if((CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)") AND NOT 
(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.4))
@@ -156,6 +154,50 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
 endif()
 
 find_package(Protobuf REQUIRED)
+if(Protobuf_VERSION GREATER_EQUAL 4.22)
+    # required by absl
+    set(CMAKE_CXX_STANDARD 17)
+
+    find_package(absl REQUIRED CONFIG)
+    set(protobuf_ABSL_USED_TARGETS
+        absl::absl_check
+        absl::absl_log
+        absl::algorithm
+        absl::base
+        absl::bind_front
+        absl::bits
+        absl::btree
+        absl::cleanup
+        absl::cord
+        absl::core_headers
+        absl::debugging
+        absl::die_if_null
+        absl::dynamic_annotations
+        absl::flags
+        absl::flat_hash_map
+        absl::flat_hash_set
+        absl::function_ref
+        absl::hash
+        absl::layout
+        absl::log_initialize
+        absl::log_severity
+        absl::memory
+        absl::node_hash_map
+        absl::node_hash_set
+        absl::optional
+        absl::span
+        absl::status
+        absl::statusor
+        absl::strings
+        absl::synchronization
+        absl::time
+        absl::type_traits
+        absl::utility
+        absl::variant
+    )
+else()
+    use_cxx11()
+endif()
 find_package(Threads REQUIRED)
 
 find_path(LEVELDB_INCLUDE_PATH NAMES leveldb/db.h)
@@ -229,7 +271,7 @@ include_directories(
 
 set(DYNAMIC_LIB
     ${GFLAGS_LIBRARY}
-    ${PROTOBUF_LIBRARIES}
+    ${PROTOBUF_LIBRARIES} ${protobuf_ABSL_USED_TARGETS}
     ${LEVELDB_LIB}
     ${PROTOC_LIB}
     ${CMAKE_THREAD_LIBS_INIT}
diff --git a/Makefile b/Makefile
index 0ed8a352..04d0a243 100644
--- a/Makefile
+++ b/Makefile
@@ -23,7 +23,7 @@ include config.mk
 # 2. Removed -Werror: Not block compilation for non-vital warnings, especially 
when the
 #    code is tested on newer systems. If the code is used in production, add 
-Werror back
 CPPFLAGS+=-DBTHREAD_USE_FAST_PTHREAD_MUTEX -D_GNU_SOURCE -DUSE_SYMBOLIZE 
-DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS 
-D__STDC_CONSTANT_MACROS -DNDEBUG -DBRPC_REVISION=\"$(shell 
./tools/get_brpc_revision.sh .)\"
-CXXFLAGS=$(CPPFLAGS) -O2 -pipe -Wall -W -fPIC -fstrict-aliasing 
-Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer -std=c++0x
+CXXFLAGS+=$(CPPFLAGS) -O2 -pipe -Wall -W -fPIC -fstrict-aliasing 
-Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer
 CFLAGS=$(CPPFLAGS) -O2 -pipe -Wall -W -fPIC -fstrict-aliasing 
-Wno-unused-parameter -fno-omit-frame-pointer
 DEBUG_CXXFLAGS = $(filter-out -DNDEBUG,$(CXXFLAGS)) -DUNIT_TEST 
-DBVAR_NOT_LINK_DEFAULT_VARIABLES
 DEBUG_CFLAGS = $(filter-out -DNDEBUG,$(CFLAGS)) -DUNIT_TEST
@@ -243,9 +243,9 @@ clean_debug:
 protoc-gen-mcpack: src/idl_options.pb.cc src/mcpack2pb/generator.o libbrpc.a
        @echo "> Linking $@"
 ifeq ($(SYSTEM),Linux)
-       $(CXX) -o $@ $(CXXFLAGS) $(HDRPATHS) $(LIBPATHS) -std=c++0x -Xlinker 
"-(" $^ -Wl,-Bstatic $(STATIC_LINKINGS) -Wl,-Bdynamic -Xlinker "-)" 
$(DYNAMIC_LINKINGS)
+       $(CXX) -o $@ $(CXXFLAGS) $(HDRPATHS) $(LIBPATHS) -Xlinker "-(" $^ 
-Wl,-Bstatic $(STATIC_LINKINGS) -Wl,-Bdynamic -Xlinker "-)" $(DYNAMIC_LINKINGS)
 else ifeq ($(SYSTEM),Darwin)
-       $(CXX) -o $@ $(CXXFLAGS) $(HDRPATHS) $(LIBPATHS) -std=c++0x $^ 
$(STATIC_LINKINGS) $(DYNAMIC_LINKINGS)
+       $(CXX) -o $@ $(CXXFLAGS) $(HDRPATHS) $(LIBPATHS) $^ $(STATIC_LINKINGS) 
$(DYNAMIC_LINKINGS)
 endif
 
 # force generation of pb headers before compiling to avoid fail-to-import 
issues in compiling pb.cc
diff --git a/config_brpc.sh b/config_brpc.sh
index 7e459b75..777b76be 100755
--- a/config_brpc.sh
+++ b/config_brpc.sh
@@ -262,10 +262,89 @@ if [ -z "$GFLAGS_NS" ]; then
 fi
 
 PROTOBUF_HDR=$(find_dir_of_header_or_die google/protobuf/message.h)
+PROTOBUF_VERSION=$(grep '#define GOOGLE_PROTOBUF_VERSION [0-9]\+' 
$PROTOBUF_HDR/google/protobuf/stubs/common.h | awk '{print $3}')
+if [ "$PROTOBUF_VERSION" -ge 4022000 ]; then
+    ABSL_HDR=$(find_dir_of_header_or_die absl/base/config.h)
+    ABSL_LIB=$(find_dir_of_lib_or_die absl_strings)
+    ABSL_LIB_NAMES="
+        absl_bad_optional_access
+        absl_bad_variant_access
+        absl_base
+        absl_city
+        absl_civil_time
+        absl_cord
+        absl_cord_internal
+        absl_cordz_functions
+        absl_cordz_handle
+        absl_cordz_info
+        absl_crc32c
+        absl_crc_cord_state
+        absl_crc_cpu_detect
+        absl_crc_internal
+        absl_debugging_internal
+        absl_demangle_internal
+        absl_die_if_null
+        absl_examine_stack
+        absl_exponential_biased
+        absl_flags
+        absl_flags_commandlineflag
+        absl_flags_commandlineflag_internal
+        absl_flags_config
+        absl_flags_internal
+        absl_flags_marshalling
+        absl_flags_private_handle_accessor
+        absl_flags_program_name
+        absl_flags_reflection
+        absl_graphcycles_internal
+        absl_hash
+        absl_hashtablez_sampler
+        absl_int128
+        absl_kernel_timeout_internal
+        absl_leak_check
+        absl_log_entry
+        absl_log_globals
+        absl_log_initialize
+        absl_log_internal_check_op
+        absl_log_internal_conditions
+        absl_log_internal_format
+        absl_log_internal_globals
+        absl_log_internal_log_sink_set
+        absl_log_internal_message
+        absl_log_internal_nullguard
+        absl_log_internal_proto
+        absl_log_severity
+        absl_log_sink
+        absl_low_level_hash
+        absl_malloc_internal
+        absl_raw_hash_set
+        absl_raw_logging_internal
+        absl_spinlock_wait
+        absl_stacktrace
+        absl_status
+        absl_statusor
+        absl_str_format_internal
+        absl_strerror
+        absl_string_view
+        absl_strings
+        absl_strings_internal
+        absl_symbolize
+        absl_synchronization
+        absl_throw_delegate
+        absl_time
+        absl_time_zone
+    "
+    for i in $ABSL_LIB_NAMES; do
+         append_linking "$ABSL_LIB" "$i"
+    done
+    CXXFLAGS="-std=c++17"
+else
+    CXXFLAGS="-std=c++0x"
+fi
+
 LEVELDB_HDR=$(find_dir_of_header_or_die leveldb/db.h)
 
-HDRS=$($ECHO "$GFLAGS_HDR\n$PROTOBUF_HDR\n$LEVELDB_HDR\n$OPENSSL_HDR" | sort | 
uniq)
-LIBS=$($ECHO 
"$GFLAGS_LIB\n$PROTOBUF_LIB\n$LEVELDB_LIB\n$OPENSSL_LIB\n$SNAPPY_LIB" | sort | 
uniq)
+HDRS=$($ECHO 
"$GFLAGS_HDR\n$PROTOBUF_HDR\n$ABSL_HDR\n$LEVELDB_HDR\n$OPENSSL_HDR" | sort | 
uniq)
+LIBS=$($ECHO 
"$GFLAGS_LIB\n$PROTOBUF_LIB\n$ABSL_LIB\n$LEVELDB_LIB\n$OPENSSL_LIB\n$SNAPPY_LIB"
 | sort | uniq)
 
 absent_in_the_list() {
     TMP=`$ECHO "$1\n$2" | sort | uniq`
@@ -390,6 +469,8 @@ ifeq (\$(CC),gcc)
 endif
 "
 
+append_to_output "CXXFLAGS=${CXXFLAGS}"
+
 append_to_output "ifeq (\$(NEED_LIBPROTOC), 1)"
 PROTOC_LIB=$(find $PROTOBUF_LIB -name "libprotoc.*" | head -n1)
 if [ -z "$PROTOC_LIB" ]; then
diff --git a/src/brpc/esp_message.h b/src/brpc/esp_message.h
index 279f7b6a..aecc837c 100644
--- a/src/brpc/esp_message.h
+++ b/src/brpc/esp_message.h
@@ -72,7 +72,7 @@ public:
             ::google::protobuf::io::CodedOutputStream* output) const 
PB_310_OVERRIDE;
     ::google::protobuf::uint8* SerializeWithCachedSizesToArray(
             ::google::protobuf::uint8* output) const PB_310_OVERRIDE;
-    int GetCachedSize() const override { return ByteSize(); }
+    int GetCachedSize() const PB_422_OVERRIDE { return ByteSize(); }
 
 protected:
     ::google::protobuf::Metadata GetMetadata() const override;
diff --git a/src/brpc/global.cpp b/src/brpc/global.cpp
index 121b9d97..d45c67f0 100644
--- a/src/brpc/global.cpp
+++ b/src/brpc/global.cpp
@@ -297,6 +297,7 @@ static void* GlobalUpdate(void*) {
     return NULL;
 }
 
+#if GOOGLE_PROTOBUF_VERSION < 3022000
 static void BaiduStreamingLogHandler(google::protobuf::LogLevel level,
                                      const char* filename, int line,
                                      const std::string& message) {
@@ -316,6 +317,7 @@ static void 
BaiduStreamingLogHandler(google::protobuf::LogLevel level,
     }
     CHECK(false) << filename << ':' << line << ' ' << message;
 }
+#endif
 
 static void GlobalInitializeOrDieImpl() {
     //////////////////////////////////////////////////////////////////
@@ -331,8 +333,10 @@ static void GlobalInitializeOrDieImpl() {
         CHECK(SIG_ERR != signal(SIGPIPE, SIG_IGN));
     }
 
+#if GOOGLE_PROTOBUF_VERSION < 3022000
     // Make GOOGLE_LOG print to comlog device
     SetLogHandler(&BaiduStreamingLogHandler);
+#endif
 
     // Set bthread create span function
     bthread_set_create_span_func(CreateBthreadSpan);
diff --git a/src/brpc/memcache.h b/src/brpc/memcache.h
index c6fd2b95..014f075b 100644
--- a/src/brpc/memcache.h
+++ b/src/brpc/memcache.h
@@ -107,7 +107,7 @@ public:
     void SerializeWithCachedSizes(
         ::google::protobuf::io::CodedOutputStream* output) const 
PB_310_OVERRIDE;
     ::google::protobuf::uint8* 
SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const 
PB_310_OVERRIDE;
-    int GetCachedSize() const override { return _cached_size_; }
+    int GetCachedSize() const PB_422_OVERRIDE { return _cached_size_; }
     
     static const ::google::protobuf::Descriptor* descriptor();
 
@@ -125,7 +125,7 @@ private:
 
     void SharedCtor();
     void SharedDtor();
-    void SetCachedSize(int size) const override;
+    void SetCachedSize(int size) const PB_422_OVERRIDE;
 
     int _pipelined_count;
     butil::IOBuf _buf;
@@ -220,7 +220,7 @@ public:
     void SerializeWithCachedSizes(
         ::google::protobuf::io::CodedOutputStream* output) const 
PB_310_OVERRIDE;
     ::google::protobuf::uint8* 
SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const 
PB_310_OVERRIDE;
-    int GetCachedSize() const override { return _cached_size_; }
+    int GetCachedSize() const PB_422_OVERRIDE { return _cached_size_; }
 
     static const ::google::protobuf::Descriptor* descriptor();
 
@@ -233,7 +233,7 @@ private:
 
     void SharedCtor();
     void SharedDtor();
-    void SetCachedSize(int size) const override;
+    void SetCachedSize(int size) const PB_422_OVERRIDE;
 
     std::string _err;
     butil::IOBuf _buf;
diff --git a/src/brpc/nshead_message.h b/src/brpc/nshead_message.h
index 11cc1c60..6cd06caf 100644
--- a/src/brpc/nshead_message.h
+++ b/src/brpc/nshead_message.h
@@ -67,7 +67,7 @@ public:
     void SerializeWithCachedSizes(
         ::google::protobuf::io::CodedOutputStream* output) const 
PB_310_OVERRIDE;
     ::google::protobuf::uint8* 
SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const 
PB_310_OVERRIDE;
-    int GetCachedSize() const override { return ByteSize(); }
+    int GetCachedSize() const PB_422_OVERRIDE { return ByteSize(); }
 
 protected:
     ::google::protobuf::Metadata GetMetadata() const override;
diff --git a/src/brpc/pb_compat.h b/src/brpc/pb_compat.h
index 70faeb54..d089530f 100644
--- a/src/brpc/pb_compat.h
+++ b/src/brpc/pb_compat.h
@@ -19,6 +19,12 @@
 #ifndef BRPC_PB_COMPAT_H
 #define BRPC_PB_COMPAT_H
 
+#if GOOGLE_PROTOBUF_VERSION < 4022000
+# define PB_422_OVERRIDE override
+#else
+# define PB_422_OVERRIDE
+#endif
+
 #if GOOGLE_PROTOBUF_VERSION < 3021000
 # define PB_321_OVERRIDE override
 #else
diff --git a/src/brpc/redis.h b/src/brpc/redis.h
index d02e8941..6b949ea4 100644
--- a/src/brpc/redis.h
+++ b/src/brpc/redis.h
@@ -125,7 +125,7 @@ public:
     void SerializeWithCachedSizes(
         ::google::protobuf::io::CodedOutputStream* output) const 
PB_310_OVERRIDE;
     ::google::protobuf::uint8* 
SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const 
PB_310_OVERRIDE;
-    int GetCachedSize() const override { return _cached_size_; }
+    int GetCachedSize() const PB_422_OVERRIDE { return _cached_size_; }
 
     static const ::google::protobuf::Descriptor* descriptor();
     
@@ -137,7 +137,7 @@ protected:
 private:
     void SharedCtor();
     void SharedDtor();
-    void SetCachedSize(int size) const override;
+    void SetCachedSize(int size) const PB_422_OVERRIDE;
     bool AddCommandWithArgs(const char* fmt, ...);
 
     int _ncommand;    // # of valid commands
@@ -198,7 +198,7 @@ public:
     void SerializeWithCachedSizes(
         ::google::protobuf::io::CodedOutputStream* output) const 
PB_310_OVERRIDE;
     ::google::protobuf::uint8* 
SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const 
PB_310_OVERRIDE;
-    int GetCachedSize() const override { return _cached_size_; }
+    int GetCachedSize() const PB_422_OVERRIDE { return _cached_size_; }
 
     static const ::google::protobuf::Descriptor* descriptor();
 
@@ -208,7 +208,7 @@ protected:
 private:
     void SharedCtor();
     void SharedDtor();
-    void SetCachedSize(int size) const override;
+    void SetCachedSize(int size) const PB_422_OVERRIDE;
 
     RedisReply _first_reply;
     RedisReply* _other_replies;
diff --git a/src/brpc/serialized_request.h b/src/brpc/serialized_request.h
index 0fbf76ba..5b68262e 100644
--- a/src/brpc/serialized_request.h
+++ b/src/brpc/serialized_request.h
@@ -53,7 +53,7 @@ public:
     void Clear() override;
     bool IsInitialized() const override;
     int ByteSize() const;
-    int GetCachedSize() const override { return (int)_serialized.size(); }
+    int GetCachedSize() const PB_422_OVERRIDE { return 
(int)_serialized.size(); }
     butil::IOBuf& serialized_data() { return _serialized; }
     const butil::IOBuf& serialized_data() const { return _serialized; }
 
@@ -71,7 +71,7 @@ private:
     void MergeFrom(const SerializedRequest& from);
     void SharedCtor();
     void SharedDtor();
-    void SetCachedSize(int size) const override;
+    void SetCachedSize(int size) const PB_422_OVERRIDE;
   
 private:
     butil::IOBuf _serialized;
diff --git a/src/json2pb/json_to_pb.cpp b/src/json2pb/json_to_pb.cpp
index 6a6f46cc..2652d6d4 100644
--- a/src/json2pb/json_to_pb.cpp
+++ b/src/json2pb/json_to_pb.cpp
@@ -534,8 +534,12 @@ bool JsonValueToProtoMessage(const 
BUTIL_RAPIDJSON_NAMESPACE::Value& json_value,
     for (int i = 0; i < descriptor->extension_range_count(); ++i) {
         const google::protobuf::Descriptor::ExtensionRange*
             ext_range = descriptor->extension_range(i);
-        for (int tag_number = ext_range->start; tag_number < ext_range->end;
-             ++tag_number) {
+#if GOOGLE_PROTOBUF_VERSION < 4022000
+        for (int tag_number = ext_range->start; tag_number < ext_range->end; 
++tag_number)
+#else
+        for (int tag_number = ext_range->start_number(); tag_number < 
ext_range->end_number(); ++tag_number)
+#endif
+        {
             const google::protobuf::FieldDescriptor* field =
                 reflection->FindKnownExtensionByNumber(tag_number);
             if (field) {
diff --git a/src/json2pb/pb_to_json.cpp b/src/json2pb/pb_to_json.cpp
index c6ce183a..a299e424 100644
--- a/src/json2pb/pb_to_json.cpp
+++ b/src/json2pb/pb_to_json.cpp
@@ -75,8 +75,12 @@ bool PbToJsonConverter::Convert(const 
google::protobuf::Message& message, Handle
     for (int i = 0; i < ext_range_count; ++i) {
         const google::protobuf::Descriptor::ExtensionRange*
             ext_range = descriptor->extension_range(i);
-        for (int tag_number = ext_range->start;
-             tag_number < ext_range->end; ++tag_number) {
+#if GOOGLE_PROTOBUF_VERSION < 4022000
+        for (int tag_number = ext_range->start; tag_number < ext_range->end; 
++tag_number)
+#else
+        for (int tag_number = ext_range->start_number(); tag_number < 
ext_range->end_number(); ++tag_number)
+#endif
+        {
             const google::protobuf::FieldDescriptor* field =
                     reflection->FindKnownExtensionByNumber(tag_number);
             if (field) {


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org
For additional commands, e-mail: dev-h...@brpc.apache.org

Reply via email to