File: modules.jam

package info (click to toggle)
boost-build 2.0-m12-2
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 8,692 kB
  • ctags: 6,963
  • sloc: ansic: 39,914; sh: 9,086; python: 6,120; xml: 5,524; cpp: 1,467; yacc: 456; asm: 353; makefile: 184
file content (331 lines) | stat: -rwxr-xr-x 11,414 bytes parent folder | download | duplicates (2)
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
# Copyright 2003 Dave Abrahams 
# Copyright 2003, 2005 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) 

# Essentially an include guard; ensures that no module is loaded multiple times
.loaded ?= ;

# A list of modules currently being loaded for error reporting of circular dependencies
.loading ?= ;

# A list of modules needing to be tested via __test__ rule
.untested ?= ;

# A list of modules which have been tested via __test__
.tested ?= ;

# meant to be invoked from import when no __test__ rule is defined in a given
# module
local rule no-test-defined
{
    import modules ;
    if ! ( --quiet in [ modules.peek : ARGV ] )
    {
        ECHO warning: no __test__ rule defined in module $(__module__) ;
    }
}

# return the binding of the given module
rule binding ( module )
{
    return $($(module).__binding__) ;
}

# Sets the module-local value of a variable.  This is the most
# reliable way to set a module-local variable in a different module;
# it eliminates issues of name shadowing due to dynamic scoping.
rule poke ( module-name ? : variables + : value * )
{
    module $(<)
    {
        $(>) = $(3) ;
    }
}

# Returns the module-local value of a variable.  This is the most
# reliable way to examine a module-local variable in a different
# module; it eliminates issues of name shadowing due to dynamic
# scoping.
rule peek ( module-name ? : variables + )
{
    module $(<)
    {
        return $($(>)) ;
    }
}

# Call the given rule locally in the given module. Use this for rules
# which accept rule names as arguments, so that the passed rule may be
# invoked in the context of the rule's caller (for example, if the
# rule accesses module globals or is a local rule).
rule call-in ( module-name ? : rule-name args * : * )
{
    module $(module-name)
    {
        return [ $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) : $(9) ] ;
    }
}

# Given a possibly qualified rule name and arguments, remove any
# initial module qualification from the rule and invoke it in that
# module.  If there is no module qualification, the rule is invoked in
# the global module.
rule call-locally ( qualified-rule-name args * : * )
{
    local module-rule = [ MATCH (.*)\\.(.*) : $(qualified-rule-name) ] ;
    local rule-name = $(module-rule[2]) ;
    rule-name ?= $(qualified-rule-name) ;
    return [ 
      call-in $(module-rule[1])
        : $(rule-name) $(args) : $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) : $(9)
    ] ;
}

# load the indicated module if it is not already loaded.
rule load (
  module-name      # name of module to load. Rules will be defined in this module
    : filename ?   # (partial) path to file; Defaults to $(module-name).jam
    : search *     # Directories in which to search for filename. Defaults to $(BOOST_BUILD_PATH)
)
{
    # Avoid loading modules twice
    if ! ( $(module-name) in $(.loaded) )
    {
        filename ?= $(module-name).jam ;
        
        # Mark the module loaded so we don't try to load it recursively
        .loaded += $(module-name) ;
        
        # suppress tests if any module loads are already in progress. 
        local suppress-test = $(.loading[1]) ;
        
        # Push this module on the loading stack
        .loading += $(module-name) ;
        
        # Remember that it's untested
        .untested += $(module-name) ; 
        
        # Insert the new module's __name__ and __file__ globals
        poke $(module-name) : __name__ : $(module-name) ;
        poke $(module-name) : __file__ : $(filename) ;
        
        module $(module-name)
        {
            # Prepare a default behavior, in case no __test__ is defined.
            IMPORT modules : no-test-defined : $(__name__) : __test__ ;

            # Add some grist so that the module will have a unique target name
            local module-target = $(__file__:G=module@) ;
            
            local search = $(3) ;
            search ?= [ modules.peek : BOOST_BUILD_PATH ] ;
            SEARCH on $(module-target) = $(search) ;
            BINDRULE on $(module-target) = modules.record-binding ;
            
            include $(module-target) ;
            
            # Allow the module to see its own names with full qualification
            local rules = [ RULENAMES $(__name__) ] ;
            IMPORT $(__name__) : $(rules) : $(__name__) : $(__name__).$(rules) ;
        }

        if $(module-name) != modules && ! [ binding $(module-name) ]
        {
            import errors ;
            errors.error "couldn't find module" $(module-name) in $(search) ;
        }
        
        # Pop the loading stack. Must happen before testing or we'll find a circular loading dependency
        .loading = $(.loading[1--2]) ;
        
        # Run any pending tests if this is an outer load
        if ! $(suppress-test)
        {
            local argv = [ peek : ARGV ] ;
            for local m in $(.untested)
            {
                if ( ! $(m) in $(.tested) )    # avoid recursive test invocations
                   && ( ( --debug in $(argv) ) || ( --debug-module=$(m) in $(argv) ) )
                {
                    .tested += $(m) ;
                    if ! ( --quiet in $(argv) )
                    {
                        ECHO testing module $(m)... ;
                    }
                    
                    # Import m's rules into __test-$(m)__ for easy access
                    IMPORT $(m) : [ RULENAMES $(m) ] : __test-$(m)__ : [ RULENAMES $(m) ] ;
                    
                    # execute the module's __test__ rule in its own module to
                    # eliminate the inadvertent effects of testing
                    # module dependencies (such as assert) on the module itself.
                    IMPORT $(m) : __test__ : __test-$(m)__ : __test__ : LOCALIZE ;
                    
                    module __test-$(m)__
                    {
                        # set up the name of the module we're testing
                        # so that no-test-defined can find it.
                        __module__ = $(1) ; 
                        __test__ ;
                    }
                }
            }
            .untested = ;
        }
    }
    else if $(module-name) in $(.loading)
    {
        import errors ;
        errors.error loading \"$(module-name)\"
        : circular module loading dependency:
        : $(.loading)" ->" $(module-name) ;
    }
}

