Commit 9ba0bf60 authored by wahikihiki's avatar wahikihiki

cmA*Command: Turn into free functions

Ref: #19499
parent 52d9cd62
...@@ -2,19 +2,15 @@ ...@@ -2,19 +2,15 @@
file Copyright.txt or https://cmake.org/licensing for details. */ file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmAddCompileDefinitionsCommand.h" #include "cmAddCompileDefinitionsCommand.h"
#include "cmExecutionStatus.h"
#include "cmMakefile.h" #include "cmMakefile.h"
class cmExecutionStatus; bool cmAddCompileDefinitionsCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
bool cmAddCompileDefinitionsCommand::InitialPass(
std::vector<std::string> const& args, cmExecutionStatus&)
{ {
if (args.empty()) { cmMakefile& mf = status.GetMakefile();
return true;
}
for (std::string const& i : args) { for (std::string const& i : args) {
this->Makefile->AddCompileDefinition(i); mf.AddCompileDefinition(i);
} }
return true; return true;
} }
...@@ -8,29 +8,9 @@ ...@@ -8,29 +8,9 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "cm_memory.hxx"
#include "cmCommand.h"
class cmExecutionStatus; class cmExecutionStatus;
class cmAddCompileDefinitionsCommand : public cmCommand bool cmAddCompileDefinitionsCommand(std::vector<std::string> const& args,
{ cmExecutionStatus& status);
public:
/**
* This is a virtual constructor for the command.
*/
std::unique_ptr<cmCommand> Clone() override
{
return cm::make_unique<cmAddCompileDefinitionsCommand>();
}
/**
* This is called when the command is first encountered in
* the CMakeLists.txt file.
*/
bool InitialPass(std::vector<std::string> const& args,
cmExecutionStatus& status) override;
};
#endif #endif
...@@ -2,19 +2,15 @@ ...@@ -2,19 +2,15 @@
file Copyright.txt or https://cmake.org/licensing for details. */ file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmAddCompileOptionsCommand.h" #include "cmAddCompileOptionsCommand.h"
#include "cmExecutionStatus.h"
#include "cmMakefile.h" #include "cmMakefile.h"
class cmExecutionStatus; bool cmAddCompileOptionsCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
bool cmAddCompileOptionsCommand::InitialPass(
std::vector<std::string> const& args, cmExecutionStatus&)
{ {
if (args.empty()) { cmMakefile& mf = status.GetMakefile();
return true;
}
for (std::string const& i : args) { for (std::string const& i : args) {
this->Makefile->AddCompileOption(i); mf.AddCompileOption(i);
} }
return true; return true;
} }
...@@ -8,29 +8,9 @@ ...@@ -8,29 +8,9 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "cm_memory.hxx"
#include "cmCommand.h"
class cmExecutionStatus; class cmExecutionStatus;
class cmAddCompileOptionsCommand : public cmCommand bool cmAddCompileOptionsCommand(std::vector<std::string> const& args,
{ cmExecutionStatus& status);
public:
/**
* This is a virtual constructor for the command.
*/
std::unique_ptr<cmCommand> Clone() override
{
return cm::make_unique<cmAddCompileOptionsCommand>();
}
/**
* This is called when the command is first encountered in
* the CMakeLists.txt file.
*/
bool InitialPass(std::vector<std::string> const& args,
cmExecutionStatus& status) override;
};
#endif #endif
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "cmCustomCommand.h" #include "cmCustomCommand.h"
#include "cmCustomCommandLines.h" #include "cmCustomCommandLines.h"
#include "cmExecutionStatus.h"
#include "cmGlobalGenerator.h" #include "cmGlobalGenerator.h"
#include "cmMakefile.h" #include "cmMakefile.h"
#include "cmMessageType.h" #include "cmMessageType.h"
...@@ -16,21 +17,22 @@ ...@@ -16,21 +17,22 @@
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmTarget.h" #include "cmTarget.h"
class cmExecutionStatus; static bool cmAddCustomCommandCommandCheckOutputs(
const std::vector<std::string>& outputs, cmExecutionStatus& status);
// cmAddCustomCommandCommand bool cmAddCustomCommandCommand(std::vector<std::string> const& args,
bool cmAddCustomCommandCommand::InitialPass( cmExecutionStatus& status)
std::vector<std::string> const& args, cmExecutionStatus&)
{ {
/* Let's complain at the end of this function about the lack of a particular /* 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 arg. For the moment, let's say that COMMAND, and either TARGET or SOURCE
are required. are required.
*/ */
if (args.size() < 4) { if (args.size() < 4) {
this->SetError("called with wrong number of arguments."); status.SetError("called with wrong number of arguments.");
return false; return false;
} }
cmMakefile& mf = status.GetMakefile();
std::string source, target, main_dependency, working, depfile, job_pool; std::string source, target, main_dependency, working, depfile, job_pool;
std::string comment_buffer; std::string comment_buffer;
const char* comment = nullptr; const char* comment = nullptr;
...@@ -167,9 +169,9 @@ bool cmAddCustomCommandCommand::InitialPass( ...@@ -167,9 +169,9 @@ bool cmAddCustomCommandCommand::InitialPass(
doing = doing_comment; doing = doing_comment;
} else if (copy == keyDEPFILE) { } else if (copy == keyDEPFILE) {
doing = doing_depfile; doing = doing_depfile;
if (this->Makefile->GetGlobalGenerator()->GetName() != "Ninja") { if (mf.GetGlobalGenerator()->GetName() != "Ninja") {
this->SetError("Option DEPFILE not supported by " + status.SetError("Option DEPFILE not supported by " +
this->Makefile->GetGlobalGenerator()->GetName()); mf.GetGlobalGenerator()->GetName());
return false; return false;
} }
} else if (copy == keyJOB_POOL) { } else if (copy == keyJOB_POOL) {
...@@ -192,7 +194,7 @@ bool cmAddCustomCommandCommand::InitialPass( ...@@ -192,7 +194,7 @@ bool cmAddCustomCommandCommand::InitialPass(
// and later references "${CMAKE_CURRENT_SOURCE_DIR}/out.txt". // and later references "${CMAKE_CURRENT_SOURCE_DIR}/out.txt".
// This is fairly obscure so we can wait for someone to // This is fairly obscure so we can wait for someone to
// complain. // complain.
filename = this->Makefile->GetCurrentBinaryDirectory(); filename = mf.GetCurrentBinaryDirectory();
filename += "/"; filename += "/";
} }
filename += copy; filename += copy;
...@@ -269,7 +271,7 @@ bool cmAddCustomCommandCommand::InitialPass( ...@@ -269,7 +271,7 @@ bool cmAddCustomCommandCommand::InitialPass(
comment = comment_buffer.c_str(); comment = comment_buffer.c_str();
break; break;
default: default:
this->SetError("Wrong syntax. Unknown type of argument."); status.SetError("Wrong syntax. Unknown type of argument.");
return false; return false;
} }
} }
...@@ -284,31 +286,31 @@ bool cmAddCustomCommandCommand::InitialPass( ...@@ -284,31 +286,31 @@ bool cmAddCustomCommandCommand::InitialPass(
// At this point we could complain about the lack of arguments. For // At this point we could complain about the lack of arguments. For
// the moment, let's say that COMMAND, TARGET are always required. // the moment, let's say that COMMAND, TARGET are always required.
if (output.empty() && target.empty()) { if (output.empty() && target.empty()) {
this->SetError("Wrong syntax. A TARGET or OUTPUT must be specified."); status.SetError("Wrong syntax. A TARGET or OUTPUT must be specified.");
return false; return false;
} }
if (source.empty() && !target.empty() && !output.empty()) { if (source.empty() && !target.empty() && !output.empty()) {
this->SetError( status.SetError(
"Wrong syntax. A TARGET and OUTPUT can not both be specified."); "Wrong syntax. A TARGET and OUTPUT can not both be specified.");
return false; return false;
} }
if (append && output.empty()) { if (append && output.empty()) {
this->SetError("given APPEND option with no OUTPUT."); status.SetError("given APPEND option with no OUTPUT.");
return false; return false;
} }
// Make sure the output names and locations are safe. // Make sure the output names and locations are safe.
if (!this->CheckOutputs(output) || !this->CheckOutputs(outputs) || if (!cmAddCustomCommandCommandCheckOutputs(output, status) ||
!this->CheckOutputs(byproducts)) { !cmAddCustomCommandCommandCheckOutputs(outputs, status) ||
!cmAddCustomCommandCommandCheckOutputs(byproducts, status)) {
return false; return false;
} }
// Check for an append request. // Check for an append request.
if (append) { if (append) {
// Lookup an existing command. // Lookup an existing command.
if (cmSourceFile* sf = if (cmSourceFile* sf = mf.GetSourceFileWithOutput(output[0])) {
this->Makefile->GetSourceFileWithOutput(output[0])) {
if (cmCustomCommand* cc = sf->GetCustomCommand()) { if (cmCustomCommand* cc = sf->GetCustomCommand()) {
cc->AppendCommands(commandLines); cc->AppendCommands(commandLines);
cc->AppendDepends(depends); cc->AppendDepends(depends);
...@@ -321,12 +323,12 @@ bool cmAddCustomCommandCommand::InitialPass( ...@@ -321,12 +323,12 @@ bool cmAddCustomCommandCommand::InitialPass(
std::ostringstream e; std::ostringstream e;
e << "given APPEND option with output\n\"" << output[0] e << "given APPEND option with output\n\"" << output[0]
<< "\"\nwhich is not already a custom command output."; << "\"\nwhich is not already a custom command output.";
this->SetError(e.str()); status.SetError(e.str());
return false; return false;
} }
if (uses_terminal && !job_pool.empty()) { if (uses_terminal && !job_pool.empty()) {
this->SetError("JOB_POOL is shadowed by USES_TERMINAL."); status.SetError("JOB_POOL is shadowed by USES_TERMINAL.");
return false; return false;
} }
...@@ -335,22 +337,21 @@ bool cmAddCustomCommandCommand::InitialPass( ...@@ -335,22 +337,21 @@ bool cmAddCustomCommandCommand::InitialPass(
if (source.empty() && output.empty()) { if (source.empty() && output.empty()) {
// Source is empty, use the target. // Source is empty, use the target.
std::vector<std::string> no_depends; std::vector<std::string> no_depends;
this->Makefile->AddCustomCommandToTarget( mf.AddCustomCommandToTarget(target, byproducts, no_depends, commandLines,
target, byproducts, no_depends, commandLines, cctype, comment, cctype, comment, working.c_str(),
working.c_str(), escapeOldStyle, uses_terminal, depfile, job_pool, escapeOldStyle, uses_terminal, depfile,
command_expand_lists); job_pool, command_expand_lists);
} else if (target.empty()) { } else if (target.empty()) {
// Target is empty, use the output. // Target is empty, use the output.
this->Makefile->AddCustomCommandToOutput( mf.AddCustomCommandToOutput(output, byproducts, depends, main_dependency,
output, byproducts, depends, main_dependency, commandLines, comment, commandLines, comment, working.c_str(), false,
working.c_str(), false, escapeOldStyle, uses_terminal, escapeOldStyle, uses_terminal,
command_expand_lists, depfile, job_pool); command_expand_lists, depfile, job_pool);
// Add implicit dependency scanning requests if any were given. // Add implicit dependency scanning requests if any were given.
if (!implicit_depends.empty()) { if (!implicit_depends.empty()) {
bool okay = false; bool okay = false;
if (cmSourceFile* sf = if (cmSourceFile* sf = mf.GetSourceFileWithOutput(output[0])) {
this->Makefile->GetSourceFileWithOutput(output[0])) {
if (cmCustomCommand* cc = sf->GetCustomCommand()) { if (cmCustomCommand* cc = sf->GetCustomCommand()) {
okay = true; okay = true;
cc->SetImplicitDepends(implicit_depends); cc->SetImplicitDepends(implicit_depends);
...@@ -360,21 +361,21 @@ bool cmAddCustomCommandCommand::InitialPass( ...@@ -360,21 +361,21 @@ bool cmAddCustomCommandCommand::InitialPass(
std::ostringstream e; std::ostringstream e;
e << "could not locate source file with a custom command producing \"" e << "could not locate source file with a custom command producing \""
<< output[0] << "\" even though this command tried to create it!"; << output[0] << "\" even though this command tried to create it!";
this->SetError(e.str()); status.SetError(e.str());
return false; return false;
} }
} }
} else if (!byproducts.empty()) { } else if (!byproducts.empty()) {
this->SetError("BYPRODUCTS may not be specified with SOURCE signatures"); status.SetError("BYPRODUCTS may not be specified with SOURCE signatures");
return false; return false;
} else if (uses_terminal) { } else if (uses_terminal) {
this->SetError("USES_TERMINAL may not be used with SOURCE signatures"); status.SetError("USES_TERMINAL may not be used with SOURCE signatures");
return false; return false;
} else { } else {
bool issueMessage = true; bool issueMessage = true;
std::ostringstream e; std::ostringstream e;
MessageType messageType = MessageType::AUTHOR_WARNING; MessageType messageType = MessageType::AUTHOR_WARNING;
switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0050)) { switch (mf.GetPolicyStatus(cmPolicies::CMP0050)) {
case cmPolicies::WARN: case cmPolicies::WARN:
e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0050) << "\n"; e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0050) << "\n";
break; break;
...@@ -391,30 +392,31 @@ bool cmAddCustomCommandCommand::InitialPass( ...@@ -391,30 +392,31 @@ bool cmAddCustomCommandCommand::InitialPass(
if (issueMessage) { if (issueMessage) {
e << "The SOURCE signatures of add_custom_command are no longer " e << "The SOURCE signatures of add_custom_command are no longer "
"supported."; "supported.";
this->Makefile->IssueMessage(messageType, e.str()); mf.IssueMessage(messageType, e.str());
if (messageType == MessageType::FATAL_ERROR) { if (messageType == MessageType::FATAL_ERROR) {
return false; return false;
} }
} }
// Use the old-style mode for backward compatibility. // Use the old-style mode for backward compatibility.
this->Makefile->AddCustomCommandOldStyle(target, outputs, depends, source, mf.AddCustomCommandOldStyle(target, outputs, depends, source, commandLines,
commandLines, comment); comment);
} }
return true; return true;
} }
bool cmAddCustomCommandCommand::CheckOutputs( bool cmAddCustomCommandCommandCheckOutputs(
const std::vector<std::string>& outputs) const std::vector<std::string>& outputs, cmExecutionStatus& status)
{ {
cmMakefile& mf = status.GetMakefile();
for (std::string const& o : outputs) { for (std::string const& o : outputs) {
// Make sure the file will not be generated into the source // Make sure the file will not be generated into the source
// directory during an out of source build. // directory during an out of source build.
if (!this->Makefile->CanIWriteThisFile(o)) { if (!mf.CanIWriteThisFile(o)) {
std::string e = "attempted to have a file \"" + o + std::string e = "attempted to have a file \"" + o +
"\" in a source directory as an output of custom command."; "\" in a source directory as an output of custom command.";
this->SetError(e); status.SetError(e);
cmSystemTools::SetFatalErrorOccured(); cmSystemTools::SetFatalErrorOccured();
return false; return false;
} }
...@@ -425,7 +427,7 @@ bool cmAddCustomCommandCommand::CheckOutputs( ...@@ -425,7 +427,7 @@ bool cmAddCustomCommandCommand::CheckOutputs(
std::ostringstream msg; std::ostringstream msg;
msg << "called with OUTPUT containing a \"" << o[pos] msg << "called with OUTPUT containing a \"" << o[pos]
<< "\". This character is not allowed."; << "\". This character is not allowed.";
this->SetError(msg.str()); status.SetError(msg.str());
return false; return false;
} }
} }
......
...@@ -8,38 +8,9 @@ ...@@ -8,38 +8,9 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "cm_memory.hxx"
#include "cmCommand.h"
class cmExecutionStatus; class cmExecutionStatus;
/** \class cmAddCustomCommandCommand bool cmAddCustomCommandCommand(std::vector<std::string> const& args,
* \brief cmAddCustomCommandCommand defines a new command (rule) that can cmExecutionStatus& status);
* be executed within the build process
*
*/
class cmAddCustomCommandCommand : public cmCommand
{
public:
/**
* This is a virtual constructor for the command.
*/
std::unique_ptr<cmCommand> Clone() override
{
return cm::make_unique<cmAddCustomCommandCommand>();
}
/**
* This is called when the command is first encountered in
* the CMakeLists.txt file.
*/
bool InitialPass(std::vector<std::string> const& args,
cmExecutionStatus& status) override;
protected:
bool CheckOutputs(const std::vector<std::string>& outputs);
};
#endif #endif
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <utility> #include <utility>
#include "cmCustomCommandLines.h" #include "cmCustomCommandLines.h"
#include "cmExecutionStatus.h"
#include "cmGeneratorExpression.h" #include "cmGeneratorExpression.h"
#include "cmGlobalGenerator.h" #include "cmGlobalGenerator.h"
#include "cmMakefile.h" #include "cmMakefile.h"
...@@ -14,17 +15,15 @@ ...@@ -14,17 +15,15 @@
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmTarget.h" #include "cmTarget.h"
class cmExecutionStatus; bool cmAddCustomTargetCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
// cmAddCustomTargetCommand
bool cmAddCustomTargetCommand::InitialPass(
std::vector<std::string> const& args, cmExecutionStatus&)
{ {
if (args.empty()) { if (args.empty()) {
this->SetError("called with incorrect number of arguments"); status.SetError("called with incorrect number of arguments");
return false; return false;
} }
cmMakefile& mf = status.GetMakefile();
std::string const& targetName = args[0]; std::string const& targetName = args[0];
// Check the target name. // Check the target name.
...@@ -33,7 +32,7 @@ bool cmAddCustomTargetCommand::InitialPass( ...@@ -33,7 +32,7 @@ bool cmAddCustomTargetCommand::InitialPass(
e << "called with invalid target name \"" << targetName e << "called with invalid target name \"" << targetName
<< "\". Target names may not contain a slash. " << "\". Target names may not contain a slash. "
<< "Use ADD_CUSTOM_COMMAND to generate files."; << "Use ADD_CUSTOM_COMMAND to generate files.";
this->SetError(e.str()); status.SetError(e.str());
return false; return false;
} }
...@@ -122,7 +121,7 @@ bool cmAddCustomTargetCommand::InitialPass( ...@@ -122,7 +121,7 @@ bool cmAddCustomTargetCommand::InitialPass(
case doing_byproducts: { case doing_byproducts: {
std::string filename; std::string filename;
if (!cmSystemTools::FileIsFullPath(copy)) { if (!cmSystemTools::FileIsFullPath(copy)) {
filename = this->Makefile->GetCurrentBinaryDirectory(); filename = mf.GetCurrentBinaryDirectory();
filename += "/"; filename += "/";
} }
filename += copy; filename += copy;
...@@ -145,7 +144,7 @@ bool cmAddCustomTargetCommand::InitialPass( ...@@ -145,7 +144,7 @@ bool cmAddCustomTargetCommand::InitialPass(
job_pool = copy; job_pool = copy;
break; break;
default: default:
this->SetError("Wrong syntax. Unknown type of argument."); status.SetError("Wrong syntax. Unknown type of argument.");
return false; return false;
} }
} }
...@@ -156,7 +155,7 @@ bool cmAddCustomTargetCommand::InitialPass( ...@@ -156,7 +155,7 @@ bool cmAddCustomTargetCommand::InitialPass(
std::ostringstream msg; std::ostringstream msg;
msg << "called with target name containing a \"" << targetName[pos] msg << "called with target name containing a \"" << targetName[pos]
<< "\". This character is not allowed."; << "\". This character is not allowed.";
this->SetError(msg.str()); status.SetError(msg.str());
return false; return false;
} }
...@@ -168,8 +167,7 @@ bool cmAddCustomTargetCommand::InitialPass( ...@@ -168,8 +167,7 @@ bool cmAddCustomTargetCommand::InitialPass(
if (nameOk) { if (nameOk) {
nameOk = targetName.find(':') == std::string::npos; nameOk = targetName.find(':') == std::string::npos;
} }
if (!nameOk && if (!nameOk && !mf.CheckCMP0037(targetName, cmStateEnums::UTILITY)) {
!this->Makefile->CheckCMP0037(targetName, cmStateEnums::UTILITY)) {
return false; return false;
} }
...@@ -182,39 +180,37 @@ bool cmAddCustomTargetCommand::InitialPass( ...@@ -182,39 +180,37 @@ bool cmAddCustomTargetCommand::InitialPass(
// Enforce name uniqueness. // Enforce name uniqueness.
{ {
std::string msg; std::string msg;
if (!this->Makefile->EnforceUniqueName(targetName, msg, true)) { if (!mf.EnforceUniqueName(targetName, msg, true)) {
this->SetError(msg); status.SetError(msg);
return false; return false;
} }
} }
if (commandLines.empty() && !byproducts.empty()) { if (commandLines.empty() && !byproducts.empty()) {
this->Makefile->IssueMessage( mf.IssueMessage(MessageType::FATAL_ERROR,
MessageType::FATAL_ERROR, "BYPRODUCTS may not be specified without any COMMAND");
"BYPRODUCTS may not be specified without any COMMAND");
return true; return true;
} }
if (commandLines.empty() && uses_terminal) { if (commandLines.empty() && uses_terminal) {
this->Makefile->IssueMessage( mf.IssueMessage(MessageType::FATAL_ERROR,
MessageType::FATAL_ERROR, "USES_TERMINAL may not be specified without any COMMAND");
"USES_TERMINAL may not be specified without any COMMAND");
return true; return true;
} }
if (commandLines.empty() && command_expand_lists) { if (commandLines.empty() && command_expand_lists) {
this->Makefile->IssueMessage( mf.IssueMessage(
MessageType::FATAL_ERROR, MessageType::FATAL_ERROR,
"COMMAND_EXPAND_LISTS may not be specified without any COMMAND"); "COMMAND_EXPAND_LISTS may not be specified without any COMMAND");
return true; return true;
} }
if (uses_terminal && !job_pool.empty()) { if (uses_terminal && !job_pool.empty()) {
this->SetError("JOB_POOL is shadowed by USES_TERMINAL."); status.SetError("JOB_POOL is shadowed by USES_TERMINAL.");
return false; return false;
} }
// Add the utility target to the makefile. // Add the utility target to the makefile.
bool escapeOldStyle = !verbatim; bool escapeOldStyle = !verbatim;
cmTarget* target = this->Makefile->AddUtilityCommand( cmTarget* target = mf.AddUtilityCommand(
targetName, cmMakefile::TargetOrigin::Project, excludeFromAll, targetName, cmMakefile::TargetOrigin::Project, excludeFromAll,
working_directory.c_str(), byproducts, depends, commandLines, working_directory.c_str(), byproducts, depends, commandLines,
escapeOldStyle, comment, uses_terminal, command_expand_lists, job_pool); escapeOldStyle, comment, uses_terminal, command_expand_lists, job_pool);
......
...@@ -8,36 +8,9 @@ ...@@ -8,36 +8,9 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "cm_memory.hxx"
#include "cmCommand.h"
class cmExecutionStatus; class cmExecutionStatus;
/** \class cmAddCustomTargetCommand bool cmAddCustomTargetCommand(std::vector<std::string> const& args,
* \brief Command that adds a target to the build system. cmExecutionStatus& status);
*
* cmAddCustomTargetCommand adds an extra target to the build system.
* This is useful when you would like to add special
* targets like "install,", "clean," and so on.
*/
class cmAddCustomTargetCommand : public cmCommand
{
public:
/**
* This is a virtual constructor for the command.
*/
std::unique_ptr<cmCommand> Clone() override
{