diff options
author | Brad King <brad.king@kitware.com> | 2023-02-21 21:00:24 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2023-02-23 14:15:14 (GMT) |
commit | df9c4b18720c6c8263c13641bc0a6452138d6fba (patch) | |
tree | 4301ddaee071056517ef6a0964d34e2627738987 /Source | |
parent | 4da27a73bd10665e753194a4a14323c9069e9289 (diff) | |
download | CMake-df9c4b18720c6c8263c13641bc0a6452138d6fba.zip CMake-df9c4b18720c6c8263c13641bc0a6452138d6fba.tar.gz CMake-df9c4b18720c6c8263c13641bc0a6452138d6fba.tar.bz2 |
find_package: Use <PACKAGENAME>_ROOT variables as search prefixes
Extend commit eb35d8884b (find_package: Use PackageName_ROOT variables
as search prefixes, 2018-03-15, v3.12.0-rc1~349^2) to also check
upper-case `<PACKAGENAME>_ROOT` variables. Add policy `CMP0144` to
enable the behavior in a compatible way.
Fixes: #24403
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmFindPackageCommand.cxx | 43 | ||||
-rw-r--r-- | Source/cmMakefile.cxx | 21 | ||||
-rw-r--r-- | Source/cmMakefile.h | 3 | ||||
-rw-r--r-- | Source/cmPolicies.h | 5 |
4 files changed, 71 insertions, 1 deletions
diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index dd00419..5f1a2c5 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -1841,13 +1841,56 @@ void cmFindPackageCommand::PushFindPackageRootPathStack() } break; } + // Add root paths from <PACKAGENAME>_ROOT CMake and environment variables, + // if they are different than <PackageName>_ROOT, and subject to CMP0144. + std::string const rootVAR = cmSystemTools::UpperCase(rootVar); + cmValue rootDEF; + cm::optional<std::string> rootENV; + if (rootVAR != rootVar) { + rootDEF = this->Makefile->GetDefinition(rootVAR); + if (rootDEF && (rootDEF.IsEmpty() || rootDEF == rootDef)) { + rootDEF = nullptr; + } + rootENV = cmSystemTools::GetEnvVar(rootVAR); + if (rootENV && (rootENV->empty() || rootENV == rootEnv)) { + rootENV = cm::nullopt; + } + } + + switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0144)) { + case cmPolicies::WARN: + this->Makefile->MaybeWarnCMP0144(rootVAR, rootDEF, rootENV); + CM_FALLTHROUGH; + case cmPolicies::OLD: + // OLD behavior is to ignore the <PACKAGENAME>_ROOT variables. + rootDEF = nullptr; + rootENV = cm::nullopt; + break; + case cmPolicies::REQUIRED_IF_USED: + case cmPolicies::REQUIRED_ALWAYS: + this->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0144)); + return; + case cmPolicies::NEW: { + // NEW behavior is to honor the <PACKAGENAME>_ROOT variables. + } break; + } + if (rootDef) { cmExpandList(*rootDef, rootPaths); } + if (rootDEF) { + cmExpandList(*rootDEF, rootPaths); + } if (rootEnv) { std::vector<std::string> p = cmSystemTools::SplitEnvPath(*rootEnv); std::move(p.begin(), p.end(), std::back_inserter(rootPaths)); } + if (rootENV) { + std::vector<std::string> p = cmSystemTools::SplitEnvPath(*rootENV); + std::move(p.begin(), p.end(), std::back_inserter(rootPaths)); + } } void cmFindPackageCommand::PopFindPackageRootPathStack() diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index b640d64..2fc2974 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -228,6 +228,27 @@ void cmMakefile::MaybeWarnCMP0074(std::string const& rootVar, cmValue rootDef, } } +void cmMakefile::MaybeWarnCMP0144(std::string const& rootVAR, cmValue rootDEF, + cm::optional<std::string> const& rootENV) +{ + // Warn if a <PACKAGENAME>_ROOT variable we may use is set. + if ((rootDEF || rootENV) && this->WarnedCMP0144.insert(rootVAR).second) { + std::ostringstream w; + w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0144) << "\n"; + if (rootDEF) { + w << "CMake variable " << rootVAR << " is set to:\n" + << " " << *rootDEF << "\n"; + } + if (rootENV) { + w << "Environment variable " << rootVAR << " is set to:\n" + << " " << *rootENV << "\n"; + } + w << "For compatibility, find_package is ignoring the variable, but " + "code in a .cmake module might still use it."; + this->IssueMessage(MessageType::AUTHOR_WARNING, w.str()); + } +} + cmBTStringRange cmMakefile::GetIncludeDirectoriesEntries() const { return this->StateSnapshot.GetDirectory().GetIncludeDirectoriesEntries(); diff --git a/Source/cmMakefile.h b/Source/cmMakefile.h index 247d4dd..3cf6e61 100644 --- a/Source/cmMakefile.h +++ b/Source/cmMakefile.h @@ -1013,6 +1013,8 @@ public: void MaybeWarnCMP0074(std::string const& rootVar, cmValue rootDef, cm::optional<std::string> const& rootEnv); + void MaybeWarnCMP0144(std::string const& rootVAR, cmValue rootDEF, + cm::optional<std::string> const& rootENV); void MaybeWarnUninitialized(std::string const& variable, const char* sourceFilename) const; bool IsProjectFile(const char* filename) const; @@ -1190,6 +1192,7 @@ private: bool CheckSystemVars; bool CheckCMP0000; std::set<std::string> WarnedCMP0074; + std::set<std::string> WarnedCMP0144; bool IsSourceFileTryCompile; mutable bool SuppressSideEffects; ImportedTargetScope CurrentImportedTargetScope = ImportedTargetScope::Local; diff --git a/Source/cmPolicies.h b/Source/cmPolicies.h index 568eca3..1eca586 100644 --- a/Source/cmPolicies.h +++ b/Source/cmPolicies.h @@ -434,7 +434,10 @@ class cmMakefile; 3, 25, 0, cmPolicies::WARN) \ SELECT(POLICY, CMP0143, \ "Global property USE_FOLDERS treated as ON by default", 3, 26, 0, \ - cmPolicies::WARN) + cmPolicies::WARN) \ + SELECT(POLICY, CMP0144, \ + "find_package uses upper-case <PACKAGENAME>_ROOT variables.", 3, 27, \ + 0, cmPolicies::WARN) #define CM_SELECT_ID(F, A1, A2, A3, A4, A5, A6) F(A1) #define CM_FOR_EACH_POLICY_ID(POLICY) \ |