ENH: Improve errors when a policy is REQUIRED

In the future some policies may be set to REQUIRED_IF_USED or
REQUIRED_ALWAYS.  This change clarifies the error messages users receive
when violating the requirements.
This commit is contained in:
Brad King 2008-08-18 16:29:00 -04:00
parent 061d20be38
commit f50ed1fd88
4 changed files with 142 additions and 165 deletions

View File

@ -3460,50 +3460,59 @@ bool cmMakefile::EnforceUniqueName(std::string const& name, std::string& msg,
return true;
}
cmPolicies::PolicyStatus cmMakefile
::GetPolicyStatus(cmPolicies::PolicyID id)
//----------------------------------------------------------------------------
cmPolicies::PolicyStatus
cmMakefile::GetPolicyStatus(cmPolicies::PolicyID id)
{
cmPolicies::PolicyStatus status = cmPolicies::REQUIRED_IF_USED;
PolicyMap::iterator mappos;
int vecpos;
bool done = false;
// Get the current setting of the policy.
cmPolicies::PolicyStatus cur = this->GetPolicyStatusInternal(id);
// check our policy stack first
for (vecpos = static_cast<int>(this->PolicyStack.size()) - 1;
vecpos >= 0 && !done; vecpos--)
// If the policy is required to be set to NEW but is not, ignore the
// current setting and tell the caller.
if(cur != cmPolicies::NEW)
{
mappos = this->PolicyStack[vecpos].find(id);
if (mappos != this->PolicyStack[vecpos].end())
if(cur == cmPolicies::REQUIRED_ALWAYS ||
cur == cmPolicies::REQUIRED_IF_USED)
{
status = mappos->second;
done = true;
return cur;
}
cmPolicies::PolicyStatus def = this->GetPolicies()->GetPolicyStatus(id);
if(def == cmPolicies::REQUIRED_ALWAYS ||
def == cmPolicies::REQUIRED_IF_USED)
{
return def;
}
}
// if not found then
if (!done)
// The current setting is okay.
return cur;
}
//----------------------------------------------------------------------------
cmPolicies::PolicyStatus
cmMakefile::GetPolicyStatusInternal(cmPolicies::PolicyID id)
{
// Is the policy set in our stack?
for(std::vector<PolicyMap>::reverse_iterator
psi = this->PolicyStack.rbegin();
psi != this->PolicyStack.rend(); ++psi)
{
// pass the buck to our parent if we have one
if (this->LocalGenerator->GetParent())
PolicyMap::const_iterator pse = psi->find(id);
if(pse != psi->end())
{
cmMakefile *parent =
this->LocalGenerator->GetParent()->GetMakefile();
return parent->GetPolicyStatus(id);
}
// otherwise use the default
else
{
status = this->GetPolicies()->GetPolicyStatus(id);
return pse->second;
}
}
// warn if we see a REQUIRED_IF_USED above a OLD or WARN
if (!this->GetPolicies()->IsValidUsedPolicyStatus(id,status))
// If we have a parent directory, recurse up to it.
if(this->LocalGenerator->GetParent())
{
return cmPolicies::REQUIRED_IF_USED;
cmMakefile* parent = this->LocalGenerator->GetParent()->GetMakefile();
return parent->GetPolicyStatusInternal(id);
}
return status;
// The policy is not set. Use the default for this CMake version.
return this->GetPolicies()->GetPolicyStatus(id);
}
bool cmMakefile::SetPolicy(const char *id,
@ -3520,13 +3529,22 @@ bool cmMakefile::SetPolicy(const char *id,
return this->SetPolicy(pid,status);
}
//----------------------------------------------------------------------------
bool cmMakefile::SetPolicy(cmPolicies::PolicyID id,
cmPolicies::PolicyStatus status)
{
// setting a REQUIRED_ALWAYS policy to WARN or OLD is an insta error
if (this->GetPolicies()->
IsValidPolicyStatus(id,status))
// A REQUIRED_ALWAYS policy may be set only to NEW.
if(status != cmPolicies::NEW &&
this->GetPolicies()->GetPolicyStatus(id) ==
cmPolicies::REQUIRED_ALWAYS)
{
std::string msg =
this->GetPolicies()->GetRequiredAlwaysPolicyError(id);
this->IssueMessage(cmake::FATAL_ERROR, msg.c_str());
return false;
}
// Store the setting.
this->PolicyStack.back()[id] = status;
// Special hook for presenting compatibility variable as soon as
@ -3549,8 +3567,6 @@ bool cmMakefile::SetPolicy(cmPolicies::PolicyID id,
}
return true;
}
return false;
}
bool cmMakefile::PushPolicy()

