modmisc.mk revision 1.48 1 # $NetBSD: modmisc.mk,v 1.48 2020/10/24 08:46:08 rillig Exp $
2 #
3 # miscellaneous modifier tests
4
5 # do not put any dirs in this list which exist on some
6 # but not all target systems - an exists() check is below.
7 path=:/bin:/tmp::/:.:/no/such/dir:.
8 # strip cwd from path.
9 MOD_NODOT=S/:/ /g:N.:ts:
10 # and decorate, note that $'s need to be doubled. Also note that
11 # the modifier_variable can be used with other modifiers.
12 MOD_NODOTX=S/:/ /g:N.:@d@'$$d'@
13 # another mod - pretend it is more interesting
14 MOD_HOMES=S,/home/,/homes/,
15 MOD_OPT=@d@$${exists($$d):?$$d:$${d:S,/usr,/opt,}}@
16 MOD_SEP=S,:, ,g
17
18 all: modvar modvarloop modsysv emptyvar undefvar
19 all: mod-quote
20 all: mod-break-many-words
21
22 # See also sysv.mk.
23 modsysv:
24 @echo "The answer is ${libfoo.a:L:libfoo.a=42}"
25
26 # Demonstrates modifiers that are given indirectly from a variable.
27 modvar:
28 @echo "path='${path}'"
29 @echo "path='${path:${MOD_NODOT}}'"
30 @echo "path='${path:S,home,homes,:${MOD_NODOT}}'"
31 @echo "path=${path:${MOD_NODOTX}:ts:}"
32 @echo "path=${path:${MOD_HOMES}:${MOD_NODOTX}:ts:}"
33
34 .for d in ${path:${MOD_SEP}:N.} /usr/xbin
35 path_$d?= ${d:${MOD_OPT}:${MOD_HOMES}}/
36 paths+= ${d:${MOD_OPT}:${MOD_HOMES}}
37 .endfor
38
39 modvarloop:
40 @echo "path_/usr/xbin=${path_/usr/xbin}"
41 @echo "paths=${paths}"
42 @echo "PATHS=${paths:tu}"
43
44 # When a modifier is applied to the "" variable, the result is discarded.
45 emptyvar:
46 @echo S:${:S,^$,empty,}
47 @echo C:${:C,^$,empty,}
48 @echo @:${:@var@${var}@}
49
50 # The :U modifier turns even the "" variable into something that has a value.
51 # The resulting variable is empty, but is still considered to contain a
52 # single empty word. This word can be accessed by the :S and :C modifiers,
53 # but not by the :@ modifier since it explicitly skips empty words.
54 undefvar:
55 @echo S:${:U:S,^$,empty,}
56 @echo C:${:U:C,^$,empty,}
57 @echo @:${:U:@var@empty@}
58
59
60 mod-quote:
61 @echo $@: new${.newline:Q}${.newline:Q}line
62
63 # Cover the bmake_realloc in brk_string.
64 mod-break-many-words:
65 @echo $@: ${UNDEF:U:range=500:[#]}
66
67 # To apply a modifier indirectly via another variable, the whole
68 # modifier must be put into a single variable.
69 .if ${value:L:${:US}${:U,value,replacement,}} != "S,value,replacement,}"
70 . warning unexpected
71 .endif
72
73 # Adding another level of indirection (the 2 nested :U expressions) helps.
74 .if ${value:L:${:U${:US}${:U,value,replacement,}}} != "replacement"
75 . warning unexpected
76 .endif
77
78 # Multiple indirect modifiers can be applied one after another as long as
79 # they are separated with colons.
80 .if ${value:L:${:US,a,A,}:${:US,e,E,}} != "vAluE"
81 . warning unexpected
82 .endif
83
84 # An indirect variable that evaluates to the empty string is allowed though.
85 # This makes it possible to define conditional modifiers, like this:
86 #
87 # M.little-endian= S,1234,4321,
88 # M.big-endian= # none
89 .if ${value:L:${:Dempty}S,a,A,} != "vAlue"
90 . warning unexpected
91 .endif
92
93