Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timestamps for URL downloads match the download time #372

Merged
merged 9 commits into from
Aug 3, 2022
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ In the case that `find_package` requires additional arguments, the parameter `FI
If set, CPM use additional directory level in cache to improve readability of packages names in IDEs like CLion. It changes cache structure, so all dependencies are downloaded again. There is no problem to mix both structures in one cache directory but then there may be 2 copies of some dependencies.
This can also be set as an environmental variable.

### CPM_SET_RECOMMENDED_CMAKE_POLICIES
If set, CPM will default the following CMake policies to `NEW` for all packages brought
in via CPM
* CMP0077 - The option() command does nothing when a normal variable of the given name already exists.
* CMP0126 - The set(CACHE) command will not remove an existing normal variable of the given name.
* CMP0135 - Use the download time for timestamps, instead of the timestamps in the archive. This allows for proper rebuilds when a projects url changes
robertmaynard marked this conversation as resolved.
Show resolved Hide resolved

## Local package override

Library developers are often in the situation where they work on a locally checked out dependency at the same time as on a consumer project.
Expand Down
35 changes: 35 additions & 0 deletions cmake/CPM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ set(CPM_DRY_RUN
OFF
CACHE INTERNAL "Don't download or configure dependencies (for testing)"
)
set(CPM_SET_RECOMMENDED_CMAKE_POLICIES
OFF
CACHE INTERNAL "Have CPM enable all recommended CMake policies"
)

if(DEFINED ENV{CPM_SOURCE_CACHE})
set(CPM_SOURCE_CACHE_DEFAULT $ENV{CPM_SOURCE_CACHE})
Expand Down Expand Up @@ -239,8 +243,30 @@ function(cpm_create_module_file Name)
endif()
endfunction()

macro(cpm_set_cmake_policies)
if(CPM_SET_RECOMMENDED_CMAKE_POLICIES)
# the policy allows us to change options without caching
cmake_policy(SET CMP0077 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)

# the policy allows us to change set(CACHE) without caching
if(POLICY CMP0126)
cmake_policy(SET CMP0126 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0126 NEW)
endif()

# The policy uses the download time for timestamp, instead of the timestamp in the archive. This
# allows for proper rebuilds when a projects url changes
if(POLICY CMP0135)
cmake_policy(SET CMP0135 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0135 NEW)
endif()
endif()
endmacro()

# Find a package locally or fallback to CPMAddPackage
function(CPMFindPackage)
cpm_set_cmake_policies()
set(oneValueArgs NAME VERSION GIT_TAG FIND_PACKAGE_ARGUMENTS)

cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "" ${ARGN})
Expand Down Expand Up @@ -476,6 +502,8 @@ endfunction()

# Download and add a package from source
function(CPMAddPackage)
cpm_set_cmake_policies()
TheLartians marked this conversation as resolved.
Show resolved Hide resolved

list(LENGTH ARGN argnLength)
if(argnLength EQUAL 1)
cpm_parse_add_package_single_arg("${ARGN}" ARGN)
Expand Down Expand Up @@ -567,6 +595,13 @@ function(CPMAddPackage)
endif()

list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS URL "${CPM_ARGS_URL}")

# Prefer to use the download time for timestamp, instead of the timestamp in the archive unless
# explicitly set by user. This allows for proper rebuilds when a projects url changes
if(POLICY CMP0135)
cmake_policy(SET CMP0135 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0135 NEW)
endif()
robertmaynard marked this conversation as resolved.
Show resolved Hide resolved
endif()

# Check for required arguments
Expand Down