Add CMake options to exclude examples and tests from build

PiperOrigin-RevId: 258136393
Change-Id: Ifb2d7a24f93cd1d2782b4e4d6ac2c34a0d1c2bff
This commit is contained in:
Christian Blichmann 2019-07-15 04:42:39 -07:00 committed by Copybara-Service
parent 99ac7fa60d
commit 6d33c1f908
9 changed files with 536 additions and 501 deletions

View File

@ -25,10 +25,13 @@ project(sandboxed_api C CXX ASM)
# SAPI-wide setting for the language level # SAPI-wide setting for the language level
set(SAPI_CXX_STANDARD 11) set(SAPI_CXX_STANDARD 11)
# Sapi CMake modules, order matters
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
include(SapiOptions)
include(SapiDeps) include(SapiDeps)
include(SapiUtil) # Needs to come after SapiDeps include(SapiUtil)
include(SapiBuildDefs) include(SapiBuildDefs)
include(GoogleTest) include(GoogleTest)
# Make Bazel-style includes work # Make Bazel-style includes work

View File

@ -33,6 +33,7 @@ if(_sapi_saved_CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD "${_sapi_saved_CMAKE_CXX_STANDARD}") set(CMAKE_CXX_STANDARD "${_sapi_saved_CMAKE_CXX_STANDARD}")
endif() endif()
if(SAPI_ENABLE_TESTS)
# Build Googletest directly, as recommended upstream # Build Googletest directly, as recommended upstream
find_path(googletest_src_dir find_path(googletest_src_dir
googletest/include/gtest/gtest.h googletest/include/gtest/gtest.h
@ -43,6 +44,7 @@ set(gtest_force_shared_crt ON CACHE BOOL "")
add_subdirectory(${googletest_src_dir} add_subdirectory(${googletest_src_dir}
${PROJECT_BINARY_DIR}/Dependencies/Build/googletest ${PROJECT_BINARY_DIR}/Dependencies/Build/googletest
EXCLUDE_FROM_ALL) EXCLUDE_FROM_ALL)
endif()
# Prefer to use static libraries # Prefer to use static libraries
set(_sapi_saved_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES}) set(_sapi_saved_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
@ -56,7 +58,9 @@ find_package(gflags REQUIRED)
find_package(glog REQUIRED) find_package(glog REQUIRED)
find_package(Libcap REQUIRED) find_package(Libcap REQUIRED)
find_package(Libffi REQUIRED) find_package(Libffi REQUIRED)
if(SAPI_ENABLE_EXAMPLES)
find_package(ZLIB REQUIRED) find_package(ZLIB REQUIRED)
endif()
find_package(Protobuf REQUIRED) find_package(Protobuf REQUIRED)
if(CMAKE_VERSION VERSION_LESS "3.12") if(CMAKE_VERSION VERSION_LESS "3.12")

16
cmake/SapiOptions.cmake Normal file
View File

@ -0,0 +1,16 @@
# Copyright 2019 Google LLC. All Rights Reserved.
#
# 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.
option(SAPI_ENABLE_EXAMPLES "Build example code" ON)
option(SAPI_ENABLE_TESTS "Build unit tests" ON)

View File

@ -29,6 +29,7 @@ sapi_cc_embed_data(NAME filewrapper_embedded
SOURCES testdata/filewrapper_embedded.bin SOURCES testdata/filewrapper_embedded.bin
) )
if(SAPI_ENABLE_TESTS)
# sandboxed_api/bazel:filewrapper_test # sandboxed_api/bazel:filewrapper_test
add_executable(filewrapper_test add_executable(filewrapper_test
filewrapper_test.cc filewrapper_test.cc
@ -47,3 +48,4 @@ gtest_discover_tests(filewrapper_test PROPERTIES
ENVIRONMENT "TEST_TMPDIR=/tmp" ENVIRONMENT "TEST_TMPDIR=/tmp"
ENVIRONMENT "TEST_SRCDIR=${PROJECT_BINARY_DIR}" ENVIRONMENT "TEST_SRCDIR=${PROJECT_BINARY_DIR}"
) )
endif()

View File

@ -12,5 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
if(SAPI_ENABLE_EXAMPLES)
add_subdirectory(stringop) add_subdirectory(stringop)
add_subdirectory(sum) add_subdirectory(sum)
endif()

View File

@ -13,7 +13,6 @@
# limitations under the License. # limitations under the License.
add_subdirectory(examples) add_subdirectory(examples)
add_subdirectory(testcases)
add_subdirectory(unwind) add_subdirectory(unwind)
add_subdirectory(util) add_subdirectory(util)
@ -61,17 +60,6 @@ target_link_libraries(sandbox2_syscall
PUBLIC glog::glog PUBLIC glog::glog
) )
# sandboxed_api/sandbox2:syscall_test
add_executable(syscall_test
syscall_test.cc
)
target_link_libraries(syscall_test PRIVATE
absl::strings
sandbox2::syscall
sapi::test_main
)
gtest_discover_tests(syscall_test)
# sandboxed_api/sandbox2:result # sandboxed_api/sandbox2:result
add_library(sandbox2_result STATIC add_library(sandbox2_result STATIC
result.cc result.cc
@ -419,27 +407,6 @@ target_link_libraries(sandbox2_mounts PRIVATE
sapi::statusor sapi::statusor
) )
# sandboxed_api/sandbox2:mounts_test
add_executable(mounts_test
mounts_test.cc
)
add_dependencies(mounts_test
sandbox2::testcase_minimal_dynamic
)
target_link_libraries(mounts_test PRIVATE
absl::strings
sandbox2::file_base
sandbox2::mounts
sandbox2::temp_file
sandbox2::testing
sapi::status_matchers
sapi::test_main
)
gtest_discover_tests(mounts_test PROPERTIES
ENVIRONMENT "TEST_TMPDIR=/tmp"
ENVIRONMENT "TEST_SRCDIR=${PROJECT_BINARY_DIR}"
)
# sandboxed_api/sandbox2:namespace # sandboxed_api/sandbox2:namespace
add_library(sandbox2_namespace STATIC add_library(sandbox2_namespace STATIC
namespace.cc namespace.cc
@ -461,29 +428,6 @@ target_link_libraries(sandbox2_namespace PRIVATE
sapi::raw_logging sapi::raw_logging
) )
# sandboxed_api/sandbox2:namespace_test
add_executable(namespace_test
namespace_test.cc
)
add_dependencies(mounts_test
sandbox2::testcase_hostname
sandbox2::testcase_namespace
)
target_link_libraries(namespace_test PRIVATE
absl::memory
absl::strings
sandbox2::comms
sandbox2::namespace
sandbox2::sandbox2
sandbox2::testing
sapi::status_matchers
sapi::test_main
)
gtest_discover_tests(namespace_test PROPERTIES
ENVIRONMENT "TEST_TMPDIR=/tmp"
ENVIRONMENT "TEST_SRCDIR=${PROJECT_BINARY_DIR}"
)
# sandboxed_api/sandbox2:forkingclient # sandboxed_api/sandbox2:forkingclient
add_library(sandbox2_forkingclient STATIC add_library(sandbox2_forkingclient STATIC
forkingclient.cc forkingclient.cc
@ -539,26 +483,6 @@ target_link_libraries(sandbox2_buffer PRIVATE
sapi::statusor sapi::statusor
) )
# sandboxed_api/sandbox2:buffer_test
add_executable(buffer_test
buffer_test.cc
)
add_dependencies(buffer_test
sandbox2::testcase_buffer
)
target_link_libraries(buffer_test PRIVATE
absl::memory
sandbox2::buffer
sandbox2::comms
sandbox2::sandbox2
sandbox2::testing
sapi::status_matchers
sapi::test_main
)
gtest_discover_tests(buffer_test PROPERTIES
ENVIRONMENT "TEST_SRCDIR=${PROJECT_BINARY_DIR}"
)
# sandboxed_api/sandbox2:forkserver_proto # sandboxed_api/sandbox2:forkserver_proto
protobuf_generate_cpp(_sandbox2_forkserver_pb_h _sandbox2_forkserver_pb_cc protobuf_generate_cpp(_sandbox2_forkserver_pb_h _sandbox2_forkserver_pb_cc
forkserver.proto forkserver.proto
@ -610,6 +534,99 @@ target_link_libraries(sandbox2_comms PRIVATE
sapi::statusor sapi::statusor
) )
# sandboxed_api/sandbox2:violation_proto
protobuf_generate_cpp(_sandbox2_violation_pb_cc _sandbox2_violation_pb_h
violation.proto
)
add_library(sandbox2_violation_proto STATIC
${_sandbox2_violation_pb_cc}
${_sandbox2_violation_pb_h}
)
add_library(sandbox2::violation_proto ALIAS sandbox2_violation_proto)
target_link_libraries(sandbox2_violation_proto PRIVATE
protobuf::libprotobuf
sandbox2::mounttree_proto
sapi::base
)
if(SAPI_ENABLE_TESTS)
add_subdirectory(testcases)
# sandboxed_api/sandbox2:syscall_test
add_executable(syscall_test
syscall_test.cc
)
target_link_libraries(syscall_test PRIVATE
absl::strings
sandbox2::syscall
sapi::test_main
)
gtest_discover_tests(syscall_test)
# sandboxed_api/sandbox2:mounts_test
add_executable(mounts_test
mounts_test.cc
)
add_dependencies(mounts_test
sandbox2::testcase_minimal_dynamic
)
target_link_libraries(mounts_test PRIVATE
absl::strings
sandbox2::file_base
sandbox2::mounts
sandbox2::temp_file
sandbox2::testing
sapi::status_matchers
sapi::test_main
)
gtest_discover_tests(mounts_test PROPERTIES
ENVIRONMENT "TEST_TMPDIR=/tmp"
ENVIRONMENT "TEST_SRCDIR=${PROJECT_BINARY_DIR}"
)
# sandboxed_api/sandbox2:namespace_test
add_executable(namespace_test
namespace_test.cc
)
add_dependencies(mounts_test
sandbox2::testcase_hostname
sandbox2::testcase_namespace
)
target_link_libraries(namespace_test PRIVATE
absl::memory
absl::strings
sandbox2::comms
sandbox2::namespace
sandbox2::sandbox2
sandbox2::testing
sapi::status_matchers
sapi::test_main
)
gtest_discover_tests(namespace_test PROPERTIES
ENVIRONMENT "TEST_TMPDIR=/tmp"
ENVIRONMENT "TEST_SRCDIR=${PROJECT_BINARY_DIR}"
)
# sandboxed_api/sandbox2:buffer_test
add_executable(buffer_test
buffer_test.cc
)
add_dependencies(buffer_test
sandbox2::testcase_buffer
)
target_link_libraries(buffer_test PRIVATE
absl::memory
sandbox2::buffer
sandbox2::comms
sandbox2::sandbox2
sandbox2::testing
sapi::status_matchers
sapi::test_main
)
gtest_discover_tests(buffer_test PROPERTIES
ENVIRONMENT "TEST_SRCDIR=${PROJECT_BINARY_DIR}"
)
# sandboxed_api/sandbox2:comms_test_proto # sandboxed_api/sandbox2:comms_test_proto
protobuf_generate_cpp(_sandbox2_comms_test_pb_h _sandbox2_comms_test_pb_cc protobuf_generate_cpp(_sandbox2_comms_test_pb_h _sandbox2_comms_test_pb_cc
comms_test.proto comms_test.proto
@ -854,21 +871,6 @@ target_link_libraries(sandbox2_testing PRIVATE
sapi::base sapi::base
) )
# sandboxed_api/sandbox2:violation_proto
protobuf_generate_cpp(_sandbox2_violation_pb_cc _sandbox2_violation_pb_h
violation.proto
)
add_library(sandbox2_violation_proto STATIC
${_sandbox2_violation_pb_cc}
${_sandbox2_violation_pb_h}
)
add_library(sandbox2::violation_proto ALIAS sandbox2_violation_proto)
target_link_libraries(sandbox2_violation_proto PRIVATE
protobuf::libprotobuf
sandbox2::mounttree_proto
sapi::base
)
# sandboxed_api/sandbox2:policybuilder_test # sandboxed_api/sandbox2:policybuilder_test
add_executable(policybuilder_test add_executable(policybuilder_test
policybuilder_test.cc policybuilder_test.cc
@ -892,3 +894,4 @@ gtest_discover_tests(policybuilder_test PROPERTIES
ENVIRONMENT "TEST_TMPDIR=/tmp" ENVIRONMENT "TEST_TMPDIR=/tmp"
ENVIRONMENT "TEST_SRCDIR=${PROJECT_BINARY_DIR}" ENVIRONMENT "TEST_SRCDIR=${PROJECT_BINARY_DIR}"
) )
endif()

View File

@ -12,8 +12,10 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
if(SAPI_ENABLE_EXAMPLES)
add_subdirectory(crc4) add_subdirectory(crc4)
add_subdirectory(custom_fork) add_subdirectory(custom_fork)
add_subdirectory(static) add_subdirectory(static)
add_subdirectory(tool) add_subdirectory(tool)
add_subdirectory(zlib) add_subdirectory(zlib)
endif()

View File

@ -34,18 +34,6 @@ target_link_libraries(sandbox2_util_file_helpers PRIVATE
sapi::status sapi::status
) )
# sandboxed_api/sandbox2/util:file_helpers_test
add_executable(file_helpers_test
file_helpers_test.cc
)
target_link_libraries(file_helpers_test PRIVATE
absl::strings
sandbox2::file_helpers
sapi::status_matchers
sapi::test_main
)
gtest_discover_tests(file_helpers_test)
# sandboxed_api/sandbox2/util:fileops # sandboxed_api/sandbox2/util:fileops
add_library(sandbox2_util_fileops STATIC add_library(sandbox2_util_fileops STATIC
fileops.cc fileops.cc
@ -58,6 +46,99 @@ target_link_libraries(sandbox2_util_fileops PRIVATE
sapi::base sapi::base
) )
# sandboxed_api/sandbox2/util:file_base
add_library(sandbox2_util_file_base STATIC
path.cc
path.h
)
add_library(sandbox2::file_base ALIAS sandbox2_util_file_base)
target_link_libraries(sandbox2_util_file_base PRIVATE
absl::strings
sapi::base
)
# sandboxed_api/sandbox2/util:strerror
add_library(sandbox2_util_strerror STATIC
strerror.cc
strerror.h
)
add_library(sandbox2::strerror ALIAS sandbox2_util_strerror)
target_link_libraries(sandbox2_util_strerror PRIVATE
absl::strings
sapi::base
)
# sandboxed_api/sandbox2/util:minielf
add_library(sandbox2_util_minielf STATIC
minielf.cc
minielf.h
)
add_library(sandbox2::minielf ALIAS sandbox2_util_minielf)
target_link_libraries(sandbox2_util_minielf PRIVATE
absl::strings
sandbox2::util
sapi::base
sapi::raw_logging
sapi::status
sapi::statusor
)
# sandboxed_api/sandbox2/util:temp_file
add_library(sandbox2_util_temp_file STATIC
temp_file.cc
temp_file.h
)
add_library(sandbox2::temp_file ALIAS sandbox2_util_temp_file)
target_link_libraries(sandbox2_util_temp_file PRIVATE
absl::strings
sandbox2::fileops
sandbox2::strerror
sapi::base
sapi::status
sapi::statusor
)
# sandboxed_api/sandbox2/util:maps_parser
add_library(sandbox2_util_maps_parser STATIC
maps_parser.cc
maps_parser.h
)
add_library(sandbox2::maps_parser ALIAS sandbox2_util_maps_parser)
target_link_libraries(sandbox2_util_maps_parser PRIVATE
absl::strings
sapi::base
sapi::status
sapi::statusor
)
# sandboxed_api/sandbox2/util:runfiles
add_library(sandbox2_util_runfiles STATIC
runfiles.h
runfiles_nobazel.cc
)
add_library(sandbox2::runfiles ALIAS sandbox2_util_runfiles)
target_link_libraries(sandbox2_util_runfiles PRIVATE
absl::str_format
absl::strings
sandbox2::file_base
sapi::base
sapi::flags
sapi::raw_logging
)
if(SAPI_ENABLE_TESTS)
# sandboxed_api/sandbox2/util:file_helpers_test
add_executable(file_helpers_test
file_helpers_test.cc
)
target_link_libraries(file_helpers_test PRIVATE
absl::strings
sandbox2::file_helpers
sapi::status_matchers
sapi::test_main
)
gtest_discover_tests(file_helpers_test)
# sandboxed_api/sandbox2/util:fileops_test # sandboxed_api/sandbox2/util:fileops_test
add_executable(fileops_test add_executable(fileops_test
fileops_test.cc fileops_test.cc
@ -75,17 +156,6 @@ gtest_discover_tests(fileops_test PROPERTIES
ENVIRONMENT "TEST_SRCDIR=${PROJECT_BINARY_DIR}" ENVIRONMENT "TEST_SRCDIR=${PROJECT_BINARY_DIR}"
) )
# sandboxed_api/sandbox2/util:file_base
add_library(sandbox2_util_file_base STATIC
path.cc
path.h
)
add_library(sandbox2::file_base ALIAS sandbox2_util_file_base)
target_link_libraries(sandbox2_util_file_base PRIVATE
absl::strings
sapi::base
)
# sandboxed_api/sandbox2/util:file_base_test # sandboxed_api/sandbox2/util:file_base_test
add_executable(file_base_test add_executable(file_base_test
path_test.cc path_test.cc
@ -97,17 +167,6 @@ target_link_libraries(file_base_test PRIVATE
) )
gtest_discover_tests(file_base_test) gtest_discover_tests(file_base_test)
# sandboxed_api/sandbox2/util:strerror
add_library(sandbox2_util_strerror STATIC
strerror.cc
strerror.h
)
add_library(sandbox2::strerror ALIAS sandbox2_util_strerror)
target_link_libraries(sandbox2_util_strerror PRIVATE
absl::strings
sapi::base
)
# sandboxed_api/sandbox2/util:strerror # sandboxed_api/sandbox2/util:strerror
add_executable(strerror_test add_executable(strerror_test
strerror_test.cc strerror_test.cc
@ -119,21 +178,6 @@ target_link_libraries(strerror_test PRIVATE
) )
gtest_discover_tests(strerror_test) gtest_discover_tests(strerror_test)
# sandboxed_api/sandbox2/util:minielf
add_library(sandbox2_util_minielf STATIC
minielf.cc
minielf.h
)
add_library(sandbox2::minielf ALIAS sandbox2_util_minielf)
target_link_libraries(sandbox2_util_minielf PRIVATE
absl::strings
sandbox2::util
sapi::base
sapi::raw_logging
sapi::status
sapi::statusor
)
# sandboxed_api/sandbox2/util:minielf_test # sandboxed_api/sandbox2/util:minielf_test
add_executable(minielf_test add_executable(minielf_test
minielf_test.cc minielf_test.cc
@ -154,21 +198,6 @@ gtest_discover_tests(minielf_test PROPERTIES
ENVIRONMENT "TEST_SRCDIR=${PROJECT_BINARY_DIR}" ENVIRONMENT "TEST_SRCDIR=${PROJECT_BINARY_DIR}"
) )
# sandboxed_api/sandbox2/util:temp_file
add_library(sandbox2_util_temp_file STATIC
temp_file.cc
temp_file.h
)
add_library(sandbox2::temp_file ALIAS sandbox2_util_temp_file)
target_link_libraries(sandbox2_util_temp_file PRIVATE
absl::strings
sandbox2::fileops
sandbox2::strerror
sapi::base
sapi::status
sapi::statusor
)
# sandboxed_api/sandbox2/util:temp_file_test # sandboxed_api/sandbox2/util:temp_file_test
add_executable(temp_file_test add_executable(temp_file_test
temp_file_test.cc temp_file_test.cc
@ -183,19 +212,6 @@ target_link_libraries(temp_file_test PRIVATE
) )
gtest_discover_tests(temp_file_test) gtest_discover_tests(temp_file_test)
# sandboxed_api/sandbox2/util:maps_parser
add_library(sandbox2_util_maps_parser STATIC
maps_parser.cc
maps_parser.h
)
add_library(sandbox2::maps_parser ALIAS sandbox2_util_maps_parser)
target_link_libraries(sandbox2_util_maps_parser PRIVATE
absl::strings
sapi::base
sapi::status
sapi::statusor
)
# sandboxed_api/sandbox2/util:maps_parser_test # sandboxed_api/sandbox2/util:maps_parser_test
add_executable(maps_parser_test add_executable(maps_parser_test
maps_parser_test.cc maps_parser_test.cc
@ -206,18 +222,4 @@ target_link_libraries(maps_parser_test PRIVATE
sapi::test_main sapi::test_main
) )
gtest_discover_tests(maps_parser_test) gtest_discover_tests(maps_parser_test)
endif()
# sandboxed_api/sandbox2/util:runfiles
add_library(sandbox2_util_runfiles STATIC
runfiles.h
runfiles_nobazel.cc
)
add_library(sandbox2::runfiles ALIAS sandbox2_util_runfiles)
target_link_libraries(sandbox2_util_runfiles PRIVATE
absl::str_format
absl::strings
sandbox2::file_base
sapi::base
sapi::flags
sapi::raw_logging
)

View File

@ -56,6 +56,29 @@ target_link_libraries(sapi_util_statusor PRIVATE
sapi::status 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
)
# 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 # sandboxed_api/util:status_matchers
add_library(sapi_util_status_matchers STATIC add_library(sapi_util_status_matchers STATIC
status_matchers.h status_matchers.h
@ -80,26 +103,4 @@ target_link_libraries(status_test PRIVATE
absl::type_traits absl::type_traits
) )
gtest_discover_tests(status_test) gtest_discover_tests(status_test)
endif()
# 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
)
# 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
)