1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
# $Id: modmisc.mk,v 1.1.1.3 2020/07/09 22:35:19 sjg Exp $
#
# miscellaneous modifier tests
# do not put any dirs in this list which exist on some
# but not all target systems - an exists() check is below.
path=:/bin:/tmp::/:.:/no/such/dir:.
# strip cwd from path.
MOD_NODOT=S/:/ /g:N.:ts:
# and decorate, note that $'s need to be doubled. Also note that
# the modifier_variable can be used with other modifiers.
MOD_NODOTX=S/:/ /g:N.:@d@'$$d'@
# another mod - pretend it is more interesting
MOD_HOMES=S,/home/,/homes/,
MOD_OPT=@d@$${exists($$d):?$$d:$${d:S,/usr,/opt,}}@
MOD_SEP=S,:, ,g
all: modvar modvarloop modsysv mod-HTE emptyvar undefvar
all: mod-S mod-C mod-at-varname mod-at-resolve
all: mod-subst-dollar mod-loop-dollar
modsysv:
@echo "The answer is ${libfoo.a:L:libfoo.a=42}"
modvar:
@echo "path='${path}'"
@echo "path='${path:${MOD_NODOT}}'"
@echo "path='${path:S,home,homes,:${MOD_NODOT}}'"
@echo "path=${path:${MOD_NODOTX}:ts:}"
@echo "path=${path:${MOD_HOMES}:${MOD_NODOTX}:ts:}"
.for d in ${path:${MOD_SEP}:N.} /usr/xbin
path_$d?= ${d:${MOD_OPT}:${MOD_HOMES}}/
paths+= ${d:${MOD_OPT}:${MOD_HOMES}}
.endfor
modvarloop:
@echo "path_/usr/xbin=${path_/usr/xbin}"
@echo "paths=${paths}"
@echo "PATHS=${paths:tu}"
PATHNAMES= a/b/c def a.b.c a.b/c a a.a .gitignore a a.a
mod-HTE:
@echo "dirname of '"${PATHNAMES:Q}"' is '"${PATHNAMES:H:Q}"'"
@echo "basename of '"${PATHNAMES:Q}"' is '"${PATHNAMES:T:Q}"'"
@echo "suffix of '"${PATHNAMES:Q}"' is '"${PATHNAMES:E:Q}"'"
@echo "root of '"${PATHNAMES:Q}"' is '"${PATHNAMES:R:Q}"'"
# When a modifier is applied to the "" variable, the result is discarded.
emptyvar:
@echo S:${:S,^$,empty,}
@echo C:${:C,^$,empty,}
@echo @:${:@var@${var}@}
# The :U modifier turns even the "" variable into something that has a value.
# The resulting variable is empty, but is still considered to contain a
# single empty word. This word can be accessed by the :S and :C modifiers,
# but not by the :@ modifier since it explicitly skips empty words.
undefvar:
@echo S:${:U:S,^$,empty,}
@echo C:${:U:C,^$,empty,}
@echo @:${:U:@var@empty@}
mod-S:
@echo :${:Ua b b c:S,a b,,:Q}:
@echo :${:Ua b b c:S,a b,,1:Q}:
@echo :${:Ua b b c:S,a b,,W:Q}:
@echo :${:Ua b b c:S,b,,g:Q}:
@echo :${:U1 2 3 1 2 3:S,1 2,___,Wg:S,_,x,:Q}:
mod-C:
@echo :${:Ua b b c:C,a b,,:Q}:
@echo :${:Ua b b c:C,a b,,1:Q}:
@echo :${:Ua b b c:C,a b,,W:Q}:
@echo :${:Uword1 word2:C,****,____,g:C,word,____,:Q}:
@echo :${:Ua b b c:C,b,,g:Q}:
@echo :${:U1 2 3 1 2 3:C,1 2,___,Wg:C,_,x,:Q}:
# In the :@ modifier, the name of the loop variable can even be generated
# dynamically. There's no practical use-case for this, and hopefully nobody
# will ever depend on this, but technically it's possible.
mod-at-varname:
@echo :${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@:Q}:
# The :@ modifier resolves the variables a little more often than expected.
# In particular, it resolves _all_ variables from the context, and not only
# the loop variable (in this case v).
#
# The d means direct reference, the i means indirect reference.
RESOLVE= ${RES1} $${RES1}
RES1= 1d${RES2} 1i$${RES2}
RES2= 2d${RES3} 2i$${RES3}
RES3= 3
mod-at-resolve:
@echo $@:${RESOLVE:@v@w${v}w@:Q}:
# No matter how many dollar characters there are, they all get merged
# into a single dollar by the :S modifier.
mod-subst-dollar:
@echo $@:${:U1:S,^,$,:Q}:
@echo $@:${:U2:S,^,$$,:Q}:
@echo $@:${:U3:S,^,$$$,:Q}:
@echo $@:${:U4:S,^,$$$$,:Q}:
@echo $@:${:U5:S,^,$$$$$,:Q}:
@echo $@:${:U6:S,^,$$$$$$,:Q}:
@echo $@:${:U7:S,^,$$$$$$$,:Q}:
@echo $@:${:U8:S,^,$$$$$$$$,:Q}:
# This generates no dollar at all:
@echo $@:${:UU8:S,^,${:U$$$$$$$$},:Q}:
# Here is an alternative way to generate dollar characters.
# It's unexpectedly complicated though.
@echo $@:${:U:range=5:ts\x24:C,[0-9],,g:Q}:
# Demonstrate that it is possible to generate dollar characters using the
# :@ modifier.
#
# These are edge cases that could have resulted in a parse error as well
# since the $@ at the end could have been interpreted as a variable, which
# would mean a missing closing @ delimiter.
mod-loop-dollar:
@echo $@:${:U1:@word@${word}$@:Q}:
@echo $@:${:U2:@word@$${word}$$@:Q}:
@echo $@:${:U3:@word@$$${word}$$$@:Q}:
@echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
@echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
@echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
|