CMP0037: Remove support for OLD behavior

This commit is contained in:
Brad King
2024-11-27 17:02:58 -05:00
parent 850174c507
commit 84f737545f
63 changed files with 94 additions and 501 deletions
+4 -17
View File
@@ -13,7 +13,6 @@
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmMessageType.h"
#include "cmStateTypes.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmTarget.h"
@@ -163,23 +162,11 @@ bool cmAddCustomTargetCommand(std::vector<std::string> const& args,
}
}
std::string::size_type pos = targetName.find_first_of("#<>");
if (pos != std::string::npos) {
status.SetError(cmStrCat("called with target name containing a \"",
targetName[pos],
"\". This character is not allowed."));
return false;
}
// Some requirements on custom target names already exist
// and have been checked at this point.
// The following restrictions overlap but depend on policy CMP0037.
bool nameOk = cmGeneratorExpression::IsValidTargetName(targetName) &&
!cmGlobalGenerator::IsReservedTarget(targetName);
if (nameOk) {
nameOk = targetName.find(':') == std::string::npos;
}
if (!nameOk && !mf.CheckCMP0037(targetName, cmStateEnums::UTILITY)) {
!cmGlobalGenerator::IsReservedTarget(targetName) &&
targetName.find(':') == std::string::npos;
if (!nameOk) {
mf.IssueInvalidTargetNameError(targetName);
return false;
}
+2 -1
View File
@@ -64,7 +64,8 @@ bool cmAddExecutableCommand(std::vector<std::string> const& args,
if (nameOk && !importTarget && !isAlias) {
nameOk = exename.find(':') == std::string::npos;
}
if (!nameOk && !mf.CheckCMP0037(exename, cmStateEnums::EXECUTABLE)) {
if (!nameOk) {
mf.IssueInvalidTargetNameError(exename);
return false;
}
+2 -1
View File
@@ -150,7 +150,8 @@ bool cmAddLibraryCommand(std::vector<std::string> const& args,
if (nameOk && !importTarget && !isAlias) {
nameOk = libName.find(':') == std::string::npos;
}
if (!nameOk && !mf.CheckCMP0037(libName, type)) {
if (!nameOk) {
mf.IssueInvalidTargetNameError(libName);
return false;
}
-6
View File
@@ -703,12 +703,6 @@ Json::Value CodemodelConfig::DumpTarget(cmGeneratorTarget* gt,
{
Target t(gt, this->Config);
std::string prefix = "target-" + gt->GetName();
for (char& c : prefix) {
// CMP0037 OLD behavior allows slashes in target names. Remove them.
if (c == '/' || c == '\\') {
c = '_';
}
}
if (!this->Config.empty()) {
prefix += "-" + this->Config;
}
+28 -46
View File
@@ -2726,58 +2726,38 @@ cmGlobalGenerator::SplitFrameworkPath(const std::string& path,
return cm::nullopt;
}
static bool RaiseCMP0037Message(cmake* cm, cmTarget* tgt,
std::string const& targetNameAsWritten,
std::string const& reason)
namespace {
void IssueReservedTargetNameError(cmake* cm, cmTarget* tgt,
std::string const& targetNameAsWritten,
std::string const& reason)
{
MessageType messageType = MessageType::AUTHOR_WARNING;
std::ostringstream e;
bool issueMessage = false;
switch (tgt->GetPolicyStatusCMP0037()) {
case cmPolicies::WARN:
e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0037) << '\n';
issueMessage = true;
CM_FALLTHROUGH;
case cmPolicies::OLD:
break;
case cmPolicies::NEW:
issueMessage = true;
messageType = MessageType::FATAL_ERROR;
break;
}
if (issueMessage) {
e << "The target name \"" << targetNameAsWritten << "\" is reserved "
<< reason << '.';
if (messageType == MessageType::AUTHOR_WARNING) {
e << " It may result in undefined behavior.";
}
cm->IssueMessage(messageType, e.str(), tgt->GetBacktrace());
if (messageType == MessageType::FATAL_ERROR) {
return false;
}
}
return true;
cm->IssueMessage(MessageType::FATAL_ERROR,
cmStrCat("The target name \"", targetNameAsWritten,
"\" is reserved ", reason, '.'),
tgt->GetBacktrace());
}
}
bool cmGlobalGenerator::CheckCMP0037(std::string const& targetName,
std::string const& reason) const
bool cmGlobalGenerator::CheckReservedTargetName(
std::string const& targetName, std::string const& reason) const
{
cmTarget* tgt = this->FindTarget(targetName);
if (!tgt) {
return true;
}
return RaiseCMP0037Message(this->GetCMakeInstance(), tgt, targetName,
reason);
IssueReservedTargetNameError(this->GetCMakeInstance(), tgt, targetName,
reason);
return false;
}
bool cmGlobalGenerator::CheckCMP0037Prefix(std::string const& targetPrefix,
std::string const& reason) const
bool cmGlobalGenerator::CheckReservedTargetNamePrefix(
std::string const& targetPrefix, std::string const& reason) const
{
bool ret = true;
for (auto const& tgtPair : this->TargetSearchIndex) {
if (cmHasPrefix(tgtPair.first, targetPrefix) &&
!RaiseCMP0037Message(this->GetCMakeInstance(), tgtPair.second,
tgtPair.first, reason)) {
if (cmHasPrefix(tgtPair.first, targetPrefix)) {
IssueReservedTargetNameError(this->GetCMakeInstance(), tgtPair.second,
tgtPair.first, reason);
ret = false;
}
}
@@ -2807,7 +2787,8 @@ void cmGlobalGenerator::AddGlobalTarget_Package(
static const auto reservedTargets = { "package", "PACKAGE" };
for (auto const& target : reservedTargets) {
if (!this->CheckCMP0037(target, "when CPack packaging is enabled")) {
if (!this->CheckReservedTargetName(target,
"when CPack packaging is enabled")) {
return;
}
}
@@ -2856,8 +2837,8 @@ void cmGlobalGenerator::AddGlobalTarget_PackageSource(
static const auto reservedTargets = { "package_source" };
for (auto const& target : reservedTargets) {
if (!this->CheckCMP0037(target,
"when CPack source packaging is enabled")) {
if (!this->CheckReservedTargetName(
target, "when CPack source packaging is enabled")) {
return;
}
}
@@ -2886,7 +2867,8 @@ void cmGlobalGenerator::AddGlobalTarget_Test(
static const auto reservedTargets = { "test", "RUN_TESTS" };
for (auto const& target : reservedTargets) {
if (!this->CheckCMP0037(target, "when CTest testing is enabled")) {
if (!this->CheckReservedTargetName(target,
"when CTest testing is enabled")) {
return;
}
}
@@ -3245,14 +3227,14 @@ bool cmGlobalGenerator::AddBuildDatabaseTargets()
static const auto reservedTargets = { "cmake_build_database" };
for (auto const& target : reservedTargets) {
if (!this->CheckCMP0037(target,
"when exporting build databases are enabled")) {
if (!this->CheckReservedTargetName(
target, "when exporting build databases are enabled")) {
return false;
}
}
static const auto reservedPrefixes = { "cmake_build_database-" };
for (auto const& prefix : reservedPrefixes) {
if (!this->CheckCMP0037Prefix(
if (!this->CheckReservedTargetNamePrefix(
prefix, "when exporting build databases are enabled")) {
return false;
}
+4 -4
View File
@@ -875,10 +875,10 @@ private:
void ClearGeneratorMembers();
bool CheckCMP0037(std::string const& targetName,
std::string const& reason) const;
bool CheckCMP0037Prefix(std::string const& targetPrefix,
std::string const& reason) const;
bool CheckReservedTargetName(std::string const& targetName,
std::string const& reason) const;
bool CheckReservedTargetNamePrefix(std::string const& targetPrefix,
std::string const& reason) const;
void IndexMakefile(cmMakefile* mf);
void IndexLocalGenerator(cmLocalGenerator* lg);
+8 -32
View File
@@ -195,39 +195,15 @@ Message::LogLevel cmMakefile::GetCurrentLogLevel() const
return result;
}
bool cmMakefile::CheckCMP0037(std::string const& targetName,
cmStateEnums::TargetType targetType) const
void cmMakefile::IssueInvalidTargetNameError(
std::string const& targetName) const
{
MessageType messageType = MessageType::AUTHOR_WARNING;
std::string e;
bool issueMessage = false;
switch (this->GetPolicyStatus(cmPolicies::CMP0037)) {
case cmPolicies::WARN:
if (targetType != cmStateEnums::INTERFACE_LIBRARY) {
e = cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0037), '\n');
issueMessage = true;
}
CM_FALLTHROUGH;
case cmPolicies::OLD:
break;
case cmPolicies::NEW:
issueMessage = true;
messageType = MessageType::FATAL_ERROR;
break;
}
if (issueMessage) {
e +=
cmStrCat("The target name \"", targetName,
"\" is reserved or not valid for certain "
"CMake features, such as generator expressions, and may result "
"in undefined behavior.");
this->IssueMessage(messageType, e);
if (messageType == MessageType::FATAL_ERROR) {
return false;
}
}
return true;
this->IssueMessage(
MessageType::FATAL_ERROR,
cmStrCat("The target name \"", targetName,
"\" is reserved or not valid for certain "
"CMake features, such as generator expressions, and may result "
"in undefined behavior."));
}
void cmMakefile::MaybeWarnCMP0074(std::string const& rootVar, cmValue rootDef,
+1 -2
View File
@@ -994,8 +994,7 @@ public:
/** Set whether or not to report a CMP0000 violation. */
void SetCheckCMP0000(bool b) { this->CheckCMP0000 = b; }
bool CheckCMP0037(std::string const& targetName,
cmStateEnums::TargetType targetType) const;
void IssueInvalidTargetNameError(std::string const& targetName) const;
cmBTStringRange GetIncludeDirectoriesEntries() const;
cmBTStringRange GetCompileOptionsEntries() const;
+1 -1
View File
@@ -117,7 +117,7 @@ class cmMakefile;
SELECT(POLICY, CMP0037, \
"Target names should not be reserved and should match a validity " \
"pattern.", \
3, 0, 0, WARN) \
3, 0, 0, NEW) \
SELECT(POLICY, CMP0038, "Targets may not link directly to themselves.", 3, \
0, 0, WARN) \
SELECT(POLICY, CMP0039, "Utility targets may not have link dependencies.", \