summaryrefslogtreecommitdiff
path: root/tools/build/src/util/indirect.jam
blob: 40884da96d7994c4371337f1c8d855146abb2a8b (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
# Copyright 2003 Dave Abrahams
# Copyright 2003 Vladimir Prus
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

import modules ;
import numbers ;


# The pattern that indirect rules must match: module%rule
.pattern = ^([^%]*)%([^%]+)$ ;


#
# Type checking rules.
#
local rule indirect-rule ( x )
{
    if ! [ MATCH $(.pattern) : $(x) ]
    {
        return "expected a string of the form module%rule, but got \""$(x)"\" for argument" ;
    }
}


# Make an indirect rule which calls the given rule. If context is supplied it is
# expected to be the module in which to invoke the rule by the 'call' rule
# below. Otherwise, the rule will be invoked in the module of this rule's
# caller.
#
rule make ( rulename bound-args * : context ? )
{
    context ?= [ CALLER_MODULE ] ;
    context ?= "" ;
    return $(context)%$(rulename) $(bound-args) ;
}


# Make an indirect rule which calls the given rule. 'rulename' may be a
# qualified rule; if so it is returned unchanged. Otherwise, if frames is not
# supplied, the result will be invoked (by 'call', below) in the module of the
# caller. Otherwise, frames > 1 specifies additional call frames to back up in
# order to find the module context.
#
rule make-qualified ( rulename bound-args * : frames ? )
{
    if [ MATCH $(.pattern) : $(rulename) ]
    {
        return $(rulename) $(bound-args) ;
    }
    else
    {
        frames ?= 1 ;
        # If the rule name includes a Jamfile module, grab it.
        local module-context = [ MATCH ^(Jamfile<[^>]*>)\\..* : $(rulename) ] ;

        if ! $(module-context)
        {
            # Take the first dot-separated element as module name. This disallows
            # module names with dots, but allows rule names with dots.
            module-context = [ MATCH ^([^.]*)\\..* : $(rulename) ] ;
        }
        module-context ?= [ CALLER_MODULE $(frames) ] ;
        return [ make $(rulename) $(bound-args) : $(module-context) ] ;
    }
}


# Returns the module name in which the given indirect rule will be invoked.
#
rule get-module ( [indirect-rule] x )
{
    local m = [ MATCH $(.pattern) : $(x) ] ;
    if ! $(m[1])
    {
        m = ;
    }
    return $(m[1]) ;
}


# Returns the rulename that will be called when x is invoked.
#
rule get-rule ( [indirect-rule] x )
{
    local m = [ MATCH $(.pattern) : $(x) ] ;
    return $(m[2]) ;
}


# Invoke the given indirect-rule.
#
rule call ( [indirect-rule] r args * : * )
{
    return [ modules.call-in [ get-module $(r) ] : [ get-rule $(r) ] $(args) :
        $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) : $(9) : $(10) : $(11) :
        $(12) : $(13) : $(14) : $(15) : $(16) : $(17) : $(18) : $(19) ] ;
}


rule __test__
{
    import assert ;

    rule foo-barr! ( x )
    {
        assert.equal $(x) : x ;
    }

    assert.equal [ get-rule [ make foo-barr! ] ] : foo-barr! ;
    assert.equal [ get-module [ make foo-barr! ] ] : [ CALLER_MODULE ] ;

    call [ make foo-barr! ] x ;
    call [ make foo-barr! x ] ;
    call [ make foo-barr! : [ CALLER_MODULE ] ] x ;
}