varmisc.mk revision 1.15 1 1.15 rillig # $Id: varmisc.mk,v 1.15 2020/07/26 20:30:42 rillig Exp $
2 1.1 apb #
3 1.1 apb # Miscellaneous variable tests.
4 1.1 apb
5 1.6 sjg all: unmatched_var_paren D_true U_true D_false U_false Q_lhs Q_rhs NQ_none \
6 1.9 sjg strftime cmpv manok
7 1.12 rillig all: save-dollars
8 1.14 rillig all: export-appended
9 1.15 rillig all: parse-dynamic
10 1.1 apb
11 1.1 apb unmatched_var_paren:
12 1.2 sjg @echo ${foo::=foo-text}
13 1.3 sjg
14 1.4 sjg True = ${echo true >&2:L:sh}TRUE
15 1.4 sjg False= ${echo false >&2:L:sh}FALSE
16 1.3 sjg
17 1.3 sjg VSET= is set
18 1.3 sjg .undef UNDEF
19 1.3 sjg
20 1.3 sjg U_false:
21 1.3 sjg @echo :U skipped when var set
22 1.3 sjg @echo ${VSET:U${False}}
23 1.3 sjg
24 1.3 sjg D_false:
25 1.3 sjg @echo :D skipped if var undef
26 1.3 sjg @echo ${UNDEF:D${False}}
27 1.3 sjg
28 1.3 sjg U_true:
29 1.3 sjg @echo :U expanded when var undef
30 1.3 sjg @echo ${UNDEF:U${True}}
31 1.3 sjg
32 1.3 sjg D_true:
33 1.3 sjg @echo :D expanded when var set
34 1.3 sjg @echo ${VSET:D${True}}
35 1.3 sjg
36 1.3 sjg Q_lhs:
37 1.3 sjg @echo :? only lhs when value true
38 1.3 sjg @echo ${1:L:?${True}:${False}}
39 1.3 sjg
40 1.3 sjg Q_rhs:
41 1.3 sjg @echo :? only rhs when value false
42 1.3 sjg @echo ${0:L:?${True}:${False}}
43 1.5 sjg
44 1.5 sjg NQ_none:
45 1.5 sjg @echo do not evaluate or expand :? if discarding
46 1.5 sjg @echo ${VSET:U${1:L:?${True}:${False}}}
47 1.6 sjg
48 1.6 sjg April1= 1459494000
49 1.6 sjg
50 1.6 sjg # slightly contorted syntax to use utc via variable
51 1.6 sjg strftime:
52 1.6 sjg @echo ${year=%Y month=%m day=%d:L:gmtime=1459494000}
53 1.6 sjg @echo date=${%Y%m%d:L:${gmtime=${April1}:L}}
54 1.6 sjg
55 1.7 sjg # big jumps to handle 3 digits per step
56 1.8 sjg M_cmpv.units = 1 1000 1000000
57 1.7 sjg M_cmpv = S,., ,g:_:range:@i@+ $${_:[-$$i]} \* $${M_cmpv.units:[$$i]}@:S,^,expr 0 ,1:sh
58 1.7 sjg
59 1.8 sjg Version = 123.456.789
60 1.8 sjg cmpv.only = target specific vars
61 1.7 sjg
62 1.7 sjg cmpv:
63 1.7 sjg @echo Version=${Version} == ${Version:${M_cmpv}}
64 1.7 sjg @echo Literal=3.4.5 == ${3.4.5:L:${M_cmpv}}
65 1.8 sjg @echo We have ${${.TARGET:T}.only}
66 1.9 sjg
67 1.9 sjg # catch misshandling of nested vars in .for loop
68 1.9 sjg MAN=
69 1.9 sjg MAN1= make.1
70 1.9 sjg .for s in 1 2
71 1.9 sjg .if defined(MAN$s) && !empty(MAN$s)
72 1.9 sjg MAN+= ${MAN$s}
73 1.9 sjg .endif
74 1.9 sjg .endfor
75 1.9 sjg
76 1.9 sjg manok:
77 1.9 sjg @echo MAN=${MAN}
78 1.10 rillig
79 1.10 rillig # This is an expanded variant of the above .for loop.
80 1.11 rillig # Between 2020-06-28 and 2020-07-02 this paragraph generated a wrong
81 1.10 rillig # error message "Variable VARNAME is recursive".
82 1.10 rillig # When evaluating the !empty expression, the ${:U1} was not expanded and
83 1.10 rillig # thus resulted in the seeming definition VARNAME=${VARNAME}, which is
84 1.10 rillig # obviously recursive.
85 1.10 rillig VARNAME= ${VARNAME${:U1}}
86 1.10 rillig .if defined(VARNAME${:U2}) && !empty(VARNAME${:U2})
87 1.10 rillig .endif
88 1.12 rillig
89 1.12 rillig # begin .MAKE.SAVE_DOLLARS; see Var_Set_with_flags and s2Boolean.
90 1.12 rillig SD_VALUES= 0 1 2 False True false true Yes No yes no On Off ON OFF on off
91 1.12 rillig SD_4_DOLLARS= $$$$
92 1.12 rillig
93 1.12 rillig .for val in ${SD_VALUES}
94 1.12 rillig .MAKE.SAVE_DOLLARS:= ${val} # Must be := since a simple = has no effect.
95 1.12 rillig SD.${val}:= ${SD_4_DOLLARS}
96 1.12 rillig .endfor
97 1.12 rillig .MAKE.SAVE_DOLLARS:= yes
98 1.12 rillig
99 1.12 rillig save-dollars:
100 1.12 rillig .for val in ${SD_VALUES}
101 1.12 rillig @printf '%s: %-8s = %s\n' $@ ${val} ${SD.${val}:Q}
102 1.12 rillig .endfor
103 1.13 rillig
104 1.13 rillig # Appending to an undefined variable does not add a space in front.
105 1.13 rillig .undef APPENDED
106 1.13 rillig APPENDED+= value
107 1.13 rillig .if ${APPENDED} != "value"
108 1.13 rillig .error "${APPENDED}"
109 1.13 rillig .endif
110 1.13 rillig
111 1.13 rillig # Appending to an empty variable adds a space between the old value
112 1.13 rillig # and the additional value.
113 1.13 rillig APPENDED= # empty
114 1.13 rillig APPENDED+= value
115 1.13 rillig .if ${APPENDED} != " value"
116 1.13 rillig .error "${APPENDED}"
117 1.13 rillig .endif
118 1.13 rillig
119 1.13 rillig # Appending to parameterized variables works as well.
120 1.13 rillig PARAM= param
121 1.13 rillig VAR.${PARAM}= 1
122 1.13 rillig VAR.${PARAM}+= 2
123 1.13 rillig .if ${VAR.param} != "1 2"
124 1.13 rillig .error "${VAR.param}"
125 1.13 rillig .endif
126 1.13 rillig
127 1.13 rillig # The variable name can contain arbitrary characters.
128 1.13 rillig # If the expanded variable name ends in a +, this still does not influence
129 1.13 rillig # the parser. The assignment operator is still a simple assignment.
130 1.13 rillig # Therefore, there is no need to add a space between the variable name
131 1.13 rillig # and the assignment operator.
132 1.13 rillig PARAM= +
133 1.13 rillig VAR.${PARAM}= 1
134 1.13 rillig VAR.${PARAM}+= 2
135 1.13 rillig .if ${VAR.+} != "1 2"
136 1.13 rillig .error "${VAR.+}"
137 1.13 rillig .endif
138 1.13 rillig .for param in + ! ?
139 1.13 rillig VAR.${param}= ${param}
140 1.13 rillig .endfor
141 1.13 rillig .if ${VAR.+} != "+" || ${VAR.!} != "!" || ${VAR.?} != "?"
142 1.13 rillig .error "${VAR.+}" "${VAR.!}" "${VAR.?}"
143 1.13 rillig .endif
144 1.14 rillig
145 1.14 rillig # Appending to a variable from the environment creates a copy of that variable
146 1.14 rillig # in the global context.
147 1.14 rillig # The appended value is not exported automatically.
148 1.14 rillig # When a variable is exported, the exported value is taken at the time of the
149 1.14 rillig # .export directive. Later changes to the variable have no effect.
150 1.14 rillig .export FROM_ENV_BEFORE
151 1.14 rillig FROM_ENV+= mk
152 1.14 rillig FROM_ENV_BEFORE+= mk
153 1.14 rillig FROM_ENV_AFTER+= mk
154 1.14 rillig .export FROM_ENV_AFTER
155 1.14 rillig
156 1.14 rillig export-appended:
157 1.14 rillig @echo $@: "$$FROM_ENV"
158 1.14 rillig @echo $@: "$$FROM_ENV_BEFORE"
159 1.14 rillig @echo $@: "$$FROM_ENV_AFTER"
160 1.15 rillig
161 1.15 rillig # begin parse-dynamic
162 1.15 rillig #
163 1.15 rillig # Demonstrate that the target-specific variables are not evaluated in
164 1.15 rillig # the global context. They are preserved until there is a local context
165 1.15 rillig # in which resolving them makes sense.
166 1.15 rillig
167 1.15 rillig ${:U>}= before
168 1.15 rillig G_TARGET:= $@
169 1.15 rillig G_MEMBER:= $%
170 1.15 rillig G_PREFIX:= $*
171 1.15 rillig G_ARCHIVE:= $!
172 1.15 rillig G_ALLSRC:= $>
173 1.15 rillig ${:U>}= after
174 1.15 rillig
175 1.15 rillig parse-dynamic:
176 1.15 rillig @echo $@: ${G_TARGET} ${G_MEMBER} ${G_PREFIX} ${G_ARCHIVE} ${G_ALLSRC}
177