Ticket #1062: stage.jam

File stage.jam, 17.0 KB (added by anonymous, 12 years ago)
Line 
1# Copyright 2003 Dave Abrahams
2# Copyright 2005, 2006 Rene Rivera
3# Copyright 2002, 2003, 2004, 2005, 2006 Vladimir Prus
4# Distributed under the Boost Software License, Version 1.0.
5# (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
6
7# This module defines the 'install' rule, used to copy a set of targets to
8# a single location
9
10import targets ;
11import "class" : new ;
12import property ;
13import errors : error ;
14import type : type ;
15import type ;
16import regex ;
17import generators ;
18import feature ;
19import project ;
20import property-set ;
21import virtual-target ;
22import path ;
23
24feature.feature <install-dependencies> : off on : incidental ;
25feature.feature <install-type> : : free incidental ;
26feature.feature <install-source-root> : : free path ;
27# If 'on', version symblinks for shared libraries won't be created
28# This feature has effect only on Unix.
29feature.feature <install-no-version-symlinks> : on : optional incidental ;
30feature.feature <so-version> : : free incidental ;
31
32class install-target-class : basic-target
33{
34 import feature project type errors generators path stage ;
35 import "class" : new ;
36
37 rule __init__ ( name-and-dir : project : sources * : requirements * : default-build * )
38 {
39 basic-target.__init__ $(name-and-dir) : $(project) : $(sources) : $(requirements)
40 : $(default-build) ;
41 }
42
43 # If <location> is not set, sets it based on the project data.
44 rule update-location ( property-set )
45 {
46 local loc = [ $(property-set).get <location> ] ;
47 if ! $(loc)
48 {
49 loc = [ path.root $(self.name) [ $(self.project).get location ] ] ;
50
51 property-set = [ $(property-set).add-raw $(loc:G=<location>) ] ;
52 }
53
54 return $(property-set) ;
55 }
56
57 # Takes a target that is installed and property set which is
58 # used when installing.
59 rule adjust-properties ( target : build-property-set )
60 {
61 local ps-raw ;
62 local a = [ $(target).action ] ;
63 if $(a)
64 {
65 local ps = [ $(a).properties ] ;
66 ps-raw = [ $(ps).raw ] ;
67
68 # Unless <hardcode-dll-paths>true is in properties, which can
69 # happen only if the user has explicitly requested it, nuke all
70 # <dll-path> properties
71 if [ $(property-set).get <hardcode-dll-paths> ] != true
72 {
73 ps-raw = [ property.change $(ps-raw) : <dll-path> ] ;
74 }
75
76 # If any <dll-path> properties were specified for installing,
77 # add them.
78 local l = [ $(build-property-set).get <dll-path> ] ;
79 ps-raw += $(l:G=<dll-path>) ;
80
81 # Also copy <linkflags> feature from current build
82 # set, to be used for relinking.
83 local l = [ $(build-property-set).get <linkflags> ] ;
84 ps-raw += $(l:G=<linkflags>) ;
85 }
86
87 # Remove the <tag> feature on original targets.
88 ps-raw = [ property.change $(ps-raw) : <tag> ] ;
89 # And <location>. If stage target has another stage target
90 # in sources, then we'll get virtual targets with <location>
91 # property set.
92 ps-raw = [ property.change $(ps-raw) : <location> ] ;
93
94
95 local d = [ $(build-property-set).get <dependency> ] ;
96 ps-raw += $(d:G=<dependency>) ;
97
98 local d = [ $(build-property-set).get <location> ] ;
99 ps-raw += $(d:G=<location>) ;
100
101 local ns = [ $(build-property-set).get <install-no-version-symlinks> ] ;
102 ps-raw += $(ns:G=<install-no-version-symlinks>) ;
103
104 local d = [ $(build-property-set).get <install-source-root> ] ;
105 # Make the path absolute: we'll use it to compute relative
106 # paths and making the path absolute will help.
107 if $(d)
108 {
109 d = [ path.root $(d) [ path.pwd ] ] ;
110 ps-raw += $(d:G=<install-source-root>) ;
111 }
112
113 if $(ps-raw)
114 {
115 return [ property-set.create $(ps-raw) ] ;
116 }
117 else
118 {
119 return [ property-set.empty ] ;
120 }
121 }
122
123
124 rule construct ( name : source-targets * : property-set )
125 {
126 source-targets = [
127 targets-to-stage $(source-targets) : $(property-set) ] ;
128
129 property-set = [ update-location $(property-set) ] ;
130
131 local ename = [ $(property-set).get <name> ] ;
132
133 if $(ename) && $(source-targets[2])
134 {
135 errors.error
136 "When <name> property is used in 'install', only one source is allowed" ;
137 }
138
139
140 local result ;
141 for local i in $(source-targets)
142 {
143 local staged-targets ;
144
145 local new-properties =
146 [ adjust-properties $(i) : $(property-set) ] ;
147
148 # See if something special should be done when staging this
149 # type. It is indicated by presense of special "staged" type
150 local t = [ $(i).type ] ;
151 if $(t) && [ type.registered INSTALLED_$(t) ]
152 {
153 if $(ename)
154 {
155 error.error "In 'install': <name> property specified with target that requires relinking" ;
156 }
157 else
158 {
159 local targets = [ generators.construct $(self.project) $(name) :
160 INSTALLED_$(t) : $(new-properties) : $(i) ] ;
161 staged-targets += $(targets[2-]) ;
162 }
163 }
164 else
165 {
166 staged-targets = [ stage.copy-file $(self.project) $(ename)
167 : $(i) : $(new-properties) ] ;
168 }
169
170 if ! $(staged-targets)
171 {
172 errors.error "Unable to generate staged version of " [ $(source).str ] ;
173 }
174
175 for t in $(staged-targets)
176 {
177 result += [ virtual-target.register $(t) ] ;
178 }
179 }
180
181 return [ property-set.empty ] $(result) ;
182 }
183
184
185 # Given the list of source targets explicitly passed to 'stage',
186 # returns the list of targets which must be staged.
187 rule targets-to-stage ( source-targets * : property-set )
188 {
189 local result ;
190
191 # Traverse the dependencies, if needed.
192 if [ $(property-set).get <install-dependencies> ] = "on"
193 {
194 source-targets = [ collect-targets $(source-targets) ] ;
195 }
196
197 # Filter the target types, if needed
198 local included-types = [ $(property-set).get <install-type> ] ;
199 for local r in $(source-targets)
200 {
201 local ty = [ $(r).type ] ;
202 if $(ty)
203 {
204 # Don't stage searched libs.
205 if $(ty) != SEARCHED_LIB
206 {
207 if $(included-types)
208 {
209 if [ include-type $(ty) : $(included-types) ]
210 {
211 result += $(r) ;
212 }
213 }
214 else
215 {
216 result += $(r) ;
217 }
218 }
219 }
220 else if ! $(included-types)
221 {
222 # Don't install typeless target if there's
223 # explicit list of allowed types.
224 result += $(r) ;
225 }
226 }
227
228 return $(result) ;
229 }
230
231 # CONSIDER: figure out why we can't use virtual-target.traverse here.
232 rule collect-targets ( targets * )
233 {
234 # Find subvariants
235 local s ;
236 for local t in $(targets)
237 {
238 s += [ $(t).creating-subvariant ] ;
239 }
240 s = [ sequence.unique $(s) ] ;
241
242 local result = $(targets) ;
243 for local i in $(s)
244 {
245 result += [ $(i).all-referenced-targets ] ;
246 }
247 local result2 ;
248 for local r in $(result)
249 {
250 if $(r:G) != <use>
251 {
252 result2 += $(r:G=) ;
253 }
254 }
255 result = [ sequence.unique $(result2) ] ;
256 }
257
258 # Returns true iff 'type' is subtype of some element of 'types-to-include'.
259 local rule include-type ( type : types-to-include * )
260 {
261 local found ;
262 while $(types-to-include) && ! $(found)
263 {
264 if [ type.is-subtype $(type) $(types-to-include[1]) ]
265 {
266 found = true ;
267 }
268 types-to-include = $(types-to-include[2-]) ;
269 }
270
271 return $(found) ;
272 }
273}
274
275# Creates a copy of target 'source'. The 'properties' object should
276# have a <location> property which specifies where the target must
277# be placed.
278rule copy-file ( project name ? : source : properties )
279{
280 local targets ;
281 name ?= [ $(source).name ] ;
282
283 new-a = [
284 new non-scanning-action $(source) : common.copy : $(properties) ] ;
285 local source-root = [ $(properties).get <install-source-root> ] ;
286 if $(source-root)
287 {
288 # Get the real path of the target. We probably need to strip
289 # relative path from the target name at construction...
290 local path = [ $(source).path ] ;
291 path = [ path.root $(name:D) $(path) ] ;
292 # Make the path absolute. Otherwise, it's hard to compute relative
293 # path. The 'source-root' is already absolute, see the
294 # 'adjust-properties' method above.
295 path = [ path.root $(path) [ path.pwd ] ] ;
296
297 relative = [ path.relative-to $(source-root) $(path) ] ;
298 # Note: using $(name:D=$(relative)) might be faster
299 # here, but then we need to explicitly check that
300 # relative is not ".", otherwise we might get paths like
301 #
302 # <prefix>/boost/.
303 #
304 # try to create it, and mkdir will obviously fail.
305 name = [ path.root $(name:D=) $(relative) ] ;
306 targets = [ new file-target $(name) exact : [ $(source).type ]
307 : $(project) : $(new-a) ] ;
308
309 }
310 else
311 {
312 targets = [ new file-target $(name:D=) exact : [ $(source).type ]
313 : $(project) : $(new-a) ] ;
314 }
315
316 return $(targets) ;
317}
318
319rule symlink ( name : project : source : properties )
320{
321 local a = [ new action $(source) : symlink.ln :
322 $(properties) ] ;
323 local targets = [
324 new file-target $(name) exact : [ $(source).type ] : $(project) : $(a) ] ;
325
326 return $(targets) ;
327}
328
329rule relink-file ( project : source : property-set )
330{
331 local action = [ $(source).action ] ;
332 local cloned-action = [ virtual-target.clone-action $(action) : $(project) :
333 "" : $(property-set) ] ;
334 local result = [ $(cloned-action).targets ] ;
335
336 return $(result) ;
337}
338
339# Declare installed version of the EXE type. Generator for this type will
340# cause relinking to the new location.
341type.register INSTALLED_EXE : : EXE ;
342
343class installed-exe-generator : generator
344{
345 import type property-set modules stage ;
346
347 rule __init__ ( )
348 {
349 generator.__init__ install-exe : EXE : INSTALLED_EXE ;
350 }
351
352 rule run ( project name ? : property-set : source : multiple ? )
353 {
354 if [ $(property-set).get <os> ] in NT CYGWIN
355 {
356 # Relinking is never needed on NT
357 return [ stage.copy-file $(project)
358 : $(source) : $(property-set) ] ;
359 }
360 else
361 {
362 return [ stage.relink-file $(project)
363 : $(source) : $(property-set) ] ;
364 }
365 }
366}
367
368generators.register [ new installed-exe-generator ] ;
369
370
371# Installing shared link on Unix might cause a creation of
372# versioned symbolic links.
373type.register INSTALLED_SHARED_LIB : : SHARED_LIB ;
374class installed-shared-lib-generator : generator
375{
376 import type property-set modules stage ;
377
378 rule __init__ ( )
379 {
380 generator.__init__ install-shared-lib : SHARED_LIB
381 : INSTALLED_SHARED_LIB ;
382 }
383
384 rule run ( project name ? : property-set : source : multiple ? )
385 {
386 if [ $(property-set).get <os> ] = NT
387 {
388 local copied = [ stage.copy-file $(project)
389 : $(source) : $(property-set) ] ;
390
391 copied = [ virtual-target.register $(copied) ] ;
392
393 return $(copied) ;
394 }
395 else
396 {
397 local a = [ $(source).action ] ;
398 local copied ;
399 if ! $(a)
400 {
401 # Non-derived file, just copy.
402 copied = [ stage.copy-file $(project)
403 : $(source) : $(property-set) ] ;
404 }
405 else
406 {
407 local cp = [ $(a).properties ] ;
408 local current-dll-path = [ $(cp).get <dll-path> ] ;
409 local new-dll-path = [ $(property-set).get <dll-path> ] ;
410
411 if $(current-dll-path) != $(new-dll-path)
412 {
413 # Rpath changed, need to relink.
414 copied = [ stage.relink-file
415 $(project) : $(source) : $(property-set) ] ;
416 }
417 else
418 {
419 copied = [ stage.copy-file $(project)
420 : $(source) : $(property-set) ] ;
421 }
422 }
423
424 copied = [ virtual-target.register $(copied) ] ;
425
426 local result = $(copied) ;
427 # If the name is in the form NNN.XXX.YYY.ZZZ, where all
428 # 'X', 'Y' and 'Z' are numbers, we need to create
429 # NNN.XXX and NNN.XXX.YYY symbolic links.
430 local m = [ MATCH (.*)\\.([0123456789]+)\\.([0123456789]+)\\.([0123456789]+)$
431 : [ $(copied).name ] ] ;
432 if $(m)
433 {
434 # Symlink without version at all is used to make
435 # -lsome_library work.
436 result += [ stage.symlink $(m[1]) : $(project)
437 : $(copied) : $(property-set) ] ;
438
439 # Symlinks of some libfoo.N and libfoo.N.M are used
440 # so that library can found at runtime, if libfoo.N.M.X
441 # has soname of libfoo.N. That happens when the library
442 # makes some binary compatibility guarantees. If not,
443 # it's possible to skip those symlinks.
444 local suppress =
445 [ $(property-set).get <install-no-version-symlinks> ] ;
446
447 if $(suppress) != "on"
448 {
449 result += [ stage.symlink $(m[1]).$(m[2]) : $(project)
450 : $(copied) : $(property-set) ] ;
451 result += [ stage.symlink $(m[1]).$(m[2]).$(m[3]) : $(project)
452 : $(copied) : $(property-set) ] ;
453 }
454 }
455
456 return $(result) ;
457 }
458 }
459}
460
461generators.register [ new installed-shared-lib-generator ] ;
462
463
464
465# Main target rule for 'install'
466rule install ( name : sources * : requirements * : default-build * )
467{
468 local project = [ project.current ] ;
469
470 # Unless the user has explicitly asked us to hardcode dll paths, add
471 # <hardcode-dll-paths>false in requirements, to override default
472 # value.
473 if ! <hardcode-dll-paths>true in $(requirements)
474 {
475 requirements += <hardcode-dll-paths>false ;
476 }
477
478 if <tag> in $(requirements:G)
479 {
480 errors.user-error
481 "The <tag> property is not allowed for the 'install' rule" ;
482 }
483
484 targets.main-target-alternative
485 [ new install-target-class $(name) : $(project)
486 : [ targets.main-target-sources $(sources) : $(name) ]
487 : [ targets.main-target-requirements $(requirements) : $(project) ]
488 : [ targets.main-target-default-build $(default-build) : $(project) ]
489 ] ;
490}
491
492IMPORT $(__name__) : install : : install ;
493IMPORT $(__name__) : install : : stage ;