From d8ecc2545798c77ec5bf22bb420a3343d4843ef6 Mon Sep 17 00:00:00 2001 From: Sylvain Joubert Date: Fri, 11 Aug 2017 13:26:33 +0200 Subject: Add PREPEND sub-command to string command --- Help/command/string.rst | 9 ++++ Help/release/dev/string_prepend.rst | 4 ++ Source/cmStringCommand.cxx | 27 ++++++++++++ Source/cmStringCommand.h | 1 + Tests/RunCMake/string/Prepend.cmake | 58 ++++++++++++++++++++++++++ Tests/RunCMake/string/PrependNoArgs-result.txt | 1 + Tests/RunCMake/string/PrependNoArgs-stderr.txt | 4 ++ Tests/RunCMake/string/PrependNoArgs.cmake | 1 + Tests/RunCMake/string/RunCMakeTest.cmake | 3 ++ 9 files changed, 108 insertions(+) create mode 100644 Help/release/dev/string_prepend.rst create mode 100644 Tests/RunCMake/string/Prepend.cmake create mode 100644 Tests/RunCMake/string/PrependNoArgs-result.txt create mode 100644 Tests/RunCMake/string/PrependNoArgs-stderr.txt create mode 100644 Tests/RunCMake/string/PrependNoArgs.cmake diff --git a/Help/command/string.rst b/Help/command/string.rst index 4f0c45c..ba4a412 100644 --- a/Help/command/string.rst +++ b/Help/command/string.rst @@ -132,6 +132,15 @@ APPEND Append all the input arguments to the string. +PREPEND +""""""" + +:: + + string(PREPEND [...]) + +Prepend all the input arguments to the string. + CONCAT """""" diff --git a/Help/release/dev/string_prepend.rst b/Help/release/dev/string_prepend.rst new file mode 100644 index 0000000..f896e91 --- /dev/null +++ b/Help/release/dev/string_prepend.rst @@ -0,0 +1,4 @@ +string_prepend +-------------- + +* The :command:`string` command learned a new ``PREPEND`` subcommand. diff --git a/Source/cmStringCommand.cxx b/Source/cmStringCommand.cxx index 5a6cf48..b07eae1 100644 --- a/Source/cmStringCommand.cxx +++ b/Source/cmStringCommand.cxx @@ -62,6 +62,9 @@ bool cmStringCommand::InitialPass(std::vector const& args, if (subCommand == "APPEND") { return this->HandleAppendCommand(args); } + if (subCommand == "PREPEND") { + return this->HandlePrependCommand(args); + } if (subCommand == "CONCAT") { return this->HandleConcatCommand(args); } @@ -643,6 +646,30 @@ bool cmStringCommand::HandleAppendCommand(std::vector const& args) return true; } +bool cmStringCommand::HandlePrependCommand( + std::vector const& args) +{ + if (args.size() < 2) { + this->SetError("sub-command PREPEND requires at least one argument."); + return false; + } + + // Skip if nothing to prepend. + if (args.size() < 3) { + return true; + } + + const std::string& variable = args[1]; + + std::string value = cmJoin(cmMakeRange(args).advance(2), std::string()); + const char* oldValue = this->Makefile->GetDefinition(variable); + if (oldValue) { + value += oldValue; + } + this->Makefile->AddDefinition(variable, value.c_str()); + return true; +} + bool cmStringCommand::HandleConcatCommand(std::vector const& args) { if (args.size() < 2) { diff --git a/Source/cmStringCommand.h b/Source/cmStringCommand.h index 88015ad..0dded15 100644 --- a/Source/cmStringCommand.h +++ b/Source/cmStringCommand.h @@ -46,6 +46,7 @@ protected: bool HandleLengthCommand(std::vector const& args); bool HandleSubstringCommand(std::vector const& args); bool HandleAppendCommand(std::vector const& args); + bool HandlePrependCommand(std::vector const& args); bool HandleConcatCommand(std::vector const& args); bool HandleStripCommand(std::vector const& args); bool HandleRandomCommand(std::vector const& args); diff --git a/Tests/RunCMake/string/Prepend.cmake b/Tests/RunCMake/string/Prepend.cmake new file mode 100644 index 0000000..8eaeebb --- /dev/null +++ b/Tests/RunCMake/string/Prepend.cmake @@ -0,0 +1,58 @@ +set(out) +string(PREPEND out) +if(DEFINED out) + message(FATAL_ERROR "\"string(PREPEND out)\" set out to \"${out}\"") +endif() + +set(out "") +string(PREPEND out) +if(NOT out STREQUAL "") + message(FATAL_ERROR "\"string(PREPEND out)\" set out to \"${out}\"") +endif() + +set(out x) +string(PREPEND out) +if(NOT out STREQUAL "x") + message(FATAL_ERROR "\"string(PREPEND out)\" set out to \"${out}\"") +endif() + + +set(out) +string(PREPEND out a) +if(NOT out STREQUAL "a") + message(FATAL_ERROR "\"string(PREPEND out a)\" set out to \"${out}\"") +endif() + +set(out "") +string(PREPEND out a) +if(NOT out STREQUAL "a") + message(FATAL_ERROR "\"string(PREPEND out a)\" set out to \"${out}\"") +endif() + +set(out x) +string(PREPEND out a) +if(NOT out STREQUAL "ax") + message(FATAL_ERROR "\"string(PREPEND out a)\" set out to \"${out}\"") +endif() + + +set(out x) +string(PREPEND out a "b") +if(NOT out STREQUAL "abx") + message(FATAL_ERROR "\"string(PREPEND out a \"b\")\" set out to \"${out}\"") +endif() + +set(b) +set(out x) +string(PREPEND out ${b}) +if(NOT out STREQUAL "x") + message(FATAL_ERROR "\"string(PREPEND out \${b})\" set out to \"${out}\"") +endif() + +set(b b) +set(out x) +string(PREPEND out a "${b}" [[ +${c}]]) +if(NOT out STREQUAL "ab\${c}x") + message(FATAL_ERROR "\"string(PREPEND out a \"\${b}\" [[\${c}]])\" set out to \"${out}\"") +endif() diff --git a/Tests/RunCMake/string/PrependNoArgs-result.txt b/Tests/RunCMake/string/PrependNoArgs-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/string/PrependNoArgs-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/string/PrependNoArgs-stderr.txt b/Tests/RunCMake/string/PrependNoArgs-stderr.txt new file mode 100644 index 0000000..8d433f9 --- /dev/null +++ b/Tests/RunCMake/string/PrependNoArgs-stderr.txt @@ -0,0 +1,4 @@ +CMake Error at PrependNoArgs.cmake:1 \(string\): + string sub-command PREPEND requires at least one argument. +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/string/PrependNoArgs.cmake b/Tests/RunCMake/string/PrependNoArgs.cmake new file mode 100644 index 0000000..f324f28 --- /dev/null +++ b/Tests/RunCMake/string/PrependNoArgs.cmake @@ -0,0 +1 @@ +string(PREPEND) diff --git a/Tests/RunCMake/string/RunCMakeTest.cmake b/Tests/RunCMake/string/RunCMakeTest.cmake index 32b61b5..513d1e3 100644 --- a/Tests/RunCMake/string/RunCMakeTest.cmake +++ b/Tests/RunCMake/string/RunCMakeTest.cmake @@ -3,6 +3,9 @@ include(RunCMake) run_cmake(Append) run_cmake(AppendNoArgs) +run_cmake(Prepend) +run_cmake(PrependNoArgs) + run_cmake(Concat) run_cmake(ConcatNoArgs) -- cgit v0.12