# This helper is used by load (above) to record the binding (path) of
# each loaded module.
rule record-binding ( module-target : binding )
{
    $(.loading[-1]).__binding__ = $(binding) ;
}

# Transform each path in the list, with all backslashes converted to
# forward slashes and all detectable redundancy removed.  Something
# like this is probably needed in path.jam, but I'm not sure of that,
# I don't understand it, and I'm not ready to move all of path.jam
# into the kernel.
local rule normalize-raw-paths ( paths * )
{
    local result ;
    for p in $(paths:T)
    {
        result += [ NORMALIZE_PATH $(p) ] ;
    }
    return $(result) ;
}

.cwd = [ PWD ] ;


# load the indicated module and import rule names into the current
# module. Any members of rules-opt will be available without
# qualification in the caller's module. Any members of rename-opt will
# be taken as the names of the rules in the caller's module, in place
# of the names they have in the imported module. If rules-opt = '*',
# all rules from the indicated module are imported into the caller's
# module. If rename-opt is supplied, it must have the same number of
# elements as rules-opt.
rule import ( module-names + : rules-opt * : rename-opt * )
{
    if $(rules-opt) = * || ! $(rules-opt) 
    {
        if $(rename-opt)
        {
            errors.error "rule aliasing is only available for explicit imports." ;
        }
    }
    
    if $(module-names[2]) && ( $(rules-opt) || $(rename-opt) )
    {
        errors.error when loading multiple modules, no specific rules or renaming is allowed ;
    }
    
    local caller = [ CALLER_MODULE ] ;    
        
    # Import each specified module
    for local m in $(module-names)
    {
        if ! $(m) in $(.loaded)
        {           
            # if the importing module isn't already in the BOOST_BUILD_PATH,
            # prepend it to the path.  We don't want to invert the search
            # order of modules that are already there.
                        
            local caller-location ; 
            if $(caller)
            {
                caller-location = [ binding $(caller) ] ;
                caller-location = $(caller-location:D) ;
                caller-location = [ normalize-raw-paths $(caller-location:R=$(.cwd)) ] ;
            }
            
            local search = [ peek : BOOST_BUILD_PATH ] ;
            search = [ normalize-raw-paths $(search:R=$(.cwd)) ] ;
            
            if $(caller-location) && ! $(caller-location) in $(search)
            {
                search = $(caller-location) $(search) ;
            }
                        
            load $(m) : : $(search) ;
        }
        
        IMPORT_MODULE $(m) : $(caller) ;
                    
        if $(rules-opt)
        {
            local source-names ;
            if $(rules-opt) = *
            {
                local all-rules = [ RULENAMES $(m) ] ;
                source-names = $(all-rules) ;
            }
            else
            {
                source-names = $(rules-opt) ;
            }
            local target-names = $(rename-opt) ;
            target-names ?= $(source-names) ;
            IMPORT $(m) : $(source-names) : $(caller) : $(target-names) ;
        }
    }
}

# Define exported copies in $(target-module) of all rules exported
# from $(source-module).  Also make them available in the global
# module with qualification, so that it is just as though the rules
# were defined originally in $(target-module).
rule clone-rules (
    source-module
    target-module 
    )
{
    local rules = [ RULENAMES $(source-module) ] ;
    
    IMPORT $(source-module) : $(rules) : $(target-module) : $(rules) : LOCALIZE ;
    EXPORT $(target-module) : $(rules) ;
    IMPORT $(target-module) : $(rules) : : $(target-module).$(rules) ;
}

# These rules need to be available in all modules to implement
# module loading itself and other fundamental operations.
local globalize = peek poke record-binding ;
IMPORT modules : $(globalize) : : modules.$(globalize) ;

local rule __test__ ( )
{
    import assert ;
    import modules : normalize-raw-paths ;
    
    module modules.__test__
    {
        foo = bar ;
    }
    
    assert.result bar : peek modules.__test__ : foo ;
    poke modules.__test__ : foo : bar baz ;
    assert.result bar baz : peek modules.__test__ : foo ;
    assert.result c:/foo/bar : normalize-raw-paths c:/x/../foo/./xx/yy/../../bar ;
    assert.result . : normalize-raw-paths . ;
    assert.result .. : normalize-raw-paths .. ;
    assert.result ../.. : normalize-raw-paths ../.. ;
    assert.result .. : normalize-raw-paths ./.. ;
    assert.result / / : normalize-raw-paths / \\ ;
    assert.result a : normalize-raw-paths a ;
    assert.result a : normalize-raw-paths a/ ;
    assert.result /a : normalize-raw-paths /a/ ;
    assert.result / : normalize-raw-paths /a/.. ;
}