Skip to content

Commit

Permalink
Make CMake always build test protos into a static library
Browse files Browse the repository at this point in the history
When building with `-Dprotobuf_BUILD_SHARED_LIBS=ON`, we currently put all test
protos into their own shared library. PR #15887 seems to be pushing us over a
limit on the number of exported symbols so that this library no longer links
successfully, though.

This change fixes that problem by always building the test protos into a static
library. This should be fine since it's purely for testing and not meant to be
installed. The only things that depend on it are executables, so we don't need
to worry about ODR violations.

PiperOrigin-RevId: 617929396
  • Loading branch information
acozzette authored and copybara-github committed Mar 21, 2024
1 parent 0641202 commit 7793c72
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 48 deletions.
16 changes: 3 additions & 13 deletions cmake/conformance.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ elseif(protobuf_JSONCPP_PROVIDER STREQUAL "package")
find_package(jsoncpp REQUIRED)
endif()

set(protoc_cpp_args)
if (protobuf_BUILD_SHARED_LIBS)
set(protoc_cpp_args "dllexport_decl=PROTOBUF_TEST_EXPORTS:")
endif ()

file(MAKE_DIRECTORY ${protobuf_BINARY_DIR}/conformance)

add_custom_command(
Expand All @@ -26,7 +21,7 @@ add_custom_command(
DEPENDS ${protobuf_PROTOC_EXE} ${protobuf_SOURCE_DIR}/conformance/conformance.proto
COMMAND ${protobuf_PROTOC_EXE} ${protobuf_SOURCE_DIR}/conformance/conformance.proto
--proto_path=${protobuf_SOURCE_DIR}/conformance
--cpp_out=${protoc_cpp_args}${protobuf_BINARY_DIR}/conformance
--cpp_out=${protobuf_BINARY_DIR}/conformance
)

file(MAKE_DIRECTORY ${protobuf_BINARY_DIR}/src)
Expand All @@ -52,10 +47,10 @@ add_custom_command(
${protobuf_SOURCE_DIR}/src/google/protobuf/editions/golden/test_messages_proto3_editions.proto
${protobuf_SOURCE_DIR}/src/google/protobuf/editions/golden/test_messages_proto2_editions.proto
--proto_path=${protobuf_SOURCE_DIR}/src
--cpp_out=${protoc_cpp_args}${protobuf_BINARY_DIR}/src
--cpp_out=${protobuf_BINARY_DIR}/src
)

add_library(libconformance_common ${protobuf_SHARED_OR_STATIC}
add_library(libconformance_common STATIC
${protobuf_BINARY_DIR}/conformance/conformance.pb.h
${protobuf_BINARY_DIR}/conformance/conformance.pb.cc
${protobuf_BINARY_DIR}/src/google/protobuf/test_messages_proto2.pb.h
Expand All @@ -71,11 +66,6 @@ target_link_libraries(libconformance_common
${protobuf_LIB_PROTOBUF}
${protobuf_ABSL_USED_TARGETS}
)
if(protobuf_BUILD_SHARED_LIBS)
target_compile_definitions(libconformance_common
PUBLIC PROTOBUF_USE_DLLS
PRIVATE LIBPROTOBUF_TEST_EXPORTS)
endif()

add_executable(conformance_test_runner
${protobuf_SOURCE_DIR}/conformance/binary_json_conformance_suite.cc
Expand Down
25 changes: 2 additions & 23 deletions cmake/tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ set(tests_protos
${util_test_protos_files}
)

set(protoc_cpp_args)
if (protobuf_BUILD_SHARED_LIBS)
set(protoc_cpp_args "dllexport_decl=PROTOBUF_TEST_EXPORTS")
endif ()

file(MAKE_DIRECTORY ${protobuf_BINARY_DIR}/src)

set(lite_test_proto_files)
Expand All @@ -32,23 +27,17 @@ foreach(proto_file ${lite_test_protos})
LANGUAGE cpp
OUT_VAR pb_generated_files
IMPORT_DIRS ${protobuf_SOURCE_DIR}/src
PLUGIN_OPTIONS ${protoc_cpp_args}
)
set(lite_test_proto_files ${lite_test_proto_files} ${pb_generated_files})
endforeach(proto_file)

set(tests_proto_files)
foreach(proto_file ${tests_protos})
if (MSVC AND protobuf_BUILD_SHARED_LIBS AND ${proto_file} MATCHES ".*enormous.*")
# Our enormous protos are too big for windows DLLs.
continue()
endif ()
protobuf_generate(
PROTOS ${proto_file}
LANGUAGE cpp
OUT_VAR pb_generated_files
IMPORT_DIRS ${protobuf_SOURCE_DIR}/src
PLUGIN_OPTIONS ${protoc_cpp_args}
)
set(tests_proto_files ${tests_proto_files} ${pb_generated_files})
endforeach(proto_file)
Expand Down Expand Up @@ -96,7 +85,7 @@ else()
set(protobuf_GTEST_ARGS)
endif()

add_library(libtest_common ${protobuf_SHARED_OR_STATIC}
add_library(libtest_common STATIC
${tests_proto_files}
)
target_link_libraries(libtest_common
Expand All @@ -109,11 +98,6 @@ target_link_libraries(libtest_common
if (MSVC)
target_compile_options(libtest_common PRIVATE /bigobj)
endif ()
if(protobuf_BUILD_SHARED_LIBS)
target_compile_definitions(libtest_common
PUBLIC PROTOBUF_USE_DLLS
PRIVATE LIBPROTOBUF_TEST_EXPORTS)
endif()

add_executable(tests ${tests_files} ${common_test_files})
if (MSVC)
Expand Down Expand Up @@ -155,19 +139,14 @@ target_link_libraries(test_plugin
GTest::gmock
)

add_library(libtest_common_lite ${protobuf_SHARED_OR_STATIC}
add_library(libtest_common_lite STATIC
${lite_test_proto_files}
)
target_link_libraries(libtest_common_lite
${protobuf_LIB_PROTOBUF_LITE}
${protobuf_ABSL_USED_TARGETS}
GTest::gmock
)
if(protobuf_BUILD_SHARED_LIBS)
target_compile_definitions(libtest_common_lite
PUBLIC PROTOBUF_USE_DLLS
PRIVATE LIBPROTOBUF_TEST_EXPORTS)
endif()

add_executable(lite-test
${protobuf_lite_test_files}
Expand Down
12 changes: 0 additions & 12 deletions src/google/protobuf/port_def.inc
Original file line number Diff line number Diff line change
Expand Up @@ -367,18 +367,6 @@ static_assert(PROTOBUF_ABSL_MIN(20230125, 3),
# define PROTOC_EXPORT
#endif

#if defined(PROTOBUF_USE_DLLS) && defined(_MSC_VER)
# if defined(LIBPROTOBUF_TEST_EXPORTS)
# define PROTOBUF_TEST_EXPORTS __declspec(dllexport)
# else
# define PROTOBUF_TEST_EXPORTS __declspec(dllimport)
# endif // defined(LIBPROTOBUF_TEST_EXPORTS)
#elif defined(PROTOBUF_USE_DLLS) && defined(LIBPROTOBUF_TEST_EXPORTS)
# define PROTOBUF_TEST_EXPORTS __attribute__((visibility("default")))
#else
# define PROTOBUF_TEST_EXPORTS
#endif

#if defined(PROTOBUF_PREDICT_TRUE) || defined(PROTOBUF_PREDICT_FALSE)
#error PROTOBUF_PREDICT_(TRUE|FALSE) was previously defined
#endif
Expand Down

0 comments on commit 7793c72

Please sign in to comment.