cmSourceGroupCommand: Port away from cmCommand

This commit is contained in:
Regina Pfeifer
2019-09-18 17:39:47 +02:00
parent 42e413bcbb
commit d5c63f073f
3 changed files with 49 additions and 85 deletions
+1 -2
View File
@@ -288,8 +288,7 @@ void GetProjectCommands(cmState* state)
state->AddBuiltinCommand("qt_wrap_cpp", cmQTWrapCPPCommand); state->AddBuiltinCommand("qt_wrap_cpp", cmQTWrapCPPCommand);
state->AddBuiltinCommand("qt_wrap_ui", cmQTWrapUICommand); state->AddBuiltinCommand("qt_wrap_ui", cmQTWrapUICommand);
state->AddBuiltinCommand("remove_definitions", cmRemoveDefinitionsCommand); state->AddBuiltinCommand("remove_definitions", cmRemoveDefinitionsCommand);
state->AddBuiltinCommand("source_group", state->AddBuiltinCommand("source_group", cmSourceGroupCommand);
cm::make_unique<cmSourceGroupCommand>());
state->AddDisallowedCommand( state->AddDisallowedCommand(
"export_library_dependencies", cmExportLibraryDependenciesCommand, "export_library_dependencies", cmExportLibraryDependenciesCommand,
+46 -34
View File
@@ -3,16 +3,22 @@
#include "cmSourceGroupCommand.h" #include "cmSourceGroupCommand.h"
#include <cstddef> #include <cstddef>
#include <map>
#include <set> #include <set>
#include <utility> #include <utility>
#include "cmAlgorithms.h" #include "cmAlgorithms.h"
#include "cmExecutionStatus.h"
#include "cmMakefile.h" #include "cmMakefile.h"
#include "cmSourceGroup.h" #include "cmSourceGroup.h"
#include "cmStringAlgorithms.h" #include "cmStringAlgorithms.h"
#include "cmSystemTools.h" #include "cmSystemTools.h"
namespace { namespace {
using ParsedArguments = std::map<std::string, std::vector<std::string>>;
using ExpectedOptions = std::vector<std::string>;
const std::string kTreeOptionName = "TREE"; const std::string kTreeOptionName = "TREE";
const std::string kPrefixOptionName = "PREFIX"; const std::string kPrefixOptionName = "PREFIX";
const std::string kFilesOptionName = "FILES"; const std::string kFilesOptionName = "FILES";
@@ -117,13 +123,8 @@ bool addFilesToItsSourceGroups(const std::string& root,
return true; return true;
} }
}
class cmExecutionStatus; ExpectedOptions getExpectedOptions()
// cmSourceGroupCommand
cmSourceGroupCommand::ExpectedOptions
cmSourceGroupCommand::getExpectedOptions() const
{ {
ExpectedOptions options; ExpectedOptions options;
@@ -135,15 +136,14 @@ cmSourceGroupCommand::getExpectedOptions() const
return options; return options;
} }
bool cmSourceGroupCommand::isExpectedOption( bool isExpectedOption(const std::string& argument,
const std::string& argument, const ExpectedOptions& expectedOptions) const ExpectedOptions& expectedOptions)
{ {
return cmContains(expectedOptions, argument); return cmContains(expectedOptions, argument);
} }
void cmSourceGroupCommand::parseArguments( void parseArguments(const std::vector<std::string>& args,
const std::vector<std::string>& args, ParsedArguments& parsedArguments)
cmSourceGroupCommand::ParsedArguments& parsedArguments)
{ {
const ExpectedOptions expectedOptions = getExpectedOptions(); const ExpectedOptions expectedOptions = getExpectedOptions();
size_t i = 0; size_t i = 0;
@@ -172,21 +172,35 @@ void cmSourceGroupCommand::parseArguments(
} }
} }
bool cmSourceGroupCommand::InitialPass(std::vector<std::string> const& args, } // namespace
cmExecutionStatus&)
static bool checkArgumentsPreconditions(const ParsedArguments& parsedArguments,
std::string& errorMsg);
static bool processTree(cmMakefile& mf, ParsedArguments& parsedArguments,
std::string& errorMsg);
static bool checkSingleParameterArgumentPreconditions(
const std::string& argument, const ParsedArguments& parsedArguments,
std::string& errorMsg);
bool cmSourceGroupCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{ {
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();
// If only two arguments are given, the pre-1.8 version of the // If only two arguments are given, the pre-1.8 version of the
// command is being invoked. // command is being invoked.
if (args.size() == 2 && args[1] != "FILES") { if (args.size() == 2 && args[1] != "FILES") {
cmSourceGroup* sg = this->Makefile->GetOrCreateSourceGroup(args[0]); cmSourceGroup* sg = mf.GetOrCreateSourceGroup(args[0]);
if (!sg) { if (!sg) {
this->SetError("Could not create or find source group"); status.SetError("Could not create or find source group");
return false; return false;
} }
@@ -204,21 +218,21 @@ bool cmSourceGroupCommand::InitialPass(std::vector<std::string> const& args,
} }
if (parsedArguments.find(kTreeOptionName) != parsedArguments.end()) { if (parsedArguments.find(kTreeOptionName) != parsedArguments.end()) {
if (!processTree(parsedArguments, errorMsg)) { if (!processTree(mf, parsedArguments, errorMsg)) {
this->SetError(errorMsg); status.SetError(errorMsg);
return false; return false;
} }
} else { } else {
if (parsedArguments.find(kSourceGroupOptionName) == if (parsedArguments.find(kSourceGroupOptionName) ==
parsedArguments.end()) { parsedArguments.end()) {
this->SetError("Missing source group name."); status.SetError("Missing source group name.");
return false; return false;
} }
cmSourceGroup* sg = this->Makefile->GetOrCreateSourceGroup(args[0]); cmSourceGroup* sg = mf.GetOrCreateSourceGroup(args[0]);
if (!sg) { if (!sg) {
this->SetError("Could not create or find source group"); status.SetError("Could not create or find source group");
return false; return false;
} }
@@ -234,8 +248,7 @@ bool cmSourceGroupCommand::InitialPass(std::vector<std::string> const& args,
for (auto const& filesArg : filesArguments) { for (auto const& filesArg : filesArguments) {
std::string src = filesArg; std::string src = filesArg;
if (!cmSystemTools::FileIsFullPath(src)) { if (!cmSystemTools::FileIsFullPath(src)) {
src = src = cmStrCat(mf.GetCurrentSourceDirectory(), '/', filesArg);
cmStrCat(this->Makefile->GetCurrentSourceDirectory(), '/', filesArg);
} }
src = cmSystemTools::CollapseFullPath(src); src = cmSystemTools::CollapseFullPath(src);
sg->AddGroupFile(src); sg->AddGroupFile(src);
@@ -245,8 +258,8 @@ bool cmSourceGroupCommand::InitialPass(std::vector<std::string> const& args,
return true; return true;
} }
bool cmSourceGroupCommand::checkArgumentsPreconditions( static bool checkArgumentsPreconditions(const ParsedArguments& parsedArguments,
const ParsedArguments& parsedArguments, std::string& errorMsg) const std::string& errorMsg)
{ {
return checkSingleParameterArgumentPreconditions( return checkSingleParameterArgumentPreconditions(
kPrefixOptionName, parsedArguments, errorMsg) && kPrefixOptionName, parsedArguments, errorMsg) &&
@@ -256,8 +269,8 @@ bool cmSourceGroupCommand::checkArgumentsPreconditions(
parsedArguments, errorMsg); parsedArguments, errorMsg);
} }
bool cmSourceGroupCommand::processTree(ParsedArguments& parsedArguments, static bool processTree(cmMakefile& mf, ParsedArguments& parsedArguments,
std::string& errorMsg) std::string& errorMsg)
{ {
const std::string root = const std::string root =
cmSystemTools::CollapseFullPath(parsedArguments[kTreeOptionName].front()); cmSystemTools::CollapseFullPath(parsedArguments[kTreeOptionName].front());
@@ -265,9 +278,8 @@ bool cmSourceGroupCommand::processTree(ParsedArguments& parsedArguments,
? "" ? ""
: parsedArguments[kPrefixOptionName].front(); : parsedArguments[kPrefixOptionName].front();
const std::vector<std::string> filesVector = const std::vector<std::string> filesVector = prepareFilesPathsForTree(
prepareFilesPathsForTree(parsedArguments[kFilesOptionName], parsedArguments[kFilesOptionName], mf.GetCurrentSourceDirectory());
this->Makefile->GetCurrentSourceDirectory());
if (!rootIsPrefix(root, filesVector, errorMsg)) { if (!rootIsPrefix(root, filesVector, errorMsg)) {
return false; return false;
@@ -276,13 +288,13 @@ bool cmSourceGroupCommand::processTree(ParsedArguments& parsedArguments,
std::set<std::string> sourceGroupPaths = std::set<std::string> sourceGroupPaths =
getSourceGroupFilesPaths(root, filesVector); getSourceGroupFilesPaths(root, filesVector);
return addFilesToItsSourceGroups(root, sourceGroupPaths, prefix, return addFilesToItsSourceGroups(root, sourceGroupPaths, prefix, mf,
*(this->Makefile), errorMsg); errorMsg);
} }
bool cmSourceGroupCommand::checkSingleParameterArgumentPreconditions( static bool checkSingleParameterArgumentPreconditions(
const std::string& argument, const ParsedArguments& parsedArguments, const std::string& argument, const ParsedArguments& parsedArguments,
std::string& errorMsg) const std::string& errorMsg)
{ {
auto foundArgument = parsedArguments.find(argument); auto foundArgument = parsedArguments.find(argument);
if (foundArgument != parsedArguments.end()) { if (foundArgument != parsedArguments.end()) {
+2 -49
View File
@@ -5,59 +5,12 @@
#include "cmConfigure.h" // IWYU pragma: keep #include "cmConfigure.h" // IWYU pragma: keep
#include <map>
#include <string> #include <string>
#include <vector> #include <vector>
#include <cm/memory>
#include "cmCommand.h"
class cmExecutionStatus; class cmExecutionStatus;
/** \class cmSourceGroupCommand bool cmSourceGroupCommand(std::vector<std::string> const& args,
* \brief Adds a cmSourceGroup to the cmMakefile. cmExecutionStatus& status);
*
* cmSourceGroupCommand is used to define cmSourceGroups which split up
* source files in to named, organized groups in the generated makefiles.
*/
class cmSourceGroupCommand : public cmCommand
{
public:
/**
* This is a virtual constructor for the command.
*/
std::unique_ptr<cmCommand> Clone() override
{
return cm::make_unique<cmSourceGroupCommand>();
}
/**
* 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;
private:
using ParsedArguments = std::map<std::string, std::vector<std::string>>;
using ExpectedOptions = std::vector<std::string>;
ExpectedOptions getExpectedOptions() const;
bool isExpectedOption(const std::string& argument,
const ExpectedOptions& expectedOptions);
void parseArguments(const std::vector<std::string>& args,
cmSourceGroupCommand::ParsedArguments& parsedArguments);
bool processTree(ParsedArguments& parsedArguments, std::string& errorMsg);
bool checkArgumentsPreconditions(const ParsedArguments& parsedArguments,
std::string& errorMsg) const;
bool checkSingleParameterArgumentPreconditions(
const std::string& argument, const ParsedArguments& parsedArguments,
std::string& errorMsg) const;
};
#endif #endif