From 996fc21020b9a1ba4d8f22b01f1d08cab3fa8a08 Mon Sep 17 00:00:00 2001 From: Aiden Woodruff Date: Fri, 7 Aug 2026 12:44:51 -0400 Subject: [PATCH 1/2] Tests: Enable toolchain parts of RunCMake.CompilerChange on Windows A regression was missed partially due to this suite's condition. The compiler specific pieces still need this condition because a shell script needs to pretend to be a compiler, but the toolchain parts are platform agnostic. Issue: #28024 --- Tests/RunCMake/CMakeLists.txt | 4 +- .../CompilerChange/RunCMakeTest.cmake | 84 ++++++++++--------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 1406ca291a..be7f7578f4 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -392,9 +392,7 @@ add_RunCMake_test(CMakeRelease -DCMake_TEST_JQ=${CMake_TEST_JQ}) if(CMAKE_GENERATOR MATCHES "Make|Ninja") add_RunCMake_test(Color) endif() -if(UNIX AND "${CMAKE_GENERATOR}" MATCHES "Unix Makefiles|Ninja|FASTBuild") - add_RunCMake_test(CompilerChange) -endif() +add_RunCMake_test(CompilerChange) add_RunCMake_test(CompilerNotFound) if(DEFINED CMake_TEST_OBJC) list(APPEND CMake_TEST_LANG_VARS -DCMake_TEST_OBJC=${CMake_TEST_OBJC}) diff --git a/Tests/RunCMake/CompilerChange/RunCMakeTest.cmake b/Tests/RunCMake/CompilerChange/RunCMakeTest.cmake index c6b3767995..3d6546cc4c 100644 --- a/Tests/RunCMake/CompilerChange/RunCMakeTest.cmake +++ b/Tests/RunCMake/CompilerChange/RunCMakeTest.cmake @@ -1,47 +1,49 @@ include(RunCMake) -# Detect the compiler in use in the current environment. -run_cmake(FindCompiler) -include(${RunCMake_BINARY_DIR}/FindCompiler-build/cc.cmake) -if(NOT CMAKE_C_COMPILER) - message(FATAL_ERROR "FindCompiler provided no compiler!") +if(UNIX AND "${RunCMake_GENERATOR}" MATCHES "Unix Makefiles|Ninja|FASTBuild") + # Detect the compiler in use in the current environment. + run_cmake(FindCompiler) + include(${RunCMake_BINARY_DIR}/FindCompiler-build/cc.cmake) + if(NOT CMAKE_C_COMPILER) + message(FATAL_ERROR "FindCompiler provided no compiler!") + endif() + if(NOT IS_ABSOLUTE "${CMAKE_C_COMPILER}") + message(FATAL_ERROR "FindCompiler provided non-absolute path \"${CMAKE_C_COMPILER}\"!") + endif() + if(NOT EXISTS "${CMAKE_C_COMPILER}") + message(FATAL_ERROR "FindCompiler provided non-existing path \"${CMAKE_C_COMPILER}\"!") + endif() + + # Now that we have the full compiler path, hide CC. + unset(ENV{CC}) + + # Wrap around the real compiler so we can change the compiler + # path without changing the underlying compiler. + set(ccIn ${RunCMake_SOURCE_DIR}/cc.sh.in) + set(cc1 ${RunCMake_BINARY_DIR}/cc1.sh) + set(cc2 ${RunCMake_BINARY_DIR}/cc2.sh) + set(cc3 CMAKE_C_COMPILER-NOTFOUND) + configure_file(${ccIn} ${cc1} @ONLY) + configure_file(${ccIn} ${cc2} @ONLY) + + block() + set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/ChangeCompiler-build) + run_cmake_with_options(FirstCompiler -DCMAKE_C_COMPILER=${cc1}) + set(RunCMake_TEST_NO_CLEAN 1) + run_cmake_with_options(SecondCompiler -DCMAKE_C_COMPILER=${cc2}) + run_cmake_with_options(EmptyCompiler -DCMAKE_C_COMPILER=) + endblock() + + block() + set(cc1_dot ${RunCMake_BINARY_DIR}/./cc1.sh) + set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CompilerPath-build) + set(RunCMake_TEST_VARIANT_DESCRIPTION "-step1") + run_cmake_with_options(CompilerPath "-DCMAKE_C_COMPILER=${cc1_dot}" -DCACHE_ENTRY=cached) + set(RunCMake_TEST_NO_CLEAN 1) + set(RunCMake_TEST_VARIANT_DESCRIPTION "-step2") + run_cmake_with_options(CompilerPath "-DCMAKE_C_COMPILER=${cc1_dot}") + endblock() endif() -if(NOT IS_ABSOLUTE "${CMAKE_C_COMPILER}") - message(FATAL_ERROR "FindCompiler provided non-absolute path \"${CMAKE_C_COMPILER}\"!") -endif() -if(NOT EXISTS "${CMAKE_C_COMPILER}") - message(FATAL_ERROR "FindCompiler provided non-existing path \"${CMAKE_C_COMPILER}\"!") -endif() - -# Now that we have the full compiler path, hide CC. -unset(ENV{CC}) - -# Wrap around the real compiler so we can change the compiler -# path without changing the underlying compiler. -set(ccIn ${RunCMake_SOURCE_DIR}/cc.sh.in) -set(cc1 ${RunCMake_BINARY_DIR}/cc1.sh) -set(cc2 ${RunCMake_BINARY_DIR}/cc2.sh) -set(cc3 CMAKE_C_COMPILER-NOTFOUND) -configure_file(${ccIn} ${cc1} @ONLY) -configure_file(${ccIn} ${cc2} @ONLY) - -block() - set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/ChangeCompiler-build) - run_cmake_with_options(FirstCompiler -DCMAKE_C_COMPILER=${cc1}) - set(RunCMake_TEST_NO_CLEAN 1) - run_cmake_with_options(SecondCompiler -DCMAKE_C_COMPILER=${cc2}) - run_cmake_with_options(EmptyCompiler -DCMAKE_C_COMPILER=) -endblock() - -block() - set(cc1_dot ${RunCMake_BINARY_DIR}/./cc1.sh) - set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CompilerPath-build) - set(RunCMake_TEST_VARIANT_DESCRIPTION "-step1") - run_cmake_with_options(CompilerPath "-DCMAKE_C_COMPILER=${cc1_dot}" -DCACHE_ENTRY=cached) - set(RunCMake_TEST_NO_CLEAN 1) - set(RunCMake_TEST_VARIANT_DESCRIPTION "-step2") - run_cmake_with_options(CompilerPath "-DCMAKE_C_COMPILER=${cc1_dot}") -endblock() # Set up "toolchain" files file(WRITE "${RunCMake_BINARY_DIR}/foo.cmake" "set(toolchain_var foo)\n") From f5837208f5d0d5e28fbfc9a847d899343c84510b Mon Sep 17 00:00:00 2001 From: Aiden Woodruff Date: Fri, 7 Aug 2026 12:57:39 -0400 Subject: [PATCH 2/2] CMAKE_TOOLCHAIN_FILE: Fix Windows saved input toolchain file syntax error The value of _CMAKE_INPUT_TOOLCHAIN_FILE can contain backslashes when Windows path separators are used. This commit ensures regression tests check such cases and saves the value with a bracket argument to avoid causing a syntax error due to the backslashes. Fixes: #28024 --- Modules/CMakeDetermineSystem.cmake | 2 +- Tests/RunCMake/CompilerChange/RunCMakeTest.cmake | 15 ++++++++++++--- .../ToolchainFromCmdline-step2-stderr.txt | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Modules/CMakeDetermineSystem.cmake b/Modules/CMakeDetermineSystem.cmake index 3720283672..db90f9930d 100644 --- a/Modules/CMakeDetermineSystem.cmake +++ b/Modules/CMakeDetermineSystem.cmake @@ -227,7 +227,7 @@ if(CMAKE_BINARY_DIR) set(INCLUDE_CMAKE_TOOLCHAIN_FILE_IF_REQUIRED) if(CMAKE_TOOLCHAIN_FILE) string(CONCAT INCLUDE_CMAKE_TOOLCHAIN_FILE_IF_REQUIRED - "set(_CMAKE_INPUT_TOOLCHAIN_FILE \"${_CMAKE_INPUT_TOOLCHAIN_FILE}\")\n" + "set(_CMAKE_INPUT_TOOLCHAIN_FILE [==[${_CMAKE_INPUT_TOOLCHAIN_FILE}]==])\n" "set(_CMAKE_SYSTEM_TOOLCHAIN_FILE \"${CMAKE_TOOLCHAIN_FILE}\")\n" "include(\"\${_CMAKE_SYSTEM_TOOLCHAIN_FILE}\")" ) diff --git a/Tests/RunCMake/CompilerChange/RunCMakeTest.cmake b/Tests/RunCMake/CompilerChange/RunCMakeTest.cmake index 3d6546cc4c..56e8af3dc5 100644 --- a/Tests/RunCMake/CompilerChange/RunCMakeTest.cmake +++ b/Tests/RunCMake/CompilerChange/RunCMakeTest.cmake @@ -48,6 +48,15 @@ endif() # Set up "toolchain" files file(WRITE "${RunCMake_BINARY_DIR}/foo.cmake" "set(toolchain_var foo)\n") file(WRITE "${RunCMake_BINARY_DIR}/bar.cmake" "set(toolchain_var bar)\n") +if(UNIX) + # Can't use file(TO_NATIVE_PATH) because it escapes spaces and cmake_path is + # 3.20 + set(foo_TOOLCHAIN_FILE "${RunCMake_BINARY_DIR}/foo.cmake") + set(bar_TOOLCHAIN_FILE "${RunCMake_BINARY_DIR}/bar.cmake") +else() + set(foo_TOOLCHAIN_FILE "${RunCMake_BINARY_DIR}\\foo.cmake") + set(bar_TOOLCHAIN_FILE "${RunCMake_BINARY_DIR}\\bar.cmake") +endif() # New toolchain path comes from file block() @@ -70,11 +79,11 @@ endblock() block() set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/ToolchainFromCmdline-build) run_cmake_with_options(ToolchainFromCmdline-step1 - "-DCMAKE_TOOLCHAIN_FILE=${RunCMake_BINARY_DIR}/foo.cmake" + "-DCMAKE_TOOLCHAIN_FILE=${foo_TOOLCHAIN_FILE}" ) set(RunCMake_TEST_NO_CLEAN 1) run_cmake_with_options(ToolchainFromCmdline-step2 - "-DCMAKE_TOOLCHAIN_FILE=${RunCMake_BINARY_DIR}/bar.cmake" + "-DCMAKE_TOOLCHAIN_FILE=${bar_TOOLCHAIN_FILE}" ) endblock() @@ -85,7 +94,7 @@ block() ${RunCMake_SOURCE_DIR}/ToolchainFromCmdlineOnce-stdout.txt ) run_cmake_with_options(ToolchainFromCmdlineOnce-step1 - "-DCMAKE_TOOLCHAIN_FILE=${RunCMake_BINARY_DIR}/foo.cmake" + "-DCMAKE_TOOLCHAIN_FILE=${foo_TOOLCHAIN_FILE}" ) set(RunCMake_TEST_NO_CLEAN 1) run_cmake(ToolchainFromCmdlineOnce-step2) diff --git a/Tests/RunCMake/CompilerChange/ToolchainFromCmdline-step2-stderr.txt b/Tests/RunCMake/CompilerChange/ToolchainFromCmdline-step2-stderr.txt index e65f0d973a..617a64998c 100644 --- a/Tests/RunCMake/CompilerChange/ToolchainFromCmdline-step2-stderr.txt +++ b/Tests/RunCMake/CompilerChange/ToolchainFromCmdline-step2-stderr.txt @@ -2,4 +2,4 @@ You have changed variables that require your cache to be deleted. Configure will be re-run and you may have to reset some variables. The following variables have changed: CMAKE_TOOLCHAIN_FILE= [^ -]*/Tests/RunCMake/CompilerChange/bar\.cmake +]*[\/]Tests[\/]RunCMake[\/]CompilerChange[\/]bar\.cmake