Build test protos into a common shared library in cmake.

This will reduce the number of times our test protos are built, and may fix the flakes we're seeing in aarch64.  At the very least, it should reduce them by a factor of 3 and marginally speed up our builds.

PiperOrigin-RevId: 568963559
pull/14206/head
Mike Kruskal 1 year ago committed by Copybara-Service
parent 2c7193b810
commit ed763d0294
  1. 53
      cmake/conformance.cmake
  2. 68
      cmake/tests.cmake
  3. 12
      src/google/protobuf/port_def.inc

@ -12,6 +12,11 @@ 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 ()
add_custom_command(
OUTPUT
${protobuf_SOURCE_DIR}/conformance/conformance.pb.h
@ -19,7 +24,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=${protobuf_SOURCE_DIR}/conformance
--cpp_out=${protoc_cpp_args}${protobuf_SOURCE_DIR}/conformance
)
add_custom_command(
@ -33,33 +38,39 @@ add_custom_command(
COMMAND ${protobuf_PROTOC_EXE} ${protobuf_SOURCE_DIR}/src/google/protobuf/test_messages_proto3.proto
${protobuf_SOURCE_DIR}/src/google/protobuf/test_messages_proto2.proto
--proto_path=${protobuf_SOURCE_DIR}/src
--cpp_out=${protobuf_SOURCE_DIR}/src
--cpp_out=${protoc_cpp_args}${protobuf_SOURCE_DIR}/src
)
add_library(libconformance_common ${protobuf_SHARED_OR_STATIC}
${protobuf_SOURCE_DIR}/conformance/conformance.pb.h
${protobuf_SOURCE_DIR}/conformance/conformance.pb.cc
${protobuf_SOURCE_DIR}/src/google/protobuf/test_messages_proto2.pb.h
${protobuf_SOURCE_DIR}/src/google/protobuf/test_messages_proto2.pb.cc
${protobuf_SOURCE_DIR}/src/google/protobuf/test_messages_proto3.pb.h
${protobuf_SOURCE_DIR}/src/google/protobuf/test_messages_proto3.pb.cc
)
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
${protobuf_SOURCE_DIR}/conformance/binary_json_conformance_suite.h
${protobuf_SOURCE_DIR}/conformance/conformance.pb.h
${protobuf_SOURCE_DIR}/conformance/conformance.pb.cc
${protobuf_SOURCE_DIR}/conformance/conformance_test.cc
${protobuf_SOURCE_DIR}/conformance/conformance_test_runner.cc
${protobuf_SOURCE_DIR}/conformance/conformance_test_main.cc
${protobuf_SOURCE_DIR}/conformance/text_format_conformance_suite.cc
${protobuf_SOURCE_DIR}/conformance/text_format_conformance_suite.h
${protobuf_SOURCE_DIR}/src/google/protobuf/test_messages_proto2.pb.h
${protobuf_SOURCE_DIR}/src/google/protobuf/test_messages_proto2.pb.cc
${protobuf_SOURCE_DIR}/src/google/protobuf/test_messages_proto3.pb.h
${protobuf_SOURCE_DIR}/src/google/protobuf/test_messages_proto3.pb.cc
)
add_executable(conformance_cpp
${protobuf_SOURCE_DIR}/conformance/conformance.pb.h
${protobuf_SOURCE_DIR}/conformance/conformance.pb.cc
${protobuf_SOURCE_DIR}/conformance/conformance_cpp.cc
${protobuf_SOURCE_DIR}/src/google/protobuf/test_messages_proto2.pb.h
${protobuf_SOURCE_DIR}/src/google/protobuf/test_messages_proto2.pb.cc
${protobuf_SOURCE_DIR}/src/google/protobuf/test_messages_proto3.pb.h
${protobuf_SOURCE_DIR}/src/google/protobuf/test_messages_proto3.pb.cc
)
target_include_directories(
@ -73,10 +84,16 @@ target_include_directories(
target_include_directories(conformance_test_runner PRIVATE ${ABSL_ROOT_DIR})
target_include_directories(conformance_cpp PRIVATE ${ABSL_ROOT_DIR})
target_link_libraries(conformance_test_runner ${protobuf_LIB_PROTOBUF})
target_link_libraries(conformance_test_runner ${protobuf_ABSL_USED_TARGETS})
target_link_libraries(conformance_cpp ${protobuf_LIB_PROTOBUF})
target_link_libraries(conformance_cpp ${protobuf_ABSL_USED_TARGETS})
target_link_libraries(conformance_test_runner
libconformance_common
${protobuf_LIB_PROTOBUF}
${protobuf_ABSL_USED_TARGETS}
)
target_link_libraries(conformance_cpp
libconformance_common
${protobuf_LIB_PROTOBUF}
${protobuf_ABSL_USED_TARGETS}
)
add_test(NAME conformance_cpp_test
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/conformance_test_runner

@ -18,6 +18,10 @@ set(tests_protos
${lite_test_protos}
)
set(protoc_cpp_args)
if (protobuf_BUILD_SHARED_LIBS)
set(protoc_cpp_args "dllexport_decl=PROTOBUF_TEST_EXPORTS:")
endif ()
macro(compile_proto_file filename)
string(REPLACE .proto .pb.h pb_hdr ${filename})
string(REPLACE .proto .pb.cc pb_src ${filename})
@ -26,7 +30,7 @@ macro(compile_proto_file filename)
DEPENDS ${protobuf_PROTOC_EXE} ${filename}
COMMAND ${protobuf_PROTOC_EXE} ${filename}
--proto_path=${protobuf_SOURCE_DIR}/src
--cpp_out=${protobuf_SOURCE_DIR}/src
--cpp_out=${protoc_cpp_args}${protobuf_SOURCE_DIR}/src
--experimental_allow_proto3_optional
)
endmacro(compile_proto_file)
@ -39,6 +43,10 @@ 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 ()
compile_proto_file(${proto_file})
set(tests_proto_files ${tests_proto_files} ${pb_src} ${pb_hdr})
endforeach(proto_file)
@ -85,17 +93,33 @@ else()
set(protobuf_GTEST_ARGS)
endif()
add_executable(tests
${tests_files}
${common_test_files}
add_library(libtest_common ${protobuf_SHARED_OR_STATIC}
${tests_proto_files}
)
target_link_libraries(libtest_common
${protobuf_LIB_PROTOC}
${protobuf_LIB_PROTOBUF}
${protobuf_ABSL_USED_TARGETS}
${protobuf_ABSL_USED_TEST_TARGETS}
GTest::gmock
)
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)
target_compile_options(tests PRIVATE
/wd4146 # unary minus operator applied to unsigned type, result still unsigned
)
endif()
target_link_libraries(tests
libtest_common
${protobuf_LIB_PROTOC}
${protobuf_LIB_PROTOBUF}
${protobuf_ABSL_USED_TARGETS}
@ -103,22 +127,10 @@ target_link_libraries(tests
GTest::gmock_main
)
set(fake_plugin_files
${fake_plugin_files}
${common_test_hdrs}
${common_test_srcs}
${tests_proto_files}
)
set(test_plugin_files
${test_plugin_files}
${common_test_hdrs}
${common_test_srcs}
${tests_proto_files}
)
add_executable(fake_plugin ${fake_plugin_files})
add_executable(fake_plugin ${fake_plugin_files} ${common_test_files})
target_include_directories(fake_plugin PRIVATE ${ABSL_ROOT_DIR})
target_link_libraries(fake_plugin
libtest_common
${protobuf_LIB_PROTOC}
${protobuf_LIB_PROTOBUF}
${protobuf_ABSL_USED_TARGETS}
@ -126,9 +138,10 @@ target_link_libraries(fake_plugin
GTest::gmock
)
add_executable(test_plugin ${test_plugin_files})
add_executable(test_plugin ${test_plugin_files} ${common_test_files})
target_include_directories(test_plugin PRIVATE ${ABSL_ROOT_DIR})
target_link_libraries(test_plugin
libtest_common
${protobuf_LIB_PROTOC}
${protobuf_LIB_PROTOBUF}
${protobuf_ABSL_USED_TARGETS}
@ -136,12 +149,27 @@ target_link_libraries(test_plugin
GTest::gmock
)
add_library(libtest_common_lite ${protobuf_SHARED_OR_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}
${lite_test_util_hdrs}
${lite_test_util_srcs}
${lite_test_proto_files}
)
target_link_libraries(lite-test
libtest_common_lite
${protobuf_LIB_PROTOBUF_LITE}
${protobuf_ABSL_USED_TARGETS}
${protobuf_ABSL_USED_TEST_TARGETS}

@ -528,6 +528,18 @@ 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

Loading…
Cancel
Save