summaryrefslogtreecommitdiff
path: root/site_scons/libdeps.py
blob: 153c551290b66ecfdf71f06181b5420fd7f13511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"""Extension to SCons providing advanced static library dependency tracking.

These modifications to a build environment, which can be attached to
StaticLibrary and Program builders via a call to setup_environment(env),
cause the build system to track library dependencies through static libraries,
and to add them to the link command executed when building programs.

For example, consider a program 'try' that depends on a lib 'tc', which in
turn uses a symbol from a lib 'tb' which in turn uses a library from 'ta'.
Without this package, the Program declaration for "try" looks like this:

Program('try', ['try.c', 'path/to/${LIBPREFIX}tc${LIBSUFFIX}',
                'path/to/${LIBPREFIX}tc${LIBSUFFIX}',
                'path/to/${LIBPREFIX}tc${LIBSUFFIX}',])

With this library, we can instead write the following

Program('try', ['try.c'], LIBDEPS=['path/to/tc'])
StaticLibrary('tc', ['c.c'], LIBDEPS=['path/to/tb'])
StaticLibrary('tb', ['b.c'], LIBDEPS=['path/to/ta'])
StaticLibrary('ta', ['a.c'])

And the build system will figure out that it needs to link libta.a and libtb.a
when building 'try'.

A StaticLibrary S may also declare programs or libraries, [L1, ...] to be dependent
upon S by setting LIBDEPS_DEPENDENTS=[L1, ...], using the same syntax as is used
for LIBDEPS, except that the libraries and programs will not have LIBPREFIX/LIBSUFFIX
automatically added when missing.
"""

# Copyright (c) 2010, Corensic Inc., All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import os

import SCons.Errors
import SCons.Scanner
import SCons.Util

libdeps_env_var = 'LIBDEPS'
syslibdeps_env_var = 'SYSLIBDEPS'
missing_syslibdep = 'MISSING_LIBDEP_'

def sorted_by_str(iterable):
    """Shorthand for sorting an iterable according to its string representation.

    We use this instead of sorted(), below, because SCons.Node objects are
    compared on object identity, rather than value, so sorts aren't stable
    across invocations of SCons.  Since dependency order changes force rebuilds,
    we use this sort to create stable dependency orders.
    """
    return sorted(iterable, key=str)

class DependencyCycleError(SCons.Errors.UserError):
    """Exception representing a cycle discovered in library dependencies."""

    def __init__(self, first_node ):
        super(DependencyCycleError, self).__init__()
        self.cycle_nodes = [first_node]

    def __str__(self):
        return "Library dependency cycle detected: " + " => ".join(str(n) for n in self.cycle_nodes)

def __get_libdeps(node):
    """Given a SCons Node, return its library dependencies.

    Computes the dependencies if they're not already cached.
    """

    cached_var_name = libdeps_env_var + '_cached'

    if not hasattr(node.attributes, cached_var_name):
        setattr(node.attributes, cached_var_name, __compute_libdeps(node))
    return getattr(node.attributes, cached_var_name)

def __compute_libdeps(node):
    """Recursively identify all library dependencies for a node."""

    if getattr(node.attributes, 'libdeps_exploring', False):
        raise DependencyCycleError(node)

    env = node.get_env()
    deps = set()
    node.attributes.libdeps_exploring = True
    try:
        try:
            for child in env.Flatten(getattr(node.attributes, 'libdeps_direct', [])):
                if not child:
                    continue
                deps.add(child)
                deps.update(__get_libdeps(child))

        except DependencyCycleError, e:
            if len(e.cycle_nodes) == 1 or e.cycle_nodes[0] != e.cycle_nodes[-1]:
                e.cycle_nodes.insert(0, node)
            raise
    finally:
        node.attributes.libdeps_exploring = False

    return deps

def __get_syslibdeps(node):
    """ Given a SCons Node, return its system library dependencies.

    These are the depencencies listed with SYSLIBDEPS, and are linked using -l.
    """
    cached_var_name = syslibdeps_env_var + '_cached'
    if not hasattr(node.attributes, cached_var_name):
        syslibdeps = node.get_env().Flatten(node.get_env().get(syslibdeps_env_var, []))
        for lib in sorted_by_str(__get_libdeps(node)):
            for syslib in node.get_env().Flatten(lib.get_env().get(syslibdeps_env_var, [])):
                if syslib:
                    if type(syslib) in (str, unicode) and syslib.startswith(missing_syslibdep):
                        print("Target '%s' depends on the availability of a "
                              "system provided library for '%s', "
                              "but no suitable library was found during configuration." %
                              (str(node), syslib[len(missing_syslibdep):]))
                        node.get_env().Exit(1)
                    syslibdeps.append(syslib)
        setattr(node.attributes, cached_var_name, syslibdeps)
    return getattr(node.attributes, cached_var_name)

