varmod-indirect.mk revision 1.2 1 1.2 rillig # $NetBSD: varmod-indirect.mk,v 1.2 2020/12/20 19:29:06 rillig Exp $
2 1.1 rillig #
3 1.1 rillig # Tests for indirect variable modifiers, such as in ${VAR:${M_modifiers}}.
4 1.1 rillig # These can be used for very basic purposes like converting a string to either
5 1.1 rillig # uppercase or lowercase, as well as for fairly advanced modifiers that first
6 1.1 rillig # look like line noise and are hard to decipher.
7 1.1 rillig #
8 1.1 rillig # TODO: Since when are indirect modifiers supported?
9 1.1 rillig
10 1.1 rillig
11 1.2 rillig # To apply a modifier indirectly via another variable, the whole
12 1.2 rillig # modifier must be put into a single variable expression.
13 1.2 rillig .if ${value:L:${:US}${:U,value,replacement,}} != "S,value,replacement,}"
14 1.2 rillig . warning unexpected
15 1.2 rillig .endif
16 1.2 rillig
17 1.2 rillig
18 1.2 rillig # Adding another level of indirection (the 2 nested :U expressions) helps.
19 1.2 rillig .if ${value:L:${:U${:US}${:U,value,replacement,}}} != "replacement"
20 1.2 rillig . warning unexpected
21 1.2 rillig .endif
22 1.2 rillig
23 1.2 rillig
24 1.2 rillig # Multiple indirect modifiers can be applied one after another as long as
25 1.2 rillig # they are separated with colons.
26 1.2 rillig .if ${value:L:${:US,a,A,}:${:US,e,E,}} != "vAluE"
27 1.2 rillig . warning unexpected
28 1.2 rillig .endif
29 1.2 rillig
30 1.2 rillig
31 1.2 rillig # An indirect variable that evaluates to the empty string is allowed though.
32 1.2 rillig # This makes it possible to define conditional modifiers, like this:
33 1.2 rillig #
34 1.2 rillig # M.little-endian= S,1234,4321,
35 1.2 rillig # M.big-endian= # none
36 1.2 rillig .if ${value:L:${:Dempty}S,a,A,} != "vAlue"
37 1.2 rillig . warning unexpected
38 1.2 rillig .endif
39 1.2 rillig
40 1.2 rillig
41 1.1 rillig # The nested variable expression expands to "tu", and this is interpreted as
42 1.1 rillig # a variable modifier for the value "Upper", resulting in "UPPER".
43 1.1 rillig .if ${Upper:L:${:Utu}} != "UPPER"
44 1.1 rillig . error
45 1.1 rillig .endif
46 1.1 rillig
47 1.1 rillig # The nested variable expression expands to "tl", and this is interpreted as
48 1.1 rillig # a variable modifier for the value "Lower", resulting in "lower".
49 1.1 rillig .if ${Lower:L:${:Utl}} != "lower"
50 1.1 rillig . error
51 1.1 rillig .endif
52 1.1 rillig
53 1.1 rillig
54 1.1 rillig # The nested variable expression is ${1 != 1:?Z:tl}, consisting of the
55 1.1 rillig # condition "1 != 1", the then-branch "Z" and the else-branch "tl". Since
56 1.1 rillig # the condition evaluates to false, the then-branch is ignored (it would
57 1.1 rillig # have been an unknown modifier anyway) and the ":tl" modifier is applied.
58 1.1 rillig .if ${Mixed:L:${1 != 1:?Z:tl}} != "mixed"
59 1.1 rillig . error
60 1.1 rillig .endif
61 1.1 rillig
62 1.1 rillig
63 1.1 rillig # The indirect modifier can also replace an ':L' modifier, which allows for
64 1.1 rillig # brain twisters since by reading the expression alone, it is not possible
65 1.1 rillig # to say whether the variable name will be evaluated as a variable name or
66 1.1 rillig # as the immediate value of the expression.
67 1.1 rillig VAR= value
68 1.1 rillig M_ExpandVar= # an empty modifier
69 1.1 rillig M_VarAsValue= L
70 1.1 rillig #
71 1.1 rillig .if ${VAR:${M_ExpandVar}} != "value"
72 1.1 rillig . error
73 1.1 rillig .endif
74 1.1 rillig .if ${VAR:${M_VarAsValue}} != "VAR"
75 1.1 rillig . error
76 1.1 rillig .endif
77 1.1 rillig
78 1.1 rillig # The indirect modifier M_ListToSkip, when applied to a list of patterns,
79 1.1 rillig # expands to a sequence of ':N' modifiers, each of which filters one of the
80 1.1 rillig # patterns. This list of patterns can then be applied to another variable
81 1.1 rillig # to actually filter that variable.
82 1.1 rillig #
83 1.1 rillig M_ListToSkip= @pat@N$${pat}@:ts:
84 1.1 rillig #
85 1.1 rillig # The dollar signs need to be doubled in the above modifier expression,
86 1.1 rillig # otherwise they would be expanded too early, that is, when parsing the
87 1.1 rillig # modifier itself.
88 1.1 rillig #
89 1.1 rillig # In the following example, M_NoPrimes expands to 'N2:N3:N5:N7:N1[1379]'.
90 1.1 rillig # The 'N' comes from the expression 'N${pat}', the separating colons come
91 1.1 rillig # from the modifier ':ts:'.
92 1.1 rillig #
93 1.1 rillig #.MAKEFLAGS: -dcv # Uncomment this line to see the details
94 1.1 rillig #
95 1.1 rillig PRIMES= 2 3 5 7 1[1379]
96 1.1 rillig M_NoPrimes= ${PRIMES:${M_ListToSkip}}
97 1.1 rillig .if ${:U:range=20:${M_NoPrimes}} != "1 4 6 8 9 10 12 14 15 16 18 20"
98 1.1 rillig . error
99 1.1 rillig .endif
100 1.1 rillig .MAKEFLAGS: -d0
101 1.1 rillig
102 1.1 rillig all:
103