View File

@ -900,6 +900,7 @@ private:
typedef std::map<cmPolicies::PolicyID,
cmPolicies::PolicyStatus> PolicyMap;
std::vector<PolicyMap> PolicyStack;
cmPolicies::PolicyStatus GetPolicyStatusInternal(cmPolicies::PolicyID id);
bool CheckCMP0000;

View File

@ -347,6 +347,7 @@ void cmPolicies::DefinePolicy(cmPolicies::PolicyID iD,
this->PolicyStringMap[idString] = iD;
}
//----------------------------------------------------------------------------
bool cmPolicies::ApplyPolicyVersion(cmMakefile *mf,
const char *version)
{
@ -408,13 +409,18 @@ bool cmPolicies::ApplyPolicyVersion(cmMakefile *mf,
}
// now loop over all the policies and set them as appropriate
std::vector<cmPolicies::PolicyID> ancientPolicies;
std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
= this->Policies.begin();
for (;i != this->Policies.end(); ++i)
{
if (i->second->IsPolicyNewerThan(majorVer,minorVer,patchVer))
{
if (!mf->SetPolicy(i->second->ID, cmPolicies::WARN))
if(i->second->Status == cmPolicies::REQUIRED_ALWAYS)
{
ancientPolicies.push_back(i->first);
}
else if (!mf->SetPolicy(i->second->ID, cmPolicies::WARN))
{
return false;
}
@ -427,102 +433,13 @@ bool cmPolicies::ApplyPolicyVersion(cmMakefile *mf,
}
}
}
return true;
}
// is this a valid status the listfile can set this policy to?
bool cmPolicies::IsValidPolicyStatus(cmPolicies::PolicyID id,
cmPolicies::PolicyStatus status)
{
// if they are setting a feature to anything other than OLD or WARN and the
// feature is not known about then that is an error
if (this->Policies.find(id) == this->Policies.end())
// Make sure the project does not use any ancient policies.
if(!ancientPolicies.empty())
{
if (status == cmPolicies::WARN ||
status == cmPolicies::OLD)
{
return true;
}
cmOStringStream error;
error <<
"Error: an attempt was made to enable the new behavior for " <<
"a new feature that is in a later version of CMake than "
"what you are runing, please upgrade to a newer version "
"of CMake.";
cmSystemTools::Error(error.str().c_str());
return false;
}
// now we know the feature is defined, so the only issue is if someone is
// setting it to WARN or OLD when the feature is REQUIRED_ALWAYS
if ((status == cmPolicies::WARN ||
status == cmPolicies::OLD) &&
this->Policies[id]->Status == cmPolicies::REQUIRED_ALWAYS)
{
cmOStringStream error;
error <<
"Error: an attempt was made to enable the old behavior for " <<
"a feature that is no longer supported. The feature in " <<
"question is feature " <<
id <<
" which had new behavior introduced in CMake version " <<
this->Policies[id]->GetVersionString() <<
" please either update your CMakeLists files to conform to " <<
"the new behavior " <<
"or use an older version of CMake that still supports " <<
"the old behavior. Run cmake --help-policies " <<
id << " for more information.";
cmSystemTools::Error(error.str().c_str());
return false;
}
return true;
}
// is this a valid status the listfile can set this policy to?
bool cmPolicies::IsValidUsedPolicyStatus(cmPolicies::PolicyID id,
cmPolicies::PolicyStatus status)
{
// if they are setting a feature to anything other than OLD or WARN and the
// feature is not known about then that is an error
if (this->Policies.find(id) == this->Policies.end())
{
if (status == cmPolicies::WARN ||
status == cmPolicies::OLD)
{
return true;
}
cmOStringStream error;
error <<
"Error: an attempt was made to enable the new behavior for " <<
"a new feature that is in a later version of CMake than "
"what you are runing, please upgrade to a newer version "
"of CMake.";
cmSystemTools::Error(error.str().c_str());
return false;
}
// now we know the feature is defined, so the only issue is if someone is
// setting it to WARN or OLD when the feature is REQUIRED_ALWAYS
if ((status == cmPolicies::WARN ||
status == cmPolicies::OLD) &&
(this->Policies[id]->Status == cmPolicies::REQUIRED_ALWAYS ||
this->Policies[id]->Status == cmPolicies::REQUIRED_IF_USED))
{
cmOStringStream error;
error <<
"Error: an attempt was made to enable the old behavior for " <<
"a feature that is no longer supported. The feature in " <<
"question is feature " <<
id <<
" which had new behavior introduced in CMake version " <<
this->Policies[id]->GetVersionString() <<
" please either update your CMakeLists files to conform to " <<
"the new behavior " <<
"or use an older version of CMake that still supports " <<
"the old behavior. Run cmake --help-policies " <<
id << " for more information.";
cmSystemTools::Error(error.str().c_str());
this->DiagnoseAncientPolicies(ancientPolicies,
majorVer, minorVer, patchVer, mf);
cmSystemTools::SetFatalErrorOccured();
return false;
}
@ -671,3 +588,48 @@ void cmPolicies::GetDocumentation(std::vector<cmDocumentationEntry>& v)
v.push_back(e);
}
}
//----------------------------------------------------------------------------
std::string
cmPolicies::GetRequiredAlwaysPolicyError(cmPolicies::PolicyID id)
{
std::string pid = this->GetPolicyIDString(id);
cmOStringStream e;
e << "Policy " << pid << " may not be set to OLD behavior because this "
<< "version of CMake no longer supports it. "
<< "The policy was introduced in "
<< "CMake version " << this->Policies[id]->GetVersionString()
<< ", and use of NEW behavior is now required."
<< "\n"
<< "Please either update your CMakeLists.txt files to conform to "
<< "the new behavior or use an older version of CMake that still "
<< "supports the old behavior. "
<< "Run cmake --help-policy " << pid << " for more information.";
return e.str();
}
//----------------------------------------------------------------------------
void
cmPolicies::DiagnoseAncientPolicies(std::vector<PolicyID> const& ancient,
unsigned int majorVer,
unsigned int minorVer,
unsigned int patchVer,
cmMakefile* mf)
{
cmOStringStream e;
e << "The project requests behavior compatible with CMake version \""
<< majorVer << "." << minorVer << "." << patchVer
<< "\", which requires OLD the behavior for some policies:\n";
for(std::vector<PolicyID>::const_iterator
i = ancient.begin(); i != ancient.end(); ++i)
{
cmPolicy const* policy = this->Policies[*i];
e << " " << policy->IDString << ": " << policy->ShortDescription << "\n";
}
e << "However, this version of CMake no longer supports the OLD "
<< "behavior for these policies. "
<< "Please either update your CMakeLists.txt files to conform to "
<< "the new behavior or use an older version of CMake that still "
<< "supports the old behavior.";
mf->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
}

