diff options
author | Craig Scott <craig.scott@crascit.com> | 2021-02-18 01:31:48 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2021-02-22 16:02:20 (GMT) |
commit | b0da6712439fe44d84a158dceb5f946cc30ef68a (patch) | |
tree | d12768dcd5e02057096eba1e77f3926d1228fc3b /Modules/ExternalProject.cmake | |
parent | 404cddb7bbb0e05bba2f3023f0ee2fa302c35124 (diff) | |
download | CMake-b0da6712439fe44d84a158dceb5f946cc30ef68a.zip CMake-b0da6712439fe44d84a158dceb5f946cc30ef68a.tar.gz CMake-b0da6712439fe44d84a158dceb5f946cc30ef68a.tar.bz2 |
FetchContent: Don't update timestamps if files don't change
The refactoring in 17e5516e60 (FetchContent: Invoke steps directly and
avoid a separate sub-build, 2021-01-29) uses a different way of writing
out the step scripts and updating time stamps when steps are executed.
That inadvertently always wrote out the scripts for custom commands,
even when the contents didn't change. This caused their timestamp to
always be updated, resulting in those steps always being seen as
out-of-date and needing to be re-executed.
The way timestamps were checked to determine whether to re-execute
a step also did not adequately account for file systems which only have
second-resolution timestamps. The IS_NEWER_THAN if condition also
returns true when timestamps are the same, so one needs to use the
negative form to get a true "is newer than" test.
ExternalProject is not susceptible to this problem because it uses
file(GENERATE) to write out the script files and that only updates the file's
timestamp if the contents change. It also mostly leaves timestamp
checking to the build tool.
Diffstat (limited to 'Modules/ExternalProject.cmake')
-rw-r--r-- | Modules/ExternalProject.cmake | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/Modules/ExternalProject.cmake b/Modules/ExternalProject.cmake index 4a9809b..987b69a 100644 --- a/Modules/ExternalProject.cmake +++ b/Modules/ExternalProject.cmake @@ -2579,11 +2579,24 @@ function(_ep_write_command_script endif() if(genex_supported) - # Only written at generation phase + # Only written at generation phase. This will only change the file's + # timestamp if the contents change. file(GENERATE OUTPUT "${script_filename}" CONTENT "${script_content}") else() - # Written immediately, needed if script has to be invoked in configure phase - file(WRITE "${script_filename}" "${script_content}") + # Update the file immediately, needed if script has to be invoked in the + # configure phase (e.g. via FetchContent). We need to be careful to avoid + # updating the timestamp if the file contents don't change. The file(WRITE) + # command always updates the file, so avoid it if we don't need to call it. + set(doWrite TRUE) + if(EXISTS "${script_filename}") + file(READ "${script_filename}" existing_content) + if(existing_content STREQUAL script_content) + set(doWrite FALSE) + endif() + endif() + if(doWrite) + file(WRITE "${script_filename}" "${script_content}") + endif() endif() endfunction() @@ -3916,7 +3929,12 @@ function(_ep_do_preconfigure_steps_now name) if(NOT need_to_run) foreach(dep_file ${script_file} ${_EPdepends_${STEP}}) - if(NOT EXISTS ${dep_file} OR ${dep_file} IS_NEWER_THAN ${stamp_file}) + # IS_NEWER_THAN is also true if the timestamps are the same. On some + # file systems, we only have second resolution timestamps and the + # likelihood of having the same timestamp is high. Use the negative + # form to ensure we actually get a true "is newer than" test. + if(NOT EXISTS ${dep_file} OR + NOT ${stamp_file} IS_NEWER_THAN ${dep_file}) set(need_to_run TRUE) break() endif() |