sandboxed-api/cmake/SapiDeps.cmake
Christian Blichmann 41e0ca0a68 CMake: Use min v3.13 and FetchContent
This updates the mininum required version of CMake to 3.13, which is
present in Debian Buster.

At the same time, move all of our dependency handling to use
`FetchContent_Declare()`, `FetchContent_Populate()` and
`FetchContent_MakeAvailable()`. Since the latter was ony introduced in
3.14, provide a simple "polyfill" implementation for that.

As an added benefit, the configure step for libffi and libunwind will
now not be re-run every time the `CMakeLists.txt` changes.

Other changes:
- Explicitly spell out that we're testing up to 3.22 in
  `cmake_minimum_version()`
- Rename `check_target()` to `sapi_check_target()` to avoid conflicts
  with Abseil.
2022-01-25 13:59:45 +01:00

125 lines
3.4 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.
function(sapi_check_target target)
if(NOT TARGET ${target})
message(FATAL_ERROR " SAPI: compiling Sandboxed API requires a ${target}
CMake target in your project")
endif()
endfunction()
include(SapiFetchContent)
# Use static libraries
set(_sapi_saved_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
if (SAPI_ENABLE_SHARED_LIBS)
set(SAPI_LIB_TYPE SHARED)
set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_SHARED_LIBRARY_SUFFIX})
set(BUILD_SHARED_LIBS ON CACHE BOOL "" FORCE)
# Imply linking with system-wide libs
set(SAPI_DOWNLOAD_LIBCAP OFF CACHE BOOL "" FORCE)
set(SAPI_DOWNLOAD_LIBFFI OFF CACHE BOOL "" FORCE)
set(SAPI_DOWNLOAD_PROTOBUF OFF CACHE BOOL "" FORCE)
set(SAPI_DOWNLOAD_ZLIB OFF CACHE BOOL "" FORCE)
add_compile_definitions(SAPI_LIB_IS_SHARED=1)
else()
set(SAPI_LIB_TYPE STATIC)
set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
endif()
set(_sapi_saved_BUILD_TESTING ${BUILD_TESTING})
set(BUILD_TESTING OFF) # No need to build test code of our deps
if(SAPI_ENABLE_TESTS)
if(SAPI_DOWNLOAD_GOOGLETEST)
include(cmake/googletest.cmake)
endif()
sapi_check_target(gtest)
sapi_check_target(gtest_main)
sapi_check_target(gmock)
if(SAPI_DOWNLOAD_BENCHMARK)
include(cmake/benchmark.cmake)
endif()
sapi_check_target(benchmark)
endif()
if(SAPI_DOWNLOAD_ABSL)
include(cmake/abseil-cpp.cmake)
endif()
sapi_check_target(absl::core_headers)
if(SAPI_DOWNLOAD_LIBCAP)
include(cmake/libcap.cmake)
sapi_check_target(libcap::libcap)
else()
find_package(Libcap REQUIRED)
endif()
if(SAPI_DOWNLOAD_LIBFFI)
include(cmake/libffi.cmake)
sapi_check_target(libffi::libffi)
else()
find_package(Libffi REQUIRED)
endif()
if(SAPI_DOWNLOAD_LIBUNWIND)
include(cmake/libunwind.cmake)
endif()
sapi_check_target(unwind_ptrace_wrapped)
if(SAPI_DOWNLOAD_GFLAGS)
include(cmake/gflags.cmake)
sapi_check_target(gflags)
else()
find_package(gflags REQUIRED)
endif()
if(SAPI_DOWNLOAD_GLOG)
include(cmake/glog.cmake)
endif()
sapi_check_target(glog::glog)
# TODO(cblichmann): Check if this is needed/works with system lib
add_dependencies(glog gflags::gflags)
if(SAPI_DOWNLOAD_PROTOBUF)
include(cmake/protobuf.cmake)
endif()
find_package(Protobuf REQUIRED)
if(SAPI_ENABLE_EXAMPLES)
if(SAPI_DOWNLOAD_ZLIB)
include(cmake/zlib.cmake)
sapi_check_target(ZLIB::ZLIB)
else()
find_package(ZLIB REQUIRED)
endif()
endif()
find_package(Threads REQUIRED)
if(NOT SAPI_ENABLE_GENERATOR)
# Find Python 3 and add its location to the cache so that its available in
# the add_sapi_library() macro in embedding projects.
find_package(Python3 COMPONENTS Interpreter REQUIRED)
set(SAPI_PYTHON3_EXECUTABLE "${Python3_EXECUTABLE}" CACHE INTERNAL "" FORCE)
endif()
# Undo global changes
if(_sapi_saved_BUILD_TESTING)
set(BUILD_TESTING "${_sapi_saved_BUILD_TESTING}")
endif()
set(CMAKE_FIND_LIBRARY_SUFFIXES ${_sapi_saved_CMAKE_FIND_LIBRARY_SUFFIXES})