From d47c066112d7c7cff081fdee938b3771724c84c5 Mon Sep 17 00:00:00 2001 From: Christian Blichmann Date: Thu, 21 Nov 2019 04:03:47 -0800 Subject: [PATCH] Enable sapi_test target in CMake, add Benchmark dependency PiperOrigin-RevId: 281719298 Change-Id: Idd2570fff4907c81cdab1070bd1f3e41e29c76f6 --- cmake/SapiDeps.cmake | 5 ++++ cmake/SapiOptions.cmake | 1 + cmake/benchmark/CMakeLists.txt.in | 28 ++++++++++++++++++++++ cmake/benchmark/Download.cmake | 40 +++++++++++++++++++++++++++++++ sandboxed_api/CMakeLists.txt | 19 +++++++++++++-- 5 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 cmake/benchmark/CMakeLists.txt.in create mode 100644 cmake/benchmark/Download.cmake diff --git a/cmake/SapiDeps.cmake b/cmake/SapiDeps.cmake index be65091..95e95f0 100644 --- a/cmake/SapiDeps.cmake +++ b/cmake/SapiDeps.cmake @@ -34,6 +34,11 @@ if(SAPI_ENABLE_TESTS) check_target(gtest) check_target(gtest_main) check_target(gmock) + + if(SAPI_USE_BENCHMARK) + include(cmake/benchmark/Download.cmake) + endif() + check_target(benchmark) endif() if(SAPI_USE_ABSL) diff --git a/cmake/SapiOptions.cmake b/cmake/SapiOptions.cmake index 5e0d3b7..cc2807f 100644 --- a/cmake/SapiOptions.cmake +++ b/cmake/SapiOptions.cmake @@ -21,6 +21,7 @@ # depend on some of these libraries (e.g. Abseil). option(SAPI_USE_ABSL "Download Abseil at config time" ON) option(SAPI_USE_GOOGLETEST "Download googletest at config time" ON) +option(SAPI_USE_BENCHMARK "Download benchmark at config time" ON) option(SAPI_USE_GFLAGS "Download gflags at config time" ON) option(SAPI_USE_GLOG "Download glog at config time" ON) option(SAPI_USE_PROTOBUF "Download protobuf at config time" ON) diff --git a/cmake/benchmark/CMakeLists.txt.in b/cmake/benchmark/CMakeLists.txt.in new file mode 100644 index 0000000..f32579b --- /dev/null +++ b/cmake/benchmark/CMakeLists.txt.in @@ -0,0 +1,28 @@ +# 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. + +cmake_minimum_required(VERSION 3.10) +project(benchmark-download NONE) + +include(ExternalProject) +ExternalProject_Add(benchmark + GIT_REPOSITORY https://github.com/google/benchmark.git + GIT_TAG 090faecb454fbd6e6e17a75ef8146acb037118d4 # 2019-05-13 + SOURCE_DIR "${CMAKE_BINARY_DIR}/benchmark-src" + BINARY_DIR "${CMAKE_BINARY_DIR}/benchmark-build" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" +) diff --git a/cmake/benchmark/Download.cmake b/cmake/benchmark/Download.cmake new file mode 100644 index 0000000..ec1e186 --- /dev/null +++ b/cmake/benchmark/Download.cmake @@ -0,0 +1,40 @@ +# 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. + +# Downloads and unpacks Benchmark at configure time + +set(workdir "${CMAKE_BINARY_DIR}/benchmark-download") + +configure_file("${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt.in" + "${workdir}/CMakeLists.txt") +execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . + RESULT_VARIABLE error + WORKING_DIRECTORY "${workdir}") +if(error) + message(FATAL_ERROR "CMake step for ${PROJECT_NAME} failed: ${error}") +endif() + +execute_process(COMMAND ${CMAKE_COMMAND} --build . + RESULT_VARIABLE error + WORKING_DIRECTORY "${workdir}") +if(error) + message(FATAL_ERROR "Build step for ${PROJECT_NAME} failed: ${error}") +endif() + +set(BENCHMARK_ENABLE_TESTING OFF) +set(BENCHMARK_ENABLE_EXCEPTIONS OFF) +set(BENCHMARK_ENABLE_GTEST_TESTS OFF) + +add_subdirectory("${CMAKE_BINARY_DIR}/benchmark-src" + "${CMAKE_BINARY_DIR}/benchmark-build" EXCLUDE_FROM_ALL) diff --git a/sandboxed_api/CMakeLists.txt b/sandboxed_api/CMakeLists.txt index 0b8869a..049550b 100644 --- a/sandboxed_api/CMakeLists.txt +++ b/sandboxed_api/CMakeLists.txt @@ -168,5 +168,20 @@ target_link_libraries(sapi_client PRIVATE sapi::vars ) -# sandboxed_api:sapi_test -# TODO(cblichmann): Add sapi_test once generator can be invoked via CMake +if(SAPI_ENABLE_TESTS) + # sandboxed_api:sapi_test + add_executable(sapi_test + sapi_test.cc + ) + target_link_libraries(sapi_test PRIVATE + absl::memory + benchmark + sapi::sapi + sapi::status + sapi::status_matchers + sapi::stringop_sapi + sapi::sum_sapi + sapi::test_main + ) + gtest_discover_tests(sapi_test) +endif()