Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Protobuf 22 #2546

Merged
merged 4 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

基于 CONFIG 方式查找 protobuf 可以推导依赖, 但不是所有 protobuf 都提供, 同时 protoc 相关的任务也需要改

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)
Expand Down Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion src/brpc/esp_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class EspMessage : public ::google::protobuf::Message {
::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;
Expand Down
4 changes: 4 additions & 0 deletions src/brpc/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,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) {
Expand All @@ -311,6 +312,7 @@ static void BaiduStreamingLogHandler(google::protobuf::LogLevel level,
}
CHECK(false) << filename << ':' << line << ' ' << message;
}
#endif

static void GlobalInitializeOrDieImpl() {
//////////////////////////////////////////////////////////////////
Expand All @@ -326,8 +328,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

// Setting the variable here does not work, the profiler probably check
// the variable before main() for only once.
Expand Down
8 changes: 4 additions & 4 deletions src/brpc/memcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class MemcacheRequest : public ::google::protobuf::Message {
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();

Expand All @@ -125,7 +125,7 @@ class MemcacheRequest : public ::google::protobuf::Message {

void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const override;
void SetCachedSize(int size) const PB_422_OVERRIDE;

int _pipelined_count;
butil::IOBuf _buf;
Expand Down Expand Up @@ -220,7 +220,7 @@ class MemcacheResponse : public ::google::protobuf::Message {
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();

Expand All @@ -233,7 +233,7 @@ class MemcacheResponse : public ::google::protobuf::Message {

void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const override;
void SetCachedSize(int size) const PB_422_OVERRIDE;

std::string _err;
butil::IOBuf _buf;
Expand Down
2 changes: 1 addition & 1 deletion src/brpc/nshead_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class NsheadMessage : public ::google::protobuf::Message {
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;
Expand Down
6 changes: 6 additions & 0 deletions src/brpc/pb_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/brpc/redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class RedisRequest : public ::google::protobuf::Message {
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();

Expand All @@ -137,7 +137,7 @@ class RedisRequest : public ::google::protobuf::Message {
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
Expand Down Expand Up @@ -198,7 +198,7 @@ class RedisResponse : public ::google::protobuf::Message {
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();

Expand All @@ -208,7 +208,7 @@ class RedisResponse : public ::google::protobuf::Message {
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;
Expand Down
4 changes: 2 additions & 2 deletions src/brpc/serialized_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SerializedRequest : public ::google::protobuf::Message {
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; }

Expand All @@ -71,7 +71,7 @@ class SerializedRequest : public ::google::protobuf::Message {
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;
Expand Down
8 changes: 6 additions & 2 deletions src/json2pb/json_to_pb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions src/json2pb/pb_to_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading