Skip to content

Commit

Permalink
upgrade protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
cassio-lazaro committed Feb 12, 2025
1 parent daf383b commit 0407871
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 95 deletions.
33 changes: 29 additions & 4 deletions external/protobuf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ if (EMIL_HOST_BUILD)
FetchContent_Declare(
protobuf
GIT_REPOSITORY https://github.com/protocolbuffers/protobuf
GIT_TAG f0dc78d7e6e331b8c6bb2d5283e06aa26883ca7c # v21.12
GIT_TAG b407e8416e3893036aee5af9a12bd9b6a0e2b2e6 # v29.3
)

set_directory_properties(PROPERTIES EXCLUDE_FROM_ALL On)
Expand All @@ -15,18 +15,42 @@ if (EMIL_HOST_BUILD)
$<$<CXX_COMPILER_ID:GNU>:-Wno-stringop-overflow>
)

set(protobuf_BUILD_TESTS Off CACHE INTERNAL "")
set(protobuf_BUILD_PROTOBUF_BINARIES Off CACHE INTERNAL "")
set(protobuf_VERBOSE On CACHE INTERNAL "")
set(protobuf_BUILD_LIBPROTOC On CACHE INTERNAL "")
set(protobuf_BUILD_PROTOC_BINARIES On CACHE INTERNAL "")
set(protobuf_MSVC_STATIC_RUNTIME Off CACHE INTERNAL "")
set(protobuf_WITH_ZLIB Off CACHE INTERNAL "")
set(protobuf_BUILD_TESTS Off CACHE INTERNAL "")
set(protobuf_INSTALL Off CACHE INTERNAL "")

set(ABSL_PROPAGATE_CXX_STD On CACHE INTERNAL "")
set(ABSL_FIND_GOOGLETEST On CACHE INTERNAL "")
set(ABSL_USE_EXTERNAL_GOOGLETEST Off CACHE INTERNAL "")
set(ABSL_BUILD_TESTING Off CACHE INTERNAL "")
set(ABSL_BUILD_TEST_HELPERS Off CACHE INTERNAL "")
set(ABSL_ENABLE_INSTALL Off CACHE INTERNAL "")

FetchContent_MakeAvailable(protobuf)


function(workaround_to_build_protobuf)
message(STATUS "CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}")

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(absl_strings PRIVATE -Wno-array-bounds)
endif()

if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
message(STATUS "Remove warning as error for utf8_range target")
# set_target_properties(utf8_range PROPERTIES COMPILE_WARNING_AS_ERROR Off)
set_target_properties(utf8_validity PROPERTIES COMPILE_WARNING_AS_ERROR Off)
endif()
endfunction()

function(add_protobuf_target_properties)
foreach(target ${ARGN})
set_target_properties(${target} PROPERTIES FOLDER External/Protobuf)
if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
# The "invalid noreturn" warning can't be ignored on GCC
# Disable warning-as-error for these targets completely
# This should be re-evaluated when https://github.com/protocolbuffers/protobuf/issues/9817 is fixed
Expand All @@ -35,5 +59,6 @@ if (EMIL_HOST_BUILD)
endforeach()
endfunction()

workaround_to_build_protobuf()
add_protobuf_target_properties(libprotobuf libprotoc)
endif()
2 changes: 1 addition & 1 deletion external/protoc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function(emil_fetch_protocol_buffer_compiler)
set(protobuf_version "21.12")
set(protobuf_version "29.3")

if (CMAKE_HOST_WIN32)
set(os_postfix "win32")
Expand Down
4 changes: 2 additions & 2 deletions hal/interfaces/test_doubles/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_library(hal.interfaces_test_doubles STATIC)
emil_build_for(hal.interfaces_test_doubles BOOL BUILD_TESTING)
add_library(hal.interfaces_test_doubles STATIC EXCLUDE_FROM_ALL)
# emil_build_for(hal.interfaces_test_doubles BOOL BUILD_TESTING)

