From ee2027807334b4eef501815a982cbe3db9b5a37b Mon Sep 17 00:00:00 2001 From: Mats Taraldsvik Date: Thu, 28 Feb 2019 06:19:59 +0100 Subject: [PATCH] CMake: Replace ExternalProject_Add with FetchContent --- CMakeLists.txt | 3 +- cmake/OpenCensusDeps.cmake | 95 ++++++++++------------------- cmake/abseil.CMakeLists.txt | 31 ---------- cmake/googletest.CMakeLists.txt | 35 ----------- cmake/prometheus-cpp.CMakeLists.txt | 31 ---------- 5 files changed, 35 insertions(+), 160 deletions(-) delete mode 100644 cmake/abseil.CMakeLists.txt delete mode 100644 cmake/googletest.CMakeLists.txt delete mode 100644 cmake/prometheus-cpp.CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 45e76dae..53e89f31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.11) # Use ccache if it's present. find_program(CCACHE_PROGRAM ccache) @@ -30,6 +30,7 @@ include(CTest) # Defines option BUILD_TESTING. enable_testing() list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) + include(OpenCensusDeps) include(OpenCensusHelpers) diff --git a/cmake/OpenCensusDeps.cmake b/cmake/OpenCensusDeps.cmake index 469ed53d..1e36a029 100644 --- a/cmake/OpenCensusDeps.cmake +++ b/cmake/OpenCensusDeps.cmake @@ -12,10 +12,29 @@ # See the License for the specific language governing permissions and # limitations under the License. -if(BUILD_TESTING) - if(NOT TARGET gtest_main) - message(STATUS "Dependency: googletest (BUILD_TESTING=${BUILD_TESTING})") +include(FetchContent) + +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/abseil/googletest + GIT_TAG ed2fe122f8dc9aca844d724986d1d5cf5b65ea4e +) +FetchContent_Declare( + abseil + GIT_REPOSITORY https://github.com/abseil/abseil-cpp + GIT_TAG master +) +FetchContent_Declare( + prometheus + GIT_REPOSITORY https://github.com/jupp0r/prometheus-cpp + GIT_TAG master +) +FetchContent_GetProperties(googletest) +if(BUILD_TESTING) + message(STATUS "Dependency: googletest (BUILD_TESTING=${BUILD_TESTING})") + + if(NOT googletest_POPULATED) if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") # All the libraries in the build must use either /MD or /MT (runtime # library to link) @@ -28,74 +47,26 @@ if(BUILD_TESTING) "Use shared (DLL) run-time lib even when Google Test is built as static lib." ON) endif() - - configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/googletest.CMakeLists.txt - ${CMAKE_BINARY_DIR}/googletest-download/CMakeLists.txt) - execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . - RESULT_VARIABLE result - WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download) - if(result) - message(FATAL_ERROR "CMake step failed: ${result}") - endif() - execute_process(COMMAND ${CMAKE_COMMAND} --build . - RESULT_VARIABLE result - WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download) - if(result) - message(FATAL_ERROR "Build step failed: ${result}") - endif() - - add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src - ${CMAKE_BINARY_DIR}/googletest-build EXCLUDE_FROM_ALL) + + FetchContent_Populate(googletest) + add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} EXCLUDE_FROM_ALL) endif() endif() -# Load abseil second, it depends on googletest. -if(NOT TARGET absl::base) - message(STATUS "Dependency: abseil") - - configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/abseil.CMakeLists.txt - ${CMAKE_BINARY_DIR}/abseil-download/CMakeLists.txt) - execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . - RESULT_VARIABLE result - WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/abseil-download) - if(result) - message(FATAL_ERROR "CMake step failed: ${result}") - endif() - execute_process(COMMAND ${CMAKE_COMMAND} --build . - RESULT_VARIABLE result - WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/abseil-download) - if(result) - message(FATAL_ERROR "Build step failed: ${result}") - endif() - - add_subdirectory(${CMAKE_BINARY_DIR}/abseil-src - ${CMAKE_BINARY_DIR}/abseil-build EXCLUDE_FROM_ALL) +FetchContent_GetProperties(abseil) +if(NOT abseil_POPULATED) + FetchContent_Populate(abseil) + add_subdirectory(${abseil_SOURCE_DIR} ${abseil_BINARY_DIR} EXCLUDE_FROM_ALL) endif() -if(NOT TARGET prometheus-cpp::core) - message(STATUS "Dependency: prometheus-cpp") - - configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/prometheus-cpp.CMakeLists.txt - ${CMAKE_BINARY_DIR}/prometheus-download/CMakeLists.txt) - execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . - RESULT_VARIABLE result - WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/prometheus-download) - if(result) - message(FATAL_ERROR "CMake step failed: ${result}") - endif() - execute_process(COMMAND ${CMAKE_COMMAND} --build . - RESULT_VARIABLE result - WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/prometheus-download) - if(result) - message(FATAL_ERROR "Build step failed: ${result}") - endif() - +FetchContent_GetProperties(prometheus) +if(NOT prometheus_POPULATED) set(ENABLE_PUSH OFF CACHE BOOL "Build prometheus-cpp push library" FORCE) set(ENABLE_PULL OFF CACHE BOOL "Build prometheus-cpp pull library" FORCE) set(ENABLE_COMPRESSION OFF CACHE BOOL "Enable gzip compression for prometheus-cpp" FORCE) set(ENABLE_TESTING OFF CACHE BOOL "Build test for prometheus-cpp" FORCE) - add_subdirectory(${CMAKE_BINARY_DIR}/prometheus-src - ${CMAKE_BINARY_DIR}/prometheus-build EXCLUDE_FROM_ALL) + FetchContent_Populate(prometheus) + add_subdirectory(${prometheus_SOURCE_DIR} ${prometheus_BINARY_DIR} EXCLUDE_FROM_ALL) endif() diff --git a/cmake/abseil.CMakeLists.txt b/cmake/abseil.CMakeLists.txt deleted file mode 100644 index 429adbfa..00000000 --- a/cmake/abseil.CMakeLists.txt +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright 2018, OpenCensus Authors -# -# 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.5) - -project(abseil-download NONE) - -include(ExternalProject) -externalproject_add(abseil_project - GIT_REPOSITORY https://github.com/abseil/abseil-cpp - GIT_TAG "master" - SOURCE_DIR "${CMAKE_BINARY_DIR}/abseil-src" - BINARY_DIR "${CMAKE_BINARY_DIR}/abseil-build" - UPDATE_COMMAND "" - PATCH_COMMAND "" - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - TEST_COMMAND "" - LOG_DOWNLOAD ON) diff --git a/cmake/googletest.CMakeLists.txt b/cmake/googletest.CMakeLists.txt deleted file mode 100644 index 6fb9054d..00000000 --- a/cmake/googletest.CMakeLists.txt +++ /dev/null @@ -1,35 +0,0 @@ -# Copyright 2018, OpenCensus Authors -# -# 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.5) - -project(googletest-download NONE) - -include(ExternalProject) - -# TODO: Un-pin googletest version, go back to living at HEAD. It's currently -# pinned to a commit right before a build break. Un-pin it once -# https://github.com/google/googletest/issues/2136 is fixed. -externalproject_add(googletest_project - GIT_REPOSITORY https://github.com/abseil/googletest - GIT_TAG "ed2fe122f8dc9aca844d724986d1d5cf5b65ea4e" - SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src" - BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build" - UPDATE_COMMAND "" - PATCH_COMMAND "" - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - TEST_COMMAND "" - LOG_DOWNLOAD ON) diff --git a/cmake/prometheus-cpp.CMakeLists.txt b/cmake/prometheus-cpp.CMakeLists.txt deleted file mode 100644 index e9e60d88..00000000 --- a/cmake/prometheus-cpp.CMakeLists.txt +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright 2018, OpenCensus Authors -# -# 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.5) - -project(prometheus-cpp-download NONE) - -include(ExternalProject) -externalproject_add(prometheus_cpp_project - GIT_REPOSITORY https://github.com/jupp0r/prometheus-cpp - GIT_TAG "master" - SOURCE_DIR "${CMAKE_BINARY_DIR}/prometheus-src" - BINARY_DIR "${CMAKE_BINARY_DIR}/prometheus-build" - UPDATE_COMMAND "" - PATCH_COMMAND "" - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - TEST_COMMAND "" - LOG_DOWNLOAD ON)