Disallow linking to utility targets (#13902).

This commit is contained in:
Stephen Kelly 2013-11-05 19:15:53 +01:00
parent 301bb5cdda
commit 596b2a8c08
17 changed files with 106 additions and 0 deletions

View File

@ -72,3 +72,4 @@ All Policies
/policy/CMP0036 /policy/CMP0036
/policy/CMP0037 /policy/CMP0037
/policy/CMP0038 /policy/CMP0038
/policy/CMP0039

17
Help/policy/CMP0039.rst Normal file
View File

@ -0,0 +1,17 @@
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.

View File

@ -296,6 +296,11 @@ cmPolicies::cmPolicies()
CMP0038, "CMP0038", CMP0038, "CMP0038",
"Targets may not link directly to themselves.", "Targets may not link directly to themselves.",
3,0,0,0, cmPolicies::WARN); 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() cmPolicies::~cmPolicies()

View File

@ -90,6 +90,7 @@ public:
CMP0036, ///< Disallow command: build_name CMP0036, ///< Disallow command: build_name
CMP0037, ///< Target names should match a validity pattern. CMP0037, ///< Target names should match a validity pattern.
CMP0038, ///< Targets may not link directly to themselves CMP0038, ///< Targets may not link directly to themselves
CMP0039, ///< Utility targets may not have link dependencies
/** \brief Always the last entry. /** \brief Always the last entry.
* *

View File

@ -101,6 +101,37 @@ bool cmTargetLinkLibrariesCommand
return true; 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 // but we might not have any libs after variable expansion
if(args.size() < 2) if(args.size() < 2)
{ {

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,9 @@
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\)

View File

@ -0,0 +1,7 @@
cmake_policy(SET CMP0039 NEW)
add_custom_target(utility
COMMAND ${CMAKE_COMMAND} -E echo test
)
target_link_libraries(utility m)

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1 @@
^$

View File

@ -0,0 +1,7 @@
cmake_policy(SET CMP0039 OLD)
add_custom_target(utility
COMMAND ${CMAKE_COMMAND} -E echo test
)
target_link_libraries(utility m)

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1,10 @@
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.

View File

@ -0,0 +1,5 @@
add_custom_target(utility
COMMAND ${CMAKE_COMMAND} -E echo test
)
target_link_libraries(utility m)

View File

@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 2.8.12)
project(${RunCMake_TEST} CXX)
include(${RunCMake_TEST}.cmake NO_POLICY_SCOPE)

View File

@ -0,0 +1,5 @@
include(RunCMake)
run_cmake(CMP0039-WARN)
run_cmake(CMP0039-NEW)
run_cmake(CMP0039-OLD)

View File

@ -60,6 +60,7 @@ if (NOT "${CMAKE_TEST_GENERATOR}" MATCHES "(MSYS|MinGW|NMake|Borland) Makefiles"
add_RunCMake_test(CMP0037) add_RunCMake_test(CMP0037)
endif() endif()
add_RunCMake_test(CMP0038) add_RunCMake_test(CMP0038)
add_RunCMake_test(CMP0039)
add_RunCMake_test(CTest) add_RunCMake_test(CTest)
if(UNIX AND "${CMAKE_TEST_GENERATOR}" MATCHES "Unix Makefiles") if(UNIX AND "${CMAKE_TEST_GENERATOR}" MATCHES "Unix Makefiles")
add_RunCMake_test(CompilerChange) add_RunCMake_test(CompilerChange)