target_link_libraries(hal.interfaces_test_doubles PRIVATE
hal.interfaces
Expand Down
4 changes: 2 additions & 2 deletions hal/synchronous_interfaces/test_doubles/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_library(hal.synchronous_interfaces_test_doubles STATIC)
emil_build_for(hal.synchronous_interfaces_test_doubles BOOL BUILD_TESTING)
add_library(hal.synchronous_interfaces_test_doubles STATIC EXCLUDE_FROM_ALL)
# emil_build_for(hal.synchronous_interfaces_test_doubles BOOL BUILD_TESTING)

target_link_libraries(hal.synchronous_interfaces_test_doubles PUBLIC
hal.synchronous_interfaces
Expand Down
3 changes: 2 additions & 1 deletion infra/syntax/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ if (EMIL_HOST_BUILD)
)

target_link_libraries(infra.syntax PUBLIC
protobuf::libprotoc
libprotoc
pugixml
libprotobuf
)
endif()

Expand Down
4 changes: 2 additions & 2 deletions infra/timer/test_helper/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_library(infra.timer_test_helper STATIC)
emil_build_for(infra.timer_test_helper BOOL BUILD_TESTING)
add_library(infra.timer_test_helper STATIC EXCLUDE_FROM_ALL)
# emil_build_for(infra.timer_test_helper BOOL BUILD_TESTING)

target_link_libraries(infra.timer_test_helper PRIVATE
infra.timer
Expand Down
4 changes: 2 additions & 2 deletions infra/util/test_helper/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_library(infra.util_test_helper INTERFACE)
emil_build_for(infra.util_test_helper BOOL BUILD_TESTING)
add_library(infra.util_test_helper INTERFACE EXCLUDE_FROM_ALL)
# emil_build_for(infra.util_test_helper BOOL BUILD_TESTING)

target_link_libraries(infra.util_test_helper INTERFACE
gmock
Expand Down
5 changes: 2 additions & 3 deletions protobuf/echo/test/TestMessages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,8 @@ message TestOptionalEverything {
optional bool v9 = 10;
optional string v10 = 11 [(string_size) = 20];
optional string v11 = 12;
optional TestUInt32 v12 = 13;
optional bytes v13 = 14 [(bytes_size) = 10];
optional bytes v14 = 15;
optional bytes v12 = 13 [(bytes_size) = 10];
optional bytes v13 = 14;
}

message TestNestedMessage
Expand Down
29 changes: 16 additions & 13 deletions protobuf/echo/test/TestProtoMessageReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,24 +252,28 @@ TEST(ProtoMessageReceiverTest, parse_optionals_all_none)
EXPECT_EQ(infra::none, receiver.message.v9);
EXPECT_EQ(infra::none, receiver.message.v10);
EXPECT_EQ(infra::none, receiver.message.v11);
EXPECT_EQ(infra::none, receiver.message.v12);
EXPECT_EQ(infra::none, receiver.message.v13);
EXPECT_EQ(infra::none, receiver.message.v14);
}

TEST(ProtoMessageReceiverTest, parse_optionals_all_filled)
{
services::ProtoMessageReceiver<test_messages::TestOptionalEverything> receiver;

infra::StdVectorInputStreamReader::WithStorage data(infra::inPlace, std::initializer_list<uint8_t>{
0x08, 0x01, 0x10, 0x02, 0x18, 0x03, 0x20, 0x04,
0x28, 0x05, 0x35, 0x06, 0x00, 0x00, 0x00, 0x39,
0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x41, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x4d, 0x09, 0x00, 0x00, 0x00, 0x50, 0x01,
0x5a, 0x01, 0x61, 0x62, 0x01, 0x62, 0x6a, 0x02,
0x08, 0x0a, 0x72, 0x02, 0x0b, 0x0b, 0x7a, 0x02,
0x0c, 0x0c });
0x08, 0x01,
0x10, 0x02,
0x18, 0x03,
0x20, 0x04,
0x28, 0x05,
0x35, 0x06, 0x00, 0x00, 0x00,
0x39, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x41, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x4d, 0x09, 0x00, 0x00, 0x00,
0x50, 0x01,
0x5a, 0x01, 0x61,
0x62, 0x01, 0x62,
0x6a, 0x02, 0x0b, 0x0b,
0x72, 0x02, 0x0c, 0x0c });
receiver.Feed(data);

EXPECT_EQ(test_messages::Enumeration::val1, *receiver.message.v0);
Expand All @@ -284,7 +288,6 @@ TEST(ProtoMessageReceiverTest, parse_optionals_all_filled)
EXPECT_EQ(true, *receiver.message.v9);
EXPECT_EQ("a", *receiver.message.v10);
EXPECT_EQ("b", *receiver.message.v11);
EXPECT_EQ(test_messages::TestUInt32(10), *receiver.message.v12);
EXPECT_EQ((infra::BoundedVector<uint8_t>::WithMaxSize<2>(static_cast<std::size_t>(2), 11)), *receiver.message.v13);
EXPECT_EQ((std::vector<uint8_t>(2, 12)), *receiver.message.v14);
EXPECT_EQ((infra::BoundedVector<uint8_t>::WithMaxSize<2>(static_cast<std::size_t>(2), 11)), *receiver.message.v12);
EXPECT_EQ((std::vector<uint8_t>(2, 12)), *receiver.message.v13);
}
26 changes: 16 additions & 10 deletions protobuf/echo/test/TestProtoMessageSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,21 +254,27 @@ TEST_F(ProtoMessageSenderTest, format_optionals_all_filled)
message.v9 = true;
message.v10 = "a";
message.v11 = "b";
message.v12 = test_messages::TestUInt32(10);
message.v13.Emplace(static_cast<std::size_t>(2), 11);
message.v14.Emplace(2, 12);
message.v12.Emplace(static_cast<std::size_t>(2), 11);
message.v13.Emplace(2, 12);
services::ProtoMessageSender sender{ message };

