varmisc.mk revision 1.30 1 # $NetBSD: varmisc.mk,v 1.30 2021/02/04 21:42:47 rillig Exp $
2 #
3 # Miscellaneous variable tests.
4
5 all: unmatched_var_paren D_true U_true D_false U_false Q_lhs Q_rhs NQ_none \
6 strftime cmpv manok
7 all: save-dollars
8 all: export-appended
9 all: parse-dynamic
10 all: varerror-unclosed
11
12 unmatched_var_paren:
13 @echo ${foo::=foo-text}
14
15 True= ${echo true >&2:L:sh}TRUE
16 False= ${echo false >&2:L:sh}FALSE
17
18 VSET= is set
19 .undef UNDEF
20
21 U_false:
22 @echo :U skipped when var set
23 @echo ${VSET:U${False}}
24
25 D_false:
26 @echo :D skipped if var undef
27 @echo ${UNDEF:D${False}}
28
29 U_true:
30 @echo :U expanded when var undef
31 @echo ${UNDEF:U${True}}
32
33 D_true:
34 @echo :D expanded when var set
35 @echo ${VSET:D${True}}
36
37 Q_lhs:
38 @echo :? only lhs when value true
39 @echo ${1:L:?${True}:${False}}
40
41 Q_rhs:
42 @echo :? only rhs when value false
43 @echo ${0:L:?${True}:${False}}
44
45 NQ_none:
46 @echo do not evaluate or expand :? if discarding
47 @echo ${VSET:U${1:L:?${True}:${False}}}
48
49 April1= 1459494000
50
51 # slightly contorted syntax to use utc via variable
52 strftime:
53 @echo ${year=%Y month=%m day=%d:L:gmtime=1459494000}
54 @echo date=${%Y%m%d:L:${gmtime=${April1}:L}}
55
56 # big jumps to handle 3 digits per step
57 M_cmpv.units= 1 1000 1000000
58 M_cmpv= S,., ,g:_:range:@i@+ $${_:[-$$i]} \* $${M_cmpv.units:[$$i]}@:S,^,expr 0 ,1:sh
59
60 Version= 123.456.789
61 cmpv.only= target specific vars
62
63 cmpv:
64 @echo Version=${Version} == ${Version:${M_cmpv}}
65 @echo Literal=3.4.5 == ${3.4.5:L:${M_cmpv}}
66 @echo We have ${${.TARGET:T}.only}
67
68 # catch misshandling of nested vars in .for loop
69 MAN=
70 MAN1= make.1
71 .for s in 1 2
72 . if defined(MAN$s) && !empty(MAN$s)
73 MAN+= ${MAN$s}
74 . endif
75 .endfor
76
77 manok:
78 @echo MAN=${MAN}
79
80 # begin .MAKE.SAVE_DOLLARS; see Var_SetWithFlags and ParseBoolean.
81 SD_VALUES= 0 1 2 False True false true Yes No yes no On Off ON OFF on off
82 SD_4_DOLLARS= $$$$
83
84 .for val in ${SD_VALUES}
85 .MAKE.SAVE_DOLLARS:= ${val} # Must be := since a simple = has no effect.
86 SD.${val}:= ${SD_4_DOLLARS}
87 .endfor
88 .MAKE.SAVE_DOLLARS:= yes
89
90 save-dollars:
91 .for val in ${SD_VALUES}
92 @printf '%s: %-8s = %s\n' $@ ${val} ${SD.${val}:Q}
93 .endfor
94
95 # Appending to an undefined variable does not add a space in front.
96 .undef APPENDED
97 APPENDED+= value
98 .if ${APPENDED} != "value"
99 . error "${APPENDED}"
100 .endif
101
102 # Appending to an empty variable adds a space between the old value
103 # and the additional value.
104 APPENDED= # empty
105 APPENDED+= value
106 .if ${APPENDED} != " value"
107 . error "${APPENDED}"
108 .endif
109
110 # Appending to parameterized variables works as well.
111 PARAM= param
112 VAR.${PARAM}= 1
113 VAR.${PARAM}+= 2
114 .if ${VAR.param} != "1 2"
115 . error "${VAR.param}"
116 .endif
117
118 # The variable name can contain arbitrary characters.
119 # If the expanded variable name ends in a +, this still does not influence
120 # the parser. The assignment operator is still a simple assignment.
121 # Therefore, there is no need to add a space between the variable name
122 # and the assignment operator.
123 PARAM= +
124 VAR.${PARAM}= 1
125 VAR.${PARAM}+= 2
126 .if ${VAR.+} != "1 2"
127 . error "${VAR.+}"
128 .endif
129 .for param in + ! ?
130 VAR.${param}= ${param}
131 .endfor
132 .if ${VAR.+} != "+" || ${VAR.!} != "!" || ${VAR.?} != "?"
133 . error "${VAR.+}" "${VAR.!}" "${VAR.?}"
134 .endif
135
136 # Appending to a variable from the environment creates a copy of that variable
137 # in the global scope.
138 # The appended value is not exported automatically.
139 # When a variable is exported, the exported value is taken at the time of the
140 # .export directive. Later changes to the variable have no effect.
141 .export FROM_ENV_BEFORE
142 FROM_ENV+= mk
143 FROM_ENV_BEFORE+= mk
144 FROM_ENV_AFTER+= mk
145 .export FROM_ENV_AFTER
146
147 export-appended:
148 @echo $@: "$$FROM_ENV"
149 @echo $@: "$$FROM_ENV_BEFORE"
150 @echo $@: "$$FROM_ENV_AFTER"
151
152 # begin parse-dynamic
153 #
154 # Demonstrate that the target-specific variables are not evaluated in
155 # the global scope. Their expressions are preserved until there is a local
156 # scope in which resolving them makes sense.
157
158 # There are different code paths for short names ...
159 ${:U>}= before
160 GS_TARGET:= $@
161 GS_MEMBER:= $%
162 GS_PREFIX:= $*
163 GS_ARCHIVE:= $!
164 GS_ALLSRC:= $>
165 ${:U>}= after
166 # ... and for braced short names ...
167 GB_TARGET:= ${@}
168 GB_MEMBER:= ${%}
169 GB_PREFIX:= ${*}
170 GB_ARCHIVE:= ${!}
171 GB_ALLSRC:= ${>}
172 # ... and for long names.
173 GL_TARGET:= ${.TARGET}
174 GL_MEMBER:= ${.MEMBER}
175 GL_PREFIX:= ${.PREFIX}
176 GL_ARCHIVE:= ${.ARCHIVE}
177 GL_ALLSRC:= ${.ALLSRC}
178
179 parse-dynamic:
180 @echo $@: ${GS_TARGET} ${GS_MEMBER} ${GS_PREFIX} ${GS_ARCHIVE} ${GS_ALLSRC}
181 @echo $@: ${GB_TARGET} ${GB_MEMBER} ${GB_PREFIX} ${GB_ARCHIVE} ${GB_ALLSRC}
182 @echo $@: ${GL_TARGET} ${GL_MEMBER} ${GL_PREFIX} ${GL_ARCHIVE} ${GL_ALLSRC}
183
184 # Since 2020-07-28, make complains about unclosed variables.
185 # Before that, it had complained about unclosed variables only when
186 # parsing the modifiers, but not when parsing the variable name.
187
188 UNCLOSED_INDIR_1= ${UNCLOSED_ORIG
189 UNCLOSED_INDIR_2= ${UNCLOSED_INDIR_1}
190
191 FLAGS= one two
192 FLAGS+= ${FLAGS.${.ALLSRC:M*.c:T:u}}
193 FLAGS.target2.c= three four
194
195 target1.c:
196 target2.c:
197
198 all: target1-flags target2-flags
199 target1-flags: target1.c
200 @echo $@: we have: ${FLAGS}
201
202 target2-flags: target2.c
203 @echo $@: we have: ${FLAGS}
204
205 varerror-unclosed:
206 @echo $@:begin
207 @echo $(
208 @echo $(UNCLOSED
209 @echo ${UNCLOSED
210 @echo ${UNCLOSED:M${PATTERN
211 @echo ${UNCLOSED.${param
212 @echo $
213 .for i in 1 2 3
214 @echo ${UNCLOSED.${i}
215 .endfor
216 @echo ${UNCLOSED_INDIR_2}
217 @echo $@:end
218