View File

@ -75,20 +75,15 @@ public:
///! Set a policy level for this listfile
bool ApplyPolicyVersion(cmMakefile *mf, const char *version);
///! test to see if setting a policy to a specific value is valid
bool IsValidPolicyStatus(cmPolicies::PolicyID id,
cmPolicies::PolicyStatus status);
///! test to see if setting a policy to a specific value is valid, when used
bool IsValidUsedPolicyStatus(cmPolicies::PolicyID id,
cmPolicies::PolicyStatus status);
///! return a warning string for a given policy
std::string GetPolicyWarning(cmPolicies::PolicyID id);
///! return an error string for when a required policy is unspecified
std::string GetRequiredPolicyError(cmPolicies::PolicyID id);
///! return an error string for when a required policy is unspecified
std::string GetRequiredAlwaysPolicyError(cmPolicies::PolicyID id);
///! Get docs for policies
void GetDocumentation(std::vector<cmDocumentationEntry>& v);
@ -97,6 +92,9 @@ public:
std::map<PolicyID,cmPolicy *> Policies;
std::map<std::string,PolicyID> PolicyStringMap;
void DiagnoseAncientPolicies(std::vector<PolicyID> const& ancient,
unsigned int majorVer, unsigned int minorVer,
unsigned int patchVer, cmMakefile* mf);
};
#endif