diff options
author | Craig Scott <craig.scott@crascit.com> | 2023-03-17 04:37:03 (GMT) |
---|---|---|
committer | Craig Scott <craig.scott@crascit.com> | 2023-03-17 10:35:30 (GMT) |
commit | 716ce4402ada51445c73af7211192c695364a3ac (patch) | |
tree | 27ce65a1cb59fadff886040c0b351d68bca92fdb /Source/cmFindPackageCommand.cxx | |
parent | 2c2c2c2227101730ea6835b5a32a41336725e46a (diff) | |
download | CMake-716ce4402ada51445c73af7211192c695364a3ac.zip CMake-716ce4402ada51445c73af7211192c695364a3ac.tar.gz CMake-716ce4402ada51445c73af7211192c695364a3ac.tar.bz2 |
find_package: Ensure root path stack and module vars are restored
Fixes: #24595
Diffstat (limited to 'Source/cmFindPackageCommand.cxx')
-rw-r--r-- | Source/cmFindPackageCommand.cxx | 62 |
1 files changed, 50 insertions, 12 deletions
diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index c228cde..355ca58 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -456,6 +456,51 @@ int parseVersion(const std::string& version, unsigned int& major, } // anonymous namespace +class cmFindPackageCommand::FlushDebugBufferOnExit +{ + cmFindPackageCommand& Command; + +public: + FlushDebugBufferOnExit(cmFindPackageCommand& command) + : Command(command) + { + } + ~FlushDebugBufferOnExit() + { + if (!Command.DebugBuffer.empty()) { + Command.DebugMessage(Command.DebugBuffer); + } + } +}; + +class cmFindPackageCommand::PushPopRootPathStack +{ + cmFindPackageCommand& Command; + +public: + PushPopRootPathStack(cmFindPackageCommand& command) + : Command(command) + { + Command.PushFindPackageRootPathStack(); + } + ~PushPopRootPathStack() { Command.PopFindPackageRootPathStack(); } +}; + +class cmFindPackageCommand::SetRestoreFindDefinitions +{ + cmFindPackageCommand& Command; + +public: + SetRestoreFindDefinitions( + cmFindPackageCommand& command, const std::string& components, + const std::vector<std::pair<std::string, const char*>>& componentVarDefs) + : Command(command) + { + Command.SetModuleVariables(components, componentVarDefs); + } + ~SetRestoreFindDefinitions() { Command.RestoreFindDefinitions(); } +}; + cmFindPackageCommand::PathLabel cmFindPackageCommand::PathLabel::PackageRedirect("PACKAGE_REDIRECT"); cmFindPackageCommand::PathLabel cmFindPackageCommand::PathLabel::UserRegistry( @@ -992,9 +1037,11 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args) } } - this->PushFindPackageRootPathStack(); - - this->SetModuleVariables(components, componentVarDefs); + // RAII objects to ensure we leave this function with consistent state + FlushDebugBufferOnExit flushDebugBufferOnExit(*this); + PushPopRootPathStack pushPopRootPathStack(*this); + SetRestoreFindDefinitions setRestoreFindDefinitions(*this, components, + componentVarDefs); // See if we have been told to delegate to FetchContent or some other // redirected config package first. We have to check all names that @@ -1115,15 +1162,6 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args) this->AppendSuccessInformation(); - // Restore original state of "_FIND_" variables set in SetModuleVariables() - this->RestoreFindDefinitions(); - - this->PopFindPackageRootPathStack(); - - if (!this->DebugBuffer.empty()) { - this->DebugMessage(this->DebugBuffer); - } - return loadedPackage; } |