1# (C) Copyright 2011- ECMWF.
2#
3# This software is licensed under the terms of the Apache Licence Version 2.0
4# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
5# In applying this licence, ECMWF does not waive the privileges and immunities
6# granted to it by virtue of its status as an intergovernmental organisation nor
7# does it submit to any jurisdiction.
8
9##############################################################################
10#.rst:
11#
12# ecbuild_list_add_pattern
13# ========================
14#
15# Exclude items from a list that match a list of patterns. ::
16#
17#   ecbuild_list_add_pattern( LIST <input_list>
18#                             GLOB <pattern1> [ <pattern2> ... ]
19#                             [ SOURCE_DIR <source_dir> ]
20#                             [ QUIET ] )
21#
22# Options
23# -------
24#
25# LIST : required
26#   list variable to be appended to
27#
28# GLOB : required
29#   Regex pattern of exclusion
30#
31# SOURCE_DIR : optional
32#   Directory from where to start search
33#
34# QUIET  : optional
35#   Don't warn if patterns don't match
36#
37##############################################################################
38
39function( ecbuild_list_add_pattern )
40
41  set( options QUIET )
42  set( single_value_args LIST SOURCE_DIR )
43  set( multi_value_args  GLOB )
44
45  cmake_parse_arguments( _p "${options}" "${single_value_args}" "${multi_value_args}"  ${_FIRST_ARG} ${ARGN} )
46
47  if(_p_UNPARSED_ARGUMENTS)
48    ecbuild_critical("Unknown keywords given to ecbuild_list_add_pattern(): \"${_p_UNPARSED_ARGUMENTS}\"")
49  endif()
50
51  if( NOT _p_LIST  )
52    ecbuild_critical("The call to ecbuild_list_add_pattern() doesn't specify the LIST.")
53  endif()
54
55  if( NOT _p_GLOB )
56    ecbuild_critical("The call to ecbuild_list_add_pattern() doesn't specify the GLOB.")
57  endif()
58
59  #####
60
61  set( input_list ${${_p_LIST}} )
62  unset( matched_files )
63
64  foreach( pattern ${_p_GLOB} )
65
66    if( IS_ABSOLUTE ${pattern} )
67      ecbuild_debug( "ecbuild_list_add_pattern: Adding ${pattern}" )
68      file( GLOB_RECURSE matched_files ${pattern} )
69    else()
70
71      if(_p_SOURCE_DIR)
72        if( IS_ABSOLUTE ${_p_SOURCE_DIR} )
73          ecbuild_debug( "ecbuild_list_add_pattern: Adding ${_p_SOURCE_DIR}/${pattern}" )
74          file( GLOB_RECURSE matched_files ${_p_SOURCE_DIR}/${pattern} )
75        else()
76          ecbuild_debug( "ecbuild_list_add_pattern: Adding ${_p_SOURCE_DIR}/${pattern}" )
77          file( GLOB_RECURSE matched_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${_p_SOURCE_DIR}/${pattern} )
78        endif()
79      else()
80        ecbuild_debug( "ecbuild_list_add_pattern: Adding ${pattern} ")
81        file( GLOB_RECURSE matched_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${pattern} )
82      endif()
83
84    endif()
85
86    if(matched_files)
87      ecbuild_debug( "ecbuild_list_add_pattern: Found ${matched_files}" )
88      list( APPEND input_list ${matched_files} )
89      list( REMOVE_DUPLICATES input_list )
90      set( ${_p_LIST} ${input_list} PARENT_SCOPE )
91    else()
92      if(NOT _p_QUIET)
93        ecbuild_warn( "ecbuild_list_add_pattern: no matches found for patterns ${pattern}" )
94      else()
95        ecbuild_debug( "ecbuild_list_add_pattern:no matches found for patterns ${pattern}" )
96      endif()
97    endif()
98
99  endforeach()
100
101
102endfunction(ecbuild_list_add_pattern)
103