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"
2018-04-23 21:13:27 +02:00
#include <utility>
2017-04-14 19:02:05 +02:00
2020-02-01 23:06:01 +01:00
#include "cmCheckCustomOutputs.h"
2017-04-14 19:02:05 +02:00
#include "cmCustomCommandLines.h"
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
2014-08-03 19:52:23 +02:00
#include "cmGeneratorExpression.h"
#include "cmGlobalGenerator.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
2019-11-11 23:01:05 +01:00
#include "cmMessageType.h"
2018-04-23 21:13:27 +02:00
#include "cmStateTypes.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
#include "cmTarget.h"
2020-02-01 23:06:01 +01:00
bool cmAddCustomTargetCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
2016-10-30 18:24:19 +01:00
if (args.empty()) {
2020-02-01 23:06:01 +01:00
status.SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
cmMakefile& mf = status.GetMakefile();
2017-07-20 19:35:53 +02:00
std::string const& targetName = args[0];
2014-08-03 19:52:23 +02:00
// Check the target name.
2017-07-20 19:35:53 +02:00
if (targetName.find_first_of("/\\") != std::string::npos) {
2020-02-01 23:06:01 +01:00
status.SetError(cmStrCat("called with invalid target name \"", targetName,
"\". Target names may not contain a slash. "
"Use ADD_CUSTOM_COMMAND to generate files."));
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.
2020-02-01 23:06:01 +01:00
std::vector<std::string> depends;
std::vector<std::string> byproducts;
std::string working_directory;
bool verbatim = false;
2015-04-27 22:25:09 +02:00
bool uses_terminal = false;
2017-04-14 19:02:05 +02:00
bool command_expand_lists = false;
std::string comment_buffer;
2018-01-26 17:06:56 +01:00
const char* comment = nullptr;
std::vector<std::string> sources;
2019-11-11 23:01:05 +01:00
std::string job_pool;
// 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,
2019-11-11 23:01:05 +01:00
doing_job_pool,
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;
2017-04-14 19:02:05 +02:00
} else if (copy == "COMMAND_EXPAND_LISTS") {
doing = doing_nothing;
command_expand_lists = true;
2016-07-09 11:21:54 +02:00
} else if (copy == "COMMENT") {
doing = doing_comment;
2019-11-11 23:01:05 +01:00
} else if (copy == "JOB_POOL") {
doing = doing_job_pool;
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;
2018-04-23 21:13:27 +02:00
if (!cmSystemTools::FileIsFullPath(copy)) {
2020-02-01 23:06:01 +01:00
filename = cmStrCat(mf.GetCurrentBinaryDirectory(), '/');
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
filename += copy;
cmSystemTools::ConvertToUnixSlashes(filename);
2020-02-01 23:06:01 +01:00
byproducts.push_back(cmSystemTools::CollapseFullPath(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);
2018-04-23 21:13:27 +02:00
depends.push_back(std::move(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;
2019-11-11 23:01:05 +01:00
case doing_job_pool:
job_pool = copy;
break;
default:
2020-02-01 23:06:01 +01:00
status.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("#<>");
2017-07-20 19:35:53 +02:00
if (pos != std::string::npos) {
2020-02-01 23:06:01 +01:00
status.SetError(cmStrCat("called with target name containing a \"",
targetName[pos],
"\". This character is not allowed."));
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) {
2017-04-14 19:02:05 +02:00
nameOk = targetName.find(':') == std::string::npos;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
if (!nameOk && !mf.CheckCMP0037(targetName, cmStateEnums::UTILITY)) {
2018-04-23 21:13:27 +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;
2020-02-01 23:06:01 +01:00
if (!mf.EnforceUniqueName(targetName, msg, true)) {
status.SetError(msg);
2016-07-09 11:21:54 +02:00
return false;
}
}
2016-07-09 11:21:54 +02:00
if (commandLines.empty() && !byproducts.empty()) {
2020-02-01 23:06:01 +01:00
mf.IssueMessage(MessageType::FATAL_ERROR,
"BYPRODUCTS may not be specified without any COMMAND");
2015-04-27 22:25:09 +02:00
return true;
2016-07-09 11:21:54 +02:00
}
if (commandLines.empty() && uses_terminal) {
2020-02-01 23:06:01 +01:00
mf.IssueMessage(MessageType::FATAL_ERROR,
"USES_TERMINAL may not be specified without any COMMAND");
2015-04-27 22:25:09 +02:00
return true;
2016-07-09 11:21:54 +02:00
}
2017-04-14 19:02:05 +02:00
if (commandLines.empty() && command_expand_lists) {
2020-02-01 23:06:01 +01:00
mf.IssueMessage(
2019-11-11 23:01:05 +01:00
MessageType::FATAL_ERROR,
2017-04-14 19:02:05 +02:00
"COMMAND_EXPAND_LISTS may not be specified without any COMMAND");
return true;
}
2011-06-19 15:41:06 +03:00
2019-11-11 23:01:05 +01:00
if (uses_terminal && !job_pool.empty()) {
2020-02-01 23:06:01 +01:00
status.SetError("JOB_POOL is shadowed by USES_TERMINAL.");
return false;
}
// Make sure the byproduct names and locations are safe.
if (!cmCheckCustomOutputs(byproducts, "BYPRODUCTS", status)) {
2019-11-11 23:01:05 +01:00
return false;
}
// Add the utility target to the makefile.
bool escapeOldStyle = !verbatim;
2020-02-01 23:06:01 +01:00
cmTarget* target = mf.AddUtilityCommand(
2020-08-30 11:54:41 +02:00
targetName, excludeFromAll, working_directory.c_str(), byproducts, depends,
commandLines, escapeOldStyle, comment, uses_terminal, command_expand_lists,
job_pool);
// Add additional user-specified source files to the target.
target->AddSources(sources);
return true;
}