sandboxed-api/sandboxed_api/util/CMakeLists.txt
Christian Blichmann f44cca6c98 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>
2020-03-18 18:47:02 +01:00

118 lines
2.9 KiB
CMake

# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# sandboxed_api/util:status_proto
sapi_protobuf_generate_cpp(_sapi_util_status_pb_cc _sapi_util_status_pb_h
status.proto
)
add_library(sapi_util_status_proto STATIC
${_sapi_util_status_pb_cc}
${_sapi_util_status_pb_h}
)
add_library(sapi::status_proto ALIAS sapi_util_status_proto)
target_link_libraries(sapi_util_status_proto
PRIVATE sapi::base
PUBLIC protobuf::libprotobuf
)
# sandboxed_api/util:status
add_library(sapi_util_status STATIC
status.cc
status.h
status_macros.h
)
add_library(sapi::status ALIAS sapi_util_status)
target_link_libraries(sapi_util_status
PRIVATE absl::core_headers
absl::status
absl::strings
sapi::base
protobuf::libprotobuf
PUBLIC sapi::status_proto
)
# sandboxed_api/util:statusor
add_library(sapi_util_statusor STATIC
statusor.h
)
add_library(sapi::statusor ALIAS sapi_util_statusor)
target_link_libraries(sapi_util_statusor PRIVATE
absl::base
absl::core_headers
absl::variant
sapi::raw_logging
sapi::status
)
# sandboxed_api/util:flag
add_library(sapi_util_flags STATIC
flag.h
)
add_library(sapi::flags ALIAS sapi_util_flags)
target_link_libraries(sapi_util_flags PUBLIC
gflags::gflags
)
# sandboxed_api/util:raw_logging
add_library(sapi_util_raw_logging STATIC
raw_logging.cc
raw_logging.h
)
add_library(sapi::raw_logging ALIAS sapi_util_raw_logging)
target_link_libraries(sapi_util_raw_logging PRIVATE
absl::str_format
absl::strings
sandbox2::strerror
sapi::base
)
if(SAPI_ENABLE_TESTS)
# sandboxed_api/util:status_matchers
add_library(sapi_util_status_matchers STATIC
status_matchers.h
)
add_library(sapi::status_matchers ALIAS sapi_util_status_matchers)
target_link_libraries(sapi_util_status_matchers
PRIVATE absl::optional
gmock
gtest
sapi::base
PUBLIC absl::status
sapi::status
sapi::statusor
)
# sandboxed_api/util:status_test
add_executable(status_test
status_test.cc
)
target_link_libraries(status_test PRIVATE
sapi::status_matchers
sapi::test_main
absl::type_traits
)
gtest_discover_tests(status_test)
# sandboxed_api/util:statusor_test
add_executable(statusor_test
statusor_test.cc
)
target_link_libraries(statusor_test PRIVATE
sapi::status_matchers
sapi::test_main
absl::type_traits
)
gtest_discover_tests(statusor_test)
endif()