infra::StdVectorOutputStream::WithStorage stream;
sender.Fill(stream);
EXPECT_EQ((std::vector<uint8_t>{
0x08, 0x01, 0x10, 0x02, 0x18, 0x03, 0x20, 0x04,
0x28, 0x05, 0x35, 0x06, 0x00, 0x00, 0x00, 0x39,
0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x01,
0x10, 0x02,
0x18, 0x03,
0x20, 0x04,
0x28, 0x05,
0x35, 0x06, 0x00, 0x00, 0x00,
0x39, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x41, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x4d, 0x09, 0x00, 0x00, 0x00, 0x50, 0x01,
0x5a, 0x01, 0x61, 0x62, 0x01, 0x62, 0x6a, 0x02,
0x08, 0x0a, 0x72, 0x02, 0x0b, 0x0b, 0x7a, 0x02,
0x0c, 0x0c }),
0x00,
0x4d, 0x09, 0x00, 0x00, 0x00,
0x50, 0x01,
0x5a, 0x01, 0x61,
0x62, 0x01, 0x62,
0x6a, 0x02, 0x0b, 0x0b,
0x72, 0x02, 0x0c, 0x0c }),
stream.Storage());
}
4 changes: 2 additions & 2 deletions protobuf/echo/test_doubles/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_library(protobuf.test_doubles STATIC)
emil_build_for(protobuf.test_doubles BOOL BUILD_TESTING)
add_library(protobuf.test_doubles STATIC EXCLUDE_FROM_ALL)
# emil_build_for(protobuf.test_doubles BOOL BUILD_TESTING)

