2 # Copyright 2008, Google Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
15 # * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 """Input gathering tool for SCons."""
38 def GatherInputs(env
, target
, groups
=['.*'], exclude_pattern
=None):
39 """Find all (non-generated) input files used for a target.
42 target: a target node to find source files for
43 For example: File('bob.exe')
44 groups: a list of patterns to use as categories
45 For example: ['.*\\.c$', '.*\\.h$']
46 exclude_pattern: a pattern to exclude from the search
47 For example: '.*third_party.*'
49 A list of lists of files for each category.
50 Each file will be placed in the first category which matches,
51 even if categories overlap.
53 [['bob.c', 'jim.c'], ['bob.h', 'jim.h']]
56 # Compile exclude pattern if any
58 exclude_pattern
= re
.compile(exclude_pattern
)
60 def _FindSources(ptrns
, tgt
, all
):
61 """Internal Recursive function to find all pattern matches."""
62 # Recursively process lists
63 if SCons
.Util
.is_List(tgt
):
65 _FindSources(ptrns
, t
, all
)
67 # Get key to use for tracking whether we've seen this node
69 if hasattr(tgt
, 'abspath'):
70 # Use target's absolute path as the key
71 target_abspath
= tgt
.abspath
72 target_key
= target_abspath
74 # Hope node's representation is unique enough (the default repr
75 # contains a pointer to the target as a string). This works for
77 target_key
= repr(tgt
)
79 # Skip if we have been here before
80 if target_key
in all
: return
81 # Note that we have been here
82 all
[target_key
] = True
83 # Skip ones that match an exclude pattern, if we have one.
84 if (exclude_pattern
and target_abspath
85 and exclude_pattern
.match(target_abspath
)):
88 # Handle non-leaf nodes recursively
89 lst
= tgt
.children(scan
=1)
91 _FindSources(ptrns
, lst
, all
)
94 # Get real file (backed by repositories).
96 rfile_is_file
= rfile
.isfile()
98 for pattern
, lst
in ptrns
.items():
99 # Add files to the list for the first pattern that matches (implicitly,
100 # don't add directories).
101 if rfile_is_file
and pattern
.match(rfile
.path
):
102 lst
.append(rfile
.abspath
)
105 # Prepare a group for each pattern.
108 patterns
[re
.compile(g
, re
.IGNORECASE
)] = []
111 _FindSources(patterns
, target
, {})
113 return patterns
.values()
117 # NOTE: SCons requires the use of this name, which fails gpylint.
118 """SCons entry point for this tool."""
120 # Add a method to gather all inputs needed by a target.
121 env
.AddMethod(GatherInputs
, 'GatherInputs')