ctkMacroListFilter.cmake 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # See http://www.cmake.org/Wiki/CMakeMacroListOperations#LIST_FILTER
  2. # LIST_FILTER(<list> <regexp_var> [<regexp_var> ...]
  3. # [OUTPUT_VARIABLE <variable>])
  4. # Removes items from <list> which do not match any of the specified
  5. # regular expressions. An optional argument OUTPUT_VARIABLE
  6. # specifies a variable in which to store the matched items instead of
  7. # updating <list>
  8. # As regular expressions can not be given to macros (see bug #5389), we pass
  9. # variable names whose content is the regular expressions.
  10. # Note that this macro requires PARSE_ARGUMENTS macro, available here:
  11. # http://www.cmake.org/Wiki/CMakeMacroParseArguments
  12. MACRO(CtkMacroListFilter)
  13. ctkMacroParseArguments(LIST_FILTER "OUTPUT_VARIABLE" "" ${ARGV})
  14. # Check arguments.
  15. LIST(LENGTH LIST_FILTER_DEFAULT_ARGS LIST_FILTER_default_length)
  16. IF(${LIST_FILTER_default_length} EQUAL 0)
  17. MESSAGE(FATAL_ERROR "LIST_FILTER: missing list variable.")
  18. ENDIF(${LIST_FILTER_default_length} EQUAL 0)
  19. IF(${LIST_FILTER_default_length} EQUAL 1)
  20. MESSAGE(FATAL_ERROR "LIST_FILTER: missing regular expression variable.")
  21. ENDIF(${LIST_FILTER_default_length} EQUAL 1)
  22. # Reset output variable
  23. IF(NOT LIST_FILTER_OUTPUT_VARIABLE)
  24. SET(LIST_FILTER_OUTPUT_VARIABLE "LIST_FILTER_internal_output")
  25. ENDIF(NOT LIST_FILTER_OUTPUT_VARIABLE)
  26. SET(${LIST_FILTER_OUTPUT_VARIABLE})
  27. # Extract input list from arguments
  28. LIST(GET LIST_FILTER_DEFAULT_ARGS 0 LIST_FILTER_input_list)
  29. LIST(REMOVE_AT LIST_FILTER_DEFAULT_ARGS 0)
  30. FOREACH(LIST_FILTER_item ${${LIST_FILTER_input_list}})
  31. FOREACH(LIST_FILTER_regexp_var ${LIST_FILTER_DEFAULT_ARGS})
  32. FOREACH(LIST_FILTER_regexp ${${LIST_FILTER_regexp_var}})
  33. IF(${LIST_FILTER_item} MATCHES ${LIST_FILTER_regexp})
  34. LIST(APPEND ${LIST_FILTER_OUTPUT_VARIABLE} ${LIST_FILTER_item})
  35. ENDIF(${LIST_FILTER_item} MATCHES ${LIST_FILTER_regexp})
  36. ENDFOREACH(LIST_FILTER_regexp ${${LIST_FILTER_regexp_var}})
  37. ENDFOREACH(LIST_FILTER_regexp_var)
  38. ENDFOREACH(LIST_FILTER_item)
  39. # If OUTPUT_VARIABLE is not specified, overwrite the input list.
  40. IF(${LIST_FILTER_OUTPUT_VARIABLE} STREQUAL "LIST_FILTER_internal_output")
  41. SET(${LIST_FILTER_input_list} ${${LIST_FILTER_OUTPUT_VARIABLE}})
  42. ENDIF(${LIST_FILTER_OUTPUT_VARIABLE} STREQUAL "LIST_FILTER_internal_output")
  43. ENDMACRO(CtkMacroListFilter)