From 2b22c2a45fc66d56f829f73d65855529937b7042 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 31 Mar 2026 15:28:36 -0400 Subject: [PATCH 1/2] find_package: Clarify dependency ordering logic in package stack Clarify package stack manipulation from commit c6e6861e63 (install(EXPORT): Export find_dependency() calls, 2023-11-07, v3.29.0-rc1~439^2~1). Issue: #27730 --- Source/cmMakefile.cxx | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index f480874c0c..64e9f2c0f8 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -4275,14 +4275,24 @@ cmFindPackageStackRAII::~cmFindPackageStackRAII() this->Makefile->FindPackageStack = this->Makefile->FindPackageStack.Pop(); if (!this->Makefile->FindPackageStack.Empty()) { - auto top = this->Makefile->FindPackageStack.Top(); + // We have just finished an inner package found as a dependency of an + // outer package. Targets created in the outer package after this + // point may depend on the inner package, so if they are exported, + // their find_dependency call for the outer package should be + // ordered after the find_dependency call for the inner package. + // + // Any targets created by the outer package before the inner package + // was loaded will have already saved a copy of the outer package + // stack with its original index. Replace the top entry with a new + // one representing the same outer package with a new index. + cmFindPackageCall outer = this->Makefile->FindPackageStack.Top(); this->Makefile->FindPackageStack = this->Makefile->FindPackageStack.Pop(); - top.Index = this->Makefile->FindPackageStackNextIndex; + outer.Index = this->Makefile->FindPackageStackNextIndex; this->Makefile->FindPackageStackNextIndex++; this->Makefile->FindPackageStack = - this->Makefile->FindPackageStack.Push(top); + this->Makefile->FindPackageStack.Push(outer); } } From 5aa649d5f6e635bb2d0be41cd743abcbe4b5624f Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 1 Apr 2026 11:12:37 -0400 Subject: [PATCH 2/2] find_package: Save package information only after successfully loading it If a package configuration file sets `_FOUND` to false, the package is considered not found. Do not save its package info. Note that this exposes an existing pointer invalidation on nested `find_package` calls, which will be fixed in following commits. Issue: #27730 --- Source/cmFindPackageCommand.cxx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index dd866440e6..25143f5003 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -1732,6 +1732,12 @@ bool cmFindPackageCommand::HandlePackageMode( // The configuration file is invalid. result = false; } + + if (this->UseConfigFiles && found) { + this->CurrentPackageInfo->Directory = + cmSystemTools::GetFilenamePath(this->FileFound); + this->CurrentPackageInfo->Version = this->VersionFound; + } } if (this->UseFindModules && !found && @@ -1975,8 +1981,6 @@ bool cmFindPackageCommand::FindConfig() std::string init; if (found) { init = cmSystemTools::GetFilenamePath(this->FileFound); - this->CurrentPackageInfo->Directory = init; - this->CurrentPackageInfo->Version = this->VersionFound; } else { init = this->Variable + "-NOTFOUND"; }