Skip to content

Commit ab6e8d6

Browse files
authored
Performance improvement: bypass FetchContent for cached dependencies (#182)
* skip FetchContent for cached dependencies * set default value for CPM_SKIP_FETCH to avoid conflicts * add tests to check exported values
1 parent aad0397 commit ab6e8d6

File tree

5 files changed

+68
-12
lines changed

5 files changed

+68
-12
lines changed

cmake/CPM.cmake

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ function(CPMAddPackage)
286286
endif()
287287
endif()
288288

289+
set(CPM_SKIP_FETCH FALSE)
290+
289291
if(DEFINED CPM_ARGS_GIT_TAG)
290292
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_TAG ${CPM_ARGS_GIT_TAG})
291293
# If GIT_SHALLOW is explicitly specified, honor the value.
@@ -374,9 +376,15 @@ function(CPMAddPackage)
374376
set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash})
375377
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS SOURCE_DIR ${download_directory})
376378
if(EXISTS ${download_directory})
377-
# disable the download command to allow offline builds
378-
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS DOWNLOAD_COMMAND "${CMAKE_COMMAND}")
379-
set(PACKAGE_INFO "${download_directory}")
379+
# avoid FetchContent modules to improve performance
380+
set(${CPM_ARGS_NAME}_BINARY_DIR ${CMAKE_BINARY_DIR}/_deps/${lower_case_name}-build)
381+
set(${CPM_ARGS_NAME}_ADDED YES)
382+
set(${CPM_ARGS_NAME}_SOURCE_DIR ${download_directory})
383+
if(NOT CPM_ARGS_DOWNLOAD_ONLY AND EXISTS ${download_directory}/CMakeLists.txt)
384+
add_subdirectory(${download_directory} ${${CPM_ARGS_NAME}_BINARY_DIR})
385+
endif()
386+
set(CPM_SKIP_FETCH TRUE)
387+
set(PACKAGE_INFO "${PACKAGE_INFO} at ${download_directory}")
380388
else()
381389
# Enable shallow clone when GIT_TAG is not a commit hash. Our guess may not be accurate, but
382390
# it should guarantee no commit hash get mis-detected.
@@ -389,7 +397,7 @@ function(CPMAddPackage)
389397

390398
# remove timestamps so CMake will re-download the dependency
391399
file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/_deps/${lower_case_name}-subbuild)
392-
set(PACKAGE_INFO "${PACKAGE_INFO} -> ${download_directory}")
400+
set(PACKAGE_INFO "${PACKAGE_INFO} to ${download_directory}")
393401
endif()
394402
endif()
395403

@@ -405,11 +413,17 @@ function(CPMAddPackage)
405413
endif()
406414
endif()
407415

