xref: /aosp_15_r20/external/sandboxed-api/CMakeLists.txt (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1# Copyright 2019 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15cmake_minimum_required(VERSION 3.13..3.26)
16
17if(POLICY CMP0083)
18  cmake_policy(SET CMP0083 NEW)  # Add PIE flags when requested
19endif()
20
21if(POLICY CMP0135)
22  cmake_policy(SET CMP0135 NEW)  # Set download timestamp to current time
23endif()
24
25project(SandboxedAPI C CXX ASM)
26include(CTest)
27
28# TODO(cblichmann): Enable for Android once support lands
29if(NOT CMAKE_SYSTEM_NAME MATCHES "Linux")
30  message(FATAL_ERROR "Sandboxed API is only supported on Linux")
31endif()
32
33# SAPI-wide setting for the language level
34set(SAPI_CXX_STANDARD 17)
35
36if(NOT CMAKE_CXX_STANDARD)
37  set(CMAKE_CXX_STANDARD ${SAPI_CXX_STANDARD})
38elseif(CMAKE_CXX_STANDARD LESS ${SAPI_CXX_STANDARD})
39  message(FATAL_ERROR
40    "Sandboxed API requires C++17. To ensure ABI compatibility"
41    " build and link all targets of the project with the same"
42    " version of C++."
43  )
44endif()
45
46set(SAPI_BINARY_DIR "${PROJECT_BINARY_DIR}" CACHE INTERNAL "" FORCE)
47set(SAPI_SOURCE_DIR "${PROJECT_SOURCE_DIR}" CACHE INTERNAL "" FORCE)
48
49get_directory_property(_sapi_has_parent PARENT_DIRECTORY)
50if(PROJECT_IS_TOP_LEVEL OR NOT _sapi_has_parent)
51  set(SAPI_PROJECT_IS_TOP_LEVEL ON)
52endif()
53
54include(CheckCXXCompilerFlag)
55
56# Allow the header generator to auto-configure include paths. Need to set a
57# cache variable, so that sub- and super-projects also have this enabled.
58set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")
59
60set(CMAKE_SKIP_BUILD_RPATH ON)
61
62if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.14)
63  include(CheckPIESupported)
64  check_pie_supported()
65  set(CMAKE_POSITION_INDEPENDENT_CODE ON)
66endif()
67
68# SAPI CMake modules, order matters
69list(APPEND CMAKE_MODULE_PATH "${SAPI_SOURCE_DIR}/cmake"
70                              "${SAPI_SOURCE_DIR}/cmake/modules")
71include(SapiOptions)
72include(SapiDeps)
73include(SapiUtil)
74include(SapiBuildDefs)
75include(GNUInstallDirs)
76
77if(SAPI_HARDENED_SOURCE)
78  add_compile_options(-fstack-protector -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2)
79  add_link_options(-Wl,-z,relro -Wl,-z,now)
80endif()
81
82if(SAPI_FORCE_COLOR_OUTPUT)
83  if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")  # GCC
84    add_compile_options(-fdiagnostics-color=always)
85  elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")  # Clang or Apple Clang
86    add_compile_options(-fcolor-diagnostics)
87  endif()
88endif()
89
90# Make Bazel-style includes work
91configure_file(cmake/libcap_capability.h.in
92               libcap/include/sys/capability.h
93               @ONLY)
94
95# Library with basic project settings. The empty file is there to be able to
96# define header-only libraries without cumbersome target_sources() hacks.
97configure_file(cmake/sapi_force_cxx_linkage.cc.in
98               "${SAPI_BINARY_DIR}/sapi_force_cxx_linkage.cc" COPYONLY)
99add_library(sapi_base STATIC
100  "${SAPI_BINARY_DIR}/sapi_force_cxx_linkage.cc"
101)
102add_library(sapi::base ALIAS sapi_base)
103target_compile_features(sapi_base PUBLIC
104  cxx_std_${SAPI_CXX_STANDARD}
105)
106set_target_properties(sapi_base PROPERTIES
107  INTERFACE_POSITION_INDEPENDENT_CODE ON
108)
109target_include_directories(sapi_base PUBLIC
110  "${SAPI_BINARY_DIR}"
111  "${SAPI_SOURCE_DIR}"
112  "${Protobuf_INCLUDE_DIR}"
113)
114target_compile_options(sapi_base PUBLIC
115  -fno-exceptions
116)
117if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
118  target_compile_options(sapi_base PUBLIC
119    # The syscall tables in sandbox2/syscall_defs.cc are `std::array`s using
120    # CTAD and have more entries than the default limit of 256.
121    -fbracket-depth=768
122  )
123endif()
124set(_sapi_check_no_deprecated
125  -Wno-deprecated SAPI_HAS_W_NO_DEPRECATED
126)
127set(_sapi_check_frame_larger_than
128  # For sandbox2/util.cc's CloneAndJump()
129  -Wframe-larger-than=40960 SAPI_HAS_W_FRAME_LARGER_THAN
130)
131set(_sapi_check_no_deprecated_declarations
132  -Wno-deprecated-declarations SAPI_HAS_W_NO_DEPRECATED_DECLARATIONS
133)
134set(_sapi_check_no_psabi
135  -Wno-psabi SAPI_HAS_W_NO_PSABI
136)
137foreach(check IN ITEMS _sapi_check_no_deprecated
138                       _sapi_check_frame_larger_than
139                       _sapi_check_no_deprecated_declarations
140                       _sapi_check_no_psabi)
141  list(GET ${check} 0 opt_value)
142  list(GET ${check} 1 var_name)
143  check_cxx_compiler_flag(${opt_value} ${var_name})
144  if(${var_name})
145    target_compile_options(sapi_base PUBLIC ${opt_value})
146  endif()
147endforeach()
148
149add_library(sapi_test_main INTERFACE)
150add_library(sapi::test_main ALIAS sapi_test_main)
151target_link_libraries(sapi_test_main INTERFACE
152  gtest_main
153  gmock
154  sapi::base
155)
156
157if(BUILD_TESTING AND SAPI_BUILD_TESTING)
158  include(GoogleTest)
159  # Setup tests to work like with Bazel
160  create_directory_symlink("${SAPI_BINARY_DIR}" com_google_sandboxed_api)
161  enable_testing()
162endif()
163
164add_subdirectory(sandboxed_api)
165
166if(BUILD_TESTING AND SAPI_BUILD_TESTING AND SAPI_CONTRIB_BUILD_TESTING)
167  add_subdirectory(contrib)
168endif()
169