瀏覽代碼

export(): raise an error on multiple calls with same FILE

Fixes: 20472
Marc Chevrier 5 年之前
父節點
當前提交
0cd20e8f62

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

@@ -51,6 +51,14 @@ The :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` variable may also be used
 to determine whether to report an error on use of deprecated macros or
 to determine whether to report an error on use of deprecated macros or
 functions.
 functions.
 
 
+Policies Introduced by CMake 3.18
+=================================
+
+.. toctree::
+   :maxdepth: 1
+
+   CMP0103: Multiple export() with same FILE without APPEND is not allowed. </policy/CMP0103>
+
 Policies Introduced by CMake 3.17
 Policies Introduced by CMake 3.17
 =================================
 =================================
 
 

+ 22 - 0
Help/policy/CMP0103.rst

@@ -0,0 +1,22 @@
+CMP0103
+-------
+
+Multiple calls to :command:`export` command with same ``FILE`` without
+``APPEND`` is no longer allowed.
+
+In CMake 3.17 and below, multiple calls to :command:`export` command with the
+same ``FILE`` without ``APPEND`` are accepted silently but only the last
+occurrence is taken into account during the generation.
+
+The ``OLD`` behavior for this policy is to ignore the multiple occurrences of
+ :command:`export` command except the last one.
+
+The ``NEW`` behavior of this policy is to raise an error on second call to
+:command:`export` command with same ``FILE`` without ``APPEND``.
+
+This policy was introduced in CMake version 3.18.  CMake version
+|release| warns when the policy is not set and uses ``OLD`` behavior.
+Use the :command:`cmake_policy` command to set it to ``OLD`` or ``NEW``
+explicitly.
+
+.. include:: DEPRECATED.txt

+ 5 - 0
Help/release/dev/export-multiple-calls.rst

@@ -0,0 +1,5 @@
+export-multiple-calls
+---------------------
+
+* The :command:`export` command now raise an error if used multiple times with
+  same ``FILE`` without ``APPEND``. See policy :policy:`CMP0103`.

+ 23 - 0
Source/cmExportCommand.cxx

@@ -24,6 +24,7 @@
 #include "cmMessageType.h"
 #include "cmMessageType.h"
 #include "cmPolicies.h"
 #include "cmPolicies.h"
 #include "cmStateTypes.h"
 #include "cmStateTypes.h"
+#include "cmStringAlgorithms.h"
 #include "cmSystemTools.h"
 #include "cmSystemTools.h"
 #include "cmTarget.h"
 #include "cmTarget.h"
 
 
@@ -183,6 +184,28 @@ bool cmExportCommand(std::vector<std::string> const& args,
     return false;
     return false;
   }
   }
 
 
+  // if cmExportBuildFileGenerator is already defined for the file
+  // and APPEND is not specified, if CMP0103 is OLD ignore previous definition
+  // else raise an error
+  if (gg->GetExportedTargetsFile(fname) != nullptr) {
+    switch (mf.GetPolicyStatus(cmPolicies::CMP0103)) {
+      case cmPolicies::WARN:
+        mf.IssueMessage(
+          MessageType::AUTHOR_WARNING,
+          cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0103), '\n',
+                   "export() command already specified for the file\n  ",
+                   arguments.Filename, "\nDid you miss 'APPEND' keyword?"));
+        CM_FALLTHROUGH;
+      case cmPolicies::OLD:
+        break;
+      default:
+        status.SetError(cmStrCat("command already specified for the file\n  ",
+                                 arguments.Filename,
+                                 "\nDid you miss 'APPEND' keyword?"));
+        return false;
+    }
+  }
+
   // Setup export file generation.
   // Setup export file generation.
   std::unique_ptr<cmExportBuildFileGenerator> ebfg = nullptr;
   std::unique_ptr<cmExportBuildFileGenerator> ebfg = nullptr;
   if (android) {
   if (android) {

+ 4 - 1
Source/cmPolicies.h

@@ -305,7 +305,10 @@ class cmMakefile;
          17, 0, cmPolicies::WARN)                                             \
          17, 0, cmPolicies::WARN)                                             \
   SELECT(POLICY, CMP0102,                                                     \
   SELECT(POLICY, CMP0102,                                                     \
          "mark_as_advanced() does nothing if a cache entry does not exist.",  \
          "mark_as_advanced() does nothing if a cache entry does not exist.",  \
-         3, 17, 0, cmPolicies::WARN)
+         3, 17, 0, cmPolicies::WARN)                                          \
+  SELECT(POLICY, CMP0103,                                                     \
+         "multiple export() with same FILE without APPEND is not allowed.",   \
+         3, 18, 0, cmPolicies::WARN)
 
 
 #define CM_SELECT_ID(F, A1, A2, A3, A4, A5, A6) F(A1)
 #define CM_SELECT_ID(F, A1, A2, A3, A4, A5, A6) F(A1)
 #define CM_FOR_EACH_POLICY_ID(POLICY)                                         \
 #define CM_FOR_EACH_POLICY_ID(POLICY)                                         \

+ 1 - 0
Tests/RunCMake/export/Repeat-CMP0103-NEW-result.txt

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

+ 17 - 0
Tests/RunCMake/export/Repeat-CMP0103-NEW-stderr.txt

@@ -0,0 +1,17 @@
+CMake Error at Repeat.cmake:[0-9]+ \(export\):
+  export command already specified for the file
+
+    foo.cmake
+
+  Did you miss 'APPEND' keyword\?
+Call Stack \(most recent call first\):
+  Repeat-CMP0103-NEW.cmake:[0-9]+ \(include\)
+  CMakeLists.txt:[0-9]+ \(include\)
+
+
+CMake Error at Repeat/CMakeLists.txt:[0-9]+ \(export\):
+  export command already specified for the file
+
+    .+/foo.cmake
+
+  Did you miss 'APPEND' keyword\?

+ 2 - 0
Tests/RunCMake/export/Repeat-CMP0103-NEW.cmake

@@ -0,0 +1,2 @@
+cmake_policy(SET CMP0103 NEW)
+include(Repeat.cmake)

+ 2 - 0
Tests/RunCMake/export/Repeat-CMP0103-OLD.cmake

@@ -0,0 +1,2 @@
+cmake_policy(SET CMP0103 OLD)
+include(Repeat.cmake)

+ 26 - 0
Tests/RunCMake/export/Repeat-CMP0103-WARN-stderr.txt

@@ -0,0 +1,26 @@
+CMake Warning \(dev\) at Repeat.cmake:[0-9]+ \(export\):
+  Policy CMP0103 is not set: multiple export\(\) with same FILE without APPEND
+  is not allowed.  Run "cmake --help-policy CMP0103" for policy details.  Use
+  the cmake_policy command to set the policy and suppress this warning.
+
+  export\(\) command already specified for the file
+
+    foo.cmake
+
+  Did you miss 'APPEND' keyword\?
+Call Stack \(most recent call first\):
+  Repeat-CMP0103-WARN.cmake:[0-9]+ \(include\)
+  CMakeLists.txt:[0-9]+ \(include\)
+This warning is for project developers.  Use -Wno-dev to suppress it.
+
+CMake Warning \(dev\) at Repeat/CMakeLists.txt:[0-9]+ \(export\):
+  Policy CMP0103 is not set: multiple export\(\) with same FILE without APPEND
+  is not allowed.  Run "cmake --help-policy CMP0103" for policy details.  Use
+  the cmake_policy command to set the policy and suppress this warning.
+
+  export\(\) command already specified for the file
+
+    .+/foo.cmake
+
+  Did you miss 'APPEND' keyword\?
+This warning is for project developers.  Use -Wno-dev to suppress it.

+ 1 - 0
Tests/RunCMake/export/Repeat-CMP0103-WARN.cmake

@@ -0,0 +1 @@
+include(Repeat.cmake)

+ 3 - 1
Tests/RunCMake/export/RunCMakeTest.cmake

@@ -2,7 +2,9 @@ include(RunCMake)
 
 
 run_cmake(CustomTarget)
 run_cmake(CustomTarget)
 run_cmake(Empty)
 run_cmake(Empty)
-run_cmake(Repeat)
+run_cmake(Repeat-CMP0103-WARN)
+run_cmake(Repeat-CMP0103-OLD)
+run_cmake(Repeat-CMP0103-NEW)
 run_cmake(TargetNotFound)
 run_cmake(TargetNotFound)
 run_cmake(AppendExport)
 run_cmake(AppendExport)
 run_cmake(OldIface)
 run_cmake(OldIface)