1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-15 12:16:40 +08:00

CMP0037: Allow test and package targets when features are not enabled

When CMake will not generate a test, package, or package_source target,
allow projects to create their own targets with these names.

Fixes: #16062
This commit is contained in:
Brad King
2017-10-26 14:01:51 -04:00
parent 409527a03c
commit ae5f40696e
26 changed files with 195 additions and 5 deletions

View File

@@ -2220,6 +2220,45 @@ inline std::string removeQuotes(const std::string& s)
return s;
}
bool cmGlobalGenerator::CheckCMP0037(std::string const& targetName,
std::string const& reason) const
{
cmTarget* tgt = this->FindTarget(targetName);
if (!tgt) {
return true;
}
cmake::MessageType messageType = cmake::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:
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::REQUIRED_ALWAYS:
issueMessage = true;
messageType = cmake::FATAL_ERROR;
break;
}
if (issueMessage) {
e << "The target name \"" << targetName << "\" is reserved " << reason
<< ".";
if (messageType == cmake::AUTHOR_WARNING) {
e << " It may result in undefined behavior.";
}
this->GetCMakeInstance()->IssueMessage(messageType, e.str(),
tgt->GetBacktrace());
if (messageType == cmake::FATAL_ERROR) {
return false;
}
}
return true;
}
void cmGlobalGenerator::CreateDefaultGlobalTargets(
std::vector<GlobalTargetInfo>& targets)
{
@@ -2241,6 +2280,14 @@ void cmGlobalGenerator::AddGlobalTarget_Package(
return;
}
const char* reservedTargets[] = { "package", "PACKAGE" };
for (const char* const* tn = cm::cbegin(reservedTargets);
tn != cm::cend(reservedTargets); ++tn) {
if (!this->CheckCMP0037(*tn, "when CPack packaging is enabled")) {
return;
}
}
const char* cmakeCfgIntDir = this->GetCMakeCFGIntDir();
GlobalTargetInfo gti;
gti.Name = this->GetPackageTargetName();
@@ -2284,6 +2331,14 @@ void cmGlobalGenerator::AddGlobalTarget_PackageSource(
return;
}
const char* reservedTargets[] = { "package_source" };
for (const char* const* tn = cm::cbegin(reservedTargets);
tn != cm::cend(reservedTargets); ++tn) {
if (!this->CheckCMP0037(*tn, "when CPack source packaging is enabled")) {
return;
}
}
GlobalTargetInfo gti;
gti.Name = packageSourceTargetName;
gti.Message = "Run CPack packaging tool for source...";
@@ -2307,6 +2362,14 @@ void cmGlobalGenerator::AddGlobalTarget_Test(
return;
}
const char* reservedTargets[] = { "test", "RUN_TESTS" };
for (const char* const* tn = cm::cbegin(reservedTargets);
tn != cm::cend(reservedTargets); ++tn) {
if (!this->CheckCMP0037(*tn, "when CTest testing is enabled")) {
return;
}
}
const char* cmakeCfgIntDir = this->GetCMakeCFGIntDir();
GlobalTargetInfo gti;
gti.Name = this->GetTestTargetName();
@@ -2590,11 +2653,10 @@ bool cmGlobalGenerator::IsReservedTarget(std::string const& name)
// by one or more of the cmake generators.
// Adding additional targets to this list will require a policy!
const char* reservedTargets[] = {
"all", "ALL_BUILD", "help", "install", "INSTALL",
"preinstall", "clean", "edit_cache", "rebuild_cache", "test",
"RUN_TESTS", "package", "PACKAGE", "package_source", "ZERO_CHECK"
};
const char* reservedTargets[] = { "all", "ALL_BUILD", "help",
"install", "INSTALL", "preinstall",
"clean", "edit_cache", "rebuild_cache",
"ZERO_CHECK" };
return std::find(cm::cbegin(reservedTargets), cm::cend(reservedTargets),
name) != cm::cend(reservedTargets);

View File

@@ -568,6 +568,9 @@ private:
void ClearGeneratorMembers();
bool CheckCMP0037(std::string const& targetName,
std::string const& reason) const;
void IndexMakefile(cmMakefile* mf);
virtual const char* GetBuildIgnoreErrorsFlag() const { return nullptr; }

View File

@@ -225,6 +225,7 @@ class cmMakefile;
F(CMP0021) \
F(CMP0022) \
F(CMP0027) \
F(CMP0037) \
F(CMP0038) \
F(CMP0041) \
F(CMP0042) \

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,4 @@
^CMake Error at NEW-cond-package.cmake:4 \(add_custom_target\):
The target name "package" is reserved when CPack packaging is enabled.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)$

View File

@@ -0,0 +1,5 @@
cmake_policy(SET CMP0037 NEW)
file(WRITE "${CMAKE_BINARY_DIR}/CPackConfig.cmake" "")
add_custom_target(test)
add_custom_target(package)
add_custom_target(package_source)

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,5 @@
^CMake Error at NEW-cond-package_source.cmake:5 \(add_custom_target\):
The target name "package_source" is reserved when CPack source packaging is
enabled.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)$

