Make CMake always build test protos into a static library

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
pull/16251/head
Adam Cozzette 2024-03-21 12:34:18 -07:00 committed by Copybara-Service
parent 0641202bcc
commit 7793c72eab
3 changed files with 5 additions and 48 deletions

View File

@ -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(
@ -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)
@ -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
@ -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

View File

@ -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)
@ -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)
@ -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
@ -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)
@ -155,7 +139,7 @@ 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
@ -163,11 +147,6 @@ target_link_libraries(libtest_common_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}

View File

@ -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