Commit 9ba0bf60 authored by wahikihiki's avatar wahikihiki

cmA*Command: Turn into free functions

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