1# MIT License
2#
3# Copyright The SCons Foundation
4#
5# Permission is hereby granted, free of charge, to any person obtaining
6# a copy of this software and associated documentation files (the
7# "Software"), to deal in the Software without restriction, including
8# without limitation the rights to use, copy, modify, merge, publish,
9# distribute, sublicense, and/or sell copies of the Software, and to
10# permit persons to whom the Software is furnished to do so, subject to
11# the following conditions:
12#
13# The above copyright notice and this permission notice shall be included
14# in all copies or substantial portions of the Software.
15#
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24"""Dependency scanner for program files."""
25
26import SCons.Node
27import SCons.Node.FS
28import SCons.Scanner
29import SCons.Util
30
31# global, set by --debug=findlibs
32print_find_libs = None
33
34def ProgramScanner(**kw):
35    """Return a prototype Scanner instance for scanning executable
36    files for static-lib dependencies"""
37    kw['path_function'] = SCons.Scanner.FindPathDirs('LIBPATH')
38    ps = SCons.Scanner.Base(scan, "ProgramScanner", **kw)
39    return ps
40
41def _subst_libs(env, libs):
42    """Substitute environment variables and split into list."""
43    if SCons.Util.is_String(libs):
44        libs = env.subst(libs)
45        if SCons.Util.is_String(libs):
46            libs = libs.split()
47    elif SCons.Util.is_Sequence(libs):
48        _libs = []
49        for l in libs:
50            _libs += _subst_libs(env, l)
51        libs = _libs
52    else:
53        # libs is an object (Node, for example)
54        libs = [libs]
55    return libs
56
57def scan(node, env, libpath = ()):
58    """Scans program files for static-library dependencies.
59
60    It will search the LIBPATH environment variable
61    for libraries specified in the LIBS variable, returning any
62    files it finds as dependencies.
63    """
64    try:
65        libs = env['LIBS']
66    except KeyError:
67        # There are no LIBS in this environment, so just return a null list:
68        return []
69
70    libs = _subst_libs(env, libs)
71
72    try:
73        prefix = env['LIBPREFIXES']
74        if not SCons.Util.is_List(prefix):
75            prefix = [ prefix ]
76    except KeyError:
77        prefix = [ '' ]
78
79    try:
80        suffix = env['LIBSUFFIXES']
81        if not SCons.Util.is_List(suffix):
82            suffix = [ suffix ]
83    except KeyError:
84        suffix = [ '' ]
85
86    pairs = []
87    for suf in map(env.subst, suffix):
88        for pref in map(env.subst, prefix):
89            pairs.append((pref, suf))
90
91    result = []
92
93    if callable(libpath):
94        libpath = libpath()
95
96    find_file = SCons.Node.FS.find_file
97    adjustixes = SCons.Util.adjustixes
98    for lib in libs:
99        if SCons.Util.is_String(lib):
100            for pref, suf in pairs:
101                l = adjustixes(lib, pref, suf)
102                l = find_file(l, libpath, verbose=print_find_libs)
103                if l:
104                    result.append(l)
105        else:
106            result.append(lib)
107
108    return result
109
110# Local Variables:
111# tab-width:4
112# indent-tabs-mode:nil
113# End:
114# vim: set expandtab tabstop=4 shiftwidth=4:
115