def __missing_syslib(name):
    return missing_syslibdep + name

def update_scanner(builder):
    """Update the scanner for "builder" to also scan library dependencies."""

    old_scanner = builder.target_scanner

    if old_scanner:
        path_function = old_scanner.path_function
        def new_scanner(node, env, path=()):
            result = set(old_scanner.function(node, env, path))
            result.update(__get_libdeps(node))
            return sorted_by_str(result)
    else:
        path_function = None
        def new_scanner(node, env, path=()):
            result = set(__get_libdeps(node))
            return sorted_by_str(result)

    builder.target_scanner = SCons.Scanner.Scanner(function=new_scanner,
                                                    path_function=path_function)

def get_libdeps(source, target, env, for_signature):
    """Implementation of the special _LIBDEPS environment variable.

    Expands to the library dependencies for a target.
    """

    target = env.Flatten([target])
    return sorted_by_str(__get_libdeps(target[0]))

def get_libdeps_objs(source, target, env, for_signature):
    objs = set()
    for lib in get_libdeps(source, target, env, for_signature):
        objs.update(lib.sources_set)
    return sorted_by_str(objs)

def get_libdeps_special_sun(source, target, env, for_signature):
    x = get_libdeps(source, target, env, for_signature )
    return x + x + x

def get_syslibdeps(source, target, env, for_signature):
    deps = __get_syslibdeps(target[0])
    lib_link_prefix = env.subst('$LIBLINKPREFIX')
    lib_link_suffix = env.subst('$LIBLINKSUFFIX')
    result = []
    for d in deps:
        # Elements of syslibdeps are either strings (str or unicode), or they're File objects.
        # If they're File objects, they can be passed straight through.  If they're strings,
        # they're believed to represent library short names, that should be prefixed with -l
        # or the compiler-specific equivalent.  I.e., 'm' becomes '-lm', but 'File("m.a") is passed
        # through whole cloth.
        if type(d) in (str, unicode):
            result.append('%s%s%s' % (lib_link_prefix, d, lib_link_suffix))
        else:
            result.append(d)
    return result

def __append_direct_libdeps(node, prereq_nodes):
    # We do not bother to decorate nodes that are not actual Objects
    if type(node) == str:
        return
    if getattr(node.attributes, 'libdeps_direct', None) is None:
        node.attributes.libdeps_direct = []
    node.attributes.libdeps_direct.extend(prereq_nodes)

def libdeps_emitter(target, source, env):
    """SCons emitter that takes values from the LIBDEPS environment variable and
    converts them to File node objects, binding correct path information into
    those File objects.

    Emitters run on a particular "target" node during the initial execution of
    the SConscript file, rather than during the later build phase.  When they
    run, the "env" environment's working directory information is what you
    expect it to be -- that is, the working directory is considered to be the
    one that contains the SConscript file.  This allows specification of
    relative paths to LIBDEPS elements.

    This emitter also adds LIBSUFFIX and LIBPREFIX appropriately.

    NOTE: For purposes of LIBDEPS_DEPENDENTS propagation, only the first member
    of the "target" list is made a prerequisite of the elements of LIBDEPS_DEPENDENTS.
    """

    lib_builder = env['BUILDERS']['StaticLibrary']
    lib_node_factory = lib_builder.target_factory or env.File

    prog_builder = env['BUILDERS']['Program']
    prog_node_factory = prog_builder.target_factory or env.File

    libdep_files = []
    for prereq in env.Flatten([env.get(libdeps_env_var, [])]):
        prereqWithIxes = SCons.Util.adjustixes(
            prereq, lib_builder.get_prefix(env), lib_builder.get_suffix(env))
        libdep_files.append(lib_node_factory(prereqWithIxes))

    for t in target:
        # target[0] must be a Node and not a string, or else libdeps will fail to
        # work properly.
        __append_direct_libdeps(t, libdep_files)

    for dependent in env.Flatten([env.get('LIBDEPS_DEPENDENTS', [])]):
        dependentWithIxes = SCons.Util.adjustixes(
            dependent, lib_builder.get_prefix(env), lib_builder.get_suffix(env))
        dependentNode = lib_node_factory(dependentWithIxes)
        __append_direct_libdeps(dependentNode, [target[0]])

    for dependent in env.Flatten([env.get('PROGDEPS_DEPENDENTS', [])]):
        dependentWithIxes = SCons.Util.adjustixes(
            dependent, prog_builder.get_prefix(env), prog_builder.get_suffix(env))
        dependentNode = prog_node_factory(dependentWithIxes)
        __append_direct_libdeps(dependentNode, [target[0]])

    return target, source

