summaryrefslogtreecommitdiff
path: root/jam-files/boost-build/util/utility.jam
blob: c46747f58657bd8327893828a5f8c5718e3c302e (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
# Copyright 2001, 2002 Dave Abrahams
# Copyright 2002, 2003, 2004, 2005 Vladimir Prus
# Copyright 2008 Jurko Gospodnetic
# 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)

import "class" : is-instance ;
import errors ;


# For all elements of 'list' which do not already have 'suffix', add 'suffix'.
#
rule apply-default-suffix ( suffix : list * )
{
    local result ;
    for local i in $(list)
    {
        if $(i:S) = $(suffix)
        {
            result += $(i) ;
        }
        else
        {
            result += $(i)$(suffix) ;
        }
    }
    return $(result) ;
}


# If 'name' contains a dot, returns the part before the last dot. If 'name'
# contains no dot, returns it unmodified.
#
rule basename ( name )
{
    if $(name:S)
    {
        name = $(name:B) ;
    }
    return $(name) ;
}


# Return the file of the caller of the rule that called caller-file.
#
rule caller-file ( )
{
    local bt = [ BACKTRACE ] ;
    return $(bt[9]) ;
}


# Tests if 'a' is equal to 'b'. If 'a' is a class instance, calls its 'equal'
# method. Uses ordinary jam's comparison otherwise.
#
rule equal ( a b )
{
    if [ is-instance $(a) ]
    {
        return [ $(a).equal $(b) ] ;
    }
    else
    {
        if $(a) = $(b)
        {
            return true ;
        }
    }
}


# Tests if 'a' is less than 'b'. If 'a' is a class instance, calls its 'less'
# method. Uses ordinary jam's comparison otherwise.
#
rule less ( a b )
{
    if [ is-instance $(a) ]
    {
        return [ $(a).less $(b) ] ;
    }
    else
    {
        if $(a) < $(b)
        {
            return true ;
        }
    }
}


# Returns the textual representation of argument. If it is a class instance,
# class its 'str' method. Otherwise, returns the argument.
#
rule str ( value )
{
    if [ is-instance $(value) ]
    {
        return [ $(value).str ] ;
    }
    else
    {
        return $(value) ;
    }
}


# Accepts a list of gristed values and returns them ungristed. Reports an error
# in case any of the passed parameters is not gristed, i.e. surrounded in angle
# brackets < and >.
#
rule ungrist ( names * )
{
    local result ;
    for local name in $(names)
    {
        local stripped = [ MATCH ^<(.*)>$ : $(name) ] ;
        if ! $(stripped)
        {
            errors.error "in ungrist $(names) : $(name) is not of the form <.*>" ;
        }
        result += $(stripped) ;
    }
    return $(result) ;
}


# If the passed value is quoted, unquotes it. Otherwise returns the value
# unchanged.
#
rule unquote ( value ? )
{
    local match-result = [ MATCH ^(\")(.*)(\")$ : $(value) ] ;
    if $(match-result)
    {
        return $(match-result[2]) ;
    }
    else
    {
        return $(value) ;
    }
}


rule __test__ ( )
{
    import assert ;
    import "class" : new ;
    import errors : try catch ;

    assert.result 123 : str 123 ;

    class test-class__
    {
        rule __init__ (   ) {                            }
        rule str      (   ) { return "str-test-class"  ; }
        rule less     ( a ) { return "yes, of course!" ; }
        rule equal    ( a ) { return "not sure"        ; }
    }

    assert.result "str-test-class" : str [ new test-class__ ] ;
    assert.true less 1 2 ;
    assert.false less 2 1 ;
    assert.result "yes, of course!" : less [ new test-class__ ] 1 ;
    assert.true equal 1 1 ;
    assert.false equal 1 2 ;
    assert.result "not sure" : equal [ new test-class__ ] 1 ;

    assert.result foo.lib foo.lib : apply-default-suffix .lib : foo.lib foo.lib
        ;

    assert.result foo : basename foo ;
    assert.result foo : basename foo.so ;
    assert.result foo.so : basename foo.so.1 ;

    assert.result         : unquote ;
    assert.result ""      : unquote "" ;
    assert.result foo     : unquote foo ;
    assert.result \"foo   : unquote \"foo ;
    assert.result foo\"   : unquote foo\" ;
    assert.result foo     : unquote \"foo\" ;
    assert.result \"foo\" : unquote \"\"foo\"\" ;

    assert.result         : ungrist ;
    assert.result foo     : ungrist <foo> ;
    assert.result <foo>   : ungrist <<foo>> ;
    assert.result foo bar : ungrist <foo> <bar> ;

    try ;
    {
        ungrist "" ;
    }
    catch "in ungrist  :  is not of the form <.*>" ;

    try ;
    {
        ungrist <> ;
    }
    catch "in ungrist <> : <> is not of the form <.*>" ;

    try ;
    {
        ungrist foo ;
    }
    catch "in ungrist foo : foo is not of the form <.*>" ;

    try ;
    {
        ungrist <foo ;
    }
    catch "in ungrist <foo : <foo is not of the form <.*>" ;

    try ;
    {
        ungrist foo> ;
    }
    catch "in ungrist foo> : foo> is not of the form <.*>" ;

    try ;
    {
        ungrist foo bar ;
    }
    catch "in ungrist foo : foo is not of the form <.*>" ;

    try ;
    {
        ungrist foo <bar> ;
    }
    catch "in ungrist foo : foo is not of the form <.*>" ;

    try ;
    {
        ungrist <foo> bar ;
    }
    catch "in ungrist bar : bar is not of the form <.*>" ;
}