Merge topic 'string_prepend'

d8ecc254 Add PREPEND sub-command to string command

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !1129
This commit is contained in:
Brad King 2017-08-16 14:50:34 +00:00 committed by Kitware Robot
commit d817bbb8df
9 changed files with 108 additions and 0 deletions

View File

@ -132,6 +132,15 @@ APPEND
Append all the input arguments to the string.
PREPEND
"""""""
::
string(PREPEND <string variable> [<input>...])
Prepend all the input arguments to the string.
CONCAT
""""""

View File

@ -0,0 +1,4 @@
string_prepend
--------------
* The :command:`string` command learned a new ``PREPEND`` subcommand.

View File

@ -62,6 +62,9 @@ bool cmStringCommand::InitialPass(std::vector<std::string> 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<std::string> const& args)
return true;
}
bool cmStringCommand::HandlePrependCommand(
std::vector<std::string> 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<std::string> const& args)
{
if (args.size() < 2) {

View File

@ -46,6 +46,7 @@ protected:
bool HandleLengthCommand(std::vector<std::string> const& args);
bool HandleSubstringCommand(std::vector<std::string> const& args);
bool HandleAppendCommand(std::vector<std::string> const& args);
bool HandlePrependCommand(std::vector<std::string> const& args);
bool HandleConcatCommand(std::vector<std::string> const& args);
bool HandleStripCommand(std::vector<std::string> const& args);
bool HandleRandomCommand(std::vector<std::string> const& args);

View File

@ -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()

View File

@ -0,0 +1 @@
1

View File

@ -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\)

View File

@ -0,0 +1 @@
string(PREPEND)

View File

@ -3,6 +3,9 @@ include(RunCMake)
run_cmake(Append)
run_cmake(AppendNoArgs)
run_cmake(Prepend)
run_cmake(PrependNoArgs)
run_cmake(Concat)
run_cmake(ConcatNoArgs)