target_link_libraries(protobuf.test_doubles PUBLIC
gmock
Expand Down
3 changes: 2 additions & 1 deletion protobuf/echo_attributes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ if (EMIL_HOST_BUILD)
set_target_properties(protobuf.echo_attributes PROPERTIES POSITION_INDEPENDENT_CODE ON)

target_link_libraries(protobuf.echo_attributes PUBLIC
protobuf::libprotoc
libprotoc
libprotobuf
)

target_compile_definitions(protobuf.echo_attributes PRIVATE
Expand Down
30 changes: 23 additions & 7 deletions protobuf/protoc_echo_plugin/EchoObjects.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "protobuf/protoc_echo_plugin/EchoObjects.hpp"
#include "generated/EchoAttributes.pb.h"
#include "google/protobuf/compiler/cpp/helpers.h"
#include "google/protobuf/stubs/strutil.h"
#include "infra/syntax/ProtoFormatter.hpp"

namespace application
Expand Down Expand Up @@ -91,6 +90,23 @@ namespace application

return namespaceString + descriptor.name() + "Reference";
}

std::vector<std::string> Split(const std::string& str, const char* delimiter)
{
std::vector<std::string> tokens;
std::string::size_type start = 0;
std::string::size_type end = str.find(delimiter);

while (end != std::string::npos)
{
tokens.push_back(str.substr(start, end - start));
start = end + 1;
end = str.find(delimiter, start);
}
tokens.push_back(str.substr(start));

return tokens;
}
}

EchoField::EchoField(const google::protobuf::FieldDescriptor& descriptor)
Expand All @@ -107,7 +123,7 @@ namespace application

std::shared_ptr<EchoField> EchoField::GenerateField(const google::protobuf::FieldDescriptor& fieldDescriptor, EchoRoot& root)
{
if (fieldDescriptor.has_optional_keyword())
if (fieldDescriptor.is_optional() && fieldDescriptor.has_presence() && fieldDescriptor.type() != google::protobuf::FieldDescriptor::TYPE_MESSAGE)
switch (fieldDescriptor.type())
{
case google::protobuf::FieldDescriptor::TYPE_INT64:
Expand Down Expand Up @@ -480,7 +496,7 @@ namespace application
, maxStringSize(descriptor.options().GetExtension(string_size))
{
assert(maxStringSize != 0);
protoReferenceType = protoType = "services::ProtoString<" + google::protobuf::SimpleItoa(maxStringSize) + ">";
protoReferenceType = protoType = "services::ProtoString<" + std::to_string(maxStringSize) + ">";
}

void EchoFieldString::Accept(EchoFieldVisitor& visitor) const
Expand Down Expand Up @@ -518,7 +534,7 @@ namespace application
if (maxBytesSize == 0)
throw UnspecifiedBytesSize{ name };

protoReferenceType = protoType = "services::ProtoBytes<" + google::protobuf::SimpleItoa(maxBytesSize) + ">";
protoReferenceType = protoType = "services::ProtoBytes<" + std::to_string(maxBytesSize) + ">";
}

void EchoFieldBytes::Accept(EchoFieldVisitor& visitor) const
Expand Down Expand Up @@ -606,8 +622,8 @@ namespace application
if (maxArraySize == 0)
throw UnspecifiedArraySize{ name };

protoType = "services::ProtoRepeated<" + google::protobuf::SimpleItoa(maxArraySize) + ", " + type->protoType + ">";
protoReferenceType = "services::ProtoRepeated<" + google::protobuf::SimpleItoa(maxArraySize) + ", " + type->protoReferenceType + ">";
protoType = "services::ProtoRepeated<" + std::to_string(maxArraySize) + ", " + type->protoType + ">";
protoReferenceType = "services::ProtoRepeated<" + std::to_string(maxArraySize) + ", " + type->protoReferenceType + ">";
}

void EchoFieldRepeated::Accept(EchoFieldVisitor& visitor) const
Expand Down Expand Up @@ -661,7 +677,7 @@ namespace application
EchoFile::EchoFile(const google::protobuf::FileDescriptor& file, EchoRoot& root)
{
name = google::protobuf::compiler::cpp::StripProto(file.name());
packageParts = google::protobuf::Split(file.package(), ".", true);
packageParts = Split(file.package(), ".");

for (int i = 0; i != file.dependency_count(); ++i)
if (file.dependency(i)->name() != "EchoAttributes.proto")
Expand Down
Loading

0 comments on commit 0407871

Please sign in to comment.