diff --git a/Modules/Compiler/Clang-CXX.cmake b/Modules/Compiler/Clang-CXX.cmake index c0d23564fa..a1ce6a1268 100644 --- a/Modules/Compiler/Clang-CXX.cmake +++ b/Modules/Compiler/Clang-CXX.cmake @@ -32,6 +32,13 @@ endif() if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 16.0) if("x${CMAKE_CXX_COMPILER_FRONTEND_VARIANT}" STREQUAL "xGNU") + if (CMAKE_HOST_WIN32) + # `rename` doesn't overwrite and doesn't retry in case of "target file is + # busy". + set(_clang_scan_deps_mv "\"${CMAKE_COMMAND}\" -E rename") + else () + set(_clang_scan_deps_mv "mv") + endif () string(CONCAT CMAKE_CXX_SCANDEP_SOURCE "\"${CMAKE_CXX_COMPILER_CLANG_SCAN_DEPS}\"" " -format=p1689" @@ -40,7 +47,15 @@ if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 16.0) " -x c++ -c -o " " -MT " " -MD -MF " - " > ") + # Write to a temporary file. If the scan fails, we do not want to update + # the actual output file as `ninja` (at least) assumes that failed + # commands either delete or leave output files alone. See Issue#25419. + " > .tmp" + # We cannot use `copy_if_different` as the rule does not have a feature + # analogous to `ninja`'s `restat = 1`. It would also leave behind the + # `.tmp` file. + " && ${_clang_scan_deps_mv} .tmp ") + unset(_clang_scan_deps_mv) set(CMAKE_CXX_MODULE_MAP_FORMAT "clang") set(CMAKE_CXX_MODULE_MAP_FLAG "@") set(CMAKE_CXX_MODULE_BMI_ONLY_FLAG "--precompile") diff --git a/Tests/RunCMake/CXXModules/examples/scan_properties/CMakeLists.txt b/Tests/RunCMake/CXXModules/examples/scan_properties/CMakeLists.txt index f5e5da6427..47be1d9c2d 100644 --- a/Tests/RunCMake/CXXModules/examples/scan_properties/CMakeLists.txt +++ b/Tests/RunCMake/CXXModules/examples/scan_properties/CMakeLists.txt @@ -13,8 +13,8 @@ endif () # if not. string(APPEND CMAKE_CXX_MODULE_MAP_FLAG " -DCMAKE_SCANNED_THIS_SOURCE") -string(APPEND CMAKE_CXX_SCANDEP_SOURCE - " -DCMAKE_SCANNED_THIS_SOURCE") +string(REPLACE "" " -DCMAKE_SCANNED_THIS_SOURCE" + CMAKE_CXX_SCANDEP_SOURCE "${CMAKE_CXX_SCANDEP_SOURCE}") set_property(SOURCE always_scan.cxx PROPERTY CXX_SCAN_FOR_MODULES 1)