1# Copyright 2020 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 15# Example that demonstrates how to embed Sandboxed API into a project using 16# CMake. 17 18cmake_minimum_required(VERSION 3.12) 19 20project(hello_sapi_project CXX) 21 22# Path to the Sandboxed API source tree. Unlike Bazel, CMake does not download 23# downstream dependencies by default. So the option below needs to be adjusted 24# to point to a local checkout or a Git submodule. 25# The default value is chosen so that this example can be easily tried out for 26# a regular checkout of Sandboxed API. 27set(SAPI_ROOT "${PROJECT_SOURCE_DIR}/../../.." 28 CACHE PATH "Path to the Sandboxed API source tree") 29 30# Configure options and include Sandboxed API as a sub-directory. 31set(SAPI_BUILD_EXAMPLES OFF CACHE BOOL "") 32set(SAPI_BUILD_TESTING OFF CACHE BOOL "") 33add_subdirectory("${SAPI_ROOT}" 34 "${CMAKE_BINARY_DIR}/sandboxed-api-build" EXCLUDE_FROM_ALL) 35 36# Interface library with common settings for this projects 37add_library(hello_base INTERFACE) 38add_library(hello::base ALIAS hello_base) 39target_compile_features(hello_base INTERFACE cxx_std_17) 40target_include_directories(hello_base INTERFACE 41 "${PROJECT_BINARY_DIR}" # To find the generated SAPI header 42) 43 44# Library with code that should be sandboxed 45add_library(hello_lib STATIC 46 hello_lib.cc 47) 48target_link_libraries(hello_lib PRIVATE 49 hello::base 50) 51 52# Sandboxed API for the library above 53add_sapi_library(hello_sapi 54 FUNCTIONS AddTwoIntegers 55 INPUTS hello_lib.cc 56 LIBRARY hello_lib 57 LIBRARY_NAME Hello 58 NAMESPACE "" 59) 60add_library(hello::sapi ALIAS hello_sapi) 61 62# Main executable demonstrating how the sandboxed library is used 63add_executable(hello 64 hello_main.cc 65) 66target_link_libraries(hello PRIVATE 67 hello::base 68 hello::sapi 69 sapi::sapi 70) 71 72# Another example using the same library, but using the Transaction API that 73# automatically retries sandbox operations. Also demonstates error handling 74# and a custom security policy. 75add_executable(hello_transacted 76 hello_transacted.cc 77) 78target_link_libraries(hello_transacted PRIVATE 79 hello::base 80 hello::sapi 81 sapi::sapi 82) 83