Merge topic 'find-package-required'

857a039d66 find_*: Add variable to default calls to REQUIRED

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !10364
This commit is contained in:
Brad King
2025-02-28 16:15:04 +00:00
committed by Kitware Robot
38 changed files with 337 additions and 22 deletions

View File

@@ -94,6 +94,7 @@ bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
this->SelectDefaultMacMode();
bool newStyle = false;
bool haveRequiredOrOptional = false;
enum Doing
{
DoingNone,
@@ -129,8 +130,21 @@ bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
this->NoDefaultPath = true;
} else if (args[j] == "REQUIRED") {
doing = DoingNone;
if (haveRequiredOrOptional && !this->Required) {
this->SetError("cannot be both REQUIRED and OPTIONAL");
return false;
}
this->Required = true;
newStyle = true;
haveRequiredOrOptional = true;
} else if (args[j] == "OPTIONAL") {
doing = DoingNone;
if (haveRequiredOrOptional && this->Required) {
this->SetError("cannot be both REQUIRED and OPTIONAL");
return false;
}
newStyle = true;
haveRequiredOrOptional = true;
} else if (args[j] == "REGISTRY_VIEW") {
if (++j == args.size()) {
this->SetError("missing required argument for REGISTRY_VIEW");
@@ -187,6 +201,10 @@ bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
}
}
if (!haveRequiredOrOptional) {
this->Required = this->Makefile->IsOn("CMAKE_FIND_REQUIRED");
}
if (this->VariableDocumentation.empty()) {
this->VariableDocumentation = "Where can ";
if (this->Names.empty()) {

View File

@@ -747,7 +747,18 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
configArgs.push_back(i);
doing = DoingNone;
} else if (args[i] == "REQUIRED") {
this->Required = true;
if (this->Required == RequiredStatus::OptionalExplicit) {
this->SetError("cannot be both REQUIRED and OPTIONAL");
return false;
}
this->Required = RequiredStatus::RequiredExplicit;
doing = DoingComponents;
} else if (args[i] == "OPTIONAL") {
if (this->Required == RequiredStatus::RequiredExplicit) {
this->SetError("cannot be both REQUIRED and OPTIONAL");
return false;
}
this->Required = RequiredStatus::OptionalExplicit;
doing = DoingComponents;
} else if (args[i] == "COMPONENTS") {
doing = DoingComponents;
@@ -844,6 +855,11 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
}
}
if (this->Required == RequiredStatus::Optional &&
this->Makefile->IsOn("CMAKE_FIND_REQUIRED")) {
this->Required = RequiredStatus::RequiredFromFindVar;
}
if (!this->GlobalScope) {
cmValue value(
this->Makefile->GetDefinition("CMAKE_FIND_PACKAGE_TARGETS_GLOBAL"));
@@ -978,21 +994,21 @@ bool cmFindPackageCommand::FindPackage(
bool const makePackageRequiredSet =
this->Makefile->IsOn(makePackageRequiredVar);
if (makePackageRequiredSet) {
if (this->Required) {
if (this->IsRequired()) {
this->Makefile->IssueMessage(
MessageType::WARNING,
cmStrCat("for module ", this->Name,
" already called with REQUIRED, thus ",
makePackageRequiredVar, " has no effect."));
} else {
this->Required = true;
this->Required = RequiredStatus::RequiredFromPackageVar;
}
}
std::string const disableFindPackageVar =
cmStrCat("CMAKE_DISABLE_FIND_PACKAGE_", this->Name);
if (this->Makefile->IsOn(disableFindPackageVar)) {
if (this->Required) {
if (this->IsRequired()) {
this->SetError(
cmStrCat("for module ", this->Name,
(makePackageRequiredSet
@@ -1310,6 +1326,9 @@ void cmFindPackageCommand::SetModuleVariables()
{
this->AddFindDefinition("CMAKE_FIND_PACKAGE_NAME", this->Name);
// Nested find calls are not automatically required.
this->AddFindDefinition("CMAKE_FIND_REQUIRED", ""_s);
// Store the list of components and associated variable definitions.
std::string components_var = this->Name + "_FIND_COMPONENTS";
this->AddFindDefinition(components_var, this->Components);
@@ -1329,7 +1348,7 @@ void cmFindPackageCommand::SetModuleVariables()
this->AddFindDefinition(quietly, "1"_s);
}
if (this->Required) {
if (this->IsRequired()) {
// Tell the module that is about to be read that it should report
// a fatal error if the package is not found.
std::string req = cmStrCat(this->Name, "_FIND_REQUIRED");
@@ -1575,11 +1594,13 @@ bool cmFindPackageCommand::HandlePackageMode(
// package not found
if (result && !found) {
// warn if package required or neither quiet nor in config mode
if (this->Required ||
!(this->Quiet ||
(this->UseConfigFiles && !this->UseFindModules &&
this->ConsideredConfigs.empty()))) {
// warn if package required or
// (neither quiet nor in config mode and not explicitly optional)
if (this->IsRequired() ||
(!(this->Quiet ||
(this->UseConfigFiles && !this->UseFindModules &&
this->ConsideredConfigs.empty())) &&
this->Required != RequiredStatus::OptionalExplicit)) {
// The variable is not set.
std::ostringstream e;
std::ostringstream aw;
@@ -1675,11 +1696,19 @@ bool cmFindPackageCommand::HandlePackageMode(
"without ensuring that it is actually available.\n";
}
}
if (this->Required == RequiredStatus::RequiredFromFindVar) {
e << "\nThis package is considered required because the "
"CMAKE_FIND_REQUIRED variable has been enabled.\n";
} else if (this->Required == RequiredStatus::RequiredFromPackageVar) {
e << "\nThis package is considered required because the "
<< cmStrCat("CMAKE_REQUIRE_FIND_PACKAGE_", this->Name)
<< " variable has been enabled.\n";
}
this->Makefile->IssueMessage(this->Required ? MessageType::FATAL_ERROR
: MessageType::WARNING,
e.str());
if (this->Required) {
this->Makefile->IssueMessage(
this->IsRequired() ? MessageType::FATAL_ERROR : MessageType::WARNING,
e.str());
if (this->IsRequired()) {
cmSystemTools::SetFatalErrorOccurred();
}
@@ -1912,7 +1941,7 @@ bool cmFindPackageCommand::ReadPackage()
// Loop over appendices.
auto iter = this->CpsAppendices.begin();
while (iter != this->CpsAppendices.end()) {
bool required = false;
RequiredStatus required = RequiredStatus::Optional;
bool important = false;
// Check if this appendix provides any requested components.
@@ -1972,7 +2001,7 @@ bool cmFindPackageCommand::ReadPackage()
bool cmFindPackageCommand::FindPackageDependencies(
std::string const& fileName, cmPackageInfoReader const& reader,
bool required)
RequiredStatus required)
{
// Get package requirements.
for (cmPackageRequirement const& dep : reader.GetRequirements()) {
@@ -2110,7 +2139,7 @@ void cmFindPackageCommand::AppendSuccessInformation()
}
this->Makefile->GetState()->SetGlobalProperty(versionInfoPropName,
versionInfo);
if (this->Required) {
if (this->IsRequired()) {
std::string const requiredInfoPropName =
cmStrCat("_CMAKE_", this->Name, "_TYPE");
this->Makefile->GetState()->SetGlobalProperty(requiredInfoPropName,
@@ -3265,6 +3294,13 @@ bool cmFindPackageCommand::SearchEnvironmentPrefix(std::string const& prefix)
return TryGeneratedPaths(searchFn, pdt::Cps, prefix, pkgDirGen);
}
bool cmFindPackageCommand::IsRequired() const
{
return this->Required == RequiredStatus::RequiredExplicit ||
this->Required == RequiredStatus::RequiredFromPackageVar ||
this->Required == RequiredStatus::RequiredFromFindVar;
}
// TODO: Debug cmsys::Glob double slash problem.
bool cmFindPackage(std::vector<std::string> const& args,

View File

@@ -151,9 +151,17 @@ private:
using AppendixMap = std::map<std::string, Appendix>;
AppendixMap FindAppendices(std::string const& base,
cmPackageInfoReader const& baseReader) const;
enum RequiredStatus
{
Optional,
OptionalExplicit,
RequiredExplicit,
RequiredFromPackageVar,
RequiredFromFindVar
};
bool FindPackageDependencies(std::string const& fileName,
cmPackageInfoReader const& reader,
bool required);
RequiredStatus required);
bool ImportPackageTargets(std::string const& fileName,
cmPackageInfoReader& reader);
@@ -194,6 +202,8 @@ private:
bool SearchAppBundlePrefix(std::string const& prefix);
bool SearchEnvironmentPrefix(std::string const& prefix);
bool IsRequired() const;
struct OriginalDef
{
bool exists;
@@ -234,7 +244,7 @@ private:
unsigned int VersionFoundCount = 0;
KWIML_INT_uint64_t RequiredCMakeVersion = 0;
bool Quiet = false;
bool Required = false;
RequiredStatus Required = RequiredStatus::Optional;
bool UseCpsFiles = false;
bool UseConfigFiles = true;
bool UseFindModules = true;