408-
cpm_declare_fetch(
409-
"${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}" "${PACKAGE_INFO}" "${CPM_ARGS_UNPARSED_ARGUMENTS}"
416+
message(
417+
STATUS "${CPM_INDENT} adding package ${CPM_ARGS_NAME}@${CPM_ARGS_VERSION} (${PACKAGE_INFO})"
410418
)
411-
cpm_fetch_package("${CPM_ARGS_NAME}" "${DOWNLOAD_ONLY}")
412-
cpm_get_fetch_properties("${CPM_ARGS_NAME}")
419+
420+
if(NOT CPM_SKIP_FETCH)
421+
cpm_declare_fetch(
422+
"${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}" "${PACKAGE_INFO}" "${CPM_ARGS_UNPARSED_ARGUMENTS}"
423+
)
424+
cpm_fetch_package("${CPM_ARGS_NAME}" "${DOWNLOAD_ONLY}")
425+
cpm_get_fetch_properties("${CPM_ARGS_NAME}")
426+
endif()
413427

414428
set(${CPM_ARGS_NAME}_ADDED YES)
415429
cpm_export_variables("${CPM_ARGS_NAME}")
@@ -503,8 +517,6 @@ endfunction()
503517

504518
# declares a package in FetchContent_Declare
505519
function(cpm_declare_fetch PACKAGE VERSION INFO)
506-
message(STATUS "${CPM_INDENT} adding package ${PACKAGE}@${VERSION} (${INFO})")
507-
508520
if(${CPM_DRY_RUN})
509521
message(STATUS "${CPM_INDENT} package not declared (dry run)")
510522
return()

cmake/testing.cmake

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,38 @@ function(ASSERT_EMPTY)
1616
endif()
1717
endfunction()
1818

19+
function(ASSERT_DEFINED KEY)
20+
if(DEFINED ${KEY})
21+
message(STATUS "test passed: '${KEY}' is defined")
22+
else()
23+
message(FATAL_ERROR "assertion failed: '${KEY}' is not defiend")
24+
endif()
25+
endfunction()
26+
27+
function(ASSERT_TRUTHY KEY)
28+
if(${${KEY}})
29+
message(STATUS "test passed: '${KEY}' is set truthy")
30+
else()
31+
message(FATAL_ERROR "assertion failed: value of '${KEY}' is not true (${${KEY}})")
32+
endif()
33+
endfunction()
34+
1935
function(ASSERTION_FAILED)
2036
message(FATAL_ERROR "assertion failed: ${ARGN}")
2137
endfunction()
2238

2339
function(ASSERT_EXISTS file)
24-
if(NOT EXISTS ${file})
40+
if(EXISTS ${file})
41+
message(STATUS "test passed: '${file}' exists")
42+
else()
2543
message(FATAL_ERROR "assertion failed: file ${file} does not exist")
2644
endif()
2745
endfunction()
2846

2947
function(ASSERT_NOT_EXISTS file)
30-
if(EXISTS ${file})
48+
if(NOT EXISTS ${file})
49+
message(STATUS "test passed: '${file}' does not exist")
50+
else()
3151
message(FATAL_ERROR "assertion failed: file ${file} exists")
3252
endif()
3353
endfunction()

test/unit/local_dependency/ModuleCMakeLists.txt.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ endif()
2424
# ---- Call dependency method to validate correct addition of directory ----
2525

2626
dependency_function()
27+
28+
# ---- Check parameters ----
29+
30+
include(@CPM_PATH@/testing.cmake)
31+
32+
ASSERT_TRUTHY(@TEST_DEPENDENCY_NAME@_ADDED)
33+
ASSERT_DEFINED(@TEST_DEPENDENCY_NAME@_SOURCE_DIR)
34+
ASSERT_DEFINED(@TEST_DEPENDENCY_NAME@_BINARY_DIR)

test/unit/local_dependency/OverrideCMakeLists.txt.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,11 @@ CPMAddPackage(
1717
# ---- Call dependency method to validate correct addition of directory ----
1818

1919
dependency_function()
20+
21+
# ---- Check parameters ----
22+
23+
include(@CPM_PATH@/testing.cmake)
24+
25+
ASSERT_TRUTHY(Dependency_ADDED)
26+
ASSERT_DEFINED(Dependency_SOURCE_DIR)
27+
ASSERT_DEFINED(Dependency_BINARY_DIR)

test/unit/remote_dependency/CMakeLists.txt.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ CPMAddPackage(
2222
add_executable(CPMExampleCatch2 main.cpp)
2323
target_link_libraries(CPMExampleCatch2 fibonacci)
2424
set_target_properties(CPMExampleCatch2 PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-Wall -pedantic -Wextra -Werror")
25+
26+
# ---- Check parameters ----
27+
28+
include(@CPM_PATH@/testing.cmake)
29+
30+
ASSERT_TRUTHY(fibonacci_ADDED)
31+
ASSERT_DEFINED(fibonacci_SOURCE_DIR)
32+
ASSERT_DEFINED(fibonacci_BINARY_DIR)

0 commit comments

Comments
 (0)