cmake/Source/cmAddCustomCommandCommand.cxx

389 lines
12 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 "cmAddCustomCommandCommand.h"
2017-04-14 19:02:05 +02:00
#include <sstream>
2018-04-23 21:13:27 +02:00
#include <unordered_set>
2020-02-01 23:06:01 +01:00
#include "cmCheckCustomOutputs.h"
2017-04-14 19:02:05 +02:00
#include "cmCustomCommand.h"
#include "cmCustomCommandLines.h"
2020-02-01 23:06:01 +01:00
#include "cmCustomCommandTypes.h"
#include "cmExecutionStatus.h"
2017-04-14 19:02:05 +02:00
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
2019-11-11 23:01:05 +01:00
#include "cmMessageType.h"
2017-04-14 19:02:05 +02:00
#include "cmPolicies.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
2020-02-01 23:06:01 +01:00
bool cmAddCustomCommandCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
/* Let's complain at the end of this function about the lack of a particular
arg. For the moment, let's say that COMMAND, and either TARGET or SOURCE
are required.
*/
2016-07-09 11:21:54 +02:00
if (args.size() < 4) {
2020-02-01 23:06:01 +01:00
status.SetError("called with wrong number of arguments.");
2016-07-09 11:21:54 +02:00
return false;
}
2020-02-01 23:06:01 +01:00
cmMakefile& mf = status.GetMakefile();
std::string source;
std::string target;
std::string main_dependency;
std::string working;
std::string depfile;
std::string job_pool;
std::string comment_buffer;
2018-01-26 17:06:56 +01:00
const char* comment = nullptr;
2020-02-01 23:06:01 +01:00
std::vector<std::string> depends;
std::vector<std::string> outputs;
std::vector<std::string> output;
std::vector<std::string> byproducts;
bool verbatim = false;
bool append = 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 implicit_depends_lang;
2020-02-01 23:06:01 +01:00
cmImplicitDependsList implicit_depends;
// Accumulate one command line at a time.
cmCustomCommandLine currentLine;
// Save all command lines.
cmCustomCommandLines commandLines;
2020-02-01 23:06:01 +01:00
cmCustomCommandType cctype = cmCustomCommandType::POST_BUILD;
2016-07-09 11:21:54 +02:00
enum tdoing
{
doing_source,
doing_command,
doing_target,
doing_depends,
doing_implicit_depends_lang,
doing_implicit_depends_file,
doing_main_dependency,
doing_output,
doing_outputs,
2015-04-27 22:25:09 +02:00
doing_byproducts,
doing_comment,
doing_working_directory,
2016-10-30 18:24:19 +01:00
doing_depfile,
2019-11-11 23:01:05 +01:00
doing_job_pool,
doing_nothing
};
tdoing doing = doing_nothing;
2018-04-23 21:13:27 +02:00
#define MAKE_STATIC_KEYWORD(KEYWORD) \
static const std::string key##KEYWORD = #KEYWORD
MAKE_STATIC_KEYWORD(APPEND);
MAKE_STATIC_KEYWORD(ARGS);
MAKE_STATIC_KEYWORD(BYPRODUCTS);
MAKE_STATIC_KEYWORD(COMMAND);
MAKE_STATIC_KEYWORD(COMMAND_EXPAND_LISTS);
MAKE_STATIC_KEYWORD(COMMENT);
MAKE_STATIC_KEYWORD(DEPENDS);
MAKE_STATIC_KEYWORD(DEPFILE);
MAKE_STATIC_KEYWORD(IMPLICIT_DEPENDS);
2019-11-11 23:01:05 +01:00
MAKE_STATIC_KEYWORD(JOB_POOL);
2018-04-23 21:13:27 +02:00
MAKE_STATIC_KEYWORD(MAIN_DEPENDENCY);
MAKE_STATIC_KEYWORD(OUTPUT);
MAKE_STATIC_KEYWORD(OUTPUTS);
MAKE_STATIC_KEYWORD(POST_BUILD);
MAKE_STATIC_KEYWORD(PRE_BUILD);
MAKE_STATIC_KEYWORD(PRE_LINK);
MAKE_STATIC_KEYWORD(SOURCE);
MAKE_STATIC_KEYWORD(TARGET);
MAKE_STATIC_KEYWORD(USES_TERMINAL);
MAKE_STATIC_KEYWORD(VERBATIM);
MAKE_STATIC_KEYWORD(WORKING_DIRECTORY);
#undef MAKE_STATIC_KEYWORD
2020-02-01 23:06:01 +01:00
static std::unordered_set<std::string> const keywords{
keyAPPEND,
keyARGS,
keyBYPRODUCTS,
keyCOMMAND,
keyCOMMAND_EXPAND_LISTS,
keyCOMMENT,
keyDEPENDS,
keyDEPFILE,
keyIMPLICIT_DEPENDS,
keyJOB_POOL,
keyMAIN_DEPENDENCY,
keyOUTPUT,
keyOUTPUTS,
keyPOST_BUILD,
keyPRE_BUILD,
keyPRE_LINK,
keySOURCE,
keyTARGET,
keyUSES_TERMINAL,
keyVERBATIM,
keyWORKING_DIRECTORY
};
2018-04-23 21:13:27 +02:00
2018-01-26 17:06:56 +01:00
for (std::string const& copy : args) {
2018-04-23 21:13:27 +02:00
if (keywords.count(copy)) {
if (copy == keySOURCE) {
doing = doing_source;
} else if (copy == keyCOMMAND) {
doing = doing_command;
2018-04-23 21:13:27 +02:00
// Save the current command before starting the next command.
if (!currentLine.empty()) {
commandLines.push_back(currentLine);
currentLine.clear();
}
} else if (copy == keyPRE_BUILD) {
2020-02-01 23:06:01 +01:00
cctype = cmCustomCommandType::PRE_BUILD;
2018-04-23 21:13:27 +02:00
} else if (copy == keyPRE_LINK) {
2020-02-01 23:06:01 +01:00
cctype = cmCustomCommandType::PRE_LINK;
2018-04-23 21:13:27 +02:00
} else if (copy == keyPOST_BUILD) {
2020-02-01 23:06:01 +01:00
cctype = cmCustomCommandType::POST_BUILD;
2018-04-23 21:13:27 +02:00
} else if (copy == keyVERBATIM) {
verbatim = true;
} else if (copy == keyAPPEND) {
append = true;
} else if (copy == keyUSES_TERMINAL) {
uses_terminal = true;
} else if (copy == keyCOMMAND_EXPAND_LISTS) {
command_expand_lists = true;
} else if (copy == keyTARGET) {
doing = doing_target;
} else if (copy == keyARGS) {
// Ignore this old keyword.
} else if (copy == keyDEPENDS) {
doing = doing_depends;
} else if (copy == keyOUTPUTS) {
doing = doing_outputs;
} else if (copy == keyOUTPUT) {
doing = doing_output;
} else if (copy == keyBYPRODUCTS) {
doing = doing_byproducts;
} else if (copy == keyWORKING_DIRECTORY) {
doing = doing_working_directory;
} else if (copy == keyMAIN_DEPENDENCY) {
doing = doing_main_dependency;
} else if (copy == keyIMPLICIT_DEPENDS) {
doing = doing_implicit_depends_lang;
} else if (copy == keyCOMMENT) {
doing = doing_comment;
} else if (copy == keyDEPFILE) {
doing = doing_depfile;
2020-08-30 11:54:41 +02:00
if (!mf.GetGlobalGenerator()->SupportsCustomCommandDepfile()) {
2020-02-01 23:06:01 +01:00
status.SetError("Option DEPFILE not supported by " +
mf.GetGlobalGenerator()->GetName());
2018-04-23 21:13:27 +02:00
return false;
}
2019-11-11 23:01:05 +01:00
} else if (copy == keyJOB_POOL) {
doing = doing_job_pool;
2016-10-30 18:24:19 +01:00
}
2016-07-09 11:21:54 +02:00
} else {
std::string filename;
2016-07-09 11:21:54 +02:00
switch (doing) {
case doing_output:
case doing_outputs:
2015-04-27 22:25:09 +02:00
case doing_byproducts:
2018-04-23 21:13:27 +02:00
if (!cmSystemTools::FileIsFullPath(copy)) {
// This is an output to be generated, so it should be
// under the build tree. CMake 2.4 placed this under the
// source tree. However the only case that this change
// will break is when someone writes
//
// add_custom_command(OUTPUT out.txt ...)
//
// and later references "${CMAKE_CURRENT_SOURCE_DIR}/out.txt".
// This is fairly obscure so we can wait for someone to
// complain.
2020-02-01 23:06:01 +01:00
filename = cmStrCat(mf.GetCurrentBinaryDirectory(), '/');
2016-07-09 11:21:54 +02:00
}
filename += copy;
2011-01-16 11:35:12 +01:00
cmSystemTools::ConvertToUnixSlashes(filename);
break;
case doing_source:
2016-07-09 11:21:54 +02:00
// We do not want to convert the argument to SOURCE because
// that option is only available for backward compatibility.
// Old-style use of this command may use the SOURCE==TARGET
// trick which we must preserve. If we convert the source
// to a full path then it will no longer equal the target.
default:
break;
2016-07-09 11:21:54 +02:00
}
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileIsFullPath(filename)) {
2020-08-30 11:54:41 +02:00
filename = cmSystemTools::CollapseFullPath(
filename, status.GetMakefile().GetHomeOutputDirectory());
2016-07-09 11:21:54 +02:00
}
switch (doing) {
2016-10-30 18:24:19 +01:00
case doing_depfile:
depfile = copy;
break;
2019-11-11 23:01:05 +01:00
case doing_job_pool:
job_pool = copy;
break;
2016-07-09 11:21:54 +02:00
case doing_working_directory:
working = copy;
break;
case doing_source:
source = copy;
break;
case doing_output:
output.push_back(filename);
break;
case doing_main_dependency:
main_dependency = copy;
break;
case doing_implicit_depends_lang:
implicit_depends_lang = copy;
doing = doing_implicit_depends_file;
break;
case doing_implicit_depends_file: {
// An implicit dependency starting point is also an
// explicit dependency.
std::string dep = copy;
2020-02-01 23:06:01 +01:00
// Upfront path conversion is correct because Genex
// are not supported.
2016-07-09 11:21:54 +02:00
cmSystemTools::ConvertToUnixSlashes(dep);
depends.push_back(dep);
2016-07-09 11:21:54 +02:00
// Add the implicit dependency language and file.
2018-04-23 21:13:27 +02:00
implicit_depends.emplace_back(implicit_depends_lang, dep);
2016-07-09 11:21:54 +02:00
// Switch back to looking for a language.
doing = doing_implicit_depends_lang;
} break;
case doing_command:
currentLine.push_back(copy);
break;
case doing_target:
target = copy;
break;
2020-08-30 11:54:41 +02:00
case doing_depends:
2020-02-01 23:06:01 +01:00
depends.push_back(copy);
2020-08-30 11:54:41 +02:00
break;
2016-07-09 11:21:54 +02:00
case doing_outputs:
outputs.push_back(filename);
break;
case doing_byproducts:
byproducts.push_back(filename);
break;
case doing_comment:
comment_buffer = copy;
comment = comment_buffer.c_str();
break;
default:
2020-02-01 23:06:01 +01:00
status.SetError("Wrong syntax. Unknown type of argument.");
2016-07-09 11:21:54 +02:00
return false;
}
}
2016-07-09 11:21:54 +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
}
// At this point we could complain about the lack of arguments. For
// the moment, let's say that COMMAND, TARGET are always required.
2016-07-09 11:21:54 +02:00
if (output.empty() && target.empty()) {
2020-02-01 23:06:01 +01:00
status.SetError("Wrong syntax. A TARGET or OUTPUT must be specified.");
return false;
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (source.empty() && !target.empty() && !output.empty()) {
2020-02-01 23:06:01 +01:00
status.SetError(
"Wrong syntax. A TARGET and OUTPUT can not both be specified.");
return false;
2016-07-09 11:21:54 +02:00
}
if (append && output.empty()) {
2020-02-01 23:06:01 +01:00
status.SetError("given APPEND option with no OUTPUT.");
return false;
2016-07-09 11:21:54 +02:00
}
// Make sure the output names and locations are safe.
2020-02-01 23:06:01 +01:00
if (!cmCheckCustomOutputs(output, "OUTPUT", status) ||
!cmCheckCustomOutputs(outputs, "OUTPUTS", status) ||
!cmCheckCustomOutputs(byproducts, "BYPRODUCTS", status)) {
return false;
2016-07-09 11:21:54 +02:00
}
// Check for an append request.
2016-07-09 11:21:54 +02:00
if (append) {
2020-02-01 23:06:01 +01:00
if (mf.AppendCustomCommandToOutput(output[0], depends, implicit_depends,
commandLines)) {
return true;
2016-07-09 11:21:54 +02:00
}
// No command for this output exists.
2020-02-01 23:06:01 +01:00
status.SetError(
cmStrCat("given APPEND option with output\n ", output[0],
"\nwhich is not already a custom command output."));
return false;
2016-07-09 11:21:54 +02: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.");
2019-11-11 23:01:05 +01:00
return false;
}
// Choose which mode of the command to use.
bool escapeOldStyle = !verbatim;
2016-07-09 11:21:54 +02:00
if (source.empty() && output.empty()) {
// Source is empty, use the target.
std::vector<std::string> no_depends;
2020-02-01 23:06:01 +01:00
mf.AddCustomCommandToTarget(target, byproducts, no_depends, commandLines,
cctype, comment, working.c_str(),
escapeOldStyle, uses_terminal, depfile,
job_pool, command_expand_lists);
2016-07-09 11:21:54 +02:00
} else if (target.empty()) {
// Target is empty, use the output.
2020-02-01 23:06:01 +01:00
mf.AddCustomCommandToOutput(
output, byproducts, depends, main_dependency, implicit_depends,
2020-08-30 11:54:41 +02:00
commandLines, comment, working.c_str(), nullptr, false, escapeOldStyle,
2020-02-01 23:06:01 +01:00
uses_terminal, command_expand_lists, depfile, job_pool);
2016-07-09 11:21:54 +02:00
} else if (!byproducts.empty()) {
2020-02-01 23:06:01 +01:00
status.SetError("BYPRODUCTS may not be specified with SOURCE signatures");
2015-04-27 22:25:09 +02:00
return false;
2016-07-09 11:21:54 +02:00
} else if (uses_terminal) {
2020-02-01 23:06:01 +01:00
status.SetError("USES_TERMINAL may not be used with SOURCE signatures");
2015-04-27 22:25:09 +02:00
return false;
2016-07-09 11:21:54 +02:00
} else {
2014-08-03 19:52:23 +02:00
bool issueMessage = true;
2015-04-27 22:25:09 +02:00
std::ostringstream e;
2019-11-11 23:01:05 +01:00
MessageType messageType = MessageType::AUTHOR_WARNING;
2020-02-01 23:06:01 +01:00
switch (mf.GetPolicyStatus(cmPolicies::CMP0050)) {
2016-07-09 11:21:54 +02:00
case cmPolicies::WARN:
e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0050) << "\n";
break;
case cmPolicies::OLD:
issueMessage = false;
break;
case cmPolicies::REQUIRED_ALWAYS:
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::NEW:
2019-11-11 23:01:05 +01:00
messageType = MessageType::FATAL_ERROR;
2016-07-09 11:21:54 +02:00
break;
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
if (issueMessage) {
2014-08-03 19:52:23 +02:00
e << "The SOURCE signatures of add_custom_command are no longer "
"supported.";
2020-02-01 23:06:01 +01:00
mf.IssueMessage(messageType, e.str());
2019-11-11 23:01:05 +01:00
if (messageType == MessageType::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
// Use the old-style mode for backward compatibility.
2020-02-01 23:06:01 +01:00
mf.AddCustomCommandOldStyle(target, outputs, depends, source, commandLines,
comment);
2016-07-09 11:21:54 +02:00
}
return true;
}