cmake/Source/cmAddCustomTargetCommand.cxx

226 lines
6.6 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmAddCustomTargetCommand.h"
2014-08-03 19:52:23 +02:00
#include "cmGeneratorExpression.h"
#include "cmGlobalGenerator.h"
// cmAddCustomTargetCommand
2016-07-09 11:21:54 +02:00
bool cmAddCustomTargetCommand::InitialPass(
std::vector<std::string> const& args, cmExecutionStatus&)
{
2016-10-30 18:24:19 +01:00
if (args.empty()) {
this->SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
std::string targetName = args[0];
// Check the target name.
2016-07-09 11:21:54 +02:00
if (targetName.find_first_of("/\\") != targetName.npos) {
2015-04-27 22:25:09 +02:00
std::ostringstream e;
2014-08-03 19:52:23 +02:00
e << "called with invalid target name \"" << targetName
<< "\". Target names may not contain a slash. "
<< "Use ADD_CUSTOM_COMMAND to generate files.";
2015-04-27 22:25:09 +02:00
this->SetError(e.str());
2014-08-03 19:52:23 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
// Accumulate one command line at a time.
cmCustomCommandLine currentLine;
// Save all command lines.
cmCustomCommandLines commandLines;
// Accumulate dependencies.
2015-04-27 22:25:09 +02:00
std::vector<std::string> depends, byproducts;
std::string working_directory;
bool verbatim = false;
2015-04-27 22:25:09 +02:00
bool uses_terminal = false;
std::string comment_buffer;
2016-10-30 18:24:19 +01:00
const char* comment = CM_NULLPTR;
std::vector<std::string> sources;
// Keep track of parser state.
2016-07-09 11:21:54 +02:00
enum tdoing
{
doing_command,
doing_depends,
2015-04-27 22:25:09 +02:00
doing_byproducts,
doing_working_directory,
doing_comment,
doing_source,
2015-04-27 22:25:09 +02:00
doing_nothing
};
tdoing doing = doing_command;
// Look for the ALL option.
bool excludeFromAll = true;
unsigned int start = 1;
2016-07-09 11:21:54 +02:00
if (args.size() > 1) {
if (args[1] == "ALL") {
excludeFromAll = false;
start = 2;
}
2016-07-09 11:21:54 +02:00
}
// Parse the rest of the arguments.
2016-07-09 11:21:54 +02:00
for (unsigned int j = start; j < args.size(); ++j) {
std::string const& copy = args[j];
2016-07-09 11:21:54 +02:00
if (copy == "DEPENDS") {
doing = doing_depends;
2016-07-09 11:21:54 +02:00
} else if (copy == "BYPRODUCTS") {
2015-04-27 22:25:09 +02:00
doing = doing_byproducts;
2016-07-09 11:21:54 +02:00
} else if (copy == "WORKING_DIRECTORY") {
doing = doing_working_directory;
2016-07-09 11:21:54 +02:00
} else if (copy == "VERBATIM") {
2015-04-27 22:25:09 +02:00
doing = doing_nothing;
verbatim = true;
2016-07-09 11:21:54 +02:00
} else if (copy == "USES_TERMINAL") {
2015-04-27 22:25:09 +02:00
doing = doing_nothing;
uses_terminal = true;
2016-07-09 11:21:54 +02:00
} else if (copy == "COMMENT") {
doing = doing_comment;
2016-07-09 11:21:54 +02:00
} else if (copy == "COMMAND") {
doing = doing_command;
// Save the current command before starting the next command.
2016-07-09 11:21:54 +02:00
if (!currentLine.empty()) {
commandLines.push_back(currentLine);
currentLine.clear();
}
2016-07-09 11:21:54 +02:00
} else if (copy == "SOURCES") {
doing = doing_source;
2016-07-09 11:21:54 +02:00
} else {
switch (doing) {
case doing_working_directory:
working_directory = copy;
break;
case doing_command:
currentLine.push_back(copy);
break;
2016-07-09 11:21:54 +02:00
case doing_byproducts: {
2015-04-27 22:25:09 +02:00
std::string filename;
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::FileIsFullPath(copy.c_str())) {
2015-08-17 11:37:30 +02:00
filename = this->Makefile->GetCurrentBinaryDirectory();
2015-04-27 22:25:09 +02:00
filename += "/";
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
filename += copy;
cmSystemTools::ConvertToUnixSlashes(filename);
byproducts.push_back(filename);
2016-07-09 11:21:54 +02:00
} break;
case doing_depends: {
2011-06-19 15:41:06 +03:00
std::string dep = copy;
cmSystemTools::ConvertToUnixSlashes(dep);
depends.push_back(dep);
2016-07-09 11:21:54 +02:00
} break;
case doing_comment:
comment_buffer = copy;
comment = comment_buffer.c_str();
break;
case doing_source:
sources.push_back(copy);
break;
default:
this->SetError("Wrong syntax. Unknown type of argument.");
return false;
}
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
std::string::size_type pos = targetName.find_first_of("#<>");
2016-07-09 11:21:54 +02:00
if (pos != targetName.npos) {
2015-04-27 22:25:09 +02:00
std::ostringstream msg;
2014-08-03 19:52:23 +02:00
msg << "called with target name containing a \"" << targetName[pos]
<< "\". This character is not allowed.";
2015-04-27 22:25:09 +02:00
this->SetError(msg.str());
return false;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
// Some requirements on custom target names already exist
// and have been checked at this point.
// The following restrictions overlap but depend on policy CMP0037.
bool nameOk = cmGeneratorExpression::IsValidTargetName(targetName) &&
!cmGlobalGenerator::IsReservedTarget(targetName);
2016-07-09 11:21:54 +02:00
if (nameOk) {
2014-08-03 19:52:23 +02:00
nameOk = targetName.find(":") == std::string::npos;
2016-07-09 11:21:54 +02:00
}
if (!nameOk) {
2014-08-03 19:52:23 +02:00
cmake::MessageType messageType = cmake::AUTHOR_WARNING;
2015-04-27 22:25:09 +02:00
std::ostringstream e;
2014-08-03 19:52:23 +02:00
bool issueMessage = false;
2016-07-09 11:21:54 +02:00
switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0037)) {
2014-08-03 19:52:23 +02:00
case cmPolicies::WARN:
2015-08-17 11:37:30 +02:00
e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0037) << "\n";
2014-08-03 19:52:23 +02:00
issueMessage = true;
case cmPolicies::OLD:
break;
case cmPolicies::NEW:
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::REQUIRED_ALWAYS:
issueMessage = true;
messageType = cmake::FATAL_ERROR;
2016-07-09 11:21:54 +02:00
}
if (issueMessage) {
/* clang-format off */
2014-08-03 19:52:23 +02:00
e << "The target name \"" << targetName <<
"\" is reserved or not valid for certain "
"CMake features, such as generator expressions, and may result "
"in undefined behavior.";
2016-07-09 11:21:54 +02:00
/* clang-format on */
2015-04-27 22:25:09 +02:00
this->Makefile->IssueMessage(messageType, e.str());
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
if (messageType == cmake::FATAL_ERROR) {
2014-08-03 19:52:23 +02:00
return false;
}
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
// Store the last command line finished.
2016-07-09 11:21:54 +02:00
if (!currentLine.empty()) {
commandLines.push_back(currentLine);
currentLine.clear();
2016-07-09 11:21:54 +02:00
}
// Enforce name uniqueness.
{
2016-07-09 11:21:54 +02:00
std::string msg;
if (!this->Makefile->EnforceUniqueName(targetName, msg, true)) {
this->SetError(msg);
return false;
}
}
2011-06-19 15:41:06 +03:00
// Convert working directory to a full path.
2016-07-09 11:21:54 +02:00
if (!working_directory.empty()) {
2015-08-17 11:37:30 +02:00
const char* build_dir = this->Makefile->GetCurrentBinaryDirectory();
2011-06-19 15:41:06 +03:00
working_directory =
2015-04-27 22:25:09 +02:00
cmSystemTools::CollapseFullPath(working_directory, build_dir);
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (commandLines.empty() && !byproducts.empty()) {
this->Makefile->IssueMessage(
cmake::FATAL_ERROR,
2015-04-27 22:25:09 +02:00
"BYPRODUCTS may not be specified without any COMMAND");
return true;
2016-07-09 11:21:54 +02:00
}
if (commandLines.empty() && uses_terminal) {
this->Makefile->IssueMessage(
cmake::FATAL_ERROR,
2015-04-27 22:25:09 +02:00
"USES_TERMINAL may not be specified without any COMMAND");
return true;
2016-07-09 11:21:54 +02:00
}
2011-06-19 15:41:06 +03:00
// Add the utility target to the makefile.
bool escapeOldStyle = !verbatim;
2016-07-09 11:21:54 +02:00
cmTarget* target = this->Makefile->AddUtilityCommand(
targetName, excludeFromAll, working_directory.c_str(), byproducts, depends,
commandLines, escapeOldStyle, comment, uses_terminal);
// Add additional user-specified source files to the target.
target->AddSources(sources);
return true;
}