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 da7510c24a
PiperOrigin-RevId: 607031010
pull/15841/head
Daniel Richard G 9 months ago committed by Copybara-Service
parent c32c04c7ed
commit a8181d9ae8
  1. 1
      CMakeLists.txt
  2. 48
      cmake/conformance.cmake
  3. 10
      cmake/tests.cmake

@ -281,6 +281,7 @@ include_directories(
${protobuf_BINARY_DIR}
# Support #include-ing other top-level directories, i.e. upb_generator.
${protobuf_SOURCE_DIR}
${protobuf_BINARY_DIR}/src
${protobuf_SOURCE_DIR}/src)
set(protobuf_ABSL_PROVIDER "module" CACHE STRING "Provider of absl library")

@ -17,26 +17,30 @@ 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(
OUTPUT
${protobuf_SOURCE_DIR}/conformance/conformance.pb.h
${protobuf_SOURCE_DIR}/conformance/conformance.pb.cc
${protobuf_BINARY_DIR}/conformance/conformance.pb.h
${protobuf_BINARY_DIR}/conformance/conformance.pb.cc
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_SOURCE_DIR}/conformance
--cpp_out=${protoc_cpp_args}${protobuf_BINARY_DIR}/conformance
)
file(MAKE_DIRECTORY ${protobuf_BINARY_DIR}/src)
add_custom_command(
OUTPUT
${protobuf_SOURCE_DIR}/src/google/protobuf/test_messages_proto3.pb.h
${protobuf_SOURCE_DIR}/src/google/protobuf/test_messages_proto3.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/editions/golden/test_messages_proto3_editions.pb.h
${protobuf_SOURCE_DIR}/src/google/protobuf/editions/golden/test_messages_proto3_editions.pb.cc
${protobuf_SOURCE_DIR}/src/google/protobuf/editions/golden/test_messages_proto2_editions.pb.h
${protobuf_SOURCE_DIR}/src/google/protobuf/editions/golden/test_messages_proto2_editions.pb.cc
${protobuf_BINARY_DIR}/src/google/protobuf/test_messages_proto3.pb.h
${protobuf_BINARY_DIR}/src/google/protobuf/test_messages_proto3.pb.cc
${protobuf_BINARY_DIR}/src/google/protobuf/test_messages_proto2.pb.h
${protobuf_BINARY_DIR}/src/google/protobuf/test_messages_proto2.pb.cc
${protobuf_BINARY_DIR}/src/google/protobuf/editions/golden/test_messages_proto3_editions.pb.h
${protobuf_BINARY_DIR}/src/google/protobuf/editions/golden/test_messages_proto3_editions.pb.cc
${protobuf_BINARY_DIR}/src/google/protobuf/editions/golden/test_messages_proto2_editions.pb.h
${protobuf_BINARY_DIR}/src/google/protobuf/editions/golden/test_messages_proto2_editions.pb.cc
DEPENDS ${protobuf_PROTOC_EXE}
${protobuf_SOURCE_DIR}/src/google/protobuf/test_messages_proto3.proto
${protobuf_SOURCE_DIR}/src/google/protobuf/test_messages_proto2.proto
@ -48,20 +52,20 @@ 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_SOURCE_DIR}/src
--cpp_out=${protoc_cpp_args}${protobuf_BINARY_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
${protobuf_SOURCE_DIR}/src/google/protobuf/editions/golden/test_messages_proto3_editions.pb.h
${protobuf_SOURCE_DIR}/src/google/protobuf/editions/golden/test_messages_proto3_editions.pb.cc
${protobuf_SOURCE_DIR}/src/google/protobuf/editions/golden/test_messages_proto2_editions.pb.h
${protobuf_SOURCE_DIR}/src/google/protobuf/editions/golden/test_messages_proto2_editions.pb.cc
${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
${protobuf_BINARY_DIR}/src/google/protobuf/test_messages_proto2.pb.cc
${protobuf_BINARY_DIR}/src/google/protobuf/test_messages_proto3.pb.h
${protobuf_BINARY_DIR}/src/google/protobuf/test_messages_proto3.pb.cc
${protobuf_BINARY_DIR}/src/google/protobuf/editions/golden/test_messages_proto3_editions.pb.h
${protobuf_BINARY_DIR}/src/google/protobuf/editions/golden/test_messages_proto3_editions.pb.cc
${protobuf_BINARY_DIR}/src/google/protobuf/editions/golden/test_messages_proto2_editions.pb.h
${protobuf_BINARY_DIR}/src/google/protobuf/editions/golden/test_messages_proto2_editions.pb.cc
)
target_link_libraries(libconformance_common
${protobuf_LIB_PROTOBUF}

@ -21,15 +21,19 @@ 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)
macro(compile_proto_file filename)
string(REPLACE .proto .pb.h pb_hdr ${filename})
string(REPLACE .proto .pb.cc pb_src ${filename})
file(RELATIVE_PATH rel_filename ${protobuf_SOURCE_DIR} ${filename})
string(REPLACE .proto .pb.h pb_hdr ${rel_filename})
string(REPLACE .proto .pb.cc pb_src ${rel_filename})
add_custom_command(
OUTPUT ${pb_hdr} ${pb_src}
DEPENDS ${protobuf_PROTOC_EXE} ${filename}
COMMAND ${protobuf_PROTOC_EXE} ${filename}
--proto_path=${protobuf_SOURCE_DIR}/src
--cpp_out=${protoc_cpp_args}${protobuf_SOURCE_DIR}/src
--cpp_out=${protoc_cpp_args}${protobuf_BINARY_DIR}/src
--experimental_allow_proto3_optional
)
endmacro(compile_proto_file)

Loading…
Cancel
Save