diff options
author | Cristian Le <cristian.le@mpsd.mpg.de> | 2023-10-16 19:16:51 (GMT) |
---|---|---|
committer | Cristian Le <cristian.le@mpsd.mpg.de> | 2023-10-18 16:01:05 (GMT) |
commit | b37b912e8ffd2beee54d7ff5a8788cb8bb27a26f (patch) | |
tree | 7b4124dcf94632d8b2e45f80a3dcbd87826b2e6f | |
parent | a3fbc255ff200ba091485b05c41e1c111b2f0564 (diff) | |
download | CMake-b37b912e8ffd2beee54d7ff5a8788cb8bb27a26f.zip CMake-b37b912e8ffd2beee54d7ff5a8788cb8bb27a26f.tar.gz CMake-b37b912e8ffd2beee54d7ff5a8788cb8bb27a26f.tar.bz2 |
CMAKE_PROJECT_INCLUDE: Allow to run module files
Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
-rw-r--r-- | Help/release/dev/project-include-multiple.rst | 4 | ||||
-rw-r--r-- | Help/variable/CMAKE_PROJECT_INCLUDE.rst | 3 | ||||
-rw-r--r-- | Help/variable/CMAKE_PROJECT_INCLUDE_BEFORE.rst | 3 | ||||
-rw-r--r-- | Help/variable/CMAKE_PROJECT_PROJECT-NAME_INCLUDE.rst | 3 | ||||
-rw-r--r-- | Help/variable/CMAKE_PROJECT_PROJECT-NAME_INCLUDE_BEFORE.rst | 3 | ||||
-rw-r--r-- | Help/variable/CMAKE_PROJECT_TOP_LEVEL_INCLUDES.rst | 4 | ||||
-rw-r--r-- | Source/cmGlobalGenerator.cxx | 17 | ||||
-rw-r--r-- | Source/cmProjectCommand.cxx | 16 | ||||
-rw-r--r-- | Tests/RunCMake/project/CodeInjection/initial_cache_3.cmake | 28 | ||||
-rw-r--r-- | Tests/RunCMake/project/CodeInjection3-stdout.txt | 16 | ||||
-rw-r--r-- | Tests/RunCMake/project/CodeInjection3.cmake | 1 | ||||
-rw-r--r-- | Tests/RunCMake/project/RunCMakeTest.cmake | 4 |
12 files changed, 95 insertions, 7 deletions
diff --git a/Help/release/dev/project-include-multiple.rst b/Help/release/dev/project-include-multiple.rst index d4c3bb9..3f7b360 100644 --- a/Help/release/dev/project-include-multiple.rst +++ b/Help/release/dev/project-include-multiple.rst @@ -6,4 +6,6 @@ project-include-multiple :variable:`CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE`, and :variable:`CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE_BEFORE` variables learned to support a :ref:`semicolon-separated list <CMake Language Lists>` of - CMake language files to be included sequentially. + CMake language files to be included sequentially. These variables can also + reference module names to be found in :variable:`CMAKE_MODULE_PATH` or + builtin to CMake. diff --git a/Help/variable/CMAKE_PROJECT_INCLUDE.rst b/Help/variable/CMAKE_PROJECT_INCLUDE.rst index ab59ed7..217604f 100644 --- a/Help/variable/CMAKE_PROJECT_INCLUDE.rst +++ b/Help/variable/CMAKE_PROJECT_INCLUDE.rst @@ -11,7 +11,8 @@ for a more detailed discussion of files potentially included during a .. versionadded:: 3.29 This variable can be a :ref:`semicolon-separated list <CMake Language Lists>` - of CMake language files to be included sequentially. + of CMake language files to be included sequentially. It can also now refer to + module names to be found in :variable:`CMAKE_MODULE_PATH` or builtin to CMake. See also the :variable:`CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE`, :variable:`CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE_BEFORE`, diff --git a/Help/variable/CMAKE_PROJECT_INCLUDE_BEFORE.rst b/Help/variable/CMAKE_PROJECT_INCLUDE_BEFORE.rst index b5fbe6f..1dc0241 100644 --- a/Help/variable/CMAKE_PROJECT_INCLUDE_BEFORE.rst +++ b/Help/variable/CMAKE_PROJECT_INCLUDE_BEFORE.rst @@ -11,7 +11,8 @@ for a more detailed discussion of files potentially included during a .. versionadded:: 3.29 This variable can be a :ref:`semicolon-separated list <CMake Language Lists>` - of CMake language files to be included sequentially. + of CMake language files to be included sequentially. It can also now refer to + module names to be found in :variable:`CMAKE_MODULE_PATH` or builtin to CMake. See also the :variable:`CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE`, :variable:`CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE_BEFORE`, diff --git a/Help/variable/CMAKE_PROJECT_PROJECT-NAME_INCLUDE.rst b/Help/variable/CMAKE_PROJECT_PROJECT-NAME_INCLUDE.rst index 5ea5234..6d37c1a 100644 --- a/Help/variable/CMAKE_PROJECT_PROJECT-NAME_INCLUDE.rst +++ b/Help/variable/CMAKE_PROJECT_PROJECT-NAME_INCLUDE.rst @@ -9,7 +9,8 @@ discussion of files potentially included during a :command:`project` call. .. versionadded:: 3.29 This variable can be a :ref:`semicolon-separated list <CMake Language Lists>` - of CMake language files to be included sequentially. + of CMake language files to be included sequentially. It can also now refer to + module names to be found in :variable:`CMAKE_MODULE_PATH` or builtin to CMake. See also the :variable:`CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE_BEFORE`, :variable:`CMAKE_PROJECT_INCLUDE`, :variable:`CMAKE_PROJECT_INCLUDE_BEFORE`, diff --git a/Help/variable/CMAKE_PROJECT_PROJECT-NAME_INCLUDE_BEFORE.rst b/Help/variable/CMAKE_PROJECT_PROJECT-NAME_INCLUDE_BEFORE.rst index 0694823..96ddbf6 100644 --- a/Help/variable/CMAKE_PROJECT_PROJECT-NAME_INCLUDE_BEFORE.rst +++ b/Help/variable/CMAKE_PROJECT_PROJECT-NAME_INCLUDE_BEFORE.rst @@ -11,7 +11,8 @@ discussion of files potentially included during a :command:`project` call. .. versionadded:: 3.29 This variable can be a :ref:`semicolon-separated list <CMake Language Lists>` - of CMake language files to be included sequentially. + of CMake language files to be included sequentially. It can also now refer to + module names to be found in :variable:`CMAKE_MODULE_PATH` or builtin to CMake. See also the :variable:`CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE`, :variable:`CMAKE_PROJECT_INCLUDE`, :variable:`CMAKE_PROJECT_INCLUDE_BEFORE`, diff --git a/Help/variable/CMAKE_PROJECT_TOP_LEVEL_INCLUDES.rst b/Help/variable/CMAKE_PROJECT_TOP_LEVEL_INCLUDES.rst index 2010b08..54f530e 100644 --- a/Help/variable/CMAKE_PROJECT_TOP_LEVEL_INCLUDES.rst +++ b/Help/variable/CMAKE_PROJECT_TOP_LEVEL_INCLUDES.rst @@ -12,6 +12,10 @@ including things like :variable:`CMAKE_<LANG>_COMPILER`, might not be set. See :ref:`Code Injection` for a more detailed discussion of files potentially included during a :command:`project` call. +.. versionadded:: 3.29 + This variable can also now refer to module names to be found in + :variable:`CMAKE_MODULE_PATH` or builtin to CMake. + This variable is intended for specifying files that perform one-time setup for the build. It provides an injection point for things like configuring package managers, adding logic the user shares between projects (e.g. defining diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx index 933e754..e798d3e 100644 --- a/Source/cmGlobalGenerator.cxx +++ b/Source/cmGlobalGenerator.cxx @@ -694,7 +694,22 @@ void cmGlobalGenerator::EnableLanguage( std::string includes = mf->GetSafeDefinition("CMAKE_PROJECT_TOP_LEVEL_INCLUDES"); cmList includesList{ includes }; - for (std::string const& setupFile : includesList) { + for (std::string setupFile : includesList) { + // Any relative path without a .cmake extension is checked for valid + // cmake modules. This logic should be consistent with CMake's include() + // command. Otherwise default to checking relative path w.r.t. source + // directory + if (!cmSystemTools::FileIsFullPath(setupFile) && + !cmHasLiteralSuffix(setupFile, ".cmake")) { + std::string mfile = mf->GetModulesFile(cmStrCat(setupFile, ".cmake")); + if (mfile.empty()) { + cmSystemTools::Error(cmStrCat( + "CMAKE_PROJECT_TOP_LEVEL_INCLUDES module:\n ", setupFile)); + mf->GetState()->SetInTopLevelIncludes(false); + return; + } + setupFile = mfile; + } std::string absSetupFile = cmSystemTools::CollapseFullPath( setupFile, mf->GetCurrentSourceDirectory()); if (!cmSystemTools::FileExists(absSetupFile)) { diff --git a/Source/cmProjectCommand.cxx b/Source/cmProjectCommand.cxx index bb5f522..53166c1 100644 --- a/Source/cmProjectCommand.cxx +++ b/Source/cmProjectCommand.cxx @@ -375,7 +375,21 @@ static bool IncludeByVariable(cmExecutionStatus& status, cmList includeFiles{ *include }; bool failed = false; - for (auto const& filePath : includeFiles) { + for (auto filePath : includeFiles) { + // Any relative path without a .cmake extension is checked for valid cmake + // modules. This logic should be consistent with CMake's include() command. + // Otherwise default to checking relative path w.r.t. source directory + if (!cmSystemTools::FileIsFullPath(filePath) && + !cmHasLiteralSuffix(filePath, ".cmake")) { + std::string mfile = mf.GetModulesFile(cmStrCat(filePath, ".cmake")); + if (mfile.empty()) { + status.SetError( + cmStrCat("could not find requested module:\n ", filePath)); + failed = true; + continue; + } + filePath = mfile; + } std::string includeFile = cmSystemTools::CollapseFullPath( filePath, mf.GetCurrentSourceDirectory()); if (!cmSystemTools::FileExists(includeFile)) { diff --git a/Tests/RunCMake/project/CodeInjection/initial_cache_3.cmake b/Tests/RunCMake/project/CodeInjection/initial_cache_3.cmake new file mode 100644 index 0000000..dd299bc --- /dev/null +++ b/Tests/RunCMake/project/CodeInjection/initial_cache_3.cmake @@ -0,0 +1,28 @@ +set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}" CACHE STRING "") +set(CMAKE_TOOLCHAIN_FILE + "${CMAKE_CURRENT_LIST_DIR}/passthrough_toolchain_file.cmake" CACHE FILEPATH "") +set(CMAKE_PROJECT_INCLUDE + "${CMAKE_CURRENT_LIST_DIR}/cmake_project_includes_1.cmake" + "cmake_project_includes_2" + CACHE STRING "" +) +set(CMAKE_PROJECT_INCLUDE_BEFORE + "${CMAKE_CURRENT_LIST_DIR}/cmake_project_includes_before_1.cmake" + "cmake_project_includes_before_2" + CACHE STRING "" +) +set(CMAKE_PROJECT_SubProj_INCLUDE + "${CMAKE_CURRENT_LIST_DIR}/cmake_project_subproj_includes_1.cmake" + "cmake_project_subproj_includes_2" + CACHE STRING "" +) +set(CMAKE_PROJECT_SubProj_INCLUDE_BEFORE + "${CMAKE_CURRENT_LIST_DIR}/cmake_project_subproj_includes_before_1.cmake" + "cmake_project_subproj_includes_before_2" + CACHE STRING "" +) +set(CMAKE_PROJECT_TOP_LEVEL_INCLUDES + "${CMAKE_CURRENT_LIST_DIR}/cmake_project_top_level_includes_1.cmake" + "cmake_project_top_level_includes_2" + CACHE STRING "" +) diff --git a/Tests/RunCMake/project/CodeInjection3-stdout.txt b/Tests/RunCMake/project/CodeInjection3-stdout.txt new file mode 100644 index 0000000..5c18cdf --- /dev/null +++ b/Tests/RunCMake/project/CodeInjection3-stdout.txt @@ -0,0 +1,16 @@ +(-- )?Included CMAKE_PROJECT_INCLUDE_BEFORE first file +(-- )?Included CMAKE_PROJECT_INCLUDE_BEFORE second file +(-- )?Included CMAKE_TOOLCHAIN_FILE +.*Included CMAKE_PROJECT_TOP_LEVEL_INCLUDES first file +(-- )?Included CMAKE_PROJECT_TOP_LEVEL_INCLUDES second file +(-- )?Included CMAKE_PROJECT_INCLUDE first file +(-- )?Included CMAKE_PROJECT_INCLUDE second file +(-- )?Calling sub-project +(-- )?Included CMAKE_PROJECT_INCLUDE_BEFORE first file +(-- )?Included CMAKE_PROJECT_INCLUDE_BEFORE second file +(-- )?Included CMAKE_PROJECT_SubProj_INCLUDE_BEFORE first file +(-- )?Included CMAKE_PROJECT_SubProj_INCLUDE_BEFORE second file +(-- )?Included CMAKE_PROJECT_INCLUDE first file +(-- )?Included CMAKE_PROJECT_INCLUDE second file +(-- )?Included CMAKE_PROJECT_SubProj_INCLUDE first file +(-- )?Included CMAKE_PROJECT_SubProj_INCLUDE second file diff --git a/Tests/RunCMake/project/CodeInjection3.cmake b/Tests/RunCMake/project/CodeInjection3.cmake new file mode 100644 index 0000000..dcf56a1 --- /dev/null +++ b/Tests/RunCMake/project/CodeInjection3.cmake @@ -0,0 +1 @@ +add_subdirectory(CodeInjection) diff --git a/Tests/RunCMake/project/RunCMakeTest.cmake b/Tests/RunCMake/project/RunCMakeTest.cmake index 4ec1d48..16f10be 100644 --- a/Tests/RunCMake/project/RunCMakeTest.cmake +++ b/Tests/RunCMake/project/RunCMakeTest.cmake @@ -12,6 +12,10 @@ run_cmake_with_options(CodeInjection1 run_cmake_with_options(CodeInjection2 -C "${CMAKE_CURRENT_LIST_DIR}/CodeInjection/initial_cache_2.cmake" ) +# This checks that module names are also allowed. +run_cmake_with_options(CodeInjection3 + -C "${CMAKE_CURRENT_LIST_DIR}/CodeInjection/initial_cache_3.cmake" +) if(CMake_TEST_RESOURCES) run_cmake(ExplicitRC) |