Commit d257d681 authored by Brad King's avatar Brad King

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's avatarBartosz Kosiorek <gang65@poczta.onet.pl>
parent 4d53e0a7
......@@ -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());
}
......
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\)
......@@ -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.
......@@ -8,3 +8,5 @@ run_cmake(NoOutputOrTarget)
run_cmake(OutputAndTarget)
run_cmake(SourceByproducts)
run_cmake(SourceUsesTerminal)
run_cmake(TargetImported)
run_cmake(TargetNotInDir)
^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\)$
add_library(TargetImported UNKNOWN IMPORTED)
add_custom_command(TARGET TargetImported COMMAND ${CMAKE_COMMAND} -E echo tada)
^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\)$
add_subdirectory(TargetNotInDir)
add_custom_command(TARGET TargetNotInDir COMMAND ${CMAKE_COMMAND} -E echo tada)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment