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
This commit is contained in:
Lars Melchior
2021-01-20 13:15:18 +01:00
committed by GitHub
parent aad0397beb
commit ab6e8d6d8c
5 changed files with 68 additions and 12 deletions

View File

@@ -286,6 +286,8 @@ function(CPMAddPackage)
endif()
endif()
set(CPM_SKIP_FETCH FALSE)
if(DEFINED CPM_ARGS_GIT_TAG)
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_TAG ${CPM_ARGS_GIT_TAG})
# If GIT_SHALLOW is explicitly specified, honor the value.
@@ -374,9 +376,15 @@ function(CPMAddPackage)
set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash})
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS SOURCE_DIR ${download_directory})
if(EXISTS ${download_directory})
# disable the download command to allow offline builds
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS DOWNLOAD_COMMAND "${CMAKE_COMMAND}")
set(PACKAGE_INFO "${download_directory}")
# avoid FetchContent modules to improve performance
set(${CPM_ARGS_NAME}_BINARY_DIR ${CMAKE_BINARY_DIR}/_deps/${lower_case_name}-build)
set(${CPM_ARGS_NAME}_ADDED YES)
set(${CPM_ARGS_NAME}_SOURCE_DIR ${download_directory})
if(NOT CPM_ARGS_DOWNLOAD_ONLY AND EXISTS ${download_directory}/CMakeLists.txt)
add_subdirectory(${download_directory} ${${CPM_ARGS_NAME}_BINARY_DIR})
endif()
set(CPM_SKIP_FETCH TRUE)
set(PACKAGE_INFO "${PACKAGE_INFO} at ${download_directory}")
else()
# Enable shallow clone when GIT_TAG is not a commit hash. Our guess may not be accurate, but
# it should guarantee no commit hash get mis-detected.
@@ -389,7 +397,7 @@ function(CPMAddPackage)
# remove timestamps so CMake will re-download the dependency
file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/_deps/${lower_case_name}-subbuild)
set(PACKAGE_INFO "${PACKAGE_INFO} -> ${download_directory}")
set(PACKAGE_INFO "${PACKAGE_INFO} to ${download_directory}")
endif()
endif()
@@ -405,11 +413,17 @@ function(CPMAddPackage)
endif()
endif()
cpm_declare_fetch(
"${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}" "${PACKAGE_INFO}" "${CPM_ARGS_UNPARSED_ARGUMENTS}"
message(
STATUS "${CPM_INDENT} adding package ${CPM_ARGS_NAME}@${CPM_ARGS_VERSION} (${PACKAGE_INFO})"
)
cpm_fetch_package("${CPM_ARGS_NAME}" "${DOWNLOAD_ONLY}")
cpm_get_fetch_properties("${CPM_ARGS_NAME}")
if(NOT CPM_SKIP_FETCH)
cpm_declare_fetch(
"${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}" "${PACKAGE_INFO}" "${CPM_ARGS_UNPARSED_ARGUMENTS}"
)
cpm_fetch_package("${CPM_ARGS_NAME}" "${DOWNLOAD_ONLY}")
cpm_get_fetch_properties("${CPM_ARGS_NAME}")
endif()
set(${CPM_ARGS_NAME}_ADDED YES)
cpm_export_variables("${CPM_ARGS_NAME}")
@@ -503,8 +517,6 @@ endfunction()
# declares a package in FetchContent_Declare
function(cpm_declare_fetch PACKAGE VERSION INFO)
message(STATUS "${CPM_INDENT} adding package ${PACKAGE}@${VERSION} (${INFO})")
if(${CPM_DRY_RUN})
message(STATUS "${CPM_INDENT} package not declared (dry run)")
return()

View File

@@ -16,18 +16,38 @@ function(ASSERT_EMPTY)
endif()
endfunction()
function(ASSERT_DEFINED KEY)
if(DEFINED ${KEY})
message(STATUS "test passed: '${KEY}' is defined")
else()
message(FATAL_ERROR "assertion failed: '${KEY}' is not defiend")
endif()
endfunction()
function(ASSERT_TRUTHY KEY)
if(${${KEY}})
message(STATUS "test passed: '${KEY}' is set truthy")
else()
message(FATAL_ERROR "assertion failed: value of '${KEY}' is not true (${${KEY}})")
endif()
endfunction()
function(ASSERTION_FAILED)
message(FATAL_ERROR "assertion failed: ${ARGN}")
endfunction()
function(ASSERT_EXISTS file)
if(NOT EXISTS ${file})
if(EXISTS ${file})
message(STATUS "test passed: '${file}' exists")
else()
message(FATAL_ERROR "assertion failed: file ${file} does not exist")
endif()
endfunction()
function(ASSERT_NOT_EXISTS file)
if(EXISTS ${file})
if(NOT EXISTS ${file})
message(STATUS "test passed: '${file}' does not exist")
else()
message(FATAL_ERROR "assertion failed: file ${file} exists")
endif()
endfunction()

View File

@@ -24,3 +24,11 @@ endif()
# ---- Call dependency method to validate correct addition of directory ----
dependency_function()
# ---- Check parameters ----
include(@CPM_PATH@/testing.cmake)
ASSERT_TRUTHY(@TEST_DEPENDENCY_NAME@_ADDED)
ASSERT_DEFINED(@TEST_DEPENDENCY_NAME@_SOURCE_DIR)
ASSERT_DEFINED(@TEST_DEPENDENCY_NAME@_BINARY_DIR)

View File

@@ -17,3 +17,11 @@ CPMAddPackage(
# ---- Call dependency method to validate correct addition of directory ----
dependency_function()
# ---- Check parameters ----
include(@CPM_PATH@/testing.cmake)
ASSERT_TRUTHY(Dependency_ADDED)
ASSERT_DEFINED(Dependency_SOURCE_DIR)
ASSERT_DEFINED(Dependency_BINARY_DIR)

View File

@@ -22,3 +22,11 @@ CPMAddPackage(
add_executable(CPMExampleCatch2 main.cpp)
target_link_libraries(CPMExampleCatch2 fibonacci)
set_target_properties(CPMExampleCatch2 PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-Wall -pedantic -Wextra -Werror")
# ---- Check parameters ----
include(@CPM_PATH@/testing.cmake)
ASSERT_TRUTHY(fibonacci_ADDED)
ASSERT_DEFINED(fibonacci_SOURCE_DIR)
ASSERT_DEFINED(fibonacci_BINARY_DIR)