varmod-sysv.mk revision 1.10 1 1.10 rillig # $NetBSD: varmod-sysv.mk,v 1.10 2020/11/01 22:10:57 rillig Exp $
2 1.1 rillig #
3 1.2 rillig # Tests for the ${VAR:from=to} variable modifier, which replaces the suffix
4 1.2 rillig # "from" with "to". It can also use '%' as a wildcard.
5 1.2 rillig #
6 1.2 rillig # This modifier is applied when the other modifiers don't match exactly.
7 1.6 rillig #
8 1.6 rillig # See ApplyModifier_SysV.
9 1.1 rillig
10 1.8 rillig # A typical use case for the :from=to modifier is conversion of filename
11 1.8 rillig # extensions.
12 1.8 rillig .if ${src.c:L:.c=.o} != "src.o"
13 1.8 rillig . error
14 1.8 rillig .endif
15 1.8 rillig
16 1.8 rillig # The modifier applies to each word on its own.
17 1.8 rillig .if ${one.c two.c three.c:L:.c=.o} != "one.o two.o three.o"
18 1.8 rillig . error
19 1.8 rillig .endif
20 1.8 rillig
21 1.8 rillig # Words that don't match the pattern are passed unmodified.
22 1.8 rillig .if ${src.c src.h:L:.c=.o} != "src.o src.h"
23 1.8 rillig . error
24 1.8 rillig .endif
25 1.8 rillig
26 1.8 rillig # The :from=to modifier is therefore often combined with the :M modifier.
27 1.8 rillig .if ${src.c src.h:L:M*.c:.c=.o} != "src.o"
28 1.8 rillig . error
29 1.8 rillig .endif
30 1.8 rillig
31 1.8 rillig # Another use case for the :from=to modifier is to append a suffix to each
32 1.8 rillig # word. In this case, the "from" string is empty, therefore it always
33 1.8 rillig # matches. The same effect can be achieved with the :S,$,teen, modifier.
34 1.8 rillig .if ${four six seven nine:L:=teen} != "fourteen sixteen seventeen nineteen"
35 1.8 rillig . error
36 1.8 rillig .endif
37 1.1 rillig
38 1.9 rillig # The :from=to modifier can also be used to surround each word by strings.
39 1.9 rillig # It might be tempting to use this for enclosing a string in quotes for the
40 1.9 rillig # shell, but that's the job of the :Q modifier.
41 1.9 rillig .if ${one two three:L:%=(%)} != "(one) (two) (three)"
42 1.9 rillig . error
43 1.9 rillig .endif
44 1.9 rillig
45 1.8 rillig # When the :from=to modifier is parsed, it lasts until the closing brace
46 1.8 rillig # or parenthesis. The :Q in the below expression may look like a modifier
47 1.8 rillig # but isn't. It is part of the replacement string.
48 1.4 rillig .if ${a b c d e:L:%a=x:Q} != "x:Q b c d e"
49 1.4 rillig . error
50 1.4 rillig .endif
51 1.3 rillig
52 1.9 rillig # In the :from=to modifier, both parts can contain variable expressions.
53 1.9 rillig .if ${one two:L:${:Uone}=${:U1}} != "1 two"
54 1.9 rillig . error
55 1.9 rillig .endif
56 1.9 rillig
57 1.9 rillig # In the :from=to modifier, the "from" part is expanded exactly once.
58 1.9 rillig .if ${:U\$ \$\$ \$\$\$\$:${:U\$\$\$\$}=4} != "\$ \$\$ 4"
59 1.9 rillig . error
60 1.9 rillig .endif
61 1.9 rillig
62 1.9 rillig # In the :from=to modifier, the "to" part is expanded exactly twice.
63 1.9 rillig # XXX: The right-hand side should be expanded only once.
64 1.9 rillig # XXX: It's hard to get the escaping correct here, and to read that.
65 1.9 rillig # XXX: It's not intuitive why the closing brace must be escaped but not
66 1.9 rillig # the opening brace.
67 1.9 rillig .if ${:U1 2 4:4=${:Uonce\${\:Utwice\}}} != "1 2 oncetwice"
68 1.9 rillig . error
69 1.9 rillig .endif
70 1.9 rillig
71 1.9 rillig # The replacement string can contain spaces, thereby changing the number
72 1.9 rillig # of words in the variable expression.
73 1.9 rillig .if ${In:L:%=% ${:Uthe Sun}} != "In the Sun"
74 1.9 rillig . error
75 1.9 rillig .endif
76 1.9 rillig
77 1.9 rillig # If the variable is empty, it is debatable whether it consists of a single
78 1.9 rillig # empty word, or no word at all. The :from=to modifier treats it as no
79 1.9 rillig # word at all.
80 1.9 rillig .if ${:L:=suffix} != ""
81 1.9 rillig . error
82 1.9 rillig .endif
83 1.9 rillig
84 1.3 rillig # Before 2020-07-19, an ampersand could be used in the replacement part
85 1.4 rillig # of a SysV substitution modifier, and it was replaced with the whole match,
86 1.4 rillig # just like in the :S modifier.
87 1.4 rillig #
88 1.4 rillig # This was probably a copy-and-paste mistake since the code for the SysV
89 1.4 rillig # modifier looked a lot like the code for the :S and :C modifiers.
90 1.4 rillig # The ampersand is not mentioned in the manual page.
91 1.4 rillig .if ${a.bcd.e:L:a.%=%} != "bcd.e"
92 1.4 rillig . error
93 1.4 rillig .endif
94 1.4 rillig # Before 2020-07-19, the result of the expression was "a.bcd.e".
95 1.4 rillig .if ${a.bcd.e:L:a.%=&} != "&"
96 1.4 rillig . error
97 1.4 rillig .endif
98 1.3 rillig
99 1.3 rillig # Before 2020-07-20, when a SysV modifier was parsed, a single dollar
100 1.4 rillig # before the '=' was parsed (but not interpreted) as an anchor.
101 1.4 rillig # Parsing something without then evaluating it accordingly doesn't make
102 1.4 rillig # sense.
103 1.4 rillig .if ${value:L:e$=x} != "value"
104 1.4 rillig . error
105 1.4 rillig .endif
106 1.4 rillig # Before 2020-07-20, the modifier ":e$=x" was parsed as having a left-hand
107 1.4 rillig # side "e" and a right-hand side "x". The dollar was parsed (but not
108 1.4 rillig # interpreted) as 'anchor at the end'. Therefore the modifier was equivalent
109 1.4 rillig # to ":e=x", which doesn't match the string "value$". Therefore the whole
110 1.4 rillig # expression evaluated to "value$".
111 1.4 rillig .if ${${:Uvalue\$}:L:e$=x} != "valux"
112 1.4 rillig . error
113 1.4 rillig .endif
114 1.4 rillig .if ${value:L:e=x} != "valux"
115 1.4 rillig . error
116 1.4 rillig .endif
117 1.3 rillig
118 1.3 rillig # Words that don't match are copied unmodified.
119 1.4 rillig .if ${:Ufile.c file.h:%.c=%.cpp} != "file.cpp file.h"
120 1.4 rillig . error
121 1.4 rillig .endif
122 1.4 rillig
123 1.4 rillig # The % placeholder can be anywhere in the string, it doesn't have to be at
124 1.4 rillig # the beginning of the pattern.
125 1.4 rillig .if ${:Ufile.c other.c:file.%=renamed.%} != "renamed.c other.c"
126 1.4 rillig . error
127 1.4 rillig .endif
128 1.3 rillig
129 1.9 rillig # It's also possible to modify each word by replacing the prefix and adding
130 1.9 rillig # a suffix.
131 1.9 rillig .if ${one two:L:o%=a%w} != "anew two"
132 1.9 rillig . error
133 1.9 rillig .endif
134 1.9 rillig
135 1.7 rillig # Each word gets the suffix "X" appended.
136 1.7 rillig .if ${one two:L:=X} != "oneX twoX"
137 1.7 rillig . error
138 1.7 rillig .endif
139 1.7 rillig
140 1.6 rillig # The suffix "o" is replaced with "X".
141 1.6 rillig .if ${one two:L:o=X} != "one twX"
142 1.4 rillig . error
143 1.4 rillig .endif
144 1.6 rillig
145 1.6 rillig # The suffix "o" is replaced with nothing.
146 1.6 rillig .if ${one two:L:o=} != "one tw"
147 1.4 rillig . error
148 1.4 rillig .endif
149 1.6 rillig
150 1.6 rillig # The suffix "o" is replaced with a literal percent. The percent is only
151 1.6 rillig # a wildcard when it appears on the left-hand side.
152 1.6 rillig .if ${one two:L:o=%} != "one tw%"
153 1.4 rillig . error
154 1.4 rillig .endif
155 1.6 rillig
156 1.6 rillig # Each word with the suffix "o" is replaced with "X". The percent is a
157 1.6 rillig # wildcard even though the right-hand side does not contain another percent.
158 1.6 rillig .if ${one two:L:%o=X} != "one X"
159 1.4 rillig . error
160 1.4 rillig .endif
161 1.6 rillig
162 1.6 rillig # Each word with the prefix "o" is replaced with "X". The percent is a
163 1.6 rillig # wildcard even though the right-hand side does not contain another percent.
164 1.6 rillig .if ${one two:L:o%=X} != "X two"
165 1.4 rillig . error
166 1.4 rillig .endif
167 1.6 rillig
168 1.6 rillig # For each word with the prefix "o" and the suffix "e", the whole word is
169 1.6 rillig # replaced with "X".
170 1.6 rillig .if ${one two oe oxen:L:o%e=X} != "X two X oxen"
171 1.4 rillig . error
172 1.4 rillig .endif
173 1.6 rillig
174 1.6 rillig # Only the first '%' is the wildcard.
175 1.6 rillig .if ${one two o%e other%e:L:o%%e=X} != "one two X X"
176 1.4 rillig . error
177 1.4 rillig .endif
178 1.6 rillig
179 1.6 rillig # In the replacement, only the first '%' is the placeholder, all others
180 1.6 rillig # are literal percent characters.
181 1.6 rillig .if ${one two:L:%=%%} != "one% two%"
182 1.4 rillig . error
183 1.4 rillig .endif
184 1.6 rillig
185 1.6 rillig # In the word "one", only a prefix of the pattern suffix "nes" matches,
186 1.6 rillig # the whole word is too short. Therefore it doesn't match.
187 1.6 rillig .if ${one two:L:%nes=%xxx} != "one two"
188 1.4 rillig . error
189 1.3 rillig .endif
190 1.5 rillig
191 1.9 rillig # The :from=to modifier can be used to replace both the prefix and a suffix
192 1.9 rillig # of a word with other strings. This is not possible with a single :S
193 1.9 rillig # modifier, and using a :C modifier for the same task looks more complicated
194 1.9 rillig # in many cases.
195 1.9 rillig .if ${prefix-middle-suffix:L:prefix-%-suffix=p-%-s} != "p-middle-s"
196 1.9 rillig . error
197 1.9 rillig .endif
198 1.9 rillig
199 1.10 rillig # This is not a SysV modifier since the nested variable expression expands
200 1.10 rillig # to an empty string. The '=' in it should be irrelevant during parsing.
201 1.10 rillig # As of 2020-11-01, this seemingly correct modifier leads to a parse error.
202 1.10 rillig # XXX
203 1.10 rillig .if ${word203:L:from${:D=}to}
204 1.10 rillig . error
205 1.10 rillig .endif
206 1.10 rillig
207 1.10 rillig # XXX: This specially constructed case demonstrates that the SysV modifier
208 1.10 rillig # lasts longer than expected. The whole expression initially has the value
209 1.10 rillig # "fromto}...". The next modifier is a SysV modifier. ApplyModifier_SysV
210 1.10 rillig # parses the modifier as "from${:D=}to", ending at the '}'. Next, the two
211 1.10 rillig # parts of the modifier are parsed using ParseModifierPart, which scans
212 1.10 rillig # differently, properly handling nested variable expressions. The two parts
213 1.10 rillig # are now "fromto}..." and "replaced".
214 1.10 rillig .if "${:Ufromto\}...:from${:D=}to}...=replaced}" != "replaced"
215 1.10 rillig . error
216 1.10 rillig .endif
217 1.10 rillig
218 1.5 rillig # As of 2020-10-06, the right-hand side of the SysV modifier is expanded
219 1.5 rillig # twice. The first expansion happens in ApplyModifier_SysV, where the
220 1.5 rillig # modifier is split into its two parts. The second expansion happens
221 1.5 rillig # when each word is replaced in ModifyWord_SYSVSubst.
222 1.5 rillig # XXX: This is unexpected. Add more test case to demonstrate the effects
223 1.5 rillig # of removing one of the expansions.
224 1.5 rillig VALUE= value
225 1.5 rillig INDIRECT= 1:${VALUE} 2:$${VALUE} 4:$$$${VALUE}
226 1.5 rillig .if ${x:L:x=${INDIRECT}} != "1:value 2:value 4:\${VALUE}"
227 1.5 rillig . error
228 1.5 rillig .endif
229 1.8 rillig
230 1.8 rillig all:
231