Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 71fd6a7

Browse files
meastpg-easy
authored andcommitted
CMake: Replace ExternalProject_Add with FetchContent. (#290)
This reduces the amount of code for including/building dependencies.
1 parent fa8a8b4 commit 71fd6a7

File tree

7 files changed

+1118
-157
lines changed

7 files changed

+1118
-157
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ include(CTest) # Defines option BUILD_TESTING.
3030
enable_testing()
3131

3232
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
33+
3334
include(OpenCensusDeps)
3435

3536
include(OpenCensusHelpers)

cmake/FetchContent.cmake

Lines changed: 1061 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2+
# file Copyright.txt or https://cmake.org/licensing for details.
3+
4+
cmake_minimum_required(VERSION ${CMAKE_VERSION})
5+
6+
# We name the project and the target for the ExternalProject_Add() call
7+
# to something that will highlight to the user what we are working on if
8+
# something goes wrong and an error message is produced.
9+
10+
project(${contentName}-populate NONE)
11+
12+
include(ExternalProject)
13+
ExternalProject_Add(${contentName}-populate
14+
${ARG_EXTRA}
15+
SOURCE_DIR "${ARG_SOURCE_DIR}"
16+
BINARY_DIR "${ARG_BINARY_DIR}"
17+
CONFIGURE_COMMAND ""
18+
BUILD_COMMAND ""
19+
INSTALL_COMMAND ""
20+
TEST_COMMAND ""
21+
USES_TERMINAL_DOWNLOAD YES
22+
USES_TERMINAL_UPDATE YES
23+
)

cmake/OpenCensusDeps.cmake

Lines changed: 33 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,29 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
include(FetchContent)
16+
17+
fetchcontent_declare(googletest
18+
GIT_REPOSITORY
19+
https://github.com/abseil/googletest
20+
GIT_TAG
21+
ed2fe122f8dc9aca844d724986d1d5cf5b65ea4e)
22+
fetchcontent_declare(abseil
23+
GIT_REPOSITORY
24+
https://github.com/abseil/abseil-cpp
25+
GIT_TAG
26+
master)
27+
fetchcontent_declare(prometheus
28+
GIT_REPOSITORY
29+
https://github.com/jupp0r/prometheus-cpp
30+
GIT_TAG
31+
master)
32+
33+
fetchcontent_getproperties(googletest)
1534
if(BUILD_TESTING)
16-
if(NOT TARGET gtest_main)
17-
message(STATUS "Dependency: googletest (BUILD_TESTING=${BUILD_TESTING})")
35+
message(STATUS "Dependency: googletest (BUILD_TESTING=${BUILD_TESTING})")
1836

37+
if(NOT googletest_POPULATED)
1938
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
2039
# All the libraries in the build must use either /MD or /MT (runtime
2140
# library to link)
@@ -29,73 +48,27 @@ if(BUILD_TESTING)
2948
ON)
3049
endif()
3150

32-
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/googletest.CMakeLists.txt
33-
${CMAKE_BINARY_DIR}/googletest-download/CMakeLists.txt)
34-
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
35-
RESULT_VARIABLE result
36-
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)
37-
if(result)
38-
message(FATAL_ERROR "CMake step failed: ${result}")
39-
endif()
40-
execute_process(COMMAND ${CMAKE_COMMAND} --build .
41-
RESULT_VARIABLE result
42-
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)
43-
if(result)
44-
message(FATAL_ERROR "Build step failed: ${result}")
45-
endif()
46-
47-
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
48-
${CMAKE_BINARY_DIR}/googletest-build EXCLUDE_FROM_ALL)
51+
fetchcontent_populate(googletest)
52+
add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR}
53+
EXCLUDE_FROM_ALL)
4954
endif()
5055
endif()
5156

52-
# Load abseil second, it depends on googletest.
53-
if(NOT TARGET absl::base)
54-
message(STATUS "Dependency: abseil")
55-
56-
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/abseil.CMakeLists.txt
57-
${CMAKE_BINARY_DIR}/abseil-download/CMakeLists.txt)
58-
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
59-
RESULT_VARIABLE result
60-
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/abseil-download)
61-
if(result)
62-
message(FATAL_ERROR "CMake step failed: ${result}")
63-
endif()
64-
execute_process(COMMAND ${CMAKE_COMMAND} --build .
65-
RESULT_VARIABLE result
66-
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/abseil-download)
67-
if(result)
68-
message(FATAL_ERROR "Build step failed: ${result}")
69-
endif()
70-
71-
add_subdirectory(${CMAKE_BINARY_DIR}/abseil-src
72-
${CMAKE_BINARY_DIR}/abseil-build EXCLUDE_FROM_ALL)
57+
fetchcontent_getproperties(abseil)
58+
if(NOT abseil_POPULATED)
59+
fetchcontent_populate(abseil)
60+
add_subdirectory(${abseil_SOURCE_DIR} ${abseil_BINARY_DIR} EXCLUDE_FROM_ALL)
7361
endif()
7462

75-
if(NOT TARGET prometheus-cpp::core)
76-
message(STATUS "Dependency: prometheus-cpp")
77-
78-
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/prometheus-cpp.CMakeLists.txt
79-
${CMAKE_BINARY_DIR}/prometheus-download/CMakeLists.txt)
80-
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
81-
RESULT_VARIABLE result
82-
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/prometheus-download)
83-
if(result)
84-
message(FATAL_ERROR "CMake step failed: ${result}")
85-
endif()
86-
execute_process(COMMAND ${CMAKE_COMMAND} --build .
87-
RESULT_VARIABLE result
88-
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/prometheus-download)
89-
if(result)
90-
message(FATAL_ERROR "Build step failed: ${result}")
91-
endif()
92-
63+
fetchcontent_getproperties(prometheus)
64+
if(NOT prometheus_POPULATED)
9365
set(ENABLE_PUSH OFF CACHE BOOL "Build prometheus-cpp push library" FORCE)
9466
set(ENABLE_PULL OFF CACHE BOOL "Build prometheus-cpp pull library" FORCE)
9567
set(ENABLE_COMPRESSION OFF
9668
CACHE BOOL "Enable gzip compression for prometheus-cpp"
9769
FORCE)
9870
set(ENABLE_TESTING OFF CACHE BOOL "Build test for prometheus-cpp" FORCE)
99-
add_subdirectory(${CMAKE_BINARY_DIR}/prometheus-src
100-
${CMAKE_BINARY_DIR}/prometheus-build EXCLUDE_FROM_ALL)
71+
fetchcontent_populate(prometheus)
72+
add_subdirectory(${prometheus_SOURCE_DIR} ${prometheus_BINARY_DIR}
73+
EXCLUDE_FROM_ALL)
10174
endif()

cmake/abseil.CMakeLists.txt

Lines changed: 0 additions & 31 deletions
This file was deleted.

cmake/googletest.CMakeLists.txt

Lines changed: 0 additions & 35 deletions
This file was deleted.

cmake/prometheus-cpp.CMakeLists.txt

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)