View File

@@ -0,0 +1,5 @@
cmake_policy(SET CMP0037 NEW)
file(WRITE "${CMAKE_BINARY_DIR}/CPackSourceConfig.cmake" "")
add_custom_target(test)
add_custom_target(package)
add_custom_target(package_source)

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,4 @@
^CMake Error at NEW-cond-test.cmake:3 \(add_custom_target\):
The target name "test" is reserved when CTest testing is enabled.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)$

View File

@@ -0,0 +1,5 @@
cmake_policy(SET CMP0037 NEW)
enable_testing()
add_custom_target(test)
add_custom_target(package)
add_custom_target(package_source)

View File

@@ -0,0 +1,4 @@
cmake_policy(SET CMP0037 NEW)
add_custom_target(test)
add_custom_target(package)
add_custom_target(package_source)

View File

@@ -0,0 +1,5 @@
cmake_policy(SET CMP0037 OLD)
file(WRITE "${CMAKE_BINARY_DIR}/CPackConfig.cmake" "")
add_custom_target(test)
add_custom_target(package)
add_custom_target(package_source)

View File

@@ -0,0 +1,5 @@
cmake_policy(SET CMP0037 OLD)
file(WRITE "${CMAKE_BINARY_DIR}/CPackSourceConfig.cmake" "")
add_custom_target(test)
add_custom_target(package)
add_custom_target(package_source)

View File

@@ -0,0 +1,5 @@
cmake_policy(SET CMP0037 OLD)
enable_testing()
add_custom_target(test)
add_custom_target(package)
add_custom_target(package_source)

View File

@@ -0,0 +1,5 @@
cmake_policy(SET CMP0037 OLD)
add_custom_target(test)
add_custom_target(package)
add_custom_target(package_source)

View File

@@ -12,3 +12,19 @@ endif()
run_cmake(CMP0037-WARN-reserved)
run_cmake(CMP0037-OLD-reserved)
run_cmake(CMP0037-NEW-reserved)
run_cmake(NEW-cond)
run_cmake(NEW-cond-test)
run_cmake(NEW-cond-package)
run_cmake(OLD-cond)
run_cmake(OLD-cond-test)
run_cmake(OLD-cond-package)
run_cmake(WARN-cond)
run_cmake(WARN-cond-test)
run_cmake(WARN-cond-package)
if(RunCMake_GENERATOR MATCHES "Make|Ninja")
run_cmake(NEW-cond-package_source)
run_cmake(OLD-cond-package_source)
run_cmake(WARN-cond-package_source)
endif()

View File

@@ -0,0 +1,11 @@
^CMake Warning \(dev\) at WARN-cond-package.cmake:4 \(add_custom_target\):
Policy CMP0037 is not set: Target names should not be reserved and should
match a validity pattern. Run "cmake --help-policy CMP0037" for policy
details. Use the cmake_policy command to set the policy and suppress this
warning.
The target name "package" is reserved when CPack packaging is enabled. It
may result in undefined behavior.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)
This warning is for project developers. Use -Wno-dev to suppress it.$

View File

@@ -0,0 +1,5 @@
file(WRITE "${CMAKE_BINARY_DIR}/CPackConfig.cmake" "")
add_custom_target(test)
add_custom_target(package)
add_custom_target(package_source)

View File

@@ -0,0 +1,11 @@
^CMake Warning \(dev\) at WARN-cond-package_source.cmake:5 \(add_custom_target\):
Policy CMP0037 is not set: Target names should not be reserved and should
match a validity pattern. Run "cmake --help-policy CMP0037" for policy
details. Use the cmake_policy command to set the policy and suppress this
warning.
The target name "package_source" is reserved when CPack source packaging is
enabled. It may result in undefined behavior.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)
This warning is for project developers. Use -Wno-dev to suppress it.$

View File

@@ -0,0 +1,5 @@
file(WRITE "${CMAKE_BINARY_DIR}/CPackSourceConfig.cmake" "")
add_custom_target(test)
add_custom_target(package)
add_custom_target(package_source)

View File

@@ -0,0 +1,11 @@
^CMake Warning \(dev\) at WARN-cond-test.cmake:3 \(add_custom_target\):
Policy CMP0037 is not set: Target names should not be reserved and should
match a validity pattern. Run "cmake --help-policy CMP0037" for policy
details. Use the cmake_policy command to set the policy and suppress this
warning.
The target name "test" is reserved when CTest testing is enabled. It may
result in undefined behavior.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)
This warning is for project developers. Use -Wno-dev to suppress it.$

View File

@@ -0,0 +1,5 @@
enable_testing()
add_custom_target(test)
add_custom_target(package)
add_custom_target(package_source)

View File

@@ -0,0 +1,4 @@
add_custom_target(test)
add_custom_target(package)
add_custom_target(package_source)

View File

@@ -12,6 +12,7 @@
\* CMP0021
\* CMP0022
\* CMP0027
\* CMP0037
\* CMP0038
\* CMP0041
\* CMP0042