| 1 | # Copyright 2003 Dave Abrahams |
|---|
| 2 | # Copyright 2003, 2005 Vladimir Prus |
|---|
| 3 | # Distributed under the Boost Software License, Version 1.0. |
|---|
| 4 | # (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 5 | |
|---|
| 6 | # Essentially an include guard; ensures that no module is loaded multiple times |
|---|
| 7 | .loaded ?= ; |
|---|
| 8 | |
|---|
| 9 | # A list of modules currently being loaded for error reporting of circular dependencies |
|---|
| 10 | .loading ?= ; |
|---|
| 11 | |
|---|
| 12 | # A list of modules needing to be tested via __test__ rule |
|---|
| 13 | .untested ?= ; |
|---|
| 14 | |
|---|
| 15 | # A list of modules which have been tested via __test__ |
|---|
| 16 | .tested ?= ; |
|---|
| 17 | |
|---|
| 18 | # meant to be invoked from import when no __test__ rule is defined in a given |
|---|
| 19 | # module |
|---|
| 20 | local rule no-test-defined |
|---|
| 21 | { |
|---|
| 22 | import modules ; |
|---|
| 23 | if ! ( --quiet in [ modules.peek : ARGV ] ) |
|---|
| 24 | { |
|---|
| 25 | ECHO warning: no __test__ rule defined in module $(__module__) ; |
|---|
| 26 | } |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | # return the binding of the given module |
|---|
| 30 | rule binding ( module ) |
|---|
| 31 | { |
|---|
| 32 | return $($(module).__binding__) ; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | # Sets the module-local value of a variable. This is the most |
|---|
| 36 | # reliable way to set a module-local variable in a different module; |
|---|
| 37 | # it eliminates issues of name shadowing due to dynamic scoping. |
|---|
| 38 | rule poke ( module-name ? : variables + : value * ) |
|---|
| 39 | { |
|---|
| 40 | module $(<) |
|---|
| 41 | { |
|---|
| 42 | $(>) = $(3) ; |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | # Returns the module-local value of a variable. This is the most |
|---|
| 47 | # reliable way to examine a module-local variable in a different |
|---|
| 48 | # module; it eliminates issues of name shadowing due to dynamic |
|---|
| 49 | # scoping. |
|---|
| 50 | rule peek ( module-name ? : variables + ) |
|---|
| 51 | { |
|---|
| 52 | module $(<) |
|---|
| 53 | { |
|---|
| 54 | return $($(>)) ; |
|---|
| 55 | } |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | # Call the given rule locally in the given module. Use this for rules |
|---|
| 59 | # which accept rule names as arguments, so that the passed rule may be |
|---|
| 60 | # invoked in the context of the rule's caller (for example, if the |
|---|
| 61 | # rule accesses module globals or is a local rule). |
|---|
| 62 | rule call-in ( module-name ? : rule-name args * : * ) |
|---|
| 63 | { |
|---|
| 64 | module $(module-name) |
|---|
| 65 | { |
|---|
| 66 | return [ $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) : $(9) ] ; |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | # Given a possibly qualified rule name and arguments, remove any |
|---|
| 71 | # initial module qualification from the rule and invoke it in that |
|---|
| 72 | # module. If there is no module qualification, the rule is invoked in |
|---|
| 73 | # the global module. |
|---|
| 74 | rule call-locally ( qualified-rule-name args * : * ) |
|---|
| 75 | { |
|---|
| 76 | local module-rule = [ MATCH (.*)\\.(.*) : $(qualified-rule-name) ] ; |
|---|
| 77 | local rule-name = $(module-rule[2]) ; |
|---|
| 78 | rule-name ?= $(qualified-rule-name) ; |
|---|
| 79 | return [ |
|---|
| 80 | call-in $(module-rule[1]) |
|---|
| 81 | : $(rule-name) $(args) : $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) : $(9) |
|---|
| 82 | ] ; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | # load the indicated module if it is not already loaded. |
|---|
| 86 | rule load ( |
|---|
| 87 | module-name # name of module to load. Rules will be defined in this module |
|---|
| 88 | : filename ? # (partial) path to file; Defaults to $(module-name).jam |
|---|
| 89 | : search * # Directories in which to search for filename. Defaults to $(BOOST_BUILD_PATH) |
|---|
| 90 | ) |
|---|
| 91 | { |
|---|
| 92 | # Avoid loading modules twice |
|---|
| 93 | if ! ( $(module-name) in $(.loaded) ) |
|---|
| 94 | { |
|---|
| 95 | filename ?= $(module-name).jam ; |
|---|
| 96 | |
|---|
| 97 | # Mark the module loaded so we don't try to load it recursively |
|---|
| 98 | .loaded += $(module-name) ; |
|---|
| 99 | |
|---|
| 100 | # suppress tests if any module loads are already in progress. |
|---|
| 101 | local suppress-test = $(.loading[1]) ; |
|---|
| 102 | |
|---|
| 103 | # Push this module on the loading stack |
|---|
| 104 | .loading += $(module-name) ; |
|---|
| 105 | |
|---|
| 106 | # Remember that it's untested |
|---|
| 107 | .untested += $(module-name) ; |
|---|
| 108 | |
|---|
| 109 | # Insert the new module's __name__ and __file__ globals |
|---|
| 110 | poke $(module-name) : __name__ : $(module-name) ; |
|---|
| 111 | poke $(module-name) : __file__ : $(filename) ; |
|---|
| 112 | |
|---|
| 113 | module $(module-name) |
|---|
| 114 | { |
|---|
| 115 | # Prepare a default behavior, in case no __test__ is defined. |
|---|
| 116 | IMPORT modules : no-test-defined : $(__name__) : __test__ ; |
|---|
| 117 | |
|---|
| 118 | # Add some grist so that the module will have a unique target name |
|---|
| 119 | local module-target = $(__file__:G=module@) ; |
|---|
| 120 | |
|---|
| 121 | local search = $(3) ; |
|---|
| 122 | search ?= [ modules.peek : BOOST_BUILD_PATH ] ; |
|---|
| 123 | SEARCH on $(module-target) = $(search) ; |
|---|
| 124 | BINDRULE on $(module-target) = modules.record-binding ; |
|---|
| 125 | |
|---|
| 126 | include $(module-target) ; |
|---|
| 127 | |
|---|
| 128 | # Allow the module to see its own names with full qualification |
|---|
| 129 | local rules = [ RULENAMES $(__name__) ] ; |
|---|
| 130 | IMPORT $(__name__) : $(rules) : $(__name__) : $(__name__).$(rules) ; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | if $(module-name) != modules && ! [ binding $(module-name) ] |
|---|
| 134 | { |
|---|
| 135 | import errors ; |
|---|
| 136 | errors.error "couldn't find module" $(module-name) in $(search) ; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | # Pop the loading stack. Must happen before testing or we'll find a circular loading dependency |
|---|
| 140 | .loading = $(.loading[1--2]) ; |
|---|
| 141 | |
|---|
| 142 | # Run any pending tests if this is an outer load |
|---|
| 143 | if ! $(suppress-test) |
|---|
| 144 | { |
|---|
| 145 | local argv = [ peek : ARGV ] ; |
|---|
| 146 | for local m in $(.untested) |
|---|
| 147 | { |
|---|
| 148 | if ( ! $(m) in $(.tested) ) # avoid recursive test invocations |
|---|
| 149 | && ( ( --debug in $(argv) ) || ( --debug-module=$(m) in $(argv) ) ) |
|---|
| 150 | { |
|---|
| 151 | .tested += $(m) ; |
|---|
| 152 | if ! ( --quiet in $(argv) ) |
|---|
| 153 | { |
|---|
| 154 | ECHO testing module $(m)... ; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | # Import m's rules into __test-$(m)__ for easy access |
|---|
| 158 | IMPORT $(m) : [ RULENAMES $(m) ] : __test-$(m)__ : [ RULENAMES $(m) ] ; |
|---|
| 159 | |
|---|
| 160 | # execute the module's __test__ rule in its own module to |
|---|
| 161 | # eliminate the inadvertent effects of testing |
|---|
| 162 | # module dependencies (such as assert) on the module itself. |
|---|
| 163 | IMPORT $(m) : __test__ : __test-$(m)__ : __test__ : LOCALIZE ; |
|---|
| 164 | |
|---|
| 165 | module __test-$(m)__ |
|---|
| 166 | { |
|---|
| 167 | # set up the name of the module we're testing |
|---|
| 168 | # so that no-test-defined can find it. |
|---|
| 169 | __module__ = $(1) ; |
|---|
| 170 | __test__ ; |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | .untested = ; |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | else if $(module-name) in $(.loading) |
|---|
| 178 | { |
|---|
| 179 | import errors ; |
|---|
| 180 | errors.error loading \"$(module-name)\" |
|---|
| 181 | : circular module loading dependency: |
|---|
| 182 | : $(.loading)" ->" $(module-name) ; |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | # This helper is used by load (above) to record the binding (path) of |
|---|
| 187 | # each loaded module. |
|---|
| 188 | rule record-binding ( module-target : binding ) |
|---|
| 189 | { |
|---|
| 190 | $(.loading[-1]).__binding__ = $(binding) ; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | # Transform each path in the list, with all backslashes converted to |
|---|
| 194 | # forward slashes and all detectable redundancy removed. Something |
|---|
| 195 | # like this is probably needed in path.jam, but I'm not sure of that, |
|---|
| 196 | # I don't understand it, and I'm not ready to move all of path.jam |
|---|
| 197 | # into the kernel. |
|---|
| 198 | local rule normalize-raw-paths ( paths * ) |
|---|
| 199 | { |
|---|
| 200 | local result ; |
|---|
| 201 | for p in $(paths:T) |
|---|
| 202 | { |
|---|
| 203 | result += [ NORMALIZE_PATH $(p) ] ; |
|---|
| 204 | } |
|---|
| 205 | return $(result) ; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | .cwd = [ PWD ] ; |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | # load the indicated module and import rule names into the current |
|---|
| 212 | # module. Any members of rules-opt will be available without |
|---|
| 213 | # qualification in the caller's module. Any members of rename-opt will |
|---|
| 214 | # be taken as the names of the rules in the caller's module, in place |
|---|
| 215 | # of the names they have in the imported module. If rules-opt = '*', |
|---|
| 216 | # all rules from the indicated module are imported into the caller's |
|---|
| 217 | # module. If rename-opt is supplied, it must have the same number of |
|---|
| 218 | # elements as rules-opt. |
|---|
| 219 | rule import ( module-names + : rules-opt * : rename-opt * ) |
|---|
| 220 | { |
|---|
| 221 | if $(rules-opt) = * || ! $(rules-opt) |
|---|
| 222 | { |
|---|
| 223 | if $(rename-opt) |
|---|
| 224 | { |
|---|
| 225 | errors.error "rule aliasing is only available for explicit imports." ; |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | if $(module-names[2]) && ( $(rules-opt) || $(rename-opt) ) |
|---|
| 230 | { |
|---|
| 231 | errors.error when loading multiple modules, no specific rules or renaming is allowed ; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | local caller = [ CALLER_MODULE ] ; |
|---|
| 235 | |
|---|
| 236 | # Import each specified module |
|---|
| 237 | for local m in $(module-names) |
|---|
| 238 | { |
|---|
| 239 | if ! $(m) in $(.loaded) |
|---|
| 240 | { |
|---|
| 241 | # if the importing module isn't already in the BOOST_BUILD_PATH, |
|---|
| 242 | # prepend it to the path. We don't want to invert the search |
|---|
| 243 | # order of modules that are already there. |
|---|
| 244 | |
|---|
| 245 | local caller-location ; |
|---|
| 246 | if $(caller) |
|---|
| 247 | { |
|---|
| 248 | caller-location = [ binding $(caller) ] ; |
|---|
| 249 | caller-location = $(caller-location:D) ; |
|---|
| 250 | caller-location = [ normalize-raw-paths $(caller-location:R=$(.cwd)) ] ; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | local search = [ peek : BOOST_BUILD_PATH ] ; |
|---|
| 254 | search = [ normalize-raw-paths $(search:R=$(.cwd)) ] ; |
|---|
| 255 | |
|---|
| 256 | if $(caller-location) && ! $(caller-location) in $(search) |
|---|
| 257 | { |
|---|
| 258 | search = $(caller-location) $(search) ; |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | load $(m) : : $(search) ; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | IMPORT_MODULE $(m) : $(caller) ; |
|---|
| 265 | |
|---|
| 266 | if $(rules-opt) |
|---|
| 267 | { |
|---|
| 268 | local source-names ; |
|---|
| 269 | if $(rules-opt) = * |
|---|
| 270 | { |
|---|
| 271 | local all-rules = [ RULENAMES $(m) ] ; |
|---|
| 272 | source-names = $(all-rules) ; |
|---|
| 273 | } |
|---|
| 274 | else |
|---|
| 275 | { |
|---|
| 276 | source-names = $(rules-opt) ; |
|---|
| 277 | } |
|---|
| 278 | local target-names = $(rename-opt) ; |
|---|
| 279 | target-names ?= $(source-names) ; |
|---|
| 280 | IMPORT $(m) : $(source-names) : $(caller) : $(target-names) ; |
|---|
| 281 | } |
|---|
| 282 | } |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | # Define exported copies in $(target-module) of all rules exported |
|---|
| 286 | # from $(source-module). Also make them available in the global |
|---|
| 287 | # module with qualification, so that it is just as though the rules |
|---|
| 288 | # were defined originally in $(target-module). |
|---|
| 289 | rule clone-rules ( |
|---|
| 290 | source-module |
|---|
| 291 | target-module |
|---|
| 292 | ) |
|---|
| 293 | { |
|---|
| 294 | local rules = [ RULENAMES $(source-module) ] ; |
|---|
| 295 | |
|---|
| 296 | IMPORT $(source-module) : $(rules) : $(target-module) : $(rules) : LOCALIZE ; |
|---|
| 297 | EXPORT $(target-module) : $(rules) ; |
|---|
| 298 | IMPORT $(target-module) : $(rules) : : $(target-module).$(rules) ; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | # These rules need to be available in all modules to implement |
|---|
| 302 | # module loading itself and other fundamental operations. |
|---|
| 303 | local globalize = peek poke record-binding ; |
|---|
| 304 | IMPORT modules : $(globalize) : : modules.$(globalize) ; |
|---|
| 305 | |
|---|
| 306 | local rule __test__ ( ) |
|---|
| 307 | { |
|---|
| 308 | import assert ; |
|---|
| 309 | import modules : normalize-raw-paths ; |
|---|
| 310 | |
|---|
| 311 | module modules.__test__ |
|---|
| 312 | { |
|---|
| 313 | foo = bar ; |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | assert.result bar : peek modules.__test__ : foo ; |
|---|
| 317 | poke modules.__test__ : foo : bar baz ; |
|---|
| 318 | assert.result bar baz : peek modules.__test__ : foo ; |
|---|
| 319 | assert.result c:/foo/bar : normalize-raw-paths c:/x/../foo/./xx/yy/../../bar ; |
|---|
| 320 | assert.result . : normalize-raw-paths . ; |
|---|
| 321 | assert.result .. : normalize-raw-paths .. ; |
|---|
| 322 | assert.result ../.. : normalize-raw-paths ../.. ; |
|---|
| 323 | assert.result .. : normalize-raw-paths ./.. ; |
|---|
| 324 | assert.result / / : normalize-raw-paths / \\ ; |
|---|
| 325 | assert.result a : normalize-raw-paths a ; |
|---|
| 326 | assert.result a : normalize-raw-paths a/ ; |
|---|
| 327 | assert.result /a : normalize-raw-paths /a/ ; |
|---|
| 328 | assert.result / : normalize-raw-paths /a/.. ; |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | |
|---|