ctkMacroListFilter.cmake 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #! See http://www.cmake.org/Wiki/CMakeMacroListOperations#LIST_FILTER
  2. #!
  3. #! Usage:
  4. #! \code
  5. #! ctkMacroListFilter(<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()
  26. if(${LIST_FILTER_default_length} EQUAL 1)
  27. message(FATAL_ERROR "LIST_FILTER: missing regular expression variable.")
  28. endif()
  29. # Reset output variable
  30. if(NOT LIST_FILTER_OUTPUT_VARIABLE)
  31. set(LIST_FILTER_OUTPUT_VARIABLE "LIST_FILTER_internal_output")
  32. endif()
  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()
  43. endforeach()
  44. endforeach()
  45. endforeach()
  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()
  50. endmacro(ctkMacroListFilter)