Browse Source

Disallow linking to utility targets (#13902).

Stephen Kelly 12 years ago
parent
commit
596b2a8c08

+ 1 - 0
Help/manual/cmake-policies.7.rst

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

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

+ 5 - 0
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()

+ 1 - 0
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.
      *

+ 31 - 0
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)
     {

+ 1 - 0
Tests/RunCMake/CMP0039/CMP0039-NEW-result.txt

@@ -0,0 +1 @@
+1

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

+ 7 - 0
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)

+ 1 - 0
Tests/RunCMake/CMP0039/CMP0039-OLD-result.txt

@@ -0,0 +1 @@
+0

+ 1 - 0
Tests/RunCMake/CMP0039/CMP0039-OLD-stderr.txt

@@ -0,0 +1 @@
+^$

+ 7 - 0
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)

+ 1 - 0
Tests/RunCMake/CMP0039/CMP0039-WARN-result.txt

@@ -0,0 +1 @@
+0

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

+ 5 - 0
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)

+ 3 - 0
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)

+ 5 - 0
Tests/RunCMake/CMP0039/RunCMakeTest.cmake

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

+ 1 - 0
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)