[CMake] How to easily extract items from a list which match a regex?

Tristan Carel tristan.carel at gmail.com
Fri Oct 27 03:45:27 EDT 2006


Hi,

To extract the list of headers in a set of source files contained in a
variable, I don't see other solution than iterate on each element and
check that the items match the regular expression:

FILE(GLOB My_Sources "*.*")
# [...]
FOREACH(item ${My_Sources})
  STRING(REGEX MATCH ".+\\.h?h$" item ${item})
  IF(item)
    LIST(APPEND My_Headers ${item})
  ENDIF(item)
ENDFOREACH(item ${My_Sources})

I think it's too much code for a so small operation. Maybe there is a
easier way to do it. If not what do you thing about adding an option
to the LIST command?
LIST(FILTER <list> <regex> [<regex> ...] [OUTPUT_VARIABLE <variable>])

FILTER removes items from <list> which do not match any of the
specified regular expressions. An optional argument OUTPUT_VARIABLE
specifies a variable in which to store the matched items instead of
updating <list>.

So that we could write this:
LIST(FILTER My_Sources ".+\\.h?h$" OUTPUT_VARIABLE My_Headers)

-- 
Tristan Carel
Music with dinner is an insult both to the cook and the violinist.


More information about the CMake mailing list