summaryrefslogtreecommitdiff
path: root/tools/build/v2/tools/stage.py
blob: 90d3c0f976a25d5c5a39d13d25fd3be9ea5d2ec6 (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
# Status: ported.
# Base revision 64444.
#
# Copyright 2003 Dave Abrahams
# Copyright 2005, 2006 Rene Rivera
# Copyright 2002, 2003, 2004, 2005, 2006, 2010 Vladimir Prus
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)

# This module defines the 'install' rule, used to copy a set of targets to a
# single location.

import b2.build.feature as feature
import b2.build.targets as targets
import b2.build.property as property
import b2.build.property_set as property_set
import b2.build.generators as generators
import b2.build.virtual_target as virtual_target

from b2.manager import get_manager
from b2.util.sequence import unique
from b2.util import bjam_signature

import b2.build.type

import os.path
import re
import types

feature.feature('install-dependencies', ['off', 'on'], ['incidental'])
feature.feature('install-type', [], ['free', 'incidental'])
feature.feature('install-source-root', [], ['free', 'path'])
feature.feature('so-version', [], ['free', 'incidental'])

# If 'on', version symlinks for shared libraries will not be created. Affects
# Unix builds only.
feature.feature('install-no-version-symlinks', ['on'], ['optional', 'incidental'])

class InstallTargetClass(targets.BasicTarget):

    def update_location(self, ps):
        """If <location> is not set, sets it based on the project data."""

        loc = ps.get('location')
        if not loc:
            loc = os.path.join(self.project().get('location'), self.name())
            ps = ps.add_raw(["<location>" + loc])

        return ps

    def adjust_properties(self, target, build_ps):
        a = target.action()
        properties = []
        if a:
            ps = a.properties()
            properties = ps.all()
            
            # Unless <hardcode-dll-paths>true is in properties, which can happen
            # only if the user has explicitly requested it, nuke all <dll-path>
            # properties.

            if build_ps.get('hardcode-dll-paths') != ['true']:
                properties = [p for p in properties if p.feature().name() != 'dll-path']

            # If any <dll-path> properties were specified for installing, add
            # them.
            properties.extend(build_ps.get_properties('dll-path'))

            # Also copy <linkflags> feature from current build set, to be used
            # for relinking.
            properties.extend(build_ps.get_properties('linkflags'))

            # Remove the <tag> feature on original targets.
            # And <location>. If stage target has another stage target in
            # sources, then we shall get virtual targets with the <location>
            # property set.
            properties = [p for p in properties
                          if not p.feature().name() in ['tag', 'location']]

        properties.extend(build_ps.get_properties('dependency'))

        properties.extend(build_ps.get_properties('location'))
        

        properties.extend(build_ps.get_properties('install-no-version-symlinks'))

        d = build_ps.get_properties('install-source-root')

        # Make the path absolute: we shall use it to compute relative paths and
        # making the path absolute will help.
        if d:
            p = d[0]
            properties.append(property.Property(p.feature(), os.path.abspath(p.value())))

        return property_set.create(properties)
    

    def construct(self, name, source_targets, ps):

        source_targets = self.targets_to_stage(source_targets, ps)
        ps = self.update_location(ps)

        ename = ps.get('name')
        if ename:
            ename = ename[0]
        if ename and len(source_targets) > 1:
            get_manager().errors()("When <name> property is used in 'install', only one source is allowed")

        result = []

        for i in source_targets:

            staged_targets = []
            new_ps = self.adjust_properties(i, ps)

            # See if something special should be done when staging this type. It
            # is indicated by the presence of a special "INSTALLED_" type.
            t = i.type()
            if t and b2.build.type.registered("INSTALLED_" + t):

                if ename:
                    get_manager().errors()("In 'install': <name> property specified with target that requires relinking.")
                else:
                    (r, targets) = generators.construct(self.project(), name, "INSTALLED_" + t,
                                                        new_ps, [i])
                    assert isinstance(r, property_set.PropertySet)
                    staged_targets.extend(targets)
                    
            else:
                staged_targets.append(copy_file(self.project(), ename, i, new_ps))

            if not staged_targets:
                get_manager().errors()("Unable to generate staged version of " + i)

            result.extend(get_manager().virtual_targets().register(t) for t in staged_targets)

        return (property_set.empty(), result)

    def targets_to_stage(self, source_targets, ps):
        """Given the list of source targets explicitly passed to 'stage', returns the
        list of targets which must be staged."""

        result = []

        # Traverse the dependencies, if needed.
        if ps.get('install-dependencies') == ['on']:
            source_targets = self.collect_targets(source_targets)

        # Filter the target types, if needed.
        included_types = ps.get('install-type')
        for r in source_targets:
            ty = r.type()
            if ty:
                # Do not stage searched libs.
                if ty != "SEARCHED_LIB":
                    if included_types:
                        if self.include_type(ty, included_types):
                            result.append(r)
                    else:
                        result.append(r)
            elif not included_types:
                # Don't install typeless target if there is an explicit list of
                # allowed types.
                result.append(r)

        return result

    # CONSIDER: figure out why we can not use virtual-target.traverse here.
    #
    def collect_targets(self, targets):
        
        s = [t.creating_subvariant() for t in targets]
        s = unique(s)
        
        result = set(targets)
        for i in s:
            i.all_referenced_targets(result)
           
        result2 = []
        for r in result:
            if isinstance(r, property.Property):
                
                if r.feature().name() != 'use':
                    result2.append(r.value())
            else:
                result2.append(r)
        result2 = unique(result2)
        return result2

    # Returns true iff 'type' is subtype of some element of 'types-to-include'.
    #
    def include_type(self, type, types_to_include):
        return any(b2.build.type.is_subtype(type, ti) for ti in types_to_include)

