Fix path to generated proto sources when embedding

When embedding SAPI in an external CMake project, the version of
`protobuf_generate_cpp` that we lifted from upstream protobuf produces
the wrong generated file paths.

For example, given this project structure:

```
/parent/
+-- myproject/
+-- myproject_build/  <- CMake build directory
+-- sandboxed-api/    <- Checkout from GitHub
```

And a CMake file in `myproject/CMakeLists.txt` that embeds SAPI like
this:

```
cmake_minimum_required(VERSION 3.12)
project(SandboxedTest LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_subdirectory(
  ${PROJECT_SOURCE_DIR}/../sandboxed-api
  ${PROJECT_BINARY_DIR}/sandboxed-api
)
```

Then `protobuf_generate_cpp` correctly invokes the protoc compiler to
generate
`/parent/myproject_build/sandboxed-api/sandboxed_api/proto_arg.proto.pb.cc'.
However, the path of the generated source file that is passed to the C++
compiler will be
`/parent/myproject_build/sandboxed-api/sandboxed_api/../../myproject_build/sandboxed-api/sandboxed_api/proto_arg.pb.cc`.
Note the duplicated project build directory component in the
canonicalized version:
`/parent/myproject_build/myproject_build/sandboxed-api/sandboxed_api/proto_arg.pb.cc`.

This change simple omits the computation of any relative file paths and
simply uses `_pb_PROTOC_OUT_DIR` which defauls to
`CMAKE_CURRENT_BINARY_DIR`, which should always contain the correct
path.

Signed-off-by: Christian Blichmann <mail@blichmann.eu>
This commit is contained in:
Christian Blichmann 2020-03-18 18:47:02 +01:00
parent d4faf10b4a
commit f44cca6c98
7 changed files with 122 additions and 118 deletions

View File

@ -48,10 +48,10 @@ endfunction()
# Helper function that behaves just like Protobuf's protobuf_generate_cpp(),
# except that it strips import paths. This is necessary, because CMake's
# protobuf rules don't work well with imports across different directories.
function(protobuf_generate_cpp SRCS HDRS)
function(sapi_protobuf_generate_cpp SRCS HDRS)
cmake_parse_arguments(_pb "" "EXPORT_MACRO" "" ${ARGN})
if(NOT _pb_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "protobuf_generate_cpp() missing proto files")
message(FATAL_ERROR "sapi_protobuf_generate_cpp() missing proto files")
return()
endif()
@ -65,7 +65,7 @@ function(protobuf_generate_cpp SRCS HDRS)
endforeach()
set(_outvar)
protobuf_generate(APPEND_PATH
sapi_protobuf_generate(APPEND_PATH
LANGUAGE cpp
EXPORT_MACRO ${_pb_EXPORT_MACRO}
OUT_VAR _outvar
@ -83,11 +83,14 @@ function(protobuf_generate_cpp SRCS HDRS)
set(${HDRS} ${${HDRS}} PARENT_SCOPE)
endfunction()
if(NOT COMMAND protobuf_generate)
# Runs the protocol buffer compiler on the given proto files. Compatible
# with the upstream version and included here so we can add_subdirectory()
# the protobuf source tree.
function(protobuf_generate)
# Runs the protocol buffer compiler on the given proto files. Compatible
# with the upstream version and included here so we can add_subdirectory()
# the protobuf source tree.
# One difference to the protobuf version is that this function handles
# relative paths differently, which is relevant when Sandboxed API is
# embedded in another project.
# TODO(cblichmann): We should try and upstream this behavior.
function(sapi_protobuf_generate)
set(_options APPEND_PATH)
set(_singleargs LANGUAGE OUT_VAR EXPORT_MACRO PROTOC_OUT_DIR TARGET)
set(_multiargs PROTOS IMPORT_DIRS GENERATE_EXTENSIONS)
@ -95,12 +98,12 @@ if(NOT COMMAND protobuf_generate)
"${ARGN}")
if(NOT _pb_PROTOS AND NOT _pb_TARGET)
message(FATAL_ERROR "protobuf_generate missing targets or sources")
message(FATAL_ERROR "sapi_protobuf_generate missing targets or sources")
return()
endif()
if(NOT _pb_OUT_VAR AND NOT _pb_TARGET)
message(FATAL_ERROR "protobuf_generate missing target or output var")
message(FATAL_ERROR "sapi_protobuf_generate missing target or output var")
return()
endif()
@ -125,7 +128,7 @@ if(NOT COMMAND protobuf_generate)
set(_pb_GENERATE_EXTENSIONS _pb2.py)
else()
message(FATAL_ERROR
"protobuf_generate given unknown language ${_pb_LANGUAGE}")
"sapi_protobuf_generate given unknown language ${_pb_LANGUAGE}")
return()
endif()
endif()
@ -140,7 +143,8 @@ if(NOT COMMAND protobuf_generate)
endif()
if(NOT _pb_PROTOS)
message(FATAL_ERROR "protobuf_generate could not find any .proto files")
message(FATAL_ERROR
"sapi_protobuf_generate could not find any .proto files")
return()
endif()
@ -167,12 +171,11 @@ if(NOT COMMAND protobuf_generate)
get_filename_component(_abs_file ${_proto} ABSOLUTE)
get_filename_component(_abs_dir ${_abs_file} DIRECTORY)
get_filename_component(_basename ${_proto} NAME_WE)
file(RELATIVE_PATH _rel_dir "${CMAKE_CURRENT_SOURCE_DIR}" "${_abs_dir}")
set(_generated_srcs)
foreach(_ext ${_pb_GENERATE_EXTENSIONS})
list(APPEND _generated_srcs
"${_pb_PROTOC_OUT_DIR}/${_rel_dir}/${_basename}${_ext}")
# Use _pb_PROTOC_OUT_DIR directly without computing a relative path
list(APPEND _generated_srcs "${_pb_PROTOC_OUT_DIR}/${_basename}${_ext}")
endforeach()
list(APPEND _generated_srcs_all ${_generated_srcs})
@ -195,5 +198,4 @@ if(NOT COMMAND protobuf_generate)
if(_pb_TARGET)
target_sources(${_pb_TARGET} PRIVATE ${_generated_srcs_all})
endif()
endfunction()
endif()
endfunction()

View File

@ -18,7 +18,7 @@ add_subdirectory(sandbox2)
add_subdirectory(util)
# sandboxed_api:proto_arg
protobuf_generate_cpp(_sapi_proto_arg_pb_cc _sapi_proto_arg_pb_h
sapi_protobuf_generate_cpp(_sapi_proto_arg_pb_cc _sapi_proto_arg_pb_h
proto_arg.proto
)
add_library(sapi_proto_arg_proto STATIC

View File

@ -13,7 +13,8 @@
# limitations under the License.
# sandboxed_api/examples/stringop/lib:stringop_params_proto
protobuf_generate_cpp(_sapi_stringop_params_pb_cc _sapi_stringop_params_pb_h
sapi_protobuf_generate_cpp(
_sapi_stringop_params_pb_cc _sapi_stringop_params_pb_h
stringop_params.proto
)
# Object library to avoid having to use -Wl,--whole-archive. This simulates

View File

@ -13,7 +13,7 @@
# limitations under the License.
# sandboxed_api/examples/sum/lib:sum_params_proto
protobuf_generate_cpp(_sapi_sum_params_pb_cc _sapi_sum_params_pb_h
sapi_protobuf_generate_cpp(_sapi_sum_params_pb_cc _sapi_sum_params_pb_h
sum_params.proto
)
# Object library to avoid having to use -Wl,--whole-archive. This simulates

View File

@ -80,7 +80,7 @@ target_link_libraries(sandbox2_result PRIVATE
)
# sandboxed_api/sandbox2:logserver_proto
protobuf_generate_cpp(_sandbox2_logserver_pb_h _sandbox2_logserver_pb_cc
sapi_protobuf_generate_cpp(_sandbox2_logserver_pb_h _sandbox2_logserver_pb_cc
logserver.proto
)
add_library(sandbox2_logserver_proto STATIC
@ -463,7 +463,7 @@ target_link_libraries(sandbox2_buffer PRIVATE
)
# sandboxed_api/sandbox2:forkserver_proto
protobuf_generate_cpp(_sandbox2_forkserver_pb_h _sandbox2_forkserver_pb_cc
sapi_protobuf_generate_cpp(_sandbox2_forkserver_pb_h _sandbox2_forkserver_pb_cc
forkserver.proto
)
add_library(sandbox2_forkserver_proto STATIC
@ -478,7 +478,7 @@ target_link_libraries(sandbox2_forkserver_proto PRIVATE
)
# sandboxed_api/sandbox2:mounttree_proto
protobuf_generate_cpp(_sandbox2_mounttree_pb_h _sandbox2_mounttree_pb_cc
sapi_protobuf_generate_cpp(_sandbox2_mounttree_pb_h _sandbox2_mounttree_pb_cc
mounttree.proto
)
add_library(sandbox2_mounttree_proto STATIC
@ -515,7 +515,7 @@ target_link_libraries(sandbox2_comms
)
# sandboxed_api/sandbox2:violation_proto
protobuf_generate_cpp(_sandbox2_violation_pb_cc _sandbox2_violation_pb_h
sapi_protobuf_generate_cpp(_sandbox2_violation_pb_cc _sandbox2_violation_pb_h
violation.proto
)
add_library(sandbox2_violation_proto STATIC
@ -610,7 +610,8 @@ if(SAPI_ENABLE_TESTS)
)
# sandboxed_api/sandbox2:comms_test_proto
protobuf_generate_cpp(_sandbox2_comms_test_pb_h _sandbox2_comms_test_pb_cc
sapi_protobuf_generate_cpp(
_sandbox2_comms_test_pb_h _sandbox2_comms_test_pb_cc
comms_test.proto
)
add_library(sandbox2_comms_test_proto STATIC

View File

@ -40,7 +40,7 @@ target_link_libraries(sandbox2_unwind PRIVATE
)
# sandboxed_api/sandbox2/unwind:unwind_proto
protobuf_generate_cpp(_sandbox2_unwind_pb_h _sandbox2_unwind_pb_cc
sapi_protobuf_generate_cpp(_sandbox2_unwind_pb_h _sandbox2_unwind_pb_cc
unwind.proto
)
add_library(sandbox2_unwind_proto STATIC

View File

@ -13,7 +13,7 @@
# limitations under the License.
# sandboxed_api/util:status_proto
protobuf_generate_cpp(_sapi_util_status_pb_cc _sapi_util_status_pb_h
sapi_protobuf_generate_cpp(_sapi_util_status_pb_cc _sapi_util_status_pb_h
status.proto
)
add_library(sapi_util_status_proto STATIC