diff --git a/Source/cmExportPackageInfoGenerator.cxx b/Source/cmExportPackageInfoGenerator.cxx index 23f2b7284d..62a01e5eed 100644 --- a/Source/cmExportPackageInfoGenerator.cxx +++ b/Source/cmExportPackageInfoGenerator.cxx @@ -311,7 +311,8 @@ bool cmExportPackageInfoGenerator::NoteLinkedTarget( auto pkgInfo = [](cmTarget* t) -> Package { cmFindPackageStack pkgStack = t->GetFindPackageStack(); if (!pkgStack.Empty()) { - return std::make_pair(pkgStack.Top().Name, pkgStack.Top().PackageInfo); + return std::make_pair(pkgStack.Top().Name, + *pkgStack.Top().PackageInfo); } cmPackageInformation package; diff --git a/Source/cmExportSbomGenerator.cxx b/Source/cmExportSbomGenerator.cxx index 26bee6eae9..c99141f3fb 100644 --- a/Source/cmExportSbomGenerator.cxx +++ b/Source/cmExportSbomGenerator.cxx @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -320,7 +321,8 @@ bool cmExportSbomGenerator::NoteLinkedTarget( auto pkgInfo = [](cmTarget* t) -> Package { cmFindPackageStack pkgStack = t->GetFindPackageStack(); if (!pkgStack.Empty()) { - return std::make_pair(pkgStack.Top().Name, pkgStack.Top().PackageInfo); + return std::make_pair(pkgStack.Top().Name, + *pkgStack.Top().PackageInfo); } std::string const pkgName = t->GetSafeProperty("EXPORT_FIND_PACKAGE_NAME"); diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index b8ee8c905b..09a677514a 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -1215,13 +1215,15 @@ bool cmFindPackageCommand::FindPackage( } } + // Record package information discovered while it is loaded. + this->PackageInfo = std::make_shared(); + // RAII objects to ensure we leave this function with consistent state. FlushDebugBufferOnExit flushDebugBufferOnExit(*this); PushPopRootPathStack pushPopRootPathStack(*this); SetRestoreFindDefinitions setRestoreFindDefinitions(*this); - cmFindPackageStackRAII findPackageStackRAII(this->Makefile, this->Name); - - findPackageStackRAII.BindTop(this->CurrentPackageInfo); + cmFindPackageStackRAII findPackageStackRAII(this->Makefile, this->Name, + this->PackageInfo); // See if we have been told to delegate to FetchContent or some other // redirected config package first. We have to check all names that @@ -1269,8 +1271,8 @@ bool cmFindPackageCommand::FindPackage( this->Names.clear(); this->Names.emplace_back(overrideName); // Force finding this one this->Variable = cmStrCat(this->Name, "_DIR"); - this->CurrentPackageInfo->Directory = redirectsDir; - this->CurrentPackageInfo->Version = this->VersionFound; + this->PackageInfo->Directory = redirectsDir; + this->PackageInfo->Version = this->VersionFound; this->SetConfigDirCacheVariable(redirectsDir); break; } @@ -1731,9 +1733,9 @@ bool cmFindPackageCommand::HandlePackageMode( } if (this->UseConfigFiles && found) { - this->CurrentPackageInfo->Directory = + this->PackageInfo->Directory = cmSystemTools::GetFilenamePath(this->FileFound); - this->CurrentPackageInfo->Version = this->VersionFound; + this->PackageInfo->Version = this->VersionFound; } } diff --git a/Source/cmFindPackageCommand.h b/Source/cmFindPackageCommand.h index 4ac057e6e6..740cdca82f 100644 --- a/Source/cmFindPackageCommand.h +++ b/Source/cmFindPackageCommand.h @@ -285,7 +285,7 @@ private: std::set OptionalComponents; std::set RequiredTargets; std::string DebugBuffer; - cmPackageInformation* CurrentPackageInfo; + std::shared_ptr PackageInfo; enum class SearchResult { diff --git a/Source/cmFindPackageStack.h b/Source/cmFindPackageStack.h index e7fb9a6ee4..3a5804120b 100644 --- a/Source/cmFindPackageStack.h +++ b/Source/cmFindPackageStack.h @@ -39,7 +39,7 @@ class cmFindPackageCall { public: std::string const Name; - cmPackageInformation PackageInfo; + std::shared_ptr PackageInfo; unsigned int Index; }; @@ -50,19 +50,14 @@ public: class cmFindPackageStackRAII { cmMakefile* Makefile; - cmPackageInformation** Value = nullptr; public: - cmFindPackageStackRAII(cmMakefile* mf, std::string const& pkg); + cmFindPackageStackRAII(cmMakefile* mf, std::string const& pkg, + std::shared_ptr pkgInfo); ~cmFindPackageStackRAII(); cmFindPackageStackRAII(cmFindPackageStackRAII const&) = delete; cmFindPackageStackRAII& operator=(cmFindPackageStackRAII const&) = delete; - - /** Get a mutable pointer to the top of the stack. - The pointer is invalidated if BindTop is called again or when the - cmFindPackageStackRAII goes out of scope. */ - void BindTop(cmPackageInformation*& value); }; /** diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index 78455b80a3..e0f2d10067 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -51,7 +51,6 @@ #include "cmSourceFile.h" #include "cmSourceFileLocation.h" #include "cmSourceGroup.h" -#include "cmStack.h" #include "cmState.h" #include "cmStateDirectory.h" #include "cmStateTypes.h" @@ -4234,34 +4233,22 @@ cmMakefile::MacroPushPop::~MacroPushPop() this->Makefile->PopMacroScope(this->ReportError); } -cmFindPackageStackRAII::cmFindPackageStackRAII(cmMakefile* mf, - std::string const& name) +cmFindPackageStackRAII::cmFindPackageStackRAII( + cmMakefile* mf, std::string const& name, + std::shared_ptr pkgInfo) : Makefile(mf) { this->Makefile->FindPackageStack = this->Makefile->FindPackageStack.Push(cmFindPackageCall{ name, - cmPackageInformation(), + std::move(pkgInfo), this->Makefile->FindPackageStackNextIndex, }); this->Makefile->FindPackageStackNextIndex++; } -void cmFindPackageStackRAII::BindTop(cmPackageInformation*& value) -{ - if (this->Value) { - *this->Value = nullptr; - } - this->Value = &value; - value = &this->Makefile->FindPackageStack.cmStack::Top().PackageInfo; -} - cmFindPackageStackRAII::~cmFindPackageStackRAII() { - if (this->Value) { - *this->Value = nullptr; - } - this->Makefile->FindPackageStackNextIndex = this->Makefile->FindPackageStack.Top().Index + 1; this->Makefile->FindPackageStack = this->Makefile->FindPackageStack.Pop(); diff --git a/Tests/RunCMake/find_package/NestedConfig.cmake b/Tests/RunCMake/find_package/NestedConfig.cmake new file mode 100644 index 0000000000..9bd376a20c --- /dev/null +++ b/Tests/RunCMake/find_package/NestedConfig.cmake @@ -0,0 +1,3 @@ +cmake_policy(SET CMP0074 NEW) +set(Outer_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/NestedConfig) +find_package(Outer CONFIG) diff --git a/Tests/RunCMake/find_package/NestedConfig/OuterConfig.cmake b/Tests/RunCMake/find_package/NestedConfig/OuterConfig.cmake new file mode 100644 index 0000000000..2a1332b87b --- /dev/null +++ b/Tests/RunCMake/find_package/NestedConfig/OuterConfig.cmake @@ -0,0 +1 @@ +find_package(Inner CONFIG NO_DEFAULT_PATH) diff --git a/Tests/RunCMake/find_package/RunCMakeTest.cmake b/Tests/RunCMake/find_package/RunCMakeTest.cmake index f0fc80b3a7..1d96eeb745 100644 --- a/Tests/RunCMake/find_package/RunCMakeTest.cmake +++ b/Tests/RunCMake/find_package/RunCMakeTest.cmake @@ -36,6 +36,7 @@ run_cmake(MissingConfigRequired) run_cmake(MissingConfigVersion) run_cmake(MixedModeOptions) run_cmake_with_options(ModuleModeDebugPkg --debug-find-pkg=Foo,Zot) +run_cmake(NestedConfig) run_cmake(PackageRoot) run_cmake(PackageRootNestedConfig) run_cmake(PackageRootNestedModule)