modmisc.mk revision 1.44 1 1.44 rillig # $Id: modmisc.mk,v 1.44 2020/08/23 15:09:15 rillig Exp $
2 1.1 apb #
3 1.1 apb # miscellaneous modifier tests
4 1.1 apb
5 1.1 apb # do not put any dirs in this list which exist on some
6 1.1 apb # but not all target systems - an exists() check is below.
7 1.1 apb path=:/bin:/tmp::/:.:/no/such/dir:.
8 1.1 apb # strip cwd from path.
9 1.1 apb MOD_NODOT=S/:/ /g:N.:ts:
10 1.1 apb # and decorate, note that $'s need to be doubled. Also note that
11 1.1 apb # the modifier_variable can be used with other modifiers.
12 1.1 apb MOD_NODOTX=S/:/ /g:N.:@d@'$$d'@
13 1.1 apb # another mod - pretend it is more interesting
14 1.1 apb MOD_HOMES=S,/home/,/homes/,
15 1.1 apb MOD_OPT=@d@$${exists($$d):?$$d:$${d:S,/usr,/opt,}}@
16 1.1 apb MOD_SEP=S,:, ,g
17 1.1 apb
18 1.44 rillig all: modvar modvarloop modsysv emptyvar undefvar
19 1.17 rillig all: mod-tu-space
20 1.23 rillig all: mod-quote
21 1.22 rillig all: mod-break-many-words
22 1.27 rillig all: mod-remember
23 1.28 rillig all: mod-localtime
24 1.28 rillig all: mod-hash
25 1.28 rillig all: mod-range
26 1.1 apb
27 1.23 rillig # See also sysv.mk.
28 1.1 apb modsysv:
29 1.1 apb @echo "The answer is ${libfoo.a:L:libfoo.a=42}"
30 1.1 apb
31 1.23 rillig # Demonstrates modifiers that are given indirectly from a variable.
32 1.1 apb modvar:
33 1.1 apb @echo "path='${path}'"
34 1.1 apb @echo "path='${path:${MOD_NODOT}}'"
35 1.1 apb @echo "path='${path:S,home,homes,:${MOD_NODOT}}'"
36 1.1 apb @echo "path=${path:${MOD_NODOTX}:ts:}"
37 1.1 apb @echo "path=${path:${MOD_HOMES}:${MOD_NODOTX}:ts:}"
38 1.1 apb
39 1.1 apb .for d in ${path:${MOD_SEP}:N.} /usr/xbin
40 1.1 apb path_$d?= ${d:${MOD_OPT}:${MOD_HOMES}}/
41 1.1 apb paths+= ${d:${MOD_OPT}:${MOD_HOMES}}
42 1.1 apb .endfor
43 1.1 apb
44 1.1 apb modvarloop:
45 1.1 apb @echo "path_/usr/xbin=${path_/usr/xbin}"
46 1.1 apb @echo "paths=${paths}"
47 1.1 apb @echo "PATHS=${paths:tu}"
48 1.2 rillig
49 1.7 rillig # When a modifier is applied to the "" variable, the result is discarded.
50 1.7 rillig emptyvar:
51 1.7 rillig @echo S:${:S,^$,empty,}
52 1.7 rillig @echo C:${:C,^$,empty,}
53 1.7 rillig @echo @:${:@var@${var}@}
54 1.7 rillig
55 1.7 rillig # The :U modifier turns even the "" variable into something that has a value.
56 1.7 rillig # The resulting variable is empty, but is still considered to contain a
57 1.7 rillig # single empty word. This word can be accessed by the :S and :C modifiers,
58 1.7 rillig # but not by the :@ modifier since it explicitly skips empty words.
59 1.7 rillig undefvar:
60 1.7 rillig @echo S:${:U:S,^$,empty,}
61 1.7 rillig @echo C:${:U:C,^$,empty,}
62 1.7 rillig @echo @:${:U:@var@empty@}
63 1.8 rillig
64 1.17 rillig mod-tu-space:
65 1.17 rillig # The :tu and :tl modifiers operate on the variable value
66 1.17 rillig # as a single string, not as a list of words. Therefore,
67 1.17 rillig # the adjacent spaces are preserved.
68 1.17 rillig @echo $@: ${a b:L:tu:Q}
69 1.19 rillig
70 1.23 rillig mod-quote:
71 1.19 rillig @echo $@: new${.newline:Q}${.newline:Q}line
72 1.22 rillig
73 1.22 rillig # Cover the bmake_realloc in brk_string.
74 1.22 rillig mod-break-many-words:
75 1.22 rillig @echo $@: ${UNDEF:U:range=500:[#]}
76 1.27 rillig
77 1.27 rillig # Demonstrate the :_ modifier.
78 1.27 rillig # In the parameterized form, having the variable name on the right side
79 1.27 rillig # of the = assignment operator is confusing. Luckily this modifier is
80 1.27 rillig # only rarely needed.
81 1.27 rillig mod-remember:
82 1.27 rillig @echo $@: ${1 2 3:L:_:@var@${_}@}
83 1.27 rillig @echo $@: ${1 2 3:L:@var@${var:_=SAVED:}@}, SAVED=${SAVED}
84 1.28 rillig
85 1.28 rillig mod-localtime:
86 1.28 rillig @echo $@:
87 1.28 rillig @echo ${%Y:L:localtim=1593536400} # modifier name too short
88 1.28 rillig @echo ${%Y:L:localtime=1593536400} # 2020-07-01T00:00:00Z
89 1.28 rillig @echo ${%Y:L:localtimer=1593536400} # modifier name too long
90 1.28 rillig
91 1.28 rillig mod-hash:
92 1.28 rillig @echo $@:
93 1.28 rillig @echo ${12345:L:has} # modifier name too short
94 1.28 rillig @echo ${12345:L:hash} # ok
95 1.28 rillig @echo ${12345:L:hash=SHA-256} # :hash does not accept '='
96 1.28 rillig @echo ${12345:L:hasX} # misspelled
97 1.28 rillig @echo ${12345:L:hashed} # modifier name too long
98 1.28 rillig
99 1.28 rillig mod-range:
100 1.28 rillig @echo $@:
101 1.28 rillig @echo ${a b c:L:rang} # modifier name too short
102 1.28 rillig @echo ${a b c:L:range} # ok
103 1.28 rillig @echo ${a b c:L:rango} # misspelled
104 1.28 rillig @echo ${a b c:L:ranger} # modifier name too long
105 1.31 rillig
106 1.31 rillig # To apply a modifier indirectly via another variable, the whole
107 1.31 rillig # modifier must be put into a single variable.
108 1.31 rillig .if ${value:L:${:US}${:U,value,replacement,}} != "S,value,replacement,}"
109 1.31 rillig .warning unexpected
110 1.31 rillig .endif
111 1.31 rillig
112 1.31 rillig # Adding another level of indirection (the 2 nested :U expressions) helps.
113 1.31 rillig .if ${value:L:${:U${:US}${:U,value,replacement,}}} != "replacement"
114 1.31 rillig .warning unexpected
115 1.31 rillig .endif
116 1.31 rillig
117 1.31 rillig # Multiple indirect modifiers can be applied one after another as long as
118 1.31 rillig # they are separated with colons.
119 1.31 rillig .if ${value:L:${:US,a,A,}:${:US,e,E,}} != "vAluE"
120 1.31 rillig .warning unexpected
121 1.31 rillig .endif
122 1.32 rillig
123 1.32 rillig # An indirect variable that evaluates to the empty string is allowed though.
124 1.32 rillig # This makes it possible to define conditional modifiers, like this:
125 1.32 rillig #
126 1.32 rillig # M.little-endian= S,1234,4321,
127 1.32 rillig # M.big-endian= # none
128 1.32 rillig .if ${value:L:${:Dempty}S,a,A,} != "vAlue"
129 1.32 rillig .warning unexpected
130 1.32 rillig .endif
131 1.40 rillig
132