From 31ef93f19ff9c1529024ab5e9b82143b99ef1c70 Mon Sep 17 00:00:00 2001 From: Craig Scott Date: Fri, 24 Feb 2023 17:07:01 +1100 Subject: FetchContent: Only use @-@ replacements for sub-build CMakeLists.txt There was a mix of ${} and @-@ substitutions in the CMakeLists.txt.in file. It is sometimes useful to temporarily put ${} evaluations there while working on issues, but they get replaced. Using @ONLY leaves them untouched and uses a consistent substitution method for the whole file. --- Modules/FetchContent.cmake | 4 +++- Modules/FetchContent/CMakeLists.cmake.in | 12 ++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Modules/FetchContent.cmake b/Modules/FetchContent.cmake index 8afb9bc..153563f 100644 --- a/Modules/FetchContent.cmake +++ b/Modules/FetchContent.cmake @@ -1596,7 +1596,9 @@ set_property(GLOBAL PROPERTY _CMAKE_FindGit_GIT_EXECUTABLE_VERSION # has this set to something not findable on the PATH. We also ensured above # that the Debug config will be defined for multi-config generators. configure_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/FetchContent/CMakeLists.cmake.in" - "${ARG_SUBBUILD_DIR}/CMakeLists.txt") + "${ARG_SUBBUILD_DIR}/CMakeLists.txt" + @ONLY + ) execute_process( COMMAND ${CMAKE_COMMAND} ${subCMakeOpts} . RESULT_VARIABLE result diff --git a/Modules/FetchContent/CMakeLists.cmake.in b/Modules/FetchContent/CMakeLists.cmake.in index d94b0f4..aa2ad99 100644 --- a/Modules/FetchContent/CMakeLists.cmake.in +++ b/Modules/FetchContent/CMakeLists.cmake.in @@ -1,21 +1,21 @@ # Distributed under the OSI-approved BSD 3-Clause License. See accompanying # file Copyright.txt or https://cmake.org/licensing for details. -cmake_minimum_required(VERSION ${CMAKE_VERSION}) +cmake_minimum_required(VERSION @CMAKE_VERSION@) # We name the project and the target for the ExternalProject_Add() call # to something that will highlight to the user what we are working on if # something goes wrong and an error message is produced. -project(${contentName}-populate NONE) +project(@contentName@-populate NONE) @__FETCHCONTENT_CACHED_INFO@ include(ExternalProject) -ExternalProject_Add(${contentName}-populate - ${ARG_EXTRA} - SOURCE_DIR "${ARG_SOURCE_DIR}" - BINARY_DIR "${ARG_BINARY_DIR}" +ExternalProject_Add(@contentName@-populate + @ARG_EXTRA@ + SOURCE_DIR "@ARG_SOURCE_DIR@" + BINARY_DIR "@ARG_BINARY_DIR@" CONFIGURE_COMMAND "" BUILD_COMMAND "" INSTALL_COMMAND "" -- cgit v0.12 From c15674a9cb0f8c6d432a52ac6fb724cc85f68034 Mon Sep 17 00:00:00 2001 From: Craig Scott Date: Fri, 24 Feb 2023 17:10:38 +1100 Subject: FetchContent: Reject CMAKE_TOOLCHAIN_FILE env var in sub-build The sub-build should never try to use a toolchain file. The toolchain file may itself be getting downloaded by FetchContent. If the CMAKE_TOOLCHAIN_FILE environment variable is set, CMake uses it to initialize the CMake variable of the same name. We need to explicitly clear them before the first project call of the sub-build. Fixes: #24535 --- Modules/FetchContent/CMakeLists.cmake.in | 6 ++++++ Tests/RunCMake/FetchContent/IgnoreToolchainFile.cmake | 2 ++ Tests/RunCMake/FetchContent/RunCMakeTest.cmake | 1 + 3 files changed, 9 insertions(+) create mode 100644 Tests/RunCMake/FetchContent/IgnoreToolchainFile.cmake diff --git a/Modules/FetchContent/CMakeLists.cmake.in b/Modules/FetchContent/CMakeLists.cmake.in index aa2ad99..8adb533 100644 --- a/Modules/FetchContent/CMakeLists.cmake.in +++ b/Modules/FetchContent/CMakeLists.cmake.in @@ -3,6 +3,12 @@ cmake_minimum_required(VERSION @CMAKE_VERSION@) +# Reject any attempt to use a toolchain file. We must not use one because +# we could be downloading it here. If the CMAKE_TOOLCHAIN_FILE environment +# variable is set, the cache variable will have been initialized from it. +unset(CMAKE_TOOLCHAIN_FILE CACHE) +unset(ENV{CMAKE_TOOLCHAIN_FILE}) + # We name the project and the target for the ExternalProject_Add() call # to something that will highlight to the user what we are working on if # something goes wrong and an error message is produced. diff --git a/Tests/RunCMake/FetchContent/IgnoreToolchainFile.cmake b/Tests/RunCMake/FetchContent/IgnoreToolchainFile.cmake new file mode 100644 index 0000000..f8ee749 --- /dev/null +++ b/Tests/RunCMake/FetchContent/IgnoreToolchainFile.cmake @@ -0,0 +1,2 @@ +set(ENV{CMAKE_TOOLCHAIN_FILE} path/to/somewhere/iDoNotExist.cmake) +include(DownloadFile.cmake) diff --git a/Tests/RunCMake/FetchContent/RunCMakeTest.cmake b/Tests/RunCMake/FetchContent/RunCMakeTest.cmake index bb27491..3781089 100644 --- a/Tests/RunCMake/FetchContent/RunCMakeTest.cmake +++ b/Tests/RunCMake/FetchContent/RunCMakeTest.cmake @@ -7,6 +7,7 @@ run_cmake(DirectIgnoresDetails) run_cmake(FirstDetailsWin) run_cmake(DownloadTwice) run_cmake(DownloadFile) +run_cmake(IgnoreToolchainFile) run_cmake(SameGenerator) run_cmake(System) run_cmake(VarDefinitions) -- cgit v0.12