From 596b2a8c0820bed8195e2377927a18cf2d76727c Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Tue, 5 Nov 2013 19:15:53 +0100 Subject: Disallow linking to utility targets (#13902). --- Help/manual/cmake-policies.7.rst | 1 + Help/policy/CMP0039.rst | 17 ++++++++++++++ Source/cmPolicies.cxx | 5 +++++ Source/cmPolicies.h | 1 + Source/cmTargetLinkLibrariesCommand.cxx | 31 ++++++++++++++++++++++++++ Tests/RunCMake/CMP0039/CMP0039-NEW-result.txt | 1 + Tests/RunCMake/CMP0039/CMP0039-NEW-stderr.txt | 9 ++++++++ Tests/RunCMake/CMP0039/CMP0039-NEW.cmake | 7 ++++++ Tests/RunCMake/CMP0039/CMP0039-OLD-result.txt | 1 + Tests/RunCMake/CMP0039/CMP0039-OLD-stderr.txt | 1 + Tests/RunCMake/CMP0039/CMP0039-OLD.cmake | 7 ++++++ Tests/RunCMake/CMP0039/CMP0039-WARN-result.txt | 1 + Tests/RunCMake/CMP0039/CMP0039-WARN-stderr.txt | 10 +++++++++ Tests/RunCMake/CMP0039/CMP0039-WARN.cmake | 5 +++++ Tests/RunCMake/CMP0039/CMakeLists.txt | 3 +++ Tests/RunCMake/CMP0039/RunCMakeTest.cmake | 5 +++++ Tests/RunCMake/CMakeLists.txt | 1 + 17 files changed, 106 insertions(+) create mode 100644 Help/policy/CMP0039.rst create mode 100644 Tests/RunCMake/CMP0039/CMP0039-NEW-result.txt create mode 100644 Tests/RunCMake/CMP0039/CMP0039-NEW-stderr.txt create mode 100644 Tests/RunCMake/CMP0039/CMP0039-NEW.cmake create mode 100644 Tests/RunCMake/CMP0039/CMP0039-OLD-result.txt create mode 100644 Tests/RunCMake/CMP0039/CMP0039-OLD-stderr.txt create mode 100644 Tests/RunCMake/CMP0039/CMP0039-OLD.cmake create mode 100644 Tests/RunCMake/CMP0039/CMP0039-WARN-result.txt create mode 100644 Tests/RunCMake/CMP0039/CMP0039-WARN-stderr.txt create mode 100644 Tests/RunCMake/CMP0039/CMP0039-WARN.cmake create mode 100644 Tests/RunCMake/CMP0039/CMakeLists.txt create mode 100644 Tests/RunCMake/CMP0039/RunCMakeTest.cmake diff --git a/Help/manual/cmake-policies.7.rst b/Help/manual/cmake-policies.7.rst index f77a21c..2430ee9 100644 --- a/Help/manual/cmake-policies.7.rst +++ b/Help/manual/cmake-policies.7.rst @@ -72,3 +72,4 @@ All Policies /policy/CMP0036 /policy/CMP0037 /policy/CMP0038 + /policy/CMP0039 diff --git a/Help/policy/CMP0039.rst b/Help/policy/CMP0039.rst new file mode 100644 index 0000000..1d20f0c --- /dev/null +++ b/Help/policy/CMP0039.rst @@ -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. diff --git a/Source/cmPolicies.cxx b/Source/cmPolicies.cxx index af33d0b..3881c54 100644 --- a/Source/cmPolicies.cxx +++ b/Source/cmPolicies.cxx @@ -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() diff --git a/Source/cmPolicies.h b/Source/cmPolicies.h index 2ea9973..fc239d4 100644 --- a/Source/cmPolicies.h +++ b/Source/cmPolicies.h @@ -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. * diff --git a/Source/cmTargetLinkLibrariesCommand.cxx b/Source/cmTargetLinkLibrariesCommand.cxx index c289459..209609d 100644 --- a/Source/cmTargetLinkLibrariesCommand.cxx +++ b/Source/cmTargetLinkLibrariesCommand.cxx @@ -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) { diff --git a/Tests/RunCMake/CMP0039/CMP0039-NEW-result.txt b/Tests/RunCMake/CMP0039/CMP0039-NEW-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/CMP0039/CMP0039-NEW-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CMP0039/CMP0039-NEW-stderr.txt b/Tests/RunCMake/CMP0039/CMP0039-NEW-stderr.txt new file mode 100644 index 0000000..1496c05 --- /dev/null +++ b/Tests/RunCMake/CMP0039/CMP0039-NEW-stderr.txt @@ -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\) diff --git a/Tests/RunCMake/CMP0039/CMP0039-NEW.cmake b/Tests/RunCMake/CMP0039/CMP0039-NEW.cmake new file mode 100644 index 0000000..2032d64 --- /dev/null +++ b/Tests/RunCMake/CMP0039/CMP0039-NEW.cmake @@ -0,0 +1,7 @@ + +cmake_policy(SET CMP0039 NEW) + +add_custom_target(utility + COMMAND ${CMAKE_COMMAND} -E echo test +) +target_link_libraries(utility m) diff --git a/Tests/RunCMake/CMP0039/CMP0039-OLD-result.txt b/Tests/RunCMake/CMP0039/CMP0039-OLD-result.txt new file mode 100644 index 0000000..573541a --- /dev/null +++ b/Tests/RunCMake/CMP0039/CMP0039-OLD-result.txt @@ -0,0 +1 @@ +0 diff --git a/Tests/RunCMake/CMP0039/CMP0039-OLD-stderr.txt b/Tests/RunCMake/CMP0039/CMP0039-OLD-stderr.txt new file mode 100644 index 0000000..10f3293 --- /dev/null +++ b/Tests/RunCMake/CMP0039/CMP0039-OLD-stderr.txt @@ -0,0 +1 @@ +^$ diff --git a/Tests/RunCMake/CMP0039/CMP0039-OLD.cmake b/Tests/RunCMake/CMP0039/CMP0039-OLD.cmake new file mode 100644 index 0000000..9a513f4 --- /dev/null +++ b/Tests/RunCMake/CMP0039/CMP0039-OLD.cmake @@ -0,0 +1,7 @@ + +cmake_policy(SET CMP0039 OLD) + +add_custom_target(utility + COMMAND ${CMAKE_COMMAND} -E echo test +) +target_link_libraries(utility m) diff --git a/Tests/RunCMake/CMP0039/CMP0039-WARN-result.txt b/Tests/RunCMake/CMP0039/CMP0039-WARN-result.txt new file mode 100644 index 0000000..573541a --- /dev/null +++ b/Tests/RunCMake/CMP0039/CMP0039-WARN-result.txt @@ -0,0 +1 @@ +0 diff --git a/Tests/RunCMake/CMP0039/CMP0039-WARN-stderr.txt b/Tests/RunCMake/CMP0039/CMP0039-WARN-stderr.txt new file mode 100644 index 0000000..9387f8c --- /dev/null +++ b/Tests/RunCMake/CMP0039/CMP0039-WARN-stderr.txt @@ -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. diff --git a/Tests/RunCMake/CMP0039/CMP0039-WARN.cmake b/Tests/RunCMake/CMP0039/CMP0039-WARN.cmake new file mode 100644 index 0000000..6249993 --- /dev/null +++ b/Tests/RunCMake/CMP0039/CMP0039-WARN.cmake @@ -0,0 +1,5 @@ + +add_custom_target(utility + COMMAND ${CMAKE_COMMAND} -E echo test +) +target_link_libraries(utility m) diff --git a/Tests/RunCMake/CMP0039/CMakeLists.txt b/Tests/RunCMake/CMP0039/CMakeLists.txt new file mode 100644 index 0000000..2f10cb0 --- /dev/null +++ b/Tests/RunCMake/CMP0039/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 2.8.12) +project(${RunCMake_TEST} CXX) +include(${RunCMake_TEST}.cmake NO_POLICY_SCOPE) diff --git a/Tests/RunCMake/CMP0039/RunCMakeTest.cmake b/Tests/RunCMake/CMP0039/RunCMakeTest.cmake new file mode 100644 index 0000000..58e8ea9 --- /dev/null +++ b/Tests/RunCMake/CMP0039/RunCMakeTest.cmake @@ -0,0 +1,5 @@ +include(RunCMake) + +run_cmake(CMP0039-WARN) +run_cmake(CMP0039-NEW) +run_cmake(CMP0039-OLD) diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 5c4ac7d..6e72e70 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -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) -- cgit v0.12