summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAshley Whetter <ashley@awhetter.co.uk>2016-01-28 21:29:10 (GMT)
committerBrad King <brad.king@kitware.com>2016-02-03 16:13:17 (GMT)
commit0205f882ae252998686f65b843a758268b4c62bc (patch)
tree33c19806797e81f6ef65ee3fbe53b0c1188dae72
parenta58abc69c23ef30fc2215eb72878af29f7e860fd (diff)
downloadCMake-0205f882ae252998686f65b843a758268b4c62bc.zip
CMake-0205f882ae252998686f65b843a758268b4c62bc.tar.gz
CMake-0205f882ae252998686f65b843a758268b4c62bc.tar.bz2
list: Add FILTER subcommand (#3986)
Create a `list(FILTER)` command to filter lists by regular expression.
-rw-r--r--Help/command/list.rst13
-rw-r--r--Help/release/dev/list-FILTER-command.rst5
-rw-r--r--Source/cmListCommand.cxx109
-rw-r--r--Source/cmListCommand.h6
-rw-r--r--Tests/RunCMake/list/EmptyFilterRegex-result.txt1
-rw-r--r--Tests/RunCMake/list/EmptyFilterRegex-stderr.txt0
-rw-r--r--Tests/RunCMake/list/EmptyFilterRegex.cmake2
-rw-r--r--Tests/RunCMake/list/FILTER-NotList-result.txt1
-rw-r--r--Tests/RunCMake/list/FILTER-NotList-stderr.txt4
-rw-r--r--Tests/RunCMake/list/FILTER-NotList.cmake2
-rw-r--r--Tests/RunCMake/list/FILTER-REGEX-InvalidMode-result.txt1
-rw-r--r--Tests/RunCMake/list/FILTER-REGEX-InvalidMode-stderr.txt4
-rw-r--r--Tests/RunCMake/list/FILTER-REGEX-InvalidMode.cmake2
-rw-r--r--Tests/RunCMake/list/FILTER-REGEX-InvalidOperator-result.txt1
-rw-r--r--Tests/RunCMake/list/FILTER-REGEX-InvalidOperator-stderr.txt4
-rw-r--r--Tests/RunCMake/list/FILTER-REGEX-InvalidOperator.cmake2
-rw-r--r--Tests/RunCMake/list/FILTER-REGEX-InvalidRegex-result.txt1
-rw-r--r--Tests/RunCMake/list/FILTER-REGEX-InvalidRegex-stderr.txt4
-rw-r--r--Tests/RunCMake/list/FILTER-REGEX-InvalidRegex.cmake2
-rw-r--r--Tests/RunCMake/list/FILTER-REGEX-TooManyArguments-result.txt1
-rw-r--r--Tests/RunCMake/list/FILTER-REGEX-TooManyArguments-stderr.txt4
-rw-r--r--Tests/RunCMake/list/FILTER-REGEX-TooManyArguments.cmake2
-rw-r--r--Tests/RunCMake/list/FILTER-REGEX-Valid0-result.txt1
-rw-r--r--Tests/RunCMake/list/FILTER-REGEX-Valid0-stderr.txt2
-rw-r--r--Tests/RunCMake/list/FILTER-REGEX-Valid0.cmake4
-rw-r--r--Tests/RunCMake/list/FILTER-REGEX-Valid1-result.txt1
-rw-r--r--Tests/RunCMake/list/FILTER-REGEX-Valid1-stderr.txt2
-rw-r--r--Tests/RunCMake/list/FILTER-REGEX-Valid1.cmake4
-rw-r--r--Tests/RunCMake/list/RunCMakeTest.cmake9
29 files changed, 191 insertions, 3 deletions
diff --git a/Help/command/list.rst b/Help/command/list.rst
index a7a05c7..f6b75bc 100644
--- a/Help/command/list.rst
+++ b/Help/command/list.rst
@@ -9,6 +9,7 @@ List operations.
list(GET <list> <element index> [<element index> ...]
<output variable>)
list(APPEND <list> [<element> ...])
+ list(FILTER <list> <INCLUDE|EXCLUDE> REGEX <regular_expression>)
list(FIND <list> <value> <output variable>)
list(INSERT <list> <element_index> <element> [<element> ...])
list(REMOVE_ITEM <list> <value> [<value> ...])
@@ -23,6 +24,12 @@ List operations.
``APPEND`` will append elements to the list.
+``FILTER`` will include or remove items from the list that match the
+mode's pattern.
+In ``REGEX`` mode, items will be matched against the given regular expression.
+For more information on regular expressions see also the :command:`string`
+command.
+
``FIND`` will return the index of the element specified in the list or -1
if it wasn't found.
@@ -38,9 +45,9 @@ difference is that ``REMOVE_ITEM`` will remove the given items, while
``SORT`` sorts the list in-place alphabetically.
-The list subcommands ``APPEND``, ``INSERT``, ``REMOVE_AT``, ``REMOVE_ITEM``,
-``REMOVE_DUPLICATES``, ``REVERSE`` and ``SORT`` may create new values for
-the list within the current CMake variable scope. Similar to the
+The list subcommands ``APPEND``, ``INSERT``, ``FILTER``, ``REMOVE_AT``,
+``REMOVE_ITEM``, ``REMOVE_DUPLICATES``, ``REVERSE`` and ``SORT`` may create new
+values for the list within the current CMake variable scope. Similar to the
:command:`set` command, the LIST command creates new variable values in the
current scope, even if the list itself is actually defined in a parent
scope. To propagate the results of these operations upwards, use
diff --git a/Help/release/dev/list-FILTER-command.rst b/Help/release/dev/list-FILTER-command.rst
new file mode 100644
index 0000000..3fee4f0
--- /dev/null
+++ b/Help/release/dev/list-FILTER-command.rst
@@ -0,0 +1,5 @@
+list-FILTER-command
+-------------------
+
+* The :command:`list` command gained a ``FILTER`` sub-command to filter
+ list elements by regular expression.
diff --git a/Source/cmListCommand.cxx b/Source/cmListCommand.cxx
index 6041fb7..15a1af5 100644
--- a/Source/cmListCommand.cxx
+++ b/Source/cmListCommand.cxx
@@ -14,6 +14,7 @@
#include <cmsys/SystemTools.hxx>
#include "cmAlgorithms.h"
+#include <algorithm>
#include <stdlib.h> // required for atoi
#include <ctype.h>
#include <assert.h>
@@ -68,6 +69,10 @@ bool cmListCommand
{
return this->HandleReverseCommand(args);
}
+ if(subCommand == "FILTER")
+ {
+ return this->HandleFilterCommand(args);
+ }
std::string e = "does not recognize sub-command "+subCommand;
this->SetError(e);
@@ -517,3 +522,107 @@ bool cmListCommand::HandleRemoveAtCommand(
return true;
}
+//----------------------------------------------------------------------------
+bool cmListCommand::HandleFilterCommand(
+ std::vector<std::string> const& args)
+{
+ if(args.size() < 2)
+ {
+ this->SetError("sub-command FILTER requires a list to be specified.");
+ return false;
+ }
+
+ if(args.size() < 3)
+ {
+ this->SetError("sub-command FILTER requires an operator to be specified.");
+ return false;
+ }
+
+ if(args.size() < 4)
+ {
+ this->SetError("sub-command FILTER requires a mode to be specified.");
+ return false;
+ }
+
+ const std::string& listName = args[1];
+ // expand the variable
+ std::vector<std::string> varArgsExpanded;
+ if ( !this->GetList(varArgsExpanded, listName) )
+ {
+ this->SetError("sub-command FILTER requires list to be present.");
+ return false;
+ }
+
+ const std::string& op = args[2];
+ bool includeMatches;
+ if(op == "INCLUDE")
+ {
+ includeMatches = true;
+ }
+ else if(op == "EXCLUDE")
+ {
+ includeMatches = false;
+ }
+ else
+ {
+ this->SetError("sub-command FILTER does not recognize operator " + op);
+ return false;
+ }
+
+ const std::string& mode = args[3];
+ if(mode == "REGEX")
+ {
+ if(args.size() != 5)
+ {
+ this->SetError("sub-command FILTER, mode REGEX "
+ "requires five arguments.");
+ return false;
+ }
+ return this->FilterRegex(args, includeMatches, listName, varArgsExpanded);
+ }
+
+ this->SetError("sub-command FILTER does not recognize mode " + mode);
+ return false;
+}
+
+//----------------------------------------------------------------------------
+class MatchesRegex {
+public:
+ MatchesRegex(cmsys::RegularExpression& in_regex, bool in_includeMatches)
+ : regex(in_regex), includeMatches(in_includeMatches) {}
+
+ bool operator()(const std::string& target) {
+ return regex.find(target) ^ includeMatches;
+ }
+
+private:
+ cmsys::RegularExpression& regex;
+ const bool includeMatches;
+};
+
+bool cmListCommand::FilterRegex(std::vector<std::string> const& args,
+ bool includeMatches,
+ std::string const& listName,
+ std::vector<std::string>& varArgsExpanded)
+{
+ const std::string& pattern = args[4];
+ cmsys::RegularExpression regex(pattern);
+ if(!regex.is_valid())
+ {
+ std::string error = "sub-command FILTER, mode REGEX ";
+ error += "failed to compile regex \"";
+ error += pattern;
+ error += "\".";
+ this->SetError(error);
+ return false;
+ }
+
+ std::vector<std::string>::iterator argsBegin = varArgsExpanded.begin();
+ std::vector<std::string>::iterator argsEnd = varArgsExpanded.end();
+ std::vector<std::string>::iterator newArgsEnd =
+ std::remove_if(argsBegin, argsEnd, MatchesRegex(regex, includeMatches));
+
+ std::string value = cmJoin(cmMakeRange(argsBegin, newArgsEnd), ";");
+ this->Makefile->AddDefinition(listName, value.c_str());
+ return true;
+}
diff --git a/Source/cmListCommand.h b/Source/cmListCommand.h
index 5ea1d9f..25edee8 100644
--- a/Source/cmListCommand.h
+++ b/Source/cmListCommand.h
@@ -58,6 +58,12 @@ protected:
bool HandleRemoveDuplicatesCommand(std::vector<std::string> const& args);
bool HandleSortCommand(std::vector<std::string> const& args);
bool HandleReverseCommand(std::vector<std::string> const& args);
+ bool HandleFilterCommand(std::vector<std::string> const& args);
+ bool FilterRegex(std::vector<std::string> const& args,
+ bool includeMatches,
+ std::string const& listName,
+ std::vector<std::string>& varArgsExpanded
+ );
bool GetList(std::vector<std::string>& list, const std::string& var);
diff --git a/Tests/RunCMake/list/EmptyFilterRegex-result.txt b/Tests/RunCMake/list/EmptyFilterRegex-result.txt
new file mode 100644
index 0000000..573541a
--- /dev/null
+++ b/Tests/RunCMake/list/EmptyFilterRegex-result.txt
@@ -0,0 +1 @@
+0
diff --git a/Tests/RunCMake/list/EmptyFilterRegex-stderr.txt b/Tests/RunCMake/list/EmptyFilterRegex-stderr.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Tests/RunCMake/list/EmptyFilterRegex-stderr.txt
diff --git a/Tests/RunCMake/list/EmptyFilterRegex.cmake b/Tests/RunCMake/list/EmptyFilterRegex.cmake
new file mode 100644
index 0000000..33849cd
--- /dev/null
+++ b/Tests/RunCMake/list/EmptyFilterRegex.cmake
@@ -0,0 +1,2 @@
+set(mylist "")
+list(FILTER mylist INCLUDE REGEX "^FILTER_THIS_.+")
diff --git a/Tests/RunCMake/list/FILTER-NotList-result.txt b/Tests/RunCMake/list/FILTER-NotList-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-NotList-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/list/FILTER-NotList-stderr.txt b/Tests/RunCMake/list/FILTER-NotList-stderr.txt
new file mode 100644
index 0000000..159c28d
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-NotList-stderr.txt
@@ -0,0 +1,4 @@
+^CMake Error at FILTER-NotList.cmake:2 \(list\):
+ list sub-command FILTER requires list to be present.
+Call Stack \(most recent call first\):
+ CMakeLists.txt:3 \(include\)$
diff --git a/Tests/RunCMake/list/FILTER-NotList.cmake b/Tests/RunCMake/list/FILTER-NotList.cmake
new file mode 100644
index 0000000..1e15635
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-NotList.cmake
@@ -0,0 +1,2 @@
+unset(nosuchlist)
+list(FILTER nosuchlist EXCLUDE REGEX "^FILTER_THIS_.+")
diff --git a/Tests/RunCMake/list/FILTER-REGEX-InvalidMode-result.txt b/Tests/RunCMake/list/FILTER-REGEX-InvalidMode-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-REGEX-InvalidMode-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/list/FILTER-REGEX-InvalidMode-stderr.txt b/Tests/RunCMake/list/FILTER-REGEX-InvalidMode-stderr.txt
new file mode 100644
index 0000000..7f783fa
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-REGEX-InvalidMode-stderr.txt
@@ -0,0 +1,4 @@
+^CMake Error at FILTER-REGEX-InvalidMode.cmake:2 \(list\):
+ list sub-command FILTER does not recognize mode NOOP
+Call Stack \(most recent call first\):
+ CMakeLists.txt:3 \(include\)$
diff --git a/Tests/RunCMake/list/FILTER-REGEX-InvalidMode.cmake b/Tests/RunCMake/list/FILTER-REGEX-InvalidMode.cmake
new file mode 100644
index 0000000..e02b929
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-REGEX-InvalidMode.cmake
@@ -0,0 +1,2 @@
+set(mylist FILTER_THIS_BIT DO_NOT_FILTER_THIS thisisanitem FILTER_THIS_THING)
+list(FILTER mylist EXCLUDE NOOP "^FILTER_THIS_.+")
diff --git a/Tests/RunCMake/list/FILTER-REGEX-InvalidOperator-result.txt b/Tests/RunCMake/list/FILTER-REGEX-InvalidOperator-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-REGEX-InvalidOperator-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/list/FILTER-REGEX-InvalidOperator-stderr.txt b/Tests/RunCMake/list/FILTER-REGEX-InvalidOperator-stderr.txt
new file mode 100644
index 0000000..94f2427
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-REGEX-InvalidOperator-stderr.txt
@@ -0,0 +1,4 @@
+^CMake Error at FILTER-REGEX-InvalidOperator.cmake:2 \(list\):
+ list sub-command FILTER does not recognize operator RM
+Call Stack \(most recent call first\):
+ CMakeLists.txt:3 \(include\)$
diff --git a/Tests/RunCMake/list/FILTER-REGEX-InvalidOperator.cmake b/Tests/RunCMake/list/FILTER-REGEX-InvalidOperator.cmake
new file mode 100644
index 0000000..9624622
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-REGEX-InvalidOperator.cmake
@@ -0,0 +1,2 @@
+set(mylist FILTER_THIS_BIT DO_NOT_FILTER_THIS thisisanitem FILTER_THIS_THING)
+list(FILTER mylist RM REGEX "^FILTER_THIS_.+")
diff --git a/Tests/RunCMake/list/FILTER-REGEX-InvalidRegex-result.txt b/Tests/RunCMake/list/FILTER-REGEX-InvalidRegex-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-REGEX-InvalidRegex-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/list/FILTER-REGEX-InvalidRegex-stderr.txt b/Tests/RunCMake/list/FILTER-REGEX-InvalidRegex-stderr.txt
new file mode 100644
index 0000000..9e98cbf
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-REGEX-InvalidRegex-stderr.txt
@@ -0,0 +1,4 @@
+^CMake Error at FILTER-REGEX-InvalidRegex.cmake:2 \(list\):
+ list sub-command FILTER, mode REGEX failed to compile regex "UHOH!\)\(".
+Call Stack \(most recent call first\):
+ CMakeLists.txt:3 \(include\)$
diff --git a/Tests/RunCMake/list/FILTER-REGEX-InvalidRegex.cmake b/Tests/RunCMake/list/FILTER-REGEX-InvalidRegex.cmake
new file mode 100644
index 0000000..0641062
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-REGEX-InvalidRegex.cmake
@@ -0,0 +1,2 @@
+set(mylist FILTER_THIS_BIT DO_NOT_FILTER_THIS thisisanitem FILTER_THIS_THING)
+list(FILTER mylist EXCLUDE REGEX "UHOH!)(")
diff --git a/Tests/RunCMake/list/FILTER-REGEX-TooManyArguments-result.txt b/Tests/RunCMake/list/FILTER-REGEX-TooManyArguments-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-REGEX-TooManyArguments-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/list/FILTER-REGEX-TooManyArguments-stderr.txt b/Tests/RunCMake/list/FILTER-REGEX-TooManyArguments-stderr.txt
new file mode 100644
index 0000000..ec7f41c
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-REGEX-TooManyArguments-stderr.txt
@@ -0,0 +1,4 @@
+^CMake Error at FILTER-REGEX-TooManyArguments.cmake:2 \(list\):
+ list sub-command FILTER, mode REGEX requires five arguments.
+Call Stack \(most recent call first\):
+ CMakeLists.txt:3 \(include\)$
diff --git a/Tests/RunCMake/list/FILTER-REGEX-TooManyArguments.cmake b/Tests/RunCMake/list/FILTER-REGEX-TooManyArguments.cmake
new file mode 100644
index 0000000..d9cd8eb
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-REGEX-TooManyArguments.cmake
@@ -0,0 +1,2 @@
+set(mylist FILTER_THIS_BIT DO_NOT_FILTER_THIS thisisanitem FILTER_THIS_THING)
+list(FILTER mylist EXCLUDE REGEX "^FILTER_THIS_.+" one_too_many)
diff --git a/Tests/RunCMake/list/FILTER-REGEX-Valid0-result.txt b/Tests/RunCMake/list/FILTER-REGEX-Valid0-result.txt
new file mode 100644
index 0000000..573541a
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-REGEX-Valid0-result.txt
@@ -0,0 +1 @@
+0
diff --git a/Tests/RunCMake/list/FILTER-REGEX-Valid0-stderr.txt b/Tests/RunCMake/list/FILTER-REGEX-Valid0-stderr.txt
new file mode 100644
index 0000000..d9ba38d
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-REGEX-Valid0-stderr.txt
@@ -0,0 +1,2 @@
+^mylist was: FILTER_THIS_BIT;DO_NOT_FILTER_THIS;thisisanitem;FILTER_THIS_THING
+mylist is: DO_NOT_FILTER_THIS;thisisanitem$
diff --git a/Tests/RunCMake/list/FILTER-REGEX-Valid0.cmake b/Tests/RunCMake/list/FILTER-REGEX-Valid0.cmake
new file mode 100644
index 0000000..f395e61
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-REGEX-Valid0.cmake
@@ -0,0 +1,4 @@
+set(mylist FILTER_THIS_BIT DO_NOT_FILTER_THIS thisisanitem FILTER_THIS_THING)
+message("mylist was: ${mylist}")
+list(FILTER mylist EXCLUDE REGEX "^FILTER_THIS_.+")
+message("mylist is: ${mylist}")
diff --git a/Tests/RunCMake/list/FILTER-REGEX-Valid1-result.txt b/Tests/RunCMake/list/FILTER-REGEX-Valid1-result.txt
new file mode 100644
index 0000000..573541a
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-REGEX-Valid1-result.txt
@@ -0,0 +1 @@
+0
diff --git a/Tests/RunCMake/list/FILTER-REGEX-Valid1-stderr.txt b/Tests/RunCMake/list/FILTER-REGEX-Valid1-stderr.txt
new file mode 100644
index 0000000..5e5280d
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-REGEX-Valid1-stderr.txt
@@ -0,0 +1,2 @@
+^mylist was: FILTER_THIS_BIT;DO_NOT_FILTER_THIS;thisisanitem;FILTER_THIS_THING
+mylist is: FILTER_THIS_BIT;FILTER_THIS_THING$
diff --git a/Tests/RunCMake/list/FILTER-REGEX-Valid1.cmake b/Tests/RunCMake/list/FILTER-REGEX-Valid1.cmake
new file mode 100644
index 0000000..e659281
--- /dev/null
+++ b/Tests/RunCMake/list/FILTER-REGEX-Valid1.cmake
@@ -0,0 +1,4 @@
+set(mylist FILTER_THIS_BIT DO_NOT_FILTER_THIS thisisanitem FILTER_THIS_THING)
+message("mylist was: ${mylist}")
+list(FILTER mylist INCLUDE REGEX "^FILTER_THIS_.+")
+message("mylist is: ${mylist}")
diff --git a/Tests/RunCMake/list/RunCMakeTest.cmake b/Tests/RunCMake/list/RunCMakeTest.cmake
index 25d6a03..b002ab3 100644
--- a/Tests/RunCMake/list/RunCMakeTest.cmake
+++ b/Tests/RunCMake/list/RunCMakeTest.cmake
@@ -1,5 +1,6 @@
include(RunCMake)
+run_cmake(EmptyFilterRegex)
run_cmake(EmptyGet0)
run_cmake(EmptyRemoveAt0)
run_cmake(EmptyInsert-1)
@@ -8,17 +9,25 @@ run_cmake(NoArguments)
run_cmake(InvalidSubcommand)
run_cmake(GET-CMP0007-WARN)
+run_cmake(FILTER-REGEX-InvalidRegex)
run_cmake(GET-InvalidIndex)
run_cmake(INSERT-InvalidIndex)
run_cmake(REMOVE_AT-InvalidIndex)
+run_cmake(FILTER-REGEX-TooManyArguments)
run_cmake(LENGTH-TooManyArguments)
run_cmake(REMOVE_DUPLICATES-TooManyArguments)
run_cmake(REVERSE-TooManyArguments)
run_cmake(SORT-TooManyArguments)
+run_cmake(FILTER-NotList)
run_cmake(REMOVE_AT-NotList)
run_cmake(REMOVE_DUPLICATES-NotList)
run_cmake(REMOVE_ITEM-NotList)
run_cmake(REVERSE-NotList)
run_cmake(SORT-NotList)
+
+run_cmake(FILTER-REGEX-InvalidMode)
+run_cmake(FILTER-REGEX-InvalidOperator)
+run_cmake(FILTER-REGEX-Valid0)
+run_cmake(FILTER-REGEX-Valid1)