From 002979e1458a6a8c23f57f06c2edd3e7c3735dd8 Mon Sep 17 00:00:00 2001 From: Brad King Date: Sat, 12 Apr 2025 08:26:02 -0400 Subject: Tests/RunCMake/include: Match incidental line numbers more robustly --- Tests/RunCMake/include/EmptyString-stderr.txt | 2 +- Tests/RunCMake/include/EmptyStringOptional-stderr.txt | 2 +- Tests/RunCMake/include/ExportExportInclude-stderr.txt | 2 +- Tests/RunCMake/include/IncludeIsDirectory-stderr.txt | 2 +- Tests/RunCMake/include/IncludeMalformed-stderr.txt | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Tests/RunCMake/include/EmptyString-stderr.txt b/Tests/RunCMake/include/EmptyString-stderr.txt index dab16ea..32abf5d 100644 --- a/Tests/RunCMake/include/EmptyString-stderr.txt +++ b/Tests/RunCMake/include/EmptyString-stderr.txt @@ -1,5 +1,5 @@ ^CMake Warning \(dev\) at EmptyString\.cmake:1 \(include\): include\(\) given empty file name \(ignored\). Call Stack \(most recent call first\): - CMakeLists\.txt:3 \(include\) + CMakeLists\.txt:[0-9]+ \(include\) This warning is for project developers\. Use -Wno-dev to suppress it\.$ diff --git a/Tests/RunCMake/include/EmptyStringOptional-stderr.txt b/Tests/RunCMake/include/EmptyStringOptional-stderr.txt index 1b763f2..3567532 100644 --- a/Tests/RunCMake/include/EmptyStringOptional-stderr.txt +++ b/Tests/RunCMake/include/EmptyStringOptional-stderr.txt @@ -1,5 +1,5 @@ ^CMake Warning \(dev\) at EmptyStringOptional\.cmake:1 \(include\): include\(\) given empty file name \(ignored\). Call Stack \(most recent call first\): - CMakeLists\.txt:3 \(include\) + CMakeLists\.txt:[0-9]+ \(include\) This warning is for project developers\. Use -Wno-dev to suppress it\.$ diff --git a/Tests/RunCMake/include/ExportExportInclude-stderr.txt b/Tests/RunCMake/include/ExportExportInclude-stderr.txt index 22bf7a1..730a3e9 100644 --- a/Tests/RunCMake/include/ExportExportInclude-stderr.txt +++ b/Tests/RunCMake/include/ExportExportInclude-stderr.txt @@ -4,4 +4,4 @@ [^ ]*/Tests/RunCMake/include/ExportExportInclude-build/theTargets\.cmake Call Stack \(most recent call first\): - CMakeLists\.txt:3 \(include\)$ + CMakeLists\.txt:[0-9]+ \(include\)$ diff --git a/Tests/RunCMake/include/IncludeIsDirectory-stderr.txt b/Tests/RunCMake/include/IncludeIsDirectory-stderr.txt index 6ce934d..cb2908a 100644 --- a/Tests/RunCMake/include/IncludeIsDirectory-stderr.txt +++ b/Tests/RunCMake/include/IncludeIsDirectory-stderr.txt @@ -4,4 +4,4 @@ [^ ]*/Tests/RunCMake/include/IncludeIsDirectory-build Call Stack \(most recent call first\): - CMakeLists\.txt:3 \(include\)$ + CMakeLists\.txt:[0-9]+ \(include\)$ diff --git a/Tests/RunCMake/include/IncludeMalformed-stderr.txt b/Tests/RunCMake/include/IncludeMalformed-stderr.txt index e34e9bb..e9aa13a 100644 --- a/Tests/RunCMake/include/IncludeMalformed-stderr.txt +++ b/Tests/RunCMake/include/IncludeMalformed-stderr.txt @@ -2,11 +2,11 @@ Parse error\. Function missing ending "\)"\. End of file reached\. Call Stack \(most recent call first\): IncludeMalformed\.cmake:1 \(include\) - CMakeLists\.txt:3 \(include\) + CMakeLists\.txt:[0-9]+ \(include\) + CMake Error at IncludeMalformed\.cmake:1 \(include\): include could not load requested file: malformedInclude\.cmake Call Stack \(most recent call first\): - CMakeLists\.txt:3 \(include\)$ + CMakeLists\.txt:[0-9]+ \(include\)$ -- cgit v0.12 From 0b85f8f137716fd415e2ede7d1ac0167d4d5f3e2 Mon Sep 17 00:00:00 2001 From: Benjamin Buch Date: Sat, 22 Mar 2025 19:46:51 +0100 Subject: Tests: Add cases for CMAKE_PARENT_LIST_FILE with include() and find_package() Issue: #25026 Co-authored-by: Brad King --- Tests/RunCMake/find_package/ParentVariable-stdout.txt | 5 +++++ Tests/RunCMake/find_package/ParentVariable.cmake | 4 ++++ Tests/RunCMake/find_package/ParentVariable/PrimaryConfig.cmake | 3 +++ Tests/RunCMake/find_package/ParentVariable/SecondaryConfig.cmake | 1 + Tests/RunCMake/find_package/RunCMakeTest.cmake | 1 + Tests/RunCMake/include/CMakeLists.txt | 6 ++++++ Tests/RunCMake/include/ParentVariableRoot-stdout.txt | 5 +++++ Tests/RunCMake/include/ParentVariableRoot/include1.cmake | 3 +++ Tests/RunCMake/include/ParentVariableRoot/include2.cmake | 1 + Tests/RunCMake/include/ParentVariableScript-stdout.txt | 5 +++++ Tests/RunCMake/include/ParentVariableScript.cmake | 6 ++++++ Tests/RunCMake/include/ParentVariableScript/include1.cmake | 3 +++ Tests/RunCMake/include/ParentVariableScript/include2.cmake | 1 + Tests/RunCMake/include/ParentVariableSubDir-stdout.txt | 7 +++++++ Tests/RunCMake/include/ParentVariableSubDir.cmake | 1 + Tests/RunCMake/include/ParentVariableSubDir/CMakeLists.txt | 3 +++ Tests/RunCMake/include/ParentVariableSubDir/Inc/CMakeLists.txt | 1 + Tests/RunCMake/include/ParentVariableSubDir/Inc/include2.cmake | 1 + Tests/RunCMake/include/ParentVariableSubDir/include1.cmake | 5 +++++ Tests/RunCMake/include/RunCMakeTest.cmake | 3 +++ 20 files changed, 65 insertions(+) create mode 100644 Tests/RunCMake/find_package/ParentVariable-stdout.txt create mode 100644 Tests/RunCMake/find_package/ParentVariable.cmake create mode 100644 Tests/RunCMake/find_package/ParentVariable/PrimaryConfig.cmake create mode 100644 Tests/RunCMake/find_package/ParentVariable/SecondaryConfig.cmake create mode 100644 Tests/RunCMake/include/ParentVariableRoot-stdout.txt create mode 100644 Tests/RunCMake/include/ParentVariableRoot/include1.cmake create mode 100644 Tests/RunCMake/include/ParentVariableRoot/include2.cmake create mode 100644 Tests/RunCMake/include/ParentVariableScript-stdout.txt create mode 100644 Tests/RunCMake/include/ParentVariableScript.cmake create mode 100644 Tests/RunCMake/include/ParentVariableScript/include1.cmake create mode 100644 Tests/RunCMake/include/ParentVariableScript/include2.cmake create mode 100644 Tests/RunCMake/include/ParentVariableSubDir-stdout.txt create mode 100644 Tests/RunCMake/include/ParentVariableSubDir.cmake create mode 100644 Tests/RunCMake/include/ParentVariableSubDir/CMakeLists.txt create mode 100644 Tests/RunCMake/include/ParentVariableSubDir/Inc/CMakeLists.txt create mode 100644 Tests/RunCMake/include/ParentVariableSubDir/Inc/include2.cmake create mode 100644 Tests/RunCMake/include/ParentVariableSubDir/include1.cmake diff --git a/Tests/RunCMake/find_package/ParentVariable-stdout.txt b/Tests/RunCMake/find_package/ParentVariable-stdout.txt new file mode 100644 index 0000000..a5aeac3 --- /dev/null +++ b/Tests/RunCMake/find_package/ParentVariable-stdout.txt @@ -0,0 +1,5 @@ +-- ParentVariable\.cmake: '[^']*/Tests/RunCMake/find_package/CMakeLists\.txt' +-- ParentVariable/PrimaryConfig\.cmake: '[^']*/Tests/RunCMake/find_package/ParentVariable\.cmake' +-- ParentVariable/SecondaryConfig\.cmake: '[^']*/Tests/RunCMake/find_package/ParentVariable/PrimaryConfig\.cmake' +-- ParentVariable/PrimaryConfig\.cmake: '[^']*/Tests/RunCMake/find_package/ParentVariable/PrimaryConfig\.cmake' +-- ParentVariable\.cmake: '[^']*/Tests/RunCMake/find_package/ParentVariable\.cmake' diff --git a/Tests/RunCMake/find_package/ParentVariable.cmake b/Tests/RunCMake/find_package/ParentVariable.cmake new file mode 100644 index 0000000..3c4fb2e --- /dev/null +++ b/Tests/RunCMake/find_package/ParentVariable.cmake @@ -0,0 +1,4 @@ +message(STATUS "ParentVariable\.cmake: '${CMAKE_PARENT_LIST_FILE}'") +set(CMAKE_PREFIX_PATH ${CMAKE_CURRENT_SOURCE_DIR}/ParentVariable) +find_package(Primary QUIET CONFIG REQUIRED) +message(STATUS "ParentVariable\.cmake: '${CMAKE_PARENT_LIST_FILE}'") diff --git a/Tests/RunCMake/find_package/ParentVariable/PrimaryConfig.cmake b/Tests/RunCMake/find_package/ParentVariable/PrimaryConfig.cmake new file mode 100644 index 0000000..91c1ae7 --- /dev/null +++ b/Tests/RunCMake/find_package/ParentVariable/PrimaryConfig.cmake @@ -0,0 +1,3 @@ +message(STATUS "ParentVariable/PrimaryConfig\.cmake: '${CMAKE_PARENT_LIST_FILE}'") +find_package(Secondary QUIET CONFIG REQUIRED) +message(STATUS "ParentVariable/PrimaryConfig\.cmake: '${CMAKE_PARENT_LIST_FILE}'") diff --git a/Tests/RunCMake/find_package/ParentVariable/SecondaryConfig.cmake b/Tests/RunCMake/find_package/ParentVariable/SecondaryConfig.cmake new file mode 100644 index 0000000..3ffa366 --- /dev/null +++ b/Tests/RunCMake/find_package/ParentVariable/SecondaryConfig.cmake @@ -0,0 +1 @@ +message(STATUS "ParentVariable/SecondaryConfig\.cmake: '${CMAKE_PARENT_LIST_FILE}'") diff --git a/Tests/RunCMake/find_package/RunCMakeTest.cmake b/Tests/RunCMake/find_package/RunCMakeTest.cmake index ad71e5f..5affd8a 100644 --- a/Tests/RunCMake/find_package/RunCMakeTest.cmake +++ b/Tests/RunCMake/find_package/RunCMakeTest.cmake @@ -87,6 +87,7 @@ run_cmake(REGISTRY_VIEW-no-view) run_cmake(REGISTRY_VIEW-wrong-view) run_cmake(REGISTRY_VIEW-propagated) run_cmake(DebugRoot) +run_cmake(ParentVariable) if(CMAKE_HOST_WIN32 AND MINGW) run_cmake(MSYSTEM_PREFIX) diff --git a/Tests/RunCMake/include/CMakeLists.txt b/Tests/RunCMake/include/CMakeLists.txt index bf2ef15..3dd7323 100644 --- a/Tests/RunCMake/include/CMakeLists.txt +++ b/Tests/RunCMake/include/CMakeLists.txt @@ -1,3 +1,9 @@ cmake_minimum_required(VERSION 3.10) project(${RunCMake_TEST} NONE) +if(RunCMake_TEST STREQUAL "ParentVariableRoot") + message(STATUS "CMakeLists.txt: '${CMAKE_PARENT_LIST_FILE}'") + include(ParentVariableRoot/include1.cmake) + message(STATUS "CMakeLists.txt: '${CMAKE_PARENT_LIST_FILE}'") + return() +endif() include(${RunCMake_TEST}.cmake) diff --git a/Tests/RunCMake/include/ParentVariableRoot-stdout.txt b/Tests/RunCMake/include/ParentVariableRoot-stdout.txt new file mode 100644 index 0000000..a76f523 --- /dev/null +++ b/Tests/RunCMake/include/ParentVariableRoot-stdout.txt @@ -0,0 +1,5 @@ +-- CMakeLists\.txt: '[^']*/Tests/RunCMake/include/CMakeLists\.txt' +-- ParentVariableRoot/include1\.cmake: '[^']*/Tests/RunCMake/include/CMakeLists\.txt' +-- ParentVariableRoot/include2\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableRoot/include1\.cmake' +-- ParentVariableRoot/include1\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableRoot/include1\.cmake' +-- CMakeLists\.txt: '[^']*/Tests/RunCMake/include/CMakeLists\.txt' diff --git a/Tests/RunCMake/include/ParentVariableRoot/include1.cmake b/Tests/RunCMake/include/ParentVariableRoot/include1.cmake new file mode 100644 index 0000000..5ca32f0 --- /dev/null +++ b/Tests/RunCMake/include/ParentVariableRoot/include1.cmake @@ -0,0 +1,3 @@ +message(STATUS "ParentVariableRoot/include1.cmake: '${CMAKE_PARENT_LIST_FILE}'") +include(ParentVariableRoot/include2.cmake) +message(STATUS "ParentVariableRoot/include1.cmake: '${CMAKE_PARENT_LIST_FILE}'") diff --git a/Tests/RunCMake/include/ParentVariableRoot/include2.cmake b/Tests/RunCMake/include/ParentVariableRoot/include2.cmake new file mode 100644 index 0000000..3eb2003 --- /dev/null +++ b/Tests/RunCMake/include/ParentVariableRoot/include2.cmake @@ -0,0 +1 @@ +message(STATUS "ParentVariableRoot/include2.cmake: '${CMAKE_PARENT_LIST_FILE}'") diff --git a/Tests/RunCMake/include/ParentVariableScript-stdout.txt b/Tests/RunCMake/include/ParentVariableScript-stdout.txt new file mode 100644 index 0000000..3b1030e --- /dev/null +++ b/Tests/RunCMake/include/ParentVariableScript-stdout.txt @@ -0,0 +1,5 @@ +-- ParentVariableScript\.cmake: '' +-- ParentVariableScript/include1\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableScript\.cmake' +-- ParentVariableScript/include2\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableScript/include1\.cmake' +-- ParentVariableScript/include1\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableScript/include1\.cmake' +-- ParentVariableScript\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableScript\.cmake' diff --git a/Tests/RunCMake/include/ParentVariableScript.cmake b/Tests/RunCMake/include/ParentVariableScript.cmake new file mode 100644 index 0000000..a6d949a --- /dev/null +++ b/Tests/RunCMake/include/ParentVariableScript.cmake @@ -0,0 +1,6 @@ +if(DEFINED CMAKE_PARENT_LIST_FILE) + message(SEND_ERROR "`CMAKE_PARENT_LIST_FILE` is not expected to be set here") +endif() +message(STATUS "ParentVariableScript.cmake: '${CMAKE_PARENT_LIST_FILE}'") +include("${CMAKE_CURRENT_LIST_DIR}/ParentVariableScript/include1.cmake") +message(STATUS "ParentVariableScript.cmake: '${CMAKE_PARENT_LIST_FILE}'") diff --git a/Tests/RunCMake/include/ParentVariableScript/include1.cmake b/Tests/RunCMake/include/ParentVariableScript/include1.cmake new file mode 100644 index 0000000..6e588ef --- /dev/null +++ b/Tests/RunCMake/include/ParentVariableScript/include1.cmake @@ -0,0 +1,3 @@ +message(STATUS "ParentVariableScript/include1\.cmake: '${CMAKE_PARENT_LIST_FILE}'") +include("${CMAKE_CURRENT_LIST_DIR}/include2.cmake") +message(STATUS "ParentVariableScript/include1\.cmake: '${CMAKE_PARENT_LIST_FILE}'") diff --git a/Tests/RunCMake/include/ParentVariableScript/include2.cmake b/Tests/RunCMake/include/ParentVariableScript/include2.cmake new file mode 100644 index 0000000..e2de825 --- /dev/null +++ b/Tests/RunCMake/include/ParentVariableScript/include2.cmake @@ -0,0 +1 @@ +message(STATUS "ParentVariableScript/include2.cmake: '${CMAKE_PARENT_LIST_FILE}'") diff --git a/Tests/RunCMake/include/ParentVariableSubDir-stdout.txt b/Tests/RunCMake/include/ParentVariableSubDir-stdout.txt new file mode 100644 index 0000000..f42cc8d --- /dev/null +++ b/Tests/RunCMake/include/ParentVariableSubDir-stdout.txt @@ -0,0 +1,7 @@ +-- ParentVariableSubDir/CMakeLists\.txt: '[^']*/Tests/RunCMake/include/ParentVariableSubDir/CMakeLists\.txt' +-- ParentVariableSubDir/include1\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableSubDir/CMakeLists\.txt' +-- ParentVariableSubDir/Inc/include2.cmake: '[^']*/Tests/RunCMake/include/ParentVariableSubDir/include1\.cmake' +-- ParentVariableSubDir/include1\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableSubDir/include1\.cmake' +-- ParentVariableSubDir/Inc/CMakeLists\.txt: '[^']*/Tests/RunCMake/include/ParentVariableSubDir/include1\.cmake' +-- ParentVariableSubDir/include1\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableSubDir/include1\.cmake' +-- ParentVariableSubDir/CMakeLists\.txt: '[^']*/Tests/RunCMake/include/ParentVariableSubDir/CMakeLists\.txt' diff --git a/Tests/RunCMake/include/ParentVariableSubDir.cmake b/Tests/RunCMake/include/ParentVariableSubDir.cmake new file mode 100644 index 0000000..57c0da4 --- /dev/null +++ b/Tests/RunCMake/include/ParentVariableSubDir.cmake @@ -0,0 +1 @@ +add_subdirectory(ParentVariableSubDir) diff --git a/Tests/RunCMake/include/ParentVariableSubDir/CMakeLists.txt b/Tests/RunCMake/include/ParentVariableSubDir/CMakeLists.txt new file mode 100644 index 0000000..0f2c114 --- /dev/null +++ b/Tests/RunCMake/include/ParentVariableSubDir/CMakeLists.txt @@ -0,0 +1,3 @@ +message(STATUS "ParentVariableSubDir/CMakeLists.txt: '${CMAKE_PARENT_LIST_FILE}'") +include("include1.cmake") +message(STATUS "ParentVariableSubDir/CMakeLists.txt: '${CMAKE_PARENT_LIST_FILE}'") diff --git a/Tests/RunCMake/include/ParentVariableSubDir/Inc/CMakeLists.txt b/Tests/RunCMake/include/ParentVariableSubDir/Inc/CMakeLists.txt new file mode 100644 index 0000000..9aabcab --- /dev/null +++ b/Tests/RunCMake/include/ParentVariableSubDir/Inc/CMakeLists.txt @@ -0,0 +1 @@ +message(STATUS "ParentVariableSubDir/Inc/CMakeLists.txt: '${CMAKE_PARENT_LIST_FILE}'") diff --git a/Tests/RunCMake/include/ParentVariableSubDir/Inc/include2.cmake b/Tests/RunCMake/include/ParentVariableSubDir/Inc/include2.cmake new file mode 100644 index 0000000..8da759e --- /dev/null +++ b/Tests/RunCMake/include/ParentVariableSubDir/Inc/include2.cmake @@ -0,0 +1 @@ +message(STATUS "ParentVariableSubDir/Inc/include2.cmake: '${CMAKE_PARENT_LIST_FILE}'") diff --git a/Tests/RunCMake/include/ParentVariableSubDir/include1.cmake b/Tests/RunCMake/include/ParentVariableSubDir/include1.cmake new file mode 100644 index 0000000..952cd6e --- /dev/null +++ b/Tests/RunCMake/include/ParentVariableSubDir/include1.cmake @@ -0,0 +1,5 @@ +message(STATUS "ParentVariableSubDir/include1.cmake: '${CMAKE_PARENT_LIST_FILE}'") +include("Inc/include2.cmake") +message(STATUS "ParentVariableSubDir/include1.cmake: '${CMAKE_PARENT_LIST_FILE}'") +include("Inc/CMakeLists.txt") +message(STATUS "ParentVariableSubDir/include1.cmake: '${CMAKE_PARENT_LIST_FILE}'") diff --git a/Tests/RunCMake/include/RunCMakeTest.cmake b/Tests/RunCMake/include/RunCMakeTest.cmake index f748179..47fcf1e 100644 --- a/Tests/RunCMake/include/RunCMakeTest.cmake +++ b/Tests/RunCMake/include/RunCMakeTest.cmake @@ -6,6 +6,9 @@ run_cmake(CMP0024-NEW) run_cmake(ExportExportInclude) run_cmake(IncludeIsDirectory) run_cmake(IncludeMalformed) +run_cmake(ParentVariableRoot) +run_cmake(ParentVariableSubDir) +run_cmake_script(ParentVariableScript) run_cmake(CMP0146-OLD) run_cmake(CMP0146-WARN) -- cgit v0.12 From ce8be3da801229a2573573dd10f71ce2b058db8b Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 11 Apr 2025 12:21:24 -0400 Subject: cmMakefile: De-duplicate CMAKE_CURRENT_LIST_FILE variable names --- Source/cmMakefile.cxx | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index 7b2e6b1..a513f30 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -90,6 +90,12 @@ # endif #endif +namespace { +std::string const kCMAKE_CURRENT_LIST_DIR = "CMAKE_CURRENT_LIST_DIR"; +std::string const kCMAKE_CURRENT_LIST_FILE = "CMAKE_CURRENT_LIST_FILE"; +std::string const kCMAKE_PARENT_LIST_FILE = "CMAKE_PARENT_LIST_FILE"; +} + class cmMessenger; cmDirectoryId::cmDirectoryId(std::string s) @@ -611,8 +617,8 @@ cmMakefile::IncludeScope::~IncludeScope() bool cmMakefile::ReadDependentFile(std::string const& filename, bool noPolicyScope) { - if (cmValue def = this->GetDefinition("CMAKE_CURRENT_LIST_FILE")) { - this->AddDefinition("CMAKE_PARENT_LIST_FILE", *def); + if (cmValue def = this->GetDefinition(kCMAKE_CURRENT_LIST_FILE)) { + this->AddDefinition(kCMAKE_PARENT_LIST_FILE, *def); } std::string filenametoread = cmSystemTools::CollapseFullPath( filename, this->GetCurrentSourceDirectory()); @@ -811,16 +817,16 @@ void cmMakefile::RunListFile(cmListFile const& listFile, this->ListFiles.push_back(filenametoread); std::string currentParentFile = - this->GetSafeDefinition("CMAKE_PARENT_LIST_FILE"); - std::string currentFile = this->GetSafeDefinition("CMAKE_CURRENT_LIST_FILE"); + this->GetSafeDefinition(kCMAKE_PARENT_LIST_FILE); + std::string currentFile = this->GetSafeDefinition(kCMAKE_CURRENT_LIST_FILE); - this->AddDefinition("CMAKE_CURRENT_LIST_FILE", filenametoread); - this->AddDefinition("CMAKE_CURRENT_LIST_DIR", + this->AddDefinition(kCMAKE_CURRENT_LIST_FILE, filenametoread); + this->AddDefinition(kCMAKE_CURRENT_LIST_DIR, cmSystemTools::GetFilenamePath(filenametoread)); - this->MarkVariableAsUsed("CMAKE_PARENT_LIST_FILE"); - this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_FILE"); - this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_DIR"); + this->MarkVariableAsUsed(kCMAKE_PARENT_LIST_FILE); + this->MarkVariableAsUsed(kCMAKE_CURRENT_LIST_FILE); + this->MarkVariableAsUsed(kCMAKE_CURRENT_LIST_DIR); // Run the parsed commands. size_t const numberFunctions = listFile.Functions.size(); @@ -869,13 +875,13 @@ void cmMakefile::RunListFile(cmListFile const& listFile, } } - this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentParentFile); - this->AddDefinition("CMAKE_CURRENT_LIST_FILE", currentFile); - this->AddDefinition("CMAKE_CURRENT_LIST_DIR", + this->AddDefinition(kCMAKE_PARENT_LIST_FILE, currentParentFile); + this->AddDefinition(kCMAKE_CURRENT_LIST_FILE, currentFile); + this->AddDefinition(kCMAKE_CURRENT_LIST_DIR, cmSystemTools::GetFilenamePath(currentFile)); - this->MarkVariableAsUsed("CMAKE_PARENT_LIST_FILE"); - this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_FILE"); - this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_DIR"); + this->MarkVariableAsUsed(kCMAKE_PARENT_LIST_FILE); + this->MarkVariableAsUsed(kCMAKE_CURRENT_LIST_FILE); + this->MarkVariableAsUsed(kCMAKE_CURRENT_LIST_DIR); } void cmMakefile::EnforceDirectoryLevelRules() const @@ -1520,7 +1526,7 @@ void cmMakefile::Configure() cmSystemTools::MakeDirectory(filesDir); assert(cmSystemTools::FileExists(currentStart, true)); - this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentStart); + this->AddDefinition(kCMAKE_PARENT_LIST_FILE, currentStart); #ifdef CMake_ENABLE_DEBUGGER if (this->GetCMakeInstance()->GetDebugAdapter()) { @@ -3392,7 +3398,7 @@ std::string cmMakefile::GetModulesFile(cm::string_view filename, bool& system, // from which we are being called is located itself in CMAKE_ROOT, then // prefer results from CMAKE_ROOT depending on the policy setting. if (!moduleInCMakeModulePath.empty() && !moduleInCMakeRoot.empty()) { - cmValue currentFile = this->GetDefinition("CMAKE_CURRENT_LIST_FILE"); + cmValue currentFile = this->GetDefinition(kCMAKE_CURRENT_LIST_FILE); std::string mods = cmStrCat(cmSystemTools::GetCMakeRoot(), "/Modules/"); if (currentFile && cmSystemTools::IsSubDirectory(*currentFile, mods)) { system = true; -- cgit v0.12 From 41708398eb1e8ea63d7c88da60f6ee859c273e3c Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 11 Apr 2025 12:24:05 -0400 Subject: cmMakefile: Factor out base class for list file scope RAII --- Source/cmMakefile.cxx | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index a513f30..e20aecb 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -94,6 +94,18 @@ namespace { std::string const kCMAKE_CURRENT_LIST_DIR = "CMAKE_CURRENT_LIST_DIR"; std::string const kCMAKE_CURRENT_LIST_FILE = "CMAKE_CURRENT_LIST_FILE"; std::string const kCMAKE_PARENT_LIST_FILE = "CMAKE_PARENT_LIST_FILE"; + +class FileScopeBase +{ +protected: + cmMakefile* Makefile; + +public: + FileScopeBase(cmMakefile* mf) + : Makefile(mf) + { + } +}; } class cmMessenger; @@ -565,7 +577,7 @@ bool cmMakefile::IsImportedTargetGlobalScope() const return this->CurrentImportedTargetScope == ImportedTargetScope::Global; } -class cmMakefile::IncludeScope +class cmMakefile::IncludeScope : public FileScopeBase { public: IncludeScope(cmMakefile* mf, std::string const& filenametoread, @@ -577,7 +589,6 @@ public: IncludeScope& operator=(IncludeScope const&) = delete; private: - cmMakefile* Makefile; bool NoPolicyScope; bool ReportError = true; }; @@ -585,7 +596,7 @@ private: cmMakefile::IncludeScope::IncludeScope(cmMakefile* mf, std::string const& filenametoread, bool noPolicyScope) - : Makefile(mf) + : FileScopeBase(mf) , NoPolicyScope(noPolicyScope) { this->Makefile->Backtrace = this->Makefile->Backtrace.Push( @@ -659,11 +670,11 @@ bool cmMakefile::ReadDependentFile(std::string const& filename, return true; } -class cmMakefile::ListFileScope +class cmMakefile::ListFileScope : public FileScopeBase { public: ListFileScope(cmMakefile* mf, std::string const& filenametoread) - : Makefile(mf) + : FileScopeBase(mf) { this->Makefile->Backtrace = this->Makefile->Backtrace.Push( cmListFileContext::FromListFilePath(filenametoread)); @@ -689,7 +700,6 @@ public: ListFileScope& operator=(ListFileScope const&) = delete; private: - cmMakefile* Makefile; bool ReportError = true; }; @@ -1458,11 +1468,11 @@ bool cmMakefile::IsRootMakefile() const return !this->StateSnapshot.GetBuildsystemDirectoryParent().IsValid(); } -class cmMakefile::BuildsystemFileScope +class cmMakefile::BuildsystemFileScope : public FileScopeBase { public: BuildsystemFileScope(cmMakefile* mf) - : Makefile(mf) + : FileScopeBase(mf) { std::string currentStart = this->Makefile->GetCMakeInstance()->GetCMakeListFile( @@ -1500,7 +1510,6 @@ public: BuildsystemFileScope& operator=(BuildsystemFileScope const&) = delete; private: - cmMakefile* Makefile; cmGlobalGenerator* GG; cmMakefile* CurrentMakefile; cmStateSnapshot Snapshot; -- cgit v0.12 From a9ea55f0d75ef371a2612fe13b4460dba5f9880b Mon Sep 17 00:00:00 2001 From: Benjamin Buch Date: Sun, 6 Apr 2025 22:00:23 +0200 Subject: Fix CMAKE_PARENT_LIST_FILE after return from include() or find_package() Fix the implementation, clarify the documentation, and add tests. Fixes: #25026 Co-authored-by: Brad King --- Help/variable/CMAKE_PARENT_LIST_FILE.rst | 10 +++- Source/cmMakefile.cxx | 64 ++++++++++++++-------- .../find_package/ParentVariable-stdout.txt | 4 +- .../RunCMake/include/ParentVariableRoot-stdout.txt | 2 +- .../include/ParentVariableScript-stdout.txt | 4 +- Tests/RunCMake/include/ParentVariableScript.cmake | 3 + .../include/ParentVariableSubDir-stdout.txt | 4 +- 7 files changed, 59 insertions(+), 32 deletions(-) diff --git a/Help/variable/CMAKE_PARENT_LIST_FILE.rst b/Help/variable/CMAKE_PARENT_LIST_FILE.rst index cfd8608..7e71efa 100644 --- a/Help/variable/CMAKE_PARENT_LIST_FILE.rst +++ b/Help/variable/CMAKE_PARENT_LIST_FILE.rst @@ -5,5 +5,11 @@ Full path to the CMake file that included the current one. While processing a CMake file loaded by :command:`include` or :command:`find_package` this variable contains the full path to the file -including it. The top of the include stack is always the ``CMakeLists.txt`` -for the current directory. See also :variable:`CMAKE_CURRENT_LIST_FILE`. +including it. + +While processing a ``CMakeLists.txt`` file, even in subdirectories, +this variable has the same value as :variable:`CMAKE_CURRENT_LIST_FILE`. +While processing a :option:`cmake -P` script, this variable is not defined +in the outermost script. + +See also :variable:`CMAKE_CURRENT_LIST_FILE`. diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index e20aecb..786ef74 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -100,11 +100,46 @@ class FileScopeBase protected: cmMakefile* Makefile; +private: + std::string OldCurrent; + cm::optional OldParent; + public: FileScopeBase(cmMakefile* mf) : Makefile(mf) { } + void PushListFileVars(std::string const& newCurrent) + { + if (cmValue p = this->Makefile->GetDefinition(kCMAKE_PARENT_LIST_FILE)) { + this->OldParent = *p; + } + if (cmValue c = this->Makefile->GetDefinition(kCMAKE_CURRENT_LIST_FILE)) { + this->OldCurrent = *c; + this->Makefile->AddDefinition(kCMAKE_PARENT_LIST_FILE, this->OldCurrent); + this->Makefile->MarkVariableAsUsed(kCMAKE_PARENT_LIST_FILE); + } + this->Makefile->AddDefinition(kCMAKE_CURRENT_LIST_FILE, newCurrent); + this->Makefile->AddDefinition(kCMAKE_CURRENT_LIST_DIR, + cmSystemTools::GetFilenamePath(newCurrent)); + this->Makefile->MarkVariableAsUsed(kCMAKE_CURRENT_LIST_FILE); + this->Makefile->MarkVariableAsUsed(kCMAKE_CURRENT_LIST_DIR); + } + void PopListFileVars() + { + if (this->OldParent) { + this->Makefile->AddDefinition(kCMAKE_PARENT_LIST_FILE, *this->OldParent); + this->Makefile->MarkVariableAsUsed(kCMAKE_PARENT_LIST_FILE); + } else { + this->Makefile->RemoveDefinition(kCMAKE_PARENT_LIST_FILE); + } + this->Makefile->AddDefinition(kCMAKE_CURRENT_LIST_FILE, this->OldCurrent); + this->Makefile->AddDefinition( + kCMAKE_CURRENT_LIST_DIR, + cmSystemTools::GetFilenamePath(this->OldCurrent)); + this->Makefile->MarkVariableAsUsed(kCMAKE_CURRENT_LIST_FILE); + this->Makefile->MarkVariableAsUsed(kCMAKE_CURRENT_LIST_DIR); + } }; } @@ -610,10 +645,12 @@ cmMakefile::IncludeScope::IncludeScope(cmMakefile* mf, if (!this->NoPolicyScope) { this->Makefile->PushPolicy(); } + this->PushListFileVars(filenametoread); } cmMakefile::IncludeScope::~IncludeScope() { + this->PopListFileVars(); if (!this->NoPolicyScope) { // Pop the scope we pushed for the script. this->Makefile->PopPolicy(); @@ -628,9 +665,6 @@ cmMakefile::IncludeScope::~IncludeScope() bool cmMakefile::ReadDependentFile(std::string const& filename, bool noPolicyScope) { - if (cmValue def = this->GetDefinition(kCMAKE_CURRENT_LIST_FILE)) { - this->AddDefinition(kCMAKE_PARENT_LIST_FILE, *def); - } std::string filenametoread = cmSystemTools::CollapseFullPath( filename, this->GetCurrentSourceDirectory()); @@ -685,10 +719,12 @@ public: assert(this->Makefile->StateSnapshot.IsValid()); this->Makefile->PushFunctionBlockerBarrier(); + this->PushListFileVars(filenametoread); } ~ListFileScope() { + this->PopListFileVars(); this->Makefile->PopSnapshot(this->ReportError); this->Makefile->PopFunctionBlockerBarrier(this->ReportError); this->Makefile->Backtrace = this->Makefile->Backtrace.Pop(); @@ -826,18 +862,6 @@ void cmMakefile::RunListFile(cmListFile const& listFile, // add this list file to the list of dependencies this->ListFiles.push_back(filenametoread); - std::string currentParentFile = - this->GetSafeDefinition(kCMAKE_PARENT_LIST_FILE); - std::string currentFile = this->GetSafeDefinition(kCMAKE_CURRENT_LIST_FILE); - - this->AddDefinition(kCMAKE_CURRENT_LIST_FILE, filenametoread); - this->AddDefinition(kCMAKE_CURRENT_LIST_DIR, - cmSystemTools::GetFilenamePath(filenametoread)); - - this->MarkVariableAsUsed(kCMAKE_PARENT_LIST_FILE); - this->MarkVariableAsUsed(kCMAKE_CURRENT_LIST_FILE); - this->MarkVariableAsUsed(kCMAKE_CURRENT_LIST_DIR); - // Run the parsed commands. size_t const numberFunctions = listFile.Functions.size(); for (size_t i = 0; i < numberFunctions; ++i) { @@ -884,14 +908,6 @@ void cmMakefile::RunListFile(cmListFile const& listFile, } } } - - this->AddDefinition(kCMAKE_PARENT_LIST_FILE, currentParentFile); - this->AddDefinition(kCMAKE_CURRENT_LIST_FILE, currentFile); - this->AddDefinition(kCMAKE_CURRENT_LIST_DIR, - cmSystemTools::GetFilenamePath(currentFile)); - this->MarkVariableAsUsed(kCMAKE_PARENT_LIST_FILE); - this->MarkVariableAsUsed(kCMAKE_CURRENT_LIST_FILE); - this->MarkVariableAsUsed(kCMAKE_CURRENT_LIST_DIR); } void cmMakefile::EnforceDirectoryLevelRules() const @@ -1482,6 +1498,7 @@ public: this->Makefile->StateSnapshot.GetState()->CreatePolicyScopeSnapshot( this->Makefile->StateSnapshot); this->Makefile->PushFunctionBlockerBarrier(); + this->PushListFileVars(currentStart); this->GG = mf->GetGlobalGenerator(); this->CurrentMakefile = this->GG->GetCurrentMakefile(); @@ -1495,6 +1512,7 @@ public: ~BuildsystemFileScope() { + this->PopListFileVars(); this->Makefile->PopFunctionBlockerBarrier(this->ReportError); this->Makefile->PopSnapshot(this->ReportError); #if !defined(CMAKE_BOOTSTRAP) diff --git a/Tests/RunCMake/find_package/ParentVariable-stdout.txt b/Tests/RunCMake/find_package/ParentVariable-stdout.txt index a5aeac3..e4fa6b2 100644 --- a/Tests/RunCMake/find_package/ParentVariable-stdout.txt +++ b/Tests/RunCMake/find_package/ParentVariable-stdout.txt @@ -1,5 +1,5 @@ -- ParentVariable\.cmake: '[^']*/Tests/RunCMake/find_package/CMakeLists\.txt' -- ParentVariable/PrimaryConfig\.cmake: '[^']*/Tests/RunCMake/find_package/ParentVariable\.cmake' -- ParentVariable/SecondaryConfig\.cmake: '[^']*/Tests/RunCMake/find_package/ParentVariable/PrimaryConfig\.cmake' --- ParentVariable/PrimaryConfig\.cmake: '[^']*/Tests/RunCMake/find_package/ParentVariable/PrimaryConfig\.cmake' --- ParentVariable\.cmake: '[^']*/Tests/RunCMake/find_package/ParentVariable\.cmake' +-- ParentVariable/PrimaryConfig\.cmake: '[^']*/Tests/RunCMake/find_package/ParentVariable\.cmake' +-- ParentVariable\.cmake: '[^']*/Tests/RunCMake/find_package/CMakeLists\.txt' diff --git a/Tests/RunCMake/include/ParentVariableRoot-stdout.txt b/Tests/RunCMake/include/ParentVariableRoot-stdout.txt index a76f523..0460905 100644 --- a/Tests/RunCMake/include/ParentVariableRoot-stdout.txt +++ b/Tests/RunCMake/include/ParentVariableRoot-stdout.txt @@ -1,5 +1,5 @@ -- CMakeLists\.txt: '[^']*/Tests/RunCMake/include/CMakeLists\.txt' -- ParentVariableRoot/include1\.cmake: '[^']*/Tests/RunCMake/include/CMakeLists\.txt' -- ParentVariableRoot/include2\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableRoot/include1\.cmake' --- ParentVariableRoot/include1\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableRoot/include1\.cmake' +-- ParentVariableRoot/include1\.cmake: '[^']*/Tests/RunCMake/include/CMakeLists\.txt' -- CMakeLists\.txt: '[^']*/Tests/RunCMake/include/CMakeLists\.txt' diff --git a/Tests/RunCMake/include/ParentVariableScript-stdout.txt b/Tests/RunCMake/include/ParentVariableScript-stdout.txt index 3b1030e..9de7cea 100644 --- a/Tests/RunCMake/include/ParentVariableScript-stdout.txt +++ b/Tests/RunCMake/include/ParentVariableScript-stdout.txt @@ -1,5 +1,5 @@ -- ParentVariableScript\.cmake: '' -- ParentVariableScript/include1\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableScript\.cmake' -- ParentVariableScript/include2\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableScript/include1\.cmake' --- ParentVariableScript/include1\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableScript/include1\.cmake' --- ParentVariableScript\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableScript\.cmake' +-- ParentVariableScript/include1\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableScript\.cmake' +-- ParentVariableScript\.cmake: '' diff --git a/Tests/RunCMake/include/ParentVariableScript.cmake b/Tests/RunCMake/include/ParentVariableScript.cmake index a6d949a..59a8590 100644 --- a/Tests/RunCMake/include/ParentVariableScript.cmake +++ b/Tests/RunCMake/include/ParentVariableScript.cmake @@ -3,4 +3,7 @@ if(DEFINED CMAKE_PARENT_LIST_FILE) endif() message(STATUS "ParentVariableScript.cmake: '${CMAKE_PARENT_LIST_FILE}'") include("${CMAKE_CURRENT_LIST_DIR}/ParentVariableScript/include1.cmake") +if(DEFINED CMAKE_PARENT_LIST_FILE) + message(SEND_ERROR "`CMAKE_PARENT_LIST_FILE` is not expected to be set here") +endif() message(STATUS "ParentVariableScript.cmake: '${CMAKE_PARENT_LIST_FILE}'") diff --git a/Tests/RunCMake/include/ParentVariableSubDir-stdout.txt b/Tests/RunCMake/include/ParentVariableSubDir-stdout.txt index f42cc8d..2f785ee 100644 --- a/Tests/RunCMake/include/ParentVariableSubDir-stdout.txt +++ b/Tests/RunCMake/include/ParentVariableSubDir-stdout.txt @@ -1,7 +1,7 @@ -- ParentVariableSubDir/CMakeLists\.txt: '[^']*/Tests/RunCMake/include/ParentVariableSubDir/CMakeLists\.txt' -- ParentVariableSubDir/include1\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableSubDir/CMakeLists\.txt' -- ParentVariableSubDir/Inc/include2.cmake: '[^']*/Tests/RunCMake/include/ParentVariableSubDir/include1\.cmake' --- ParentVariableSubDir/include1\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableSubDir/include1\.cmake' +-- ParentVariableSubDir/include1\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableSubDir/CMakeLists\.txt' -- ParentVariableSubDir/Inc/CMakeLists\.txt: '[^']*/Tests/RunCMake/include/ParentVariableSubDir/include1\.cmake' --- ParentVariableSubDir/include1\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableSubDir/include1\.cmake' +-- ParentVariableSubDir/include1\.cmake: '[^']*/Tests/RunCMake/include/ParentVariableSubDir/CMakeLists\.txt' -- ParentVariableSubDir/CMakeLists\.txt: '[^']*/Tests/RunCMake/include/ParentVariableSubDir/CMakeLists\.txt' -- cgit v0.12