def shlibdeps_emitter(target, source, env):
    """SCons emitter that takes values from the LIBDEPS environment variable and
    converts them to File node objects, binding correct path information into
    those File objects.

    Emitters run on a particular "target" node during the initial execution of
    the SConscript file, rather than during the later build phase.  When they
    run, the "env" environment's working directory information is what you
    expect it to be -- that is, the working directory is considered to be the
    one that contains the SConscript file.  This allows specification of
    relative paths to LIBDEPS elements.

    This emitter also adds LIBSUFFIX and LIBPREFIX appropriately.

    NOTE: For purposes of LIBDEPS_DEPENDENTS propagation, only the first member
    of the "target" list is made a prerequisite of the elements of LIBDEPS_DEPENDENTS.
    """

    lib_builder = env['BUILDERS']['SharedLibrary']
    lib_node_factory = lib_builder.target_factory or env.File

    prog_builder = env['BUILDERS']['Program']
    prog_node_factory = prog_builder.target_factory or env.File

    libdep_files = []
    for prereq in env.Flatten([env.get(libdeps_env_var, [])]):
        prereqWithIxes = SCons.Util.adjustixes(
            prereq, lib_builder.get_prefix(env), lib_builder.get_suffix(env))
        libdep_files.append(lib_node_factory(prereqWithIxes))

    for t in target:
        # target[0] must be a Node and not a string, or else libdeps will fail to
        # work properly.
        __append_direct_libdeps(t, libdep_files)

    for dependent in env.Flatten([env.get('LIBDEPS_DEPENDENTS', [])]):
        dependentWithIxes = SCons.Util.adjustixes(
            dependent, lib_builder.get_prefix(env), lib_builder.get_suffix(env))
        dependentNode = lib_node_factory(dependentWithIxes)
        __append_direct_libdeps(dependentNode, [target[0]])

    for dependent in env.Flatten([env.get('PROGDEPS_DEPENDENTS', [])]):
        dependentWithIxes = SCons.Util.adjustixes(
            dependent, prog_builder.get_prefix(env), prog_builder.get_suffix(env))
        dependentNode = prog_node_factory(dependentWithIxes)
        __append_direct_libdeps(dependentNode, [target[0]])

    return target, source

def expand_libdeps_tags(source, target, env, for_signature):
    results = []
    for expansion in env.get('LIBDEPS_TAG_EXPANSIONS', []):
        results.append(expansion(source, target, env, for_signature))
    return results

def setup_environment(env, emitting_shared=False):
    """Set up the given build environment to do LIBDEPS tracking."""

    try:
        env['_LIBDEPS']
    except KeyError:
        env['_LIBDEPS'] = '$_LIBDEPS_LIBS'

    env['_LIBDEPS_TAGS'] = expand_libdeps_tags

    # TODO: remove this
    # this is a horrible horrible hack for 
    # for 32-bit solaris
    if "uname" in dir(os) and os.uname()[1] == "sun32b":
        env['_LIBDEPS_LIBS'] = get_libdeps_special_sun
    else:
        env['_LIBDEPS_LIBS'] = get_libdeps

    env['_LIBDEPS_OBJS'] = get_libdeps_objs
    env['_SYSLIBDEPS'] = get_syslibdeps
    env['_SHLIBDEPS'] = '$SHLIBDEP_GROUP_START ${_concat(SHLIBDEPPREFIX, __env__.subst(_LIBDEPS, target=TARGET, source=SOURCE), SHLIBDEPSUFFIX, __env__, target=TARGET, source=SOURCE)} $SHLIBDEP_GROUP_END'

    env[libdeps_env_var] = SCons.Util.CLVar()
    env[syslibdeps_env_var] = SCons.Util.CLVar()
    env.Append(LIBEMITTER=libdeps_emitter)
    if emitting_shared:
        env.Append(
            PROGEMITTER=shlibdeps_emitter,
            SHLIBEMITTER=shlibdeps_emitter)
    else:
        env.Append(
            PROGEMITTER=libdeps_emitter,
            SHLIBEMITTER=libdeps_emitter)
    env.Prepend(_LIBFLAGS=' $_LIBDEPS_TAGS $LINK_WHOLE_ARCHIVE_START $LINK_LIBGROUP_START $_LIBDEPS $LINK_LIBGROUP_END $LINK_WHOLE_ARCHIVE_END $_SYSLIBDEPS ')
    for builder_name in ('Program', 'SharedLibrary', 'LoadableModule'):
        try:
            update_scanner(env['BUILDERS'][builder_name])
        except KeyError:
            pass

def setup_conftests(conf):
    def FindSysLibDep(context, name, libs, **kwargs):
        var = "LIBDEPS_" + name.upper() + "_SYSLIBDEP"
        kwargs['autoadd'] = False
        for lib in libs:
            result = context.sconf.CheckLib(lib, **kwargs)
            context.did_show_result = 1
            if result:
                context.env[var] = lib
                return context.Result(result)
        context.env[var] = __missing_syslib(name)
        return context.Result(result)
    conf.AddTest('FindSysLibDep', FindSysLibDep)