xref: /aosp_15_r20/external/sandboxed-api/cmake/SapiDeps.cmake (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
15function(sapi_check_target target)
16  if(NOT TARGET ${target})
17    message(FATAL_ERROR " SAPI: compiling Sandboxed API requires a ${target}
18                   CMake target in your project")
19  endif()
20endfunction()
21
22include(SapiFetchContent)
23
24# Use static libraries
25set(_sapi_saved_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
26if (SAPI_ENABLE_SHARED_LIBS)
27  set(SAPI_LIB_TYPE SHARED)
28  set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_SHARED_LIBRARY_SUFFIX})
29  set(BUILD_SHARED_LIBS ON CACHE BOOL "" FORCE)
30  # Imply linking with system-wide libs
31  set(SAPI_DOWNLOAD_LIBCAP OFF CACHE BOOL "" FORCE)
32  set(SAPI_DOWNLOAD_LIBFFI OFF CACHE BOOL "" FORCE)
33  set(SAPI_DOWNLOAD_PROTOBUF OFF CACHE BOOL "" FORCE)
34  set(SAPI_DOWNLOAD_ZLIB OFF CACHE BOOL "" FORCE)
35  add_compile_definitions(SAPI_LIB_IS_SHARED=1)
36else()
37  set(SAPI_LIB_TYPE STATIC)
38  set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
39endif()
40
41set(_sapi_saved_BUILD_TESTING ${BUILD_TESTING})
42set(BUILD_TESTING OFF)  # No need to build test code of our deps
43
44if(SAPI_BUILD_TESTING)
45  if(SAPI_DOWNLOAD_GOOGLETEST)
46    include(cmake/googletest.cmake)
47  endif()
48  sapi_check_target(gtest)
49  sapi_check_target(gtest_main)
50  sapi_check_target(gmock)
51
52  if(SAPI_DOWNLOAD_BENCHMARK)
53    include(cmake/benchmark.cmake)
54  endif()
55  sapi_check_target(benchmark)
56endif()
57
58if(SAPI_DOWNLOAD_ABSL)
59  include(cmake/abseil-cpp.cmake)
60endif()
61sapi_check_target(absl::core_headers)
62
63if(SAPI_DOWNLOAD_LIBCAP)
64  include(cmake/libcap.cmake)
65  sapi_check_target(libcap::libcap)
66else()
67  find_package(Libcap REQUIRED)
68endif()
69
70if(SAPI_DOWNLOAD_LIBFFI)
71  include(cmake/libffi.cmake)
72  sapi_check_target(libffi::libffi)
73else()
74  find_package(Libffi REQUIRED)
75endif()
76
77if(SAPI_DOWNLOAD_LIBUNWIND)
78  include(cmake/libunwind.cmake)
79endif()
80sapi_check_target(unwind_ptrace)
81
82if(SAPI_DOWNLOAD_PROTOBUF)
83  include(cmake/protobuf.cmake)
84endif()
85find_package(Protobuf REQUIRED)
86
87if(SAPI_BUILD_EXAMPLES)
88  if(SAPI_DOWNLOAD_ZLIB)
89    include(cmake/zlib.cmake)
90    sapi_check_target(ZLIB::ZLIB)
91  else()
92    find_package(ZLIB REQUIRED)
93  endif()
94endif()
95
96find_package(Threads REQUIRED)
97
98if(SAPI_ENABLE_CLANG_TOOL)
99  # If unset (the default), CMake will build the tool first and add it as a
100  # dependency.
101  set(SAPI_CLANG_TOOL_EXECUTABLE "" CACHE FILEPATH
102    "Path to the Clang tool based header generator"
103  )
104else()
105  # Find Python 3 and add its location to the cache so that its available in
106  # the add_sapi_library() macro in embedding projects.
107  find_package(Python3 COMPONENTS Interpreter REQUIRED)
108  set(SAPI_PYTHON3_EXECUTABLE "${Python3_EXECUTABLE}" CACHE INTERNAL "" FORCE)
109endif()
110
111# Undo global changes
112if(_sapi_saved_BUILD_TESTING)
113  set(BUILD_TESTING "${_sapi_saved_BUILD_TESTING}")
114endif()
115set(CMAKE_FIND_LIBRARY_SUFFIXES ${_sapi_saved_CMAKE_FIND_LIBRARY_SUFFIXES})
116