Commit 596b2a8c authored by Stephen Kelly's avatar Stephen Kelly
Browse files

Disallow linking to utility targets (#13902).

parent 301bb5cd
......@@ -72,3 +72,4 @@ All Policies
/policy/CMP0036
/policy/CMP0037
/policy/CMP0038
/policy/CMP0039
CMP0039
-------
Utility targets may not have link dependencies
CMake 2.8.12 and lower allowed using utility targets in the left hand side
position of the :command:`target_link_libraries` command. This is an indicator
of a bug in user code.
The OLD behavior for this policy is to ignore attempts to set the link
libraries of utility targets. The NEW behavior for this policy is to
report an error if an attempt is made to set the link libraries of a
utility target.
This policy was introduced in CMake version 3.0.0. CMake version
|release| warns when the policy is not set and uses OLD behavior. Use
the cmake_policy command to set it to OLD or NEW explicitly.
......@@ -296,6 +296,11 @@ cmPolicies::cmPolicies()
CMP0038, "CMP0038",
"Targets may not link directly to themselves.",
3,0,0,0, cmPolicies::WARN);
this->DefinePolicy(
CMP0039, "CMP0039",
"Utility targets may not have link dependencies",
3,0,0,0, cmPolicies::WARN);
}
cmPolicies::~cmPolicies()
......
......@@ -90,6 +90,7 @@ public:
CMP0036, ///< Disallow command: build_name
CMP0037, ///< Target names should match a validity pattern.
CMP0038, ///< Targets may not link directly to themselves
CMP0039, ///< Utility targets may not have link dependencies
/** \brief Always the last entry.
*
......
......@@ -101,6 +101,37 @@ bool cmTargetLinkLibrariesCommand
return true;
}
if (this->Target->GetType() == cmTarget::UTILITY)
{
const char *modal = 0;
cmake::MessageType messageType = cmake::AUTHOR_WARNING;
switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0039))
{
case cmPolicies::WARN:
modal = "should";
case cmPolicies::OLD:
break;
case cmPolicies::REQUIRED_ALWAYS:
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::NEW:
modal = "must";
messageType = cmake::FATAL_ERROR;
}
if (modal)
{
cmOStringStream e;
e << this->Makefile->GetPolicies()
->GetPolicyWarning(cmPolicies::CMP0039) << "\n"
"Utility target \"" << this->Target->GetName() << "\" " << modal
<< " not be used as the target of a target_link_libraries call.";
this->Makefile->IssueMessage(messageType, e.str().c_str());
if(messageType == cmake::FATAL_ERROR)
{
return false;
}
}
}
// but we might not have any libs after variable expansion
if(args.size() < 2)
{
......
CMake Error at CMP0039-NEW.cmake:7 \(target_link_libraries\):
Policy CMP0039 is not set: Utility targets may not have link dependencies
Run "cmake --help-policy CMP0039" for policy details. Use the cmake_policy
command to set the policy and suppress this warning.
Utility target "utility" must not be used as the target of a
target_link_libraries call.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)
cmake_policy(SET CMP0039 NEW)
add_custom_target(utility
COMMAND ${CMAKE_COMMAND} -E echo test
)
target_link_libraries(utility m)
cmake_policy(SET CMP0039 OLD)
add_custom_target(utility
COMMAND ${CMAKE_COMMAND} -E echo test
)
target_link_libraries(utility m)
CMake Warning \(dev\) at CMP0039-WARN.cmake:5 \(target_link_libraries\):
Policy CMP0039 is not set: Utility targets may not have link dependencies
Run "cmake --help-policy CMP0039" for policy details. Use the cmake_policy
command to set the policy and suppress this warning.
Utility target "utility" should not be used as the target of a
target_link_libraries call.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)
This warning is for project developers. Use -Wno-dev to suppress it.
add_custom_target(utility
COMMAND ${CMAKE_COMMAND} -E echo test
)
target_link_libraries(utility m)
cmake_minimum_required(VERSION 2.8.12)
project(${RunCMake_TEST} CXX)
include(${RunCMake_TEST}.cmake NO_POLICY_SCOPE)
include(RunCMake)
run_cmake(CMP0039-WARN)
run_cmake(CMP0039-NEW)
run_cmake(CMP0039-OLD)
......@@ -60,6 +60,7 @@ if (NOT "${CMAKE_TEST_GENERATOR}" MATCHES "(MSYS|MinGW|NMake|Borland) Makefiles"
add_RunCMake_test(CMP0037)
endif()
add_RunCMake_test(CMP0038)
add_RunCMake_test(CMP0039)
add_RunCMake_test(CTest)
if(UNIX AND "${CMAKE_TEST_GENERATOR}" MATCHES "Unix Makefiles")
add_RunCMake_test(CompilerChange)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment