Protocol Buffers - Google's data interchange format (grpc依赖) https://developers.google.com/protocol-buffers/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
7.5 KiB

option(protobuf_REMOVE_INSTALLED_HEADERS
"Remove local headers so that installed ones are used instead" OFF)
option(protobuf_ABSOLUTE_TEST_PLUGIN_PATH
"Using absolute test_plugin path in tests" ON)
mark_as_advanced(protobuf_ABSOLUTE_TEST_PLUGIN_PATH)
include(${protobuf_SOURCE_DIR}/src/file_lists.cmake)
include(${protobuf_SOURCE_DIR}/cmake/protobuf-generate.cmake)
set(lite_test_protos
${protobuf_lite_test_protos_files}
)
set(tests_protos
${protobuf_test_protos_files}
${compiler_test_protos_files}
${util_test_protos_files}
)
Write generated source files to the build tree, not the source tree. (#14455) My org, as part of its reproducible-build hygiene, builds projects with the source trees in read-only mode. I approached the protobuf build in the same way, but encountered this error (among others): ``` [ 30%] Built target statusor [ 34%] Built target libprotobuf-lite [ 45%] Built target libprotobuf Consolidate compiler generated dependencies of target libprotoc [ 62%] Built target libprotoc Consolidate compiler generated dependencies of target protoc [ 62%] Built target protoc Consolidate compiler generated dependencies of target scoped_mock_log [ 62%] Built target scoped_mock_log [ 62%] Generating /home/src/protobuf/src/google/protobuf/util/message_differencer_unittest_proto3.pb.h, /home/src/protobuf/src/google/protobuf/util/message_differencer_unittest_proto3.pb.cc /home/src/protobuf/src/google/protobuf/util/message_differencer_unittest_proto3.pb.cc: Read-only file system make[2]: *** [CMakeFiles/libtest_common.dir/build.make:387: /home/src/protobuf/src/google/protobuf/util/message_differencer_unittest_proto3.pb.h] Error 1 make[1]: *** [CMakeFiles/Makefile2:1194: CMakeFiles/libtest_common.dir/all] Error 2 make: *** [Makefile:146: all] Error 2 ``` (`/home/src` is a read-only mount) Generated source files are effectively build artifacts, and should be written to the build tree, not the source tree. This PR accordingly enables a successful build and test run (lite, full, and conformance) with read-only sources. (Beyond this, I would add that CMake usually does not need source file paths to be qualified with `CMAKE_SOURCE_DIR` or `CMAKE_BINARY_DIR`; in most cases it knows to look in both locations, favoring the latter if a file is in both. The CMake code could be simplified by relying on this behavior.) Closes #14455 COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/14455 from iskunk:feature/ro-source da7510c24a3c77ab09306647ef864a886b690587 PiperOrigin-RevId: 607031010
10 months ago
file(MAKE_DIRECTORY ${protobuf_BINARY_DIR}/src)
set(lite_test_proto_files)
foreach(proto_file ${lite_test_protos})
protobuf_generate(
PROTOS ${proto_file}
LANGUAGE cpp
OUT_VAR pb_generated_files
IMPORT_DIRS ${protobuf_SOURCE_DIR}/src
)
set(lite_test_proto_files ${lite_test_proto_files} ${pb_generated_files})
endforeach(proto_file)
set(tests_proto_files)
foreach(proto_file ${tests_protos})
protobuf_generate(
PROTOS ${proto_file}
LANGUAGE cpp
OUT_VAR pb_generated_files
IMPORT_DIRS ${protobuf_SOURCE_DIR}/src
)
set(tests_proto_files ${tests_proto_files} ${pb_generated_files})
endforeach(proto_file)
set(common_test_files
${test_util_hdrs}
${lite_test_util_srcs}
${test_util_srcs}
${common_test_hdrs}
${common_test_srcs}
)
set(tests_files
${protobuf_test_files}
${compiler_test_files}
${annotation_test_util_srcs}
${editions_test_files}
${io_test_files}
${util_test_files}
${stubs_test_files}
)
if(protobuf_ABSOLUTE_TEST_PLUGIN_PATH)
add_compile_options(-DGOOGLE_PROTOBUF_FAKE_PLUGIN_PATH="$<TARGET_FILE:fake_plugin>")
add_compile_options(-DGOOGLE_PROTOBUF_TEST_PLUGIN_PATH="$<TARGET_FILE:test_plugin>")
endif()
if(MINGW)
set_source_files_properties(${tests_files} PROPERTIES COMPILE_FLAGS "-Wno-narrowing")
# required for tests on MinGW Win64
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,16777216")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wa,-mbig-obj")
endif()
endif()
if(protobuf_TEST_XML_OUTDIR)
if(NOT "${protobuf_TEST_XML_OUTDIR}" MATCHES "[/\\]$")
string(APPEND protobuf_TEST_XML_OUTDIR "/")
endif()
set(protobuf_GTEST_ARGS "--gtest_output=xml:${protobuf_TEST_XML_OUTDIR}")
else()
set(protobuf_GTEST_ARGS)
endif()
add_library(libtest_common 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 ()
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
libtest_common_lite
${protobuf_LIB_PROTOC}
${protobuf_LIB_PROTOBUF}
${protobuf_ABSL_USED_TARGETS}
${protobuf_ABSL_USED_TEST_TARGETS}
GTest::gmock_main
)
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
libtest_common_lite
${protobuf_LIB_PROTOC}
${protobuf_LIB_PROTOBUF}
${protobuf_ABSL_USED_TARGETS}
${protobuf_ABSL_USED_TEST_TARGETS}
GTest::gmock
)
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
libtest_common_lite
${protobuf_LIB_PROTOC}
${protobuf_LIB_PROTOBUF}
${protobuf_ABSL_USED_TARGETS}
${protobuf_ABSL_USED_TEST_TARGETS}
GTest::gmock
)
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
)
add_executable(lite-test
${protobuf_lite_test_files}
${lite_test_util_hdrs}
${lite_test_util_srcs}
)
target_link_libraries(lite-test
libtest_common_lite
${protobuf_LIB_PROTOBUF_LITE}
${protobuf_ABSL_USED_TARGETS}
${protobuf_ABSL_USED_TEST_TARGETS}
GTest::gmock_main
)
add_test(NAME lite-test
COMMAND lite-test ${protobuf_GTEST_ARGS}
WORKING_DIRECTORY ${protobuf_SOURCE_DIR})
add_custom_target(full-test
COMMAND tests
DEPENDS tests lite-test fake_plugin test_plugin
WORKING_DIRECTORY ${protobuf_SOURCE_DIR})
add_test(NAME full-test
COMMAND tests ${protobuf_GTEST_ARGS}
WORKING_DIRECTORY ${protobuf_SOURCE_DIR})
if (protobuf_BUILD_LIBUPB)
set(upb_test_proto_genfiles)
foreach(proto_file ${upb_test_protos_files} ${descriptor_proto_proto_srcs})
foreach(generator upb upbdefs upb_minitable)
protobuf_generate(
PROTOS ${proto_file}
LANGUAGE ${generator}
GENERATE_EXTENSIONS .${generator}.h .${generator}.c
OUT_VAR pb_generated_files
IMPORT_DIRS ${protobuf_SOURCE_DIR}/src
IMPORT_DIRS ${protobuf_SOURCE_DIR}
PLUGIN protoc-gen-${generator}=$<TARGET_FILE:protobuf::protoc-gen-${generator}>
DEPENDENCIES $<TARGET_FILE:protobuf::protoc-gen-${generator}>
)
set(upb_test_proto_genfiles ${upb_test_proto_genfiles} ${pb_generated_files})
endforeach()
endforeach(proto_file)
add_executable(upb-test
${upb_test_files}
${upb_test_proto_genfiles}
${upb_test_util_files})
target_link_libraries(upb-test
${protobuf_LIB_PROTOBUF}
${protobuf_LIB_UPB}
${protobuf_ABSL_USED_TARGETS}
${protobuf_ABSL_USED_TEST_TARGETS}
GTest::gmock_main)
add_test(NAME upb-test
COMMAND upb-test ${protobuf_GTEST_ARGS}
WORKING_DIRECTORY ${protobuf_SOURCE_DIR})
endif()
# For test purposes, remove headers that should already be installed. This
# prevents accidental conflicts and also version skew (since local headers take
# precedence over installed headers).
add_custom_target(save-installed-headers)
add_custom_target(remove-installed-headers)
add_custom_target(restore-installed-headers)
file(GLOB_RECURSE _local_hdrs
"${PROJECT_SOURCE_DIR}/src/*.h"
"${PROJECT_SOURCE_DIR}/src/*.inc"
"${PROJECT_SOURCE_DIR}/upb/*.h"
)
# Exclude the bootstrapping that are directly used by tests.
set(_exclude_hdrs
"${protobuf_SOURCE_DIR}/src/google/protobuf/cpp_features.pb.h"
"${protobuf_SOURCE_DIR}/src/google/protobuf/descriptor.pb.h"
"${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/plugin.pb.h"
"${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/java/java_features.pb.h")
# Exclude test library headers.
list(APPEND _exclude_hdrs ${test_util_hdrs} ${lite_test_util_hdrs} ${common_test_hdrs}
${compiler_test_utils_hdrs} ${upb_test_util_files})
foreach(_hdr ${_exclude_hdrs})
list(REMOVE_ITEM _local_hdrs ${_hdr})
endforeach()
foreach(_hdr ${_local_hdrs})
string(REPLACE "${protobuf_SOURCE_DIR}/src" "" _file ${_hdr})
set(_tmp_file "${CMAKE_BINARY_DIR}/tmp-install-test/${_file}")
add_custom_command(TARGET remove-installed-headers PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E remove -f "${_hdr}")
add_custom_command(TARGET save-installed-headers PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E
copy "${_hdr}" "${_tmp_file}" || true)
add_custom_command(TARGET restore-installed-headers PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E
copy "${_tmp_file}" "${_hdr}")
endforeach()
add_dependencies(remove-installed-headers save-installed-headers)
if(protobuf_REMOVE_INSTALLED_HEADERS)
# Make sure we remove all the headers *before* any codegen occurs.
add_dependencies(${protobuf_PROTOC_EXE} remove-installed-headers)
endif()