# Creates a copy of target 'source'. The 'properties' object should have a
# <location> property which specifies where the target must be placed.
#
def copy_file(project, name, source, ps):

    if not name:
        name = source.name()

    relative = ""

    new_a = virtual_target.NonScanningAction([source], "common.copy", ps)
    source_root = ps.get('install-source-root')
    if source_root:
        source_root = source_root[0]
        # Get the real path of the target. We probably need to strip relative
        # path from the target name at construction.
        path = os.path.join(source.path(), os.path.dirname(name))
        # Make the path absolute. Otherwise, it would be hard to compute the
        # relative path. The 'source-root' is already absolute, see the
        # 'adjust-properties' method above.
        path = os.path.abspath(path)

        relative = os.path.relpath(path, source_root)

    name = os.path.join(relative, os.path.basename(name))
    return virtual_target.FileTarget(name, source.type(), project, new_a, exact=True)

def symlink(name, project, source, ps):
    a = virtual_target.Action([source], "symlink.ln", ps)
    return virtual_target.FileTarget(name, source.type(), project, a, exact=True)

def relink_file(project, source, ps):
    action = source[0].action()
    cloned_action = virtual_target.clone_action(action, project, "", ps)
    targets = cloned_action.targets()
    # We relink only on Unix, where exe or shared lib is always a single file.
    assert len(targets) == 1
    return targets[0]


# Declare installed version of the EXE type. Generator for this type will cause
# relinking to the new location.
b2.build.type.register('INSTALLED_EXE', [], 'EXE')

class InstalledExeGenerator(generators.Generator):

    def __init__(self):
        generators.Generator.__init__(self, "install-exe", False, ['EXE'], ['INSTALLED_EXE'])

    def run(self, project, name, ps, source):

        need_relink = False;

        if ps.get('os') in ['NT', 'CYGWIN'] or ps.get('target-os') in ['windows', 'cygwin']:
            # Never relink
            pass
        else:
            # See if the dll-path properties are not changed during
            # install. If so, copy, don't relink.
            need_relink = ps.get('dll-path') != source[0].action().properties().get('dll-path')

        if need_relink:
            return [relink_file(project, source, ps)]
        else:
            return [copy_file(project, None, source[0], ps)]

generators.register(InstalledExeGenerator())


# Installing a shared link on Unix might cause a creation of versioned symbolic
# links.
b2.build.type.register('INSTALLED_SHARED_LIB', [], 'SHARED_LIB')

class InstalledSharedLibGenerator(generators.Generator):

    def __init__(self):
        generators.Generator.__init__(self, 'install-shared-lib', False, ['SHARED_LIB'], ['INSTALLED_SHARED_LIB'])

    def run(self, project, name, ps, source):

        source = source[0]
        if ps.get('os') in ['NT', 'CYGWIN'] or ps.get('target-os') in ['windows', 'cygwin']:
            copied = copy_file(project, None, source, ps)
            return [get_manager().virtual_targets().register(copied)]
        else:
            a = source.action()
            if not a:
                # Non-derived file, just copy.
                copied = copy_file(project, source, ps)
            else:

                need_relink = ps.get('dll-path') != source.action().properties().get('dll-path')
                
                if need_relink:
                    # Rpath changed, need to relink.
                    copied = relink_file(project, source, ps)
                else:
                    copied = copy_file(project, None, source, ps)

            result = [get_manager().virtual_targets().register(copied)]
            # If the name is in the form NNN.XXX.YYY.ZZZ, where all 'X', 'Y' and
            # 'Z' are numbers, we need to create NNN.XXX and NNN.XXX.YYY
            # symbolic links.
            m = re.match("(.*)\\.([0123456789]+)\\.([0123456789]+)\\.([0123456789]+)$",
                         copied.name());
            if m:
                # Symlink without version at all is used to make
                # -lsome_library work.
                result.append(symlink(m.group(1), project, copied, ps))

                # Symlinks of some libfoo.N and libfoo.N.M are used so that
                # library can found at runtime, if libfoo.N.M.X has soname of
                # libfoo.N. That happens when the library makes some binary
                # compatibility guarantees. If not, it is possible to skip those
                # symlinks.
                if ps.get('install-no-version-symlinks') != ['on']:
                
                    result.append(symlink(m.group(1) + '.' + m.group(2), project, copied, ps))
                    result.append(symlink(m.group(1) + '.' + m.group(2) + '.' + m.group(3),
                                          project, copied, ps))

            return result
            
generators.register(InstalledSharedLibGenerator())


# Main target rule for 'install'.
#
@bjam_signature((["name"], ["sources", "*"], ["requirements", "*"],
                 ["default_build", "*"], ["usage_requirements", "*"]))
def install(name, sources, requirements=[], default_build=[], usage_requirements=[]):

    requirements = requirements[:]
    # Unless the user has explicitly asked us to hardcode dll paths, add
    # <hardcode-dll-paths>false in requirements, to override default value.
    if not '<hardcode-dll-paths>true' in requirements:
        requirements.append('<hardcode-dll-paths>false')

    if any(r.startswith('<tag>') for r in requirements):
        get_manager().errors()("The <tag> property is not allowed for the 'install' rule")

    from b2.manager import get_manager
    t = get_manager().targets()
    
    project = get_manager().projects().current()
        
    return t.main_target_alternative(
        InstallTargetClass(name, project,
                           t.main_target_sources(sources, name),
                           t.main_target_requirements(requirements, project),
                           t.main_target_default_build(default_build, project),
                           t.main_target_usage_requirements(usage_requirements, project)))

get_manager().projects().add_rule("install", install)
get_manager().projects().add_rule("stage", install)