varmod-indirect.mk revision 1.1 1 # $NetBSD: varmod-indirect.mk,v 1.1 2020/12/01 22:16:36 rillig Exp $
2 #
3 # Tests for indirect variable modifiers, such as in ${VAR:${M_modifiers}}.
4 # These can be used for very basic purposes like converting a string to either
5 # uppercase or lowercase, as well as for fairly advanced modifiers that first
6 # look like line noise and are hard to decipher.
7 #
8 # TODO: Since when are indirect modifiers supported?
9
10
11 # The nested variable expression expands to "tu", and this is interpreted as
12 # a variable modifier for the value "Upper", resulting in "UPPER".
13 .if ${Upper:L:${:Utu}} != "UPPER"
14 . error
15 .endif
16
17 # The nested variable expression expands to "tl", and this is interpreted as
18 # a variable modifier for the value "Lower", resulting in "lower".
19 .if ${Lower:L:${:Utl}} != "lower"
20 . error
21 .endif
22
23
24 # The nested variable expression is ${1 != 1:?Z:tl}, consisting of the
25 # condition "1 != 1", the then-branch "Z" and the else-branch "tl". Since
26 # the condition evaluates to false, the then-branch is ignored (it would
27 # have been an unknown modifier anyway) and the ":tl" modifier is applied.
28 .if ${Mixed:L:${1 != 1:?Z:tl}} != "mixed"
29 . error
30 .endif
31
32
33 # The indirect modifier can also replace an ':L' modifier, which allows for
34 # brain twisters since by reading the expression alone, it is not possible
35 # to say whether the variable name will be evaluated as a variable name or
36 # as the immediate value of the expression.
37 VAR= value
38 M_ExpandVar= # an empty modifier
39 M_VarAsValue= L
40 #
41 .if ${VAR:${M_ExpandVar}} != "value"
42 . error
43 .endif
44 .if ${VAR:${M_VarAsValue}} != "VAR"
45 . error
46 .endif
47
48 # The indirect modifier M_ListToSkip, when applied to a list of patterns,
49 # expands to a sequence of ':N' modifiers, each of which filters one of the
50 # patterns. This list of patterns can then be applied to another variable
51 # to actually filter that variable.
52 #
53 M_ListToSkip= @pat@N$${pat}@:ts:
54 #
55 # The dollar signs need to be doubled in the above modifier expression,
56 # otherwise they would be expanded too early, that is, when parsing the
57 # modifier itself.
58 #
59 # In the following example, M_NoPrimes expands to 'N2:N3:N5:N7:N1[1379]'.
60 # The 'N' comes from the expression 'N${pat}', the separating colons come
61 # from the modifier ':ts:'.
62 #
63 #.MAKEFLAGS: -dcv # Uncomment this line to see the details
64 #
65 PRIMES= 2 3 5 7 1[1379]
66 M_NoPrimes= ${PRIMES:${M_ListToSkip}}
67 .if ${:U:range=20:${M_NoPrimes}} != "1 4 6 8 9 10 12 14 15 16 18 20"
68 . error
69 .endif
70 .MAKEFLAGS: -d0
71
72 all:
73