diff --git a/Modules/CMakeDetermineSystem.cmake b/Modules/CMakeDetermineSystem.cmake index 80db89c35f..dc26258eac 100644 --- a/Modules/CMakeDetermineSystem.cmake +++ b/Modules/CMakeDetermineSystem.cmake @@ -139,17 +139,24 @@ endif() # variables around so they can be used in CMakeLists.txt. # In all other cases, the host and target platform are the same. if(CMAKE_TOOLCHAIN_FILE) - # at first try to load it as path relative to the directory from which cmake has been run - include("${CMAKE_BINARY_DIR}/${CMAKE_TOOLCHAIN_FILE}" OPTIONAL RESULT_VARIABLE _INCLUDED_TOOLCHAIN_FILE) - if(NOT _INCLUDED_TOOLCHAIN_FILE) - # if the file isn't found there, check the default locations - include("${CMAKE_TOOLCHAIN_FILE}" OPTIONAL RESULT_VARIABLE _INCLUDED_TOOLCHAIN_FILE) - endif() - - if(_INCLUDED_TOOLCHAIN_FILE) - set(CMAKE_TOOLCHAIN_FILE "${_INCLUDED_TOOLCHAIN_FILE}" CACHE FILEPATH "The CMake toolchain file" FORCE) + if(IS_ABSOLUTE "${CMAKE_TOOLCHAIN_FILE}" AND EXISTS "${CMAKE_TOOLCHAIN_FILE}") + # Normalize the absolute path. + set(CMAKE_TOOLCHAIN_FILE "${CMAKE_TOOLCHAIN_FILE}" CACHE FILEPATH "The CMake toolchain file" FORCE) + set(CMAKE_TOOLCHAIN_FILE "$CACHE{CMAKE_TOOLCHAIN_FILE}") + include("${CMAKE_TOOLCHAIN_FILE}" OPTIONAL RESULT_VARIABLE _INCLUDED_TOOLCHAIN_FILE) else() - message(FATAL_ERROR "Could not find toolchain file: ${CMAKE_TOOLCHAIN_FILE}") + # at first try to load it as path relative to the directory from which cmake has been run + include("${CMAKE_BINARY_DIR}/${CMAKE_TOOLCHAIN_FILE}" OPTIONAL RESULT_VARIABLE _INCLUDED_TOOLCHAIN_FILE) + if(NOT _INCLUDED_TOOLCHAIN_FILE) + # if the file isn't found there, check the default locations + include("${CMAKE_TOOLCHAIN_FILE}" OPTIONAL RESULT_VARIABLE _INCLUDED_TOOLCHAIN_FILE) + endif() + if(_INCLUDED_TOOLCHAIN_FILE) + set(CMAKE_TOOLCHAIN_FILE "${_INCLUDED_TOOLCHAIN_FILE}" CACHE FILEPATH "The CMake toolchain file" FORCE) + endif() + endif() + if(NOT _INCLUDED_TOOLCHAIN_FILE) + message(FATAL_ERROR "Could not find toolchain file:\n \"${CMAKE_TOOLCHAIN_FILE}\"") endif() endif() diff --git a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake index 4f993585aa..9fda9eb5b1 100644 --- a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake +++ b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake @@ -264,6 +264,10 @@ function(run_Toolchain) run_cmake_with_options(toolchain-no-arg -S ${source_dir} --toolchain=) run_cmake_with_options(toolchain-valid-abs-path -S ${source_dir} --toolchain "${source_dir}/toolchain.cmake") run_cmake_with_options(toolchain-valid-rel-src-path -S ${source_dir} --toolchain=toolchain.cmake) + run_cmake_with_options(toolchain-D-abs-path -S ${source_dir} -DCMAKE_TOOLCHAIN_FILE=${source_dir}/toolchain.cmake) + if(CMAKE_HOST_UNIX AND NOT CMAKE_SYSTEM_NAME STREQUAL "CYGWIN" AND NOT CMAKE_SYSTEM_NAME STREQUAL "MSYS") + run_cmake_with_options(toolchain-D-slash-abs-path -S ${source_dir} -DCMAKE_TOOLCHAIN_FILE=/${source_dir}/toolchain.cmake) + endif() set(RunCMake_TEST_NO_CLEAN 1) set(binary_dir ${RunCMake_BINARY_DIR}/Toolchain-build) @@ -274,7 +278,6 @@ function(run_Toolchain) # precedence over source dir file(WRITE ${binary_dir}/toolchain.cmake [=[ set(CMAKE_SYSTEM_NAME Linux) -set(toolchain_file binary_dir) ]=]) run_cmake_with_options(toolchain-valid-rel-build-path -S ${source_dir} -B ${binary_dir} --toolchain toolchain.cmake) endfunction() diff --git a/Tests/RunCMake/CommandLine/Toolchain/CMakeLists.txt b/Tests/RunCMake/CommandLine/Toolchain/CMakeLists.txt index 80d42b828b..d49b6849a3 100644 --- a/Tests/RunCMake/CommandLine/Toolchain/CMakeLists.txt +++ b/Tests/RunCMake/CommandLine/Toolchain/CMakeLists.txt @@ -1,3 +1,3 @@ cmake_minimum_required(VERSION 3.20) project(Toolchain LANGUAGES NONE) -message(FATAL_ERROR "${toolchain_file}") +message(STATUS "CMAKE_TOOLCHAIN_FILE='${CMAKE_TOOLCHAIN_FILE}'") diff --git a/Tests/RunCMake/CommandLine/Toolchain/toolchain.cmake b/Tests/RunCMake/CommandLine/Toolchain/toolchain.cmake index 719556c0af..99c23014a7 100644 --- a/Tests/RunCMake/CommandLine/Toolchain/toolchain.cmake +++ b/Tests/RunCMake/CommandLine/Toolchain/toolchain.cmake @@ -1,2 +1 @@ set(CMAKE_SYSTEM_NAME Linux) -set(toolchain_file source_dir) diff --git a/Tests/RunCMake/CommandLine/toolchain-D-abs-path-stdout.txt b/Tests/RunCMake/CommandLine/toolchain-D-abs-path-stdout.txt new file mode 100644 index 0000000000..753f00b5ad --- /dev/null +++ b/Tests/RunCMake/CommandLine/toolchain-D-abs-path-stdout.txt @@ -0,0 +1 @@ +CMAKE_TOOLCHAIN_FILE='[^']*/Tests/RunCMake/CommandLine/Toolchain/toolchain\.cmake' diff --git a/Tests/RunCMake/CommandLine/toolchain-D-slash-abs-path-stdout.txt b/Tests/RunCMake/CommandLine/toolchain-D-slash-abs-path-stdout.txt new file mode 100644 index 0000000000..9e9781f38a --- /dev/null +++ b/Tests/RunCMake/CommandLine/toolchain-D-slash-abs-path-stdout.txt @@ -0,0 +1 @@ +CMAKE_TOOLCHAIN_FILE='/[^/][^']*/Tests/RunCMake/CommandLine/Toolchain/toolchain\.cmake' diff --git a/Tests/RunCMake/CommandLine/toolchain-valid-abs-path-result.txt b/Tests/RunCMake/CommandLine/toolchain-valid-abs-path-result.txt deleted file mode 100644 index d00491fd7e..0000000000 --- a/Tests/RunCMake/CommandLine/toolchain-valid-abs-path-result.txt +++ /dev/null @@ -1 +0,0 @@ -1 diff --git a/Tests/RunCMake/CommandLine/toolchain-valid-abs-path-stderr.txt b/Tests/RunCMake/CommandLine/toolchain-valid-abs-path-stderr.txt deleted file mode 100644 index 21d5db6464..0000000000 --- a/Tests/RunCMake/CommandLine/toolchain-valid-abs-path-stderr.txt +++ /dev/null @@ -1 +0,0 @@ -^CMake Error.*source_dir diff --git a/Tests/RunCMake/CommandLine/toolchain-valid-abs-path-stdout.txt b/Tests/RunCMake/CommandLine/toolchain-valid-abs-path-stdout.txt new file mode 100644 index 0000000000..753f00b5ad --- /dev/null +++ b/Tests/RunCMake/CommandLine/toolchain-valid-abs-path-stdout.txt @@ -0,0 +1 @@ +CMAKE_TOOLCHAIN_FILE='[^']*/Tests/RunCMake/CommandLine/Toolchain/toolchain\.cmake' diff --git a/Tests/RunCMake/CommandLine/toolchain-valid-rel-build-path-result.txt b/Tests/RunCMake/CommandLine/toolchain-valid-rel-build-path-result.txt deleted file mode 100644 index d00491fd7e..0000000000 --- a/Tests/RunCMake/CommandLine/toolchain-valid-rel-build-path-result.txt +++ /dev/null @@ -1 +0,0 @@ -1 diff --git a/Tests/RunCMake/CommandLine/toolchain-valid-rel-build-path-stderr.txt b/Tests/RunCMake/CommandLine/toolchain-valid-rel-build-path-stderr.txt deleted file mode 100644 index 047554673f..0000000000 --- a/Tests/RunCMake/CommandLine/toolchain-valid-rel-build-path-stderr.txt +++ /dev/null @@ -1,2 +0,0 @@ -^CMake Error at CMakeLists.txt:[0-9] \(message\): - binary_dir$ diff --git a/Tests/RunCMake/CommandLine/toolchain-valid-rel-build-path-stdout.txt b/Tests/RunCMake/CommandLine/toolchain-valid-rel-build-path-stdout.txt new file mode 100644 index 0000000000..659cf5fdea --- /dev/null +++ b/Tests/RunCMake/CommandLine/toolchain-valid-rel-build-path-stdout.txt @@ -0,0 +1 @@ +CMAKE_TOOLCHAIN_FILE='[^']*/Tests/RunCMake/CommandLine/Toolchain-build/toolchain\.cmake' diff --git a/Tests/RunCMake/CommandLine/toolchain-valid-rel-src-path-result.txt b/Tests/RunCMake/CommandLine/toolchain-valid-rel-src-path-result.txt deleted file mode 100644 index d00491fd7e..0000000000 --- a/Tests/RunCMake/CommandLine/toolchain-valid-rel-src-path-result.txt +++ /dev/null @@ -1 +0,0 @@ -1 diff --git a/Tests/RunCMake/CommandLine/toolchain-valid-rel-src-path-stderr.txt b/Tests/RunCMake/CommandLine/toolchain-valid-rel-src-path-stderr.txt deleted file mode 100644 index 21d5db6464..0000000000 --- a/Tests/RunCMake/CommandLine/toolchain-valid-rel-src-path-stderr.txt +++ /dev/null @@ -1 +0,0 @@ -^CMake Error.*source_dir diff --git a/Tests/RunCMake/CommandLine/toolchain-valid-rel-src-path-stdout.txt b/Tests/RunCMake/CommandLine/toolchain-valid-rel-src-path-stdout.txt new file mode 100644 index 0000000000..753f00b5ad --- /dev/null +++ b/Tests/RunCMake/CommandLine/toolchain-valid-rel-src-path-stdout.txt @@ -0,0 +1 @@ +CMAKE_TOOLCHAIN_FILE='[^']*/Tests/RunCMake/CommandLine/Toolchain/toolchain\.cmake'