From a1cc6b4447787b84777fdf9a860e8c39f0f4a090 Mon Sep 17 00:00:00 2001
From: Daniel Eiband <daniel.eiband@brainlab.com>
Date: Mon, 23 Sep 2019 22:18:36 +0200
Subject: [PATCH] add_custom_target: Add output checks for custom target
 byproducts

Use the output checks for byproducts of add_custom_command also for byproducts
of add_custom_target.
---
 Source/CMakeLists.txt                         |  2 ++
 Source/cmAddCustomCommandCommand.cxx          | 36 +++----------------
 Source/cmAddCustomTargetCommand.cxx           |  6 ++++
 Source/cmCheckCustomOutputs.cxx               | 36 +++++++++++++++++++
 Source/cmCheckCustomOutputs.h                 | 18 ++++++++++
 .../BadByproduct-stderr.txt                   | 16 ++++-----
 .../add_custom_target/BadByproduct-result.txt |  1 +
 .../add_custom_target/BadByproduct-stderr.txt | 36 +++++++++++++++++++
 .../add_custom_target/BadByproduct.cmake      |  6 ++++
 .../add_custom_target/RunCMakeTest.cmake      |  7 ++--
 bootstrap                                     |  1 +
 11 files changed, 122 insertions(+), 43 deletions(-)
 create mode 100644 Source/cmCheckCustomOutputs.cxx
 create mode 100644 Source/cmCheckCustomOutputs.h
 create mode 100644 Tests/RunCMake/add_custom_target/BadByproduct-result.txt
 create mode 100644 Tests/RunCMake/add_custom_target/BadByproduct-stderr.txt
 create mode 100644 Tests/RunCMake/add_custom_target/BadByproduct.cmake

diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt
index 8ed7b2f6b3..71a7dbd8c6 100644
--- a/Source/CMakeLists.txt
+++ b/Source/CMakeLists.txt
@@ -168,6 +168,8 @@ set(SRCS
   cmBinUtilsWindowsPEObjdumpGetRuntimeDependenciesTool.h
   cmCacheManager.cxx
   cmCacheManager.h
+  cmCheckCustomOutputs.h
+  cmCheckCustomOutputs.cxx
   cmCLocaleEnvironmentScope.h
   cmCLocaleEnvironmentScope.cxx
   cmCommandArgumentParserHelper.cxx
diff --git a/Source/cmAddCustomCommandCommand.cxx b/Source/cmAddCustomCommandCommand.cxx
index defefafb02..9d665c47ec 100644
--- a/Source/cmAddCustomCommandCommand.cxx
+++ b/Source/cmAddCustomCommandCommand.cxx
@@ -5,6 +5,7 @@
 #include <sstream>
 #include <unordered_set>
 
+#include "cmCheckCustomOutputs.h"
 #include "cmCustomCommand.h"
 #include "cmCustomCommandLines.h"
 #include "cmExecutionStatus.h"
@@ -16,9 +17,6 @@
 #include "cmSystemTools.h"
 #include "cmTarget.h"
 
-static bool cmAddCustomCommandCommandCheckOutputs(
-  const std::vector<std::string>& outputs, cmExecutionStatus& status);
-
 bool cmAddCustomCommandCommand(std::vector<std::string> const& args,
                                cmExecutionStatus& status)
 {
@@ -307,9 +305,9 @@ bool cmAddCustomCommandCommand(std::vector<std::string> const& args,
   }
 
   // Make sure the output names and locations are safe.
-  if (!cmAddCustomCommandCommandCheckOutputs(output, status) ||
-      !cmAddCustomCommandCommandCheckOutputs(outputs, status) ||
-      !cmAddCustomCommandCommandCheckOutputs(byproducts, status)) {
+  if (!cmCheckCustomOutputs(output, "OUTPUT", status) ||
+      !cmCheckCustomOutputs(outputs, "OUTPUTS", status) ||
+      !cmCheckCustomOutputs(byproducts, "BYPRODUCTS", status)) {
     return false;
   }
 
@@ -387,29 +385,3 @@ bool cmAddCustomCommandCommand(std::vector<std::string> const& args,
 
   return true;
 }
-
-bool cmAddCustomCommandCommandCheckOutputs(
-  const std::vector<std::string>& outputs, cmExecutionStatus& status)
-{
-  cmMakefile& mf = status.GetMakefile();
-  for (std::string const& o : outputs) {
-    // Make sure the file will not be generated into the source
-    // directory during an out of source build.
-    if (!mf.CanIWriteThisFile(o)) {
-      std::string e = "attempted to have a file\n\"" + o +
-        "\"\nin a source directory as an output of custom command.";
-      status.SetError(e);
-      cmSystemTools::SetFatalErrorOccured();
-      return false;
-    }
-
-    // Make sure the output file name has no invalid characters.
-    std::string::size_type pos = o.find_first_of("#<>");
-    if (pos != std::string::npos) {
-      status.SetError(cmStrCat("called with OUTPUT containing a \"", o[pos],
-                               "\".  This character is not allowed."));
-      return false;
-    }
-  }
-  return true;
-}
diff --git a/Source/cmAddCustomTargetCommand.cxx b/Source/cmAddCustomTargetCommand.cxx
index 9fd1234145..b580c430f7 100644
--- a/Source/cmAddCustomTargetCommand.cxx
+++ b/Source/cmAddCustomTargetCommand.cxx
@@ -4,6 +4,7 @@
 
 #include <utility>
 
+#include "cmCheckCustomOutputs.h"
 #include "cmCustomCommandLines.h"
 #include "cmExecutionStatus.h"
 #include "cmGeneratorExpression.h"
@@ -205,6 +206,11 @@ bool cmAddCustomTargetCommand(std::vector<std::string> const& args,
     return false;
   }
 
+  // Make sure the byproduct names and locations are safe.
+  if (!cmCheckCustomOutputs(byproducts, "BYPRODUCTS", status)) {
+    return false;
+  }
+
   // Add the utility target to the makefile.
   bool escapeOldStyle = !verbatim;
   cmTarget* target = mf.AddUtilityCommand(
diff --git a/Source/cmCheckCustomOutputs.cxx b/Source/cmCheckCustomOutputs.cxx
new file mode 100644
index 0000000000..a4017389fa
--- /dev/null
+++ b/Source/cmCheckCustomOutputs.cxx
@@ -0,0 +1,36 @@
+/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
+   file Copyright.txt or https://cmake.org/licensing for details.  */
+#include "cmCheckCustomOutputs.h"
+
+#include "cmExecutionStatus.h"
+#include "cmMakefile.h"
+#include "cmStringAlgorithms.h"
+#include "cmSystemTools.h"
+
+bool cmCheckCustomOutputs(const std::vector<std::string>& outputs,
+                          cm::string_view keyword, cmExecutionStatus& status)
+{
+  cmMakefile& mf = status.GetMakefile();
+
+  for (std::string const& o : outputs) {
+    // Make sure the file will not be generated into the source
+    // directory during an out of source build.
+    if (!mf.CanIWriteThisFile(o)) {
+      status.SetError(
+        cmStrCat("attempted to have a file\n\"", o,
+                 "\"\nin a source directory as an output of custom command."));
+      cmSystemTools::SetFatalErrorOccured();
+      return false;
+    }
+
+    // Make sure the output file name has no invalid characters.
+    std::string::size_type pos = o.find_first_of("#<>");
+    if (pos != std::string::npos) {
+      status.SetError(cmStrCat("called with ", keyword, " containing a \"",
+                               o[pos], "\".  This character is not allowed."));
+      return false;
+    }
+  }
+
+  return true;
+}
diff --git a/Source/cmCheckCustomOutputs.h b/Source/cmCheckCustomOutputs.h
new file mode 100644
index 0000000000..7c4b3fecc2
--- /dev/null
+++ b/Source/cmCheckCustomOutputs.h
@@ -0,0 +1,18 @@
+/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
+   file Copyright.txt or https://cmake.org/licensing for details.  */
+#ifndef cmCheckCustomOutputs_h
+#define cmCheckCustomOutputs_h
+
+#include "cmConfigure.h" // IWYU pragma: keep
+
+#include <cm/string_view>
+
+#include <string>
+#include <vector>
+
+class cmExecutionStatus;
+
+bool cmCheckCustomOutputs(const std::vector<std::string>& outputs,
+                          cm::string_view keyword, cmExecutionStatus& status);
+
+#endif
diff --git a/Tests/RunCMake/add_custom_command/BadByproduct-stderr.txt b/Tests/RunCMake/add_custom_command/BadByproduct-stderr.txt
index 6315c8bd9e..97b9ccacf0 100644
--- a/Tests/RunCMake/add_custom_command/BadByproduct-stderr.txt
+++ b/Tests/RunCMake/add_custom_command/BadByproduct-stderr.txt
@@ -1,27 +1,27 @@
 CMake Error at BadByproduct.cmake:2 \(add_custom_command\):
-  add_custom_command called with OUTPUT containing a "#".  This character is
-  not allowed.
+  add_custom_command called with BYPRODUCTS containing a "#".  This character
+  is not allowed.
 Call Stack \(most recent call first\):
   CMakeLists.txt:3 \(include\)
 
 
 CMake Error at BadByproduct.cmake:3 \(add_custom_command\):
-  add_custom_command called with OUTPUT containing a "<".  This character is
-  not allowed.
+  add_custom_command called with BYPRODUCTS containing a "<".  This character
+  is not allowed.
 Call Stack \(most recent call first\):
   CMakeLists.txt:3 \(include\)
 
 
 CMake Error at BadByproduct.cmake:4 \(add_custom_command\):
-  add_custom_command called with OUTPUT containing a ">".  This character is
-  not allowed.
+  add_custom_command called with BYPRODUCTS containing a ">".  This character
+  is not allowed.
 Call Stack \(most recent call first\):
   CMakeLists.txt:3 \(include\)
 
 
 CMake Error at BadByproduct.cmake:5 \(add_custom_command\):
-  add_custom_command called with OUTPUT containing a "<".  This character is
-  not allowed.
+  add_custom_command called with BYPRODUCTS containing a "<".  This character
+  is not allowed.
 Call Stack \(most recent call first\):
   CMakeLists.txt:3 \(include\)
 
diff --git a/Tests/RunCMake/add_custom_target/BadByproduct-result.txt b/Tests/RunCMake/add_custom_target/BadByproduct-result.txt
new file mode 100644
index 0000000000..d00491fd7e
--- /dev/null
+++ b/Tests/RunCMake/add_custom_target/BadByproduct-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/add_custom_target/BadByproduct-stderr.txt b/Tests/RunCMake/add_custom_target/BadByproduct-stderr.txt
new file mode 100644
index 0000000000..7390e6a4fb
--- /dev/null
+++ b/Tests/RunCMake/add_custom_target/BadByproduct-stderr.txt
@@ -0,0 +1,36 @@
+CMake Error at BadByproduct.cmake:2 \(add_custom_target\):
+  add_custom_target called with BYPRODUCTS containing a "#".  This character
+  is not allowed.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)
+
+
+CMake Error at BadByproduct.cmake:3 \(add_custom_target\):
+  add_custom_target called with BYPRODUCTS containing a "<".  This character
+  is not allowed.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)
+
+
+CMake Error at BadByproduct.cmake:4 \(add_custom_target\):
+  add_custom_target called with BYPRODUCTS containing a ">".  This character
+  is not allowed.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)
+
+
+CMake Error at BadByproduct.cmake:5 \(add_custom_target\):
+  add_custom_target called with BYPRODUCTS containing a "<".  This character
+  is not allowed.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)
+
+
+CMake Error at BadByproduct.cmake:6 \(add_custom_target\):
+  add_custom_target attempted to have a file
+
+  .*/j".*
+
+  in a source directory as an output of custom command.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)
diff --git a/Tests/RunCMake/add_custom_target/BadByproduct.cmake b/Tests/RunCMake/add_custom_target/BadByproduct.cmake
new file mode 100644
index 0000000000..963d6416b2
--- /dev/null
+++ b/Tests/RunCMake/add_custom_target/BadByproduct.cmake
@@ -0,0 +1,6 @@
+set(CMAKE_DISABLE_SOURCE_CHANGES ON)
+add_custom_target(a BYPRODUCTS "a#" COMMAND b)
+add_custom_target(c BYPRODUCTS "a<" COMMAND d)
+add_custom_target(e BYPRODUCTS "a>" COMMAND f)
+add_custom_target(g BYPRODUCTS "$<CONFIG>/#" COMMAND h)
+add_custom_target(i BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/j COMMAND k)
diff --git a/Tests/RunCMake/add_custom_target/RunCMakeTest.cmake b/Tests/RunCMake/add_custom_target/RunCMakeTest.cmake
index 49c7d3ea72..f5d5dd2b5e 100644
--- a/Tests/RunCMake/add_custom_target/RunCMakeTest.cmake
+++ b/Tests/RunCMake/add_custom_target/RunCMakeTest.cmake
@@ -1,11 +1,12 @@
 include(RunCMake)
 
-run_cmake(CommandExpandsEmpty)
-run_cmake(GeneratedProperty)
-run_cmake(NoArguments)
+run_cmake(BadByproduct)
 run_cmake(BadTargetName)
 run_cmake(ByproductsNoCommand)
+run_cmake(CommandExpandsEmpty)
+run_cmake(GeneratedProperty)
 run_cmake(LiteralQuotes)
+run_cmake(NoArguments)
 run_cmake(UsesTerminalNoCommand)
 
 function(run_TargetOrder)
diff --git a/bootstrap b/bootstrap
index 0923dd45af..42f869b208 100755
--- a/bootstrap
+++ b/bootstrap
@@ -278,6 +278,7 @@ CMAKE_CXX_SOURCES="\
   cmCMakePolicyCommand \
   cmCPackPropertiesGenerator \
   cmCacheManager \
+  cmCheckCustomOutputs \
   cmCommand \
   cmCommandArgumentParserHelper \
   cmCommands \
-- 
GitLab