diff options
author | Craig Scott <craig.scott@crascit.com> | 2024-04-14 08:07:41 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2024-04-19 15:44:09 (GMT) |
commit | c5231ba29e47101b772572f730af5b2bc82fbd53 (patch) | |
tree | 450618966f266499ba95aac1f7496411f01cad21 /Source/cmFindPackageCommand.cxx | |
parent | 8ac7958e3a2d24258cc4ebf0d6c44eccfb8a849a (diff) | |
download | CMake-c5231ba29e47101b772572f730af5b2bc82fbd53.zip CMake-c5231ba29e47101b772572f730af5b2bc82fbd53.tar.gz CMake-c5231ba29e47101b772572f730af5b2bc82fbd53.tar.bz2 |
find_package: Save/restore PACKAGE_PREFIX_DIR
Package configuration files generated by `configure_package_config_file`
set this variable in `@PACKAGE_INIT@` and then use it later. There may
be intervening calls to `find_package`, e.g., via `find_dependency`.
If the loaded package also used `configure_package_config_file`, it
may change the value of `PACKAGE_PREFIX_DIR`. Explicitly save and
restore the value to avoid this.
Fixes: #25827
Diffstat (limited to 'Source/cmFindPackageCommand.cxx')
-rw-r--r-- | Source/cmFindPackageCommand.cxx | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index 9b51b1a..3bfabd9 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -982,6 +982,36 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args) return true; } + // Restore PACKAGE_PREFIX_DIR to its pre-call value when we return. If our + // caller is a file generated by configure_package_config_file(), and if + // the package we are about to load also has a config file created by that + // command, it will overwrite PACKAGE_PREFIX_DIR. We need to restore it in + // case something still refers to it in our caller's scope after we return. + class RestoreVariableOnLeavingScope + { + cmMakefile* makefile_; + cm::optional<std::string> value_; + + public: + RestoreVariableOnLeavingScope(cmMakefile* makefile) + : makefile_(makefile) + { + cmValue v = makefile->GetDefinition("PACKAGE_PREFIX_DIR"); + if (v) { + value_ = *v; + } + } + ~RestoreVariableOnLeavingScope() + { + if (this->value_) { + makefile_->AddDefinition("PACKAGE_PREFIX_DIR", *value_); + } else { + makefile_->RemoveDefinition("PACKAGE_PREFIX_DIR"); + } + } + }; + RestoreVariableOnLeavingScope restorePackagePrefixDir(this->Makefile); + // Now choose what method(s) we will use to satisfy the request. Note that // we still want all the above checking of arguments, etc. regardless of the // method used. This will ensure ill-formed arguments are caught earlier, |