From dfaa87f1b3335b2e80f68726fd5e1b3bbd87d667 Mon Sep 17 00:00:00 2001 From: Regina Pfeifer Date: Mon, 8 Apr 2019 09:55:52 +0200 Subject: cmState: Support BuiltinCommands as free functions --- Source/cmCommands.cxx | 5 ++--- Source/cmEnableTestingCommand.cxx | 11 ++++------- Source/cmEnableTestingCommand.h | 26 +++----------------------- Source/cmReturnCommand.cxx | 4 ++-- Source/cmReturnCommand.h | 30 +++--------------------------- Source/cmState.cxx | 17 +++++++++++++++++ Source/cmState.h | 3 +++ 7 files changed, 34 insertions(+), 62 deletions(-) diff --git a/Source/cmCommands.cxx b/Source/cmCommands.cxx index 9ae2f71..f351ff8 100644 --- a/Source/cmCommands.cxx +++ b/Source/cmCommands.cxx @@ -162,7 +162,7 @@ void GetScriptingCommands(cmState* state) state->AddBuiltinCommand("option", cm::make_unique()); state->AddBuiltinCommand("cmake_parse_arguments", cm::make_unique()); - state->AddBuiltinCommand("return", cm::make_unique()); + state->AddBuiltinCommand("return", cmReturnCommand); state->AddBuiltinCommand("separate_arguments", cm::make_unique()); state->AddBuiltinCommand("set", cm::make_unique()); @@ -255,8 +255,7 @@ void GetProjectCommands(cmState* state) cm::make_unique()); state->AddBuiltinCommand("enable_language", cm::make_unique()); - state->AddBuiltinCommand("enable_testing", - cm::make_unique()); + state->AddBuiltinCommand("enable_testing", cmEnableTestingCommand); state->AddBuiltinCommand("get_source_file_property", cm::make_unique()); state->AddBuiltinCommand("get_target_property", diff --git a/Source/cmEnableTestingCommand.cxx b/Source/cmEnableTestingCommand.cxx index 6a64450..89212c8 100644 --- a/Source/cmEnableTestingCommand.cxx +++ b/Source/cmEnableTestingCommand.cxx @@ -2,15 +2,12 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmEnableTestingCommand.h" +#include "cmExecutionStatus.h" #include "cmMakefile.h" -class cmExecutionStatus; - -// we do this in the final pass so that we now the subdirs have all -// been defined -bool cmEnableTestingCommand::InitialPass(std::vector const&, - cmExecutionStatus&) +bool cmEnableTestingCommand(std::vector const&, + cmExecutionStatus& status) { - this->Makefile->AddDefinition("CMAKE_TESTING_ENABLED", "1"); + status.GetMakefile().AddDefinition("CMAKE_TESTING_ENABLED", "1"); return true; } diff --git a/Source/cmEnableTestingCommand.h b/Source/cmEnableTestingCommand.h index fd50ebc..e4593f2 100644 --- a/Source/cmEnableTestingCommand.h +++ b/Source/cmEnableTestingCommand.h @@ -8,13 +8,9 @@ #include #include -#include "cm_memory.hxx" - -#include "cmCommand.h" - class cmExecutionStatus; -/** \class cmEnableTestingCommand +/** * \brief Enable testing for this directory and below. * * Produce the output testfile. This produces a file in the build directory @@ -27,23 +23,7 @@ class cmExecutionStatus; * Note that CTest expects to find this file in the build directory root; * therefore, this command should be in the source directory root too. */ -class cmEnableTestingCommand : public cmCommand -{ -public: - /** - * This is a virtual constructor for the command. - */ - std::unique_ptr Clone() override - { - return cm::make_unique(); - } - - /** - * This is called when the command is first encountered in - * the CMakeLists.txt file. - */ - bool InitialPass(std::vector const&, - cmExecutionStatus&) override; -}; +bool cmEnableTestingCommand(std::vector const&, + cmExecutionStatus&); #endif diff --git a/Source/cmReturnCommand.cxx b/Source/cmReturnCommand.cxx index ceea8b4..5905669 100644 --- a/Source/cmReturnCommand.cxx +++ b/Source/cmReturnCommand.cxx @@ -5,8 +5,8 @@ #include "cmExecutionStatus.h" // cmReturnCommand -bool cmReturnCommand::InitialPass(std::vector const&, - cmExecutionStatus& status) +bool cmReturnCommand(std::vector const&, + cmExecutionStatus& status) { status.SetReturnInvoked(); return true; diff --git a/Source/cmReturnCommand.h b/Source/cmReturnCommand.h index e9264d2..2404a36 100644 --- a/Source/cmReturnCommand.h +++ b/Source/cmReturnCommand.h @@ -8,34 +8,10 @@ #include #include -#include "cm_memory.hxx" - -#include "cmCommand.h" - class cmExecutionStatus; -/** \class cmReturnCommand - * \brief Return from a directory or function - * - * cmReturnCommand returns from a directory or function - */ -class cmReturnCommand : public cmCommand -{ -public: - /** - * This is a virtual constructor for the command. - */ - std::unique_ptr Clone() override - { - return cm::make_unique(); - } - - /** - * This is called when the command is first encountered in - * the CMakeLists.txt file. - */ - bool InitialPass(std::vector const& args, - cmExecutionStatus& status) override; -}; +/// Return from a directory or function +bool cmReturnCommand(std::vector const& args, + cmExecutionStatus& status); #endif diff --git a/Source/cmState.cxx b/Source/cmState.cxx index 441f11c..0b12a65 100644 --- a/Source/cmState.cxx +++ b/Source/cmState.cxx @@ -432,6 +432,23 @@ void cmState::AddBuiltinCommand(std::string const& name, Command command) this->BuiltinCommands.emplace(name, std::move(command)); } +void cmState::AddBuiltinCommand(std::string const& name, + BuiltinCommand command) +{ + this->AddBuiltinCommand( + name, + [command](const std::vector& args, + cmExecutionStatus& status) -> bool { + std::vector expandedArguments; + if (!status.GetMakefile().ExpandArguments(args, expandedArguments)) { + // There was an error expanding arguments. It was already + // reported, so we can skip this command without error. + return true; + } + return command(expandedArguments, status); + }); +} + void cmState::AddDisallowedCommand(std::string const& name, std::unique_ptr command, cmPolicies::PolicyID policy, diff --git a/Source/cmState.h b/Source/cmState.h index 07c1c9b..8847f3b 100644 --- a/Source/cmState.h +++ b/Source/cmState.h @@ -145,6 +145,8 @@ public: using Command = std::function const&, cmExecutionStatus&)>; + using BuiltinCommand = bool (*)(std::vector const&, + cmExecutionStatus&); // Returns a command from its name, case insensitive, or nullptr Command GetCommand(std::string const& name) const; @@ -154,6 +156,7 @@ public: void AddBuiltinCommand(std::string const& name, std::unique_ptr command); void AddBuiltinCommand(std::string const& name, Command command); + void AddBuiltinCommand(std::string const& name, BuiltinCommand command); void AddDisallowedCommand(std::string const& name, std::unique_ptr command, cmPolicies::PolicyID policy, const char* message); -- cgit v0.12