modmisc.mk revision 1.15 1 # $Id: modmisc.mk,v 1.15 2020/07/19 19:36:20 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 mod-HTE emptyvar undefvar
19 all: mod-S mod-C mod-at-varname mod-at-resolve mod-at-dollar
20 all: mod-subst-dollar mod-loop-dollar
21 all: mod-C-limits
22
23 modsysv:
24 @echo "The answer is ${libfoo.a:L:libfoo.a=42}"
25
26 modvar:
27 @echo "path='${path}'"
28 @echo "path='${path:${MOD_NODOT}}'"
29 @echo "path='${path:S,home,homes,:${MOD_NODOT}}'"
30 @echo "path=${path:${MOD_NODOTX}:ts:}"
31 @echo "path=${path:${MOD_HOMES}:${MOD_NODOTX}:ts:}"
32
33 .for d in ${path:${MOD_SEP}:N.} /usr/xbin
34 path_$d?= ${d:${MOD_OPT}:${MOD_HOMES}}/
35 paths+= ${d:${MOD_OPT}:${MOD_HOMES}}
36 .endfor
37
38 modvarloop:
39 @echo "path_/usr/xbin=${path_/usr/xbin}"
40 @echo "paths=${paths}"
41 @echo "PATHS=${paths:tu}"
42
43 PATHNAMES= a/b/c def a.b.c a.b/c a a.a .gitignore a a.a
44 mod-HTE:
45 @echo "dirname of '"${PATHNAMES:Q}"' is '"${PATHNAMES:H:Q}"'"
46 @echo "basename of '"${PATHNAMES:Q}"' is '"${PATHNAMES:T:Q}"'"
47 @echo "suffix of '"${PATHNAMES:Q}"' is '"${PATHNAMES:E:Q}"'"
48 @echo "root of '"${PATHNAMES:Q}"' is '"${PATHNAMES:R:Q}"'"
49
50 # When a modifier is applied to the "" variable, the result is discarded.
51 emptyvar:
52 @echo S:${:S,^$,empty,}
53 @echo C:${:C,^$,empty,}
54 @echo @:${:@var@${var}@}
55
56 # The :U modifier turns even the "" variable into something that has a value.
57 # The resulting variable is empty, but is still considered to contain a
58 # single empty word. This word can be accessed by the :S and :C modifiers,
59 # but not by the :@ modifier since it explicitly skips empty words.
60 undefvar:
61 @echo S:${:U:S,^$,empty,}
62 @echo C:${:U:C,^$,empty,}
63 @echo @:${:U:@var@empty@}
64
65 mod-S:
66 @echo :${:Ua b b c:S,a b,,:Q}:
67 @echo :${:Ua b b c:S,a b,,1:Q}:
68 @echo :${:Ua b b c:S,a b,,W:Q}:
69 @echo :${:Ua b b c:S,b,,g:Q}:
70 @echo :${:U1 2 3 1 2 3:S,1 2,___,Wg:S,_,x,:Q}:
71
72 mod-C:
73 @echo :${:Ua b b c:C,a b,,:Q}:
74 @echo :${:Ua b b c:C,a b,,1:Q}:
75 @echo :${:Ua b b c:C,a b,,W:Q}:
76 @echo :${:Uword1 word2:C,****,____,g:C,word,____,:Q}:
77 @echo :${:Ua b b c:C,b,,g:Q}:
78 @echo :${:U1 2 3 1 2 3:C,1 2,___,Wg:C,_,x,:Q}:
79
80 # In the :@ modifier, the name of the loop variable can even be generated
81 # dynamically. There's no practical use-case for this, and hopefully nobody
82 # will ever depend on this, but technically it's possible.
83 mod-at-varname:
84 @echo :${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@:Q}:
85
86 # The :@ modifier resolves the variables a little more often than expected.
87 # In particular, it resolves _all_ variables from the context, and not only
88 # the loop variable (in this case v).
89 #
90 # The d means direct reference, the i means indirect reference.
91 RESOLVE= ${RES1} $${RES1}
92 RES1= 1d${RES2} 1i$${RES2}
93 RES2= 2d${RES3} 2i$${RES3}
94 RES3= 3
95
96 mod-at-resolve:
97 @echo $@:${RESOLVE:@v@w${v}w@:Q}:
98
99 # As of 2020-07-19, the variable name of the :@ modifier may end with one
100 # or two dollar signs, which are silently ignored. There's no point in
101 # allowing a dollar sign in that position.
102 mod-at-dollar:
103 @echo $@:${1 2 3:L:@v$@($v)@:Q}.
104 @echo $@:${1 2 3:L:@v$$@($v)@:Q}.
105 @echo $@:${1 2 3:L:@v$$$@($v)@:Q}.
106
107 # No matter how many dollar characters there are, they all get merged
108 # into a single dollar by the :S modifier.
109 mod-subst-dollar:
110 @echo $@:${:U1:S,^,$,:Q}:
111 @echo $@:${:U2:S,^,$$,:Q}:
112 @echo $@:${:U3:S,^,$$$,:Q}:
113 @echo $@:${:U4:S,^,$$$$,:Q}:
114 @echo $@:${:U5:S,^,$$$$$,:Q}:
115 @echo $@:${:U6:S,^,$$$$$$,:Q}:
116 @echo $@:${:U7:S,^,$$$$$$$,:Q}:
117 @echo $@:${:U8:S,^,$$$$$$$$,:Q}:
118 # This generates no dollar at all:
119 @echo $@:${:UU8:S,^,${:U$$$$$$$$},:Q}:
120 # Here is an alternative way to generate dollar characters.
121 # It's unexpectedly complicated though.
122 @echo $@:${:U:range=5:ts\x24:C,[0-9],,g:Q}:
123
124 # Demonstrate that it is possible to generate dollar characters using the
125 # :@ modifier.
126 #
127 # These are edge cases that could have resulted in a parse error as well
128 # since the $@ at the end could have been interpreted as a variable, which
129 # would mean a missing closing @ delimiter.
130 mod-loop-dollar:
131 @echo $@:${:U1:@word@${word}$@:Q}:
132 @echo $@:${:U2:@word@$${word}$$@:Q}:
133 @echo $@:${:U3:@word@$$${word}$$$@:Q}:
134 @echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
135 @echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
136 @echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
137
138 mod-C-limits:
139 @echo $@:00-ok:${:U1 23 456:C,..,\0\0,:Q}
140 @echo $@:11-missing:${:U1 23 456:C,..,\1\1,:Q}
141 @echo $@:11-ok:${:U1 23 456:C,(.).,\1\1,:Q}
142 @echo $@:22-missing:${:U1 23 456:C,..,\2\2,:Q}
143 @echo $@:22-missing:${:U1 23 456:C,(.).,\2\2,:Q}
144 @echo $@:22-ok:${:U1 23 456:C,(.)(.),\2\2,:Q}
145 # The :C modifier only handles single-digit capturing groups,
146 # which is more than enough for daily use.
147 @echo $@:capture:${:UabcdefghijABCDEFGHIJrest:C,(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.),\9\8\7\6\5\4\3\2\1\0\10\11\12,}
148