From d257d68138a00758910bbca3dd77dcfcc04e65cc Mon Sep 17 00:00:00 2001
From: Brad King <brad.king@kitware.com>
Date: Thu, 28 Jan 2016 10:12:26 -0500
Subject: [PATCH] add_custom_command: Clarify error when TARGET is out of scope
 (#15681)

The add_custom_command(TARGET) signature only works for targets defined
in the current directory.  Clarify this in the error message when the
target exists but was defined elsewhere.

Inspired-by: Bartosz Kosiorek <gang65@poczta.onet.pl>
---
 Source/cmMakefile.cxx                         | 19 ++++++++++++++++++-
 .../CMP0040-NEW-missing-target-stderr.txt     |  2 +-
 .../CMP0040-WARN-missing-target-stderr.txt    |  2 +-
 .../add_custom_command/RunCMakeTest.cmake     |  2 ++
 .../TargetImported-result.txt                 |  1 +
 .../TargetImported-stderr.txt                 |  4 ++++
 .../add_custom_command/TargetImported.cmake   |  2 ++
 .../TargetNotInDir-result.txt                 |  1 +
 .../TargetNotInDir-stderr.txt                 |  4 ++++
 .../add_custom_command/TargetNotInDir.cmake   |  2 ++
 .../TargetNotInDir/CMakeLists.txt             |  1 +
 11 files changed, 37 insertions(+), 3 deletions(-)
 create mode 100644 Tests/RunCMake/add_custom_command/TargetImported-result.txt
 create mode 100644 Tests/RunCMake/add_custom_command/TargetImported-stderr.txt
 create mode 100644 Tests/RunCMake/add_custom_command/TargetImported.cmake
 create mode 100644 Tests/RunCMake/add_custom_command/TargetNotInDir-result.txt
 create mode 100644 Tests/RunCMake/add_custom_command/TargetNotInDir-stderr.txt
 create mode 100644 Tests/RunCMake/add_custom_command/TargetNotInDir.cmake
 create mode 100644 Tests/RunCMake/add_custom_command/TargetNotInDir/CMakeLists.txt

diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx
index ba0d67234c..cba29eb045 100644
--- a/Source/cmMakefile.cxx
+++ b/Source/cmMakefile.cxx
@@ -791,7 +791,24 @@ cmMakefile::AddCustomCommandToTarget(const std::string& target,
 
     if(issueMessage)
       {
-      e << "The target name \"" << target << "\" is unknown in this context.";
+      if (cmTarget const* t = this->FindTargetToUse(target))
+        {
+        if (t->IsImported())
+          {
+          e << "TARGET '" << target
+            << "' is IMPORTED and does not build here.";
+          }
+        else
+          {
+          e << "TARGET '" << target
+            << "' was not created in this directory.";
+          }
+        }
+      else
+        {
+        e << "No TARGET '" << target
+          << "' has been created in this directory.";
+        }
       IssueMessage(messageType, e.str());
       }
 
diff --git a/Tests/RunCMake/CMP0040/CMP0040-NEW-missing-target-stderr.txt b/Tests/RunCMake/CMP0040/CMP0040-NEW-missing-target-stderr.txt
index 3f82d8c3d2..4a1077c7db 100644
--- a/Tests/RunCMake/CMP0040/CMP0040-NEW-missing-target-stderr.txt
+++ b/Tests/RunCMake/CMP0040/CMP0040-NEW-missing-target-stderr.txt
@@ -1,4 +1,4 @@
 CMake Error at CMP0040-NEW-missing-target.cmake:3 \(add_custom_command\):
-  The target name "foobar" is unknown in this context.
+  No TARGET 'foobar' has been created in this directory.
 Call Stack \(most recent call first\):
   CMakeLists.txt:3 \(include\)
diff --git a/Tests/RunCMake/CMP0040/CMP0040-WARN-missing-target-stderr.txt b/Tests/RunCMake/CMP0040/CMP0040-WARN-missing-target-stderr.txt
index e791f0a727..e3e3ff4687 100644
--- a/Tests/RunCMake/CMP0040/CMP0040-WARN-missing-target-stderr.txt
+++ b/Tests/RunCMake/CMP0040/CMP0040-WARN-missing-target-stderr.txt
@@ -4,7 +4,7 @@ CMake Warning \(dev\) at CMP0040-WARN-missing-target.cmake:2 \(add_custom_comman
   policy details.  Use the cmake_policy command to set the policy and
   suppress this warning.
 +
-  The target name "foobar" is unknown in this context.
+  No TARGET 'foobar' has been created in this directory.
 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/add_custom_command/RunCMakeTest.cmake b/Tests/RunCMake/add_custom_command/RunCMakeTest.cmake
index 2f5c938afb..397c63d0a8 100644
--- a/Tests/RunCMake/add_custom_command/RunCMakeTest.cmake
+++ b/Tests/RunCMake/add_custom_command/RunCMakeTest.cmake
@@ -8,3 +8,5 @@ run_cmake(NoOutputOrTarget)
 run_cmake(OutputAndTarget)
 run_cmake(SourceByproducts)
 run_cmake(SourceUsesTerminal)
+run_cmake(TargetImported)
+run_cmake(TargetNotInDir)
diff --git a/Tests/RunCMake/add_custom_command/TargetImported-result.txt b/Tests/RunCMake/add_custom_command/TargetImported-result.txt
new file mode 100644
index 0000000000..d00491fd7e
--- /dev/null
+++ b/Tests/RunCMake/add_custom_command/TargetImported-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/add_custom_command/TargetImported-stderr.txt b/Tests/RunCMake/add_custom_command/TargetImported-stderr.txt
new file mode 100644
index 0000000000..44c4ad886c
--- /dev/null
+++ b/Tests/RunCMake/add_custom_command/TargetImported-stderr.txt
@@ -0,0 +1,4 @@
+^CMake Error at TargetImported.cmake:2 \(add_custom_command\):
+  TARGET 'TargetImported' is IMPORTED and does not build here.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)$
diff --git a/Tests/RunCMake/add_custom_command/TargetImported.cmake b/Tests/RunCMake/add_custom_command/TargetImported.cmake
new file mode 100644
index 0000000000..c0f2d66a07
--- /dev/null
+++ b/Tests/RunCMake/add_custom_command/TargetImported.cmake
@@ -0,0 +1,2 @@
+add_library(TargetImported UNKNOWN IMPORTED)
+add_custom_command(TARGET TargetImported COMMAND ${CMAKE_COMMAND} -E echo tada)
diff --git a/Tests/RunCMake/add_custom_command/TargetNotInDir-result.txt b/Tests/RunCMake/add_custom_command/TargetNotInDir-result.txt
new file mode 100644
index 0000000000..d00491fd7e
--- /dev/null
+++ b/Tests/RunCMake/add_custom_command/TargetNotInDir-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/add_custom_command/TargetNotInDir-stderr.txt b/Tests/RunCMake/add_custom_command/TargetNotInDir-stderr.txt
new file mode 100644
index 0000000000..91876a0a0b
--- /dev/null
+++ b/Tests/RunCMake/add_custom_command/TargetNotInDir-stderr.txt
@@ -0,0 +1,4 @@
+^CMake Error at TargetNotInDir.cmake:2 \(add_custom_command\):
+  TARGET 'TargetNotInDir' was not created in this directory.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)$
diff --git a/Tests/RunCMake/add_custom_command/TargetNotInDir.cmake b/Tests/RunCMake/add_custom_command/TargetNotInDir.cmake
new file mode 100644
index 0000000000..a5510262aa
--- /dev/null
+++ b/Tests/RunCMake/add_custom_command/TargetNotInDir.cmake
@@ -0,0 +1,2 @@
+add_subdirectory(TargetNotInDir)
+add_custom_command(TARGET TargetNotInDir COMMAND ${CMAKE_COMMAND} -E echo tada)
diff --git a/Tests/RunCMake/add_custom_command/TargetNotInDir/CMakeLists.txt b/Tests/RunCMake/add_custom_command/TargetNotInDir/CMakeLists.txt
new file mode 100644
index 0000000000..3d51d27b31
--- /dev/null
+++ b/Tests/RunCMake/add_custom_command/TargetNotInDir/CMakeLists.txt
@@ -0,0 +1 @@
+add_custom_target(TargetNotInDir)
-- 
GitLab