ctkMacroListFilter.cmake 2.4 KB

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