Просмотр исходного кода

Add policy CMP0049 to avoid variable expansion in source lists

Stephen Kelly 12 лет назад
Родитель
Сommit
c248a437c4

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

@@ -100,3 +100,4 @@ All Policies
    /policy/CMP0046
    /policy/CMP0047
    /policy/CMP0048
+   /policy/CMP0049

+ 23 - 0
Help/policy/CMP0049.rst

@@ -0,0 +1,23 @@
+CMP0049
+-------
+
+Do not expand variables in target source entries.
+
+CMake 2.8.12 and lower performed and extra layer of variable expansion
+when evaluating source file names:
+
+.. code-block:: cmake
+
+  set(a_source foo.c)
+  add_executable(foo \${a_source})
+
+This was undocumented behavior.
+
+The OLD behavior for this policy is to expand such variables when processing
+the target sources.  The NEW behavior for this policy is to issue an error
+if such variables need to be expanded.
+
+This policy was introduced in CMake version 3.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
Help/release/3.0.rst

@@ -322,6 +322,11 @@ New Diagnostics
   messages up front and stops processing when no working compiler
   is known to be available.
 
+* Target sources specified with the :command:`add_library` or
+  :command:`add_executable` command learned to reject items which
+  require an undocumented extra layer of variable expansion.
+  See policy :policy:`CMP0049`.
+
 Deprecated and Removed Features
 ===============================
 

+ 5 - 0
Source/cmPolicies.cxx

@@ -333,6 +333,11 @@ cmPolicies::cmPolicies()
     CMP0048, "CMP0048",
     "project() command manages VERSION variables.",
     3,0,0, cmPolicies::WARN);
+
+  this->DefinePolicy(
+    CMP0049, "CMP0049",
+    "Do not expand variables in target source entries.",
+    3,0,0, cmPolicies::WARN);
 }
 
 cmPolicies::~cmPolicies()

+ 1 - 0
Source/cmPolicies.h

@@ -102,6 +102,7 @@ public:
     CMP0046, ///< Error on non-existent dependency in add_dependencies
     CMP0047, ///< Use QCC compiler id for the qcc drivers on QNX.
     CMP0048, ///< project() command manages VERSION variables
+    CMP0049, ///< Do not expand variables in target source entries
 
     /** \brief Always the last entry.
      *

+ 32 - 0
Source/cmTarget.cxx

@@ -590,6 +590,38 @@ cmSourceFile* cmTarget::AddSource(const char* s)
   // For backwards compatibility replace varibles in source names.
   // This should eventually be removed.
   this->Makefile->ExpandVariablesInString(src);
+  if (src != s)
+    {
+    cmOStringStream e;
+    bool noMessage = false;
+    cmake::MessageType messageType = cmake::AUTHOR_WARNING;
+    switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0049))
+      {
+      case cmPolicies::WARN:
+        e << (this->Makefile->GetPolicies()
+              ->GetPolicyWarning(cmPolicies::CMP0049)) << "\n";
+        break;
+      case cmPolicies::OLD:
+        noMessage = true;
+        break;
+      case cmPolicies::REQUIRED_ALWAYS:
+      case cmPolicies::REQUIRED_IF_USED:
+      case cmPolicies::NEW:
+        messageType = cmake::FATAL_ERROR;
+      }
+    if (!noMessage)
+      {
+      e << "Legacy variable expansion in source file \""
+        << s << "\" expanded to \"" << src << "\" in target \""
+        << this->GetName() << "\".  This behavior will be removed in a "
+        "future version of CMake.";
+      this->Makefile->IssueMessage(messageType, e.str().c_str());
+      if (messageType == cmake::FATAL_ERROR)
+        {
+        return 0;
+        }
+      }
+    }
 
   cmSourceFile* sf = this->Makefile->GetOrCreateSource(src.c_str());
   this->AddSourceFile(sf);

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

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

+ 6 - 0
Tests/RunCMake/CMP0049/CMP0049-NEW-stderr.txt

@@ -0,0 +1,6 @@
+CMake Error at CMP0049-NEW.cmake:5 \(add_library\):
+  Legacy variable expansion in source file "\${tgt_srcs}" expanded to
+  "empty.cpp" in target "tgt".  This behavior will be removed in a future
+  version of CMake.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)

+ 5 - 0
Tests/RunCMake/CMP0049/CMP0049-NEW.cmake

@@ -0,0 +1,5 @@
+
+cmake_policy(SET CMP0049 NEW)
+
+set(tgt_srcs empty.cpp)
+add_library(tgt \${tgt_srcs})

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

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

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

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

+ 5 - 0
Tests/RunCMake/CMP0049/CMP0049-OLD.cmake

@@ -0,0 +1,5 @@
+
+cmake_policy(SET CMP0049 OLD)
+
+set(tgt_srcs empty.cpp)
+add_library(tgt \${tgt_srcs})

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

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

+ 11 - 0
Tests/RunCMake/CMP0049/CMP0049-WARN-stderr.txt

@@ -0,0 +1,11 @@
+CMake Warning \(dev\) at CMP0049-WARN.cmake:3 \(add_library\):
+  Policy CMP0049 is not set: Do not expand variables in target source
+  entries.  Run "cmake --help-policy CMP0049" for policy details.  Use the
+  cmake_policy command to set the policy and suppress this warning.
+
+  Legacy variable expansion in source file "\${tgt_srcs}" expanded to
+  "empty.cpp" in target "tgt".  This behavior will be removed in a future
+  version of CMake.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)
+This warning is for project developers.  Use -Wno-dev to suppress it.

+ 3 - 0
Tests/RunCMake/CMP0049/CMP0049-WARN.cmake

@@ -0,0 +1,3 @@
+
+set(tgt_srcs empty.cpp)
+add_library(tgt \${tgt_srcs})

+ 3 - 0
Tests/RunCMake/CMP0049/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/CMP0049/RunCMakeTest.cmake

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

+ 7 - 0
Tests/RunCMake/CMP0049/empty.cpp

@@ -0,0 +1,7 @@
+#ifdef _WIN32
+__declspec(dllexport)
+#endif
+int empty()
+{
+  return 0;
+}

+ 1 - 0
Tests/RunCMake/CMakeLists.txt

@@ -32,6 +32,7 @@ endif()
 add_RunCMake_test(CMP0043)
 add_RunCMake_test(CMP0045)
 add_RunCMake_test(CMP0046)
+add_RunCMake_test(CMP0049)
 add_RunCMake_test(CTest)
 if(UNIX AND "${CMAKE_TEST_GENERATOR}" MATCHES "Unix Makefiles")
   add_RunCMake_test(CompilerChange)