Skip to content

Commit 0370507

Browse files
gerhardolgerhardolsTheLartians
authored
Custom cache directory name (#543)
* Custom cache directory name Enable setting a custom directory name for cached packages. Required if e.g. patch_command uses external variables or just to set a human readable name. * fixup! review comment CUSTOM_CACHE_DIR -> CUSTOM_CACHE_KEY * run cmake-format --------- Co-authored-by: Olsson Gerhard <[email protected]> Co-authored-by: Lars Melchior <[email protected]>
1 parent c0855c9 commit 0370507

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ Note that passing the variable as a configure option to CMake will always overri
190190

191191
You can use `CPM_SOURCE_CACHE` on GitHub Actions workflows [cache](https://github.com/actions/cache) and combine it with ccache, to make your CI faster. See the [wiki](https://github.com/cpm-cmake/CPM.cmake/wiki/Caching-with-CPM.cmake-and-ccache-on-GitHub-Actions) for more info.
192192

193+
The directory where the version for a project is stored is by default the hash of the arguments to `CPMAddPackage()`.
194+
If for instance the patch command uses external files, the directory name can be set with the argument `CUSTOM_CACHE_KEY`.
195+
193196
### CPM_DOWNLOAD_ALL
194197

195198
If set, CPM will forward all calls to `CPMFindPackage` as `CPMAddPackage`.

cmake/CPM.cmake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ function(CPMAddPackage)
534534
GIT_SHALLOW
535535
EXCLUDE_FROM_ALL
536536
SOURCE_SUBDIR
537+
CUSTOM_CACHE_KEY
537538
)
538539

539540
set(multiValueArgs URL OPTIONS DOWNLOAD_COMMAND)
@@ -704,7 +705,10 @@ function(CPMAddPackage)
704705
string(TOLOWER ${CPM_ARGS_NAME} lower_case_name)
705706
set(origin_parameters ${CPM_ARGS_UNPARSED_ARGUMENTS})
706707
list(SORT origin_parameters)
707-
if(CPM_USE_NAMED_CACHE_DIRECTORIES)
708+
if(CPM_ARGS_CUSTOM_CACHE_KEY)
709+
# Application set a custom unique directory name
710+
set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${CPM_ARGS_CUSTOM_CACHE_KEY})
711+
elseif(CPM_USE_NAMED_CACHE_DIRECTORIES)
708712
string(SHA1 origin_hash "${origin_parameters};NEW_CACHE_STRUCTURE_TAG")
709713
set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash}/${CPM_ARGS_NAME})
710714
else()

test/unit/cache.cmake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,19 @@ execute_process(
137137

138138
assert_equal(${ret} "0")
139139
assert_not_exists("${CPM_SOURCE_CACHE_DIR}/fibonacci")
140+
141+
# Use custom cache directory
142+
143+
set(FIBONACCI_PACKAGE_ARGS
144+
"CUSTOM_CACHE_KEY my_custom_unique_dir GIT_TAG e9ebf168ca0fffaa4ef8c6fefc6346aaa22f6ed5"
145+
)
146+
set(FIBONACCI_VERSION 1.1)
147+
update_cmake_lists()
148+
149+
execute_process(
150+
COMMAND ${CMAKE_COMMAND} -E env "CPM_SOURCE_CACHE=${CPM_SOURCE_CACHE_DIR}" ${CMAKE_COMMAND}
151+
"-S${CMAKE_CURRENT_LIST_DIR}/remote_dependency" "-B${TEST_BUILD_DIR}" RESULT_VARIABLE ret
152+
)
153+
154+
assert_equal(${ret} "0")
155+
assert_exists("${CPM_SOURCE_CACHE_DIR}/fibonacci/my_custom_unique_dir")

0 commit comments

Comments
 (0)