Home | History | Annotate | Line # | Download | only in unit-tests
modmisc.mk revision 1.42
      1  1.42  rillig # $Id: modmisc.mk,v 1.42 2020/08/16 12:30:45 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.7  rillig all:	modvar modvarloop modsysv mod-HTE emptyvar undefvar
     19  1.16  rillig all:	mod-assign
     20  1.18  rillig all:	mod-assign-nested
     21  1.17  rillig all:	mod-tu-space
     22  1.23  rillig all:	mod-quote
     23  1.22  rillig all:	mod-break-many-words
     24  1.27  rillig all:	mod-remember
     25  1.28  rillig all:	mod-gmtime
     26  1.36  rillig all:	mod-gmtime-indirect
     27  1.28  rillig all:	mod-localtime
     28  1.28  rillig all:	mod-hash
     29  1.28  rillig all:	mod-range
     30   1.1     apb 
     31  1.23  rillig # See also sysv.mk.
     32   1.1     apb modsysv:
     33   1.1     apb 	@echo "The answer is ${libfoo.a:L:libfoo.a=42}"
     34   1.1     apb 
     35  1.23  rillig # Demonstrates modifiers that are given indirectly from a variable.
     36   1.1     apb modvar:
     37   1.1     apb 	@echo "path='${path}'"
     38   1.1     apb 	@echo "path='${path:${MOD_NODOT}}'"
     39   1.1     apb 	@echo "path='${path:S,home,homes,:${MOD_NODOT}}'"
     40   1.1     apb 	@echo "path=${path:${MOD_NODOTX}:ts:}"
     41   1.1     apb 	@echo "path=${path:${MOD_HOMES}:${MOD_NODOTX}:ts:}"
     42   1.1     apb 
     43   1.1     apb .for d in ${path:${MOD_SEP}:N.} /usr/xbin
     44   1.1     apb path_$d?= ${d:${MOD_OPT}:${MOD_HOMES}}/
     45   1.1     apb paths+= ${d:${MOD_OPT}:${MOD_HOMES}}
     46   1.1     apb .endfor
     47   1.1     apb 
     48   1.1     apb modvarloop:
     49   1.1     apb 	@echo "path_/usr/xbin=${path_/usr/xbin}"
     50   1.1     apb 	@echo "paths=${paths}"
     51   1.1     apb 	@echo "PATHS=${paths:tu}"
     52   1.2  rillig 
     53   1.6  rillig PATHNAMES=	a/b/c def a.b.c a.b/c a a.a .gitignore a a.a
     54   1.4  rillig mod-HTE:
     55   1.6  rillig 	@echo "dirname of '"${PATHNAMES:Q}"' is '"${PATHNAMES:H:Q}"'"
     56   1.6  rillig 	@echo "basename of '"${PATHNAMES:Q}"' is '"${PATHNAMES:T:Q}"'"
     57   1.6  rillig 	@echo "suffix of '"${PATHNAMES:Q}"' is '"${PATHNAMES:E:Q}"'"
     58   1.6  rillig 	@echo "root of '"${PATHNAMES:Q}"' is '"${PATHNAMES:R:Q}"'"
     59   1.7  rillig 
     60   1.7  rillig # When a modifier is applied to the "" variable, the result is discarded.
     61   1.7  rillig emptyvar:
     62   1.7  rillig 	@echo S:${:S,^$,empty,}
     63   1.7  rillig 	@echo C:${:C,^$,empty,}
     64   1.7  rillig 	@echo @:${:@var@${var}@}
     65   1.7  rillig 
     66   1.7  rillig # The :U modifier turns even the "" variable into something that has a value.
     67   1.7  rillig # The resulting variable is empty, but is still considered to contain a
     68   1.7  rillig # single empty word. This word can be accessed by the :S and :C modifiers,
     69   1.7  rillig # but not by the :@ modifier since it explicitly skips empty words.
     70   1.7  rillig undefvar:
     71   1.7  rillig 	@echo S:${:U:S,^$,empty,}
     72   1.7  rillig 	@echo C:${:U:C,^$,empty,}
     73   1.7  rillig 	@echo @:${:U:@var@empty@}
     74   1.8  rillig 
     75  1.16  rillig # Just a bit of basic code coverage for the obscure ::= assignment modifiers.
     76  1.16  rillig mod-assign:
     77  1.16  rillig 	@echo $@: ${1 2 3:L:@i@${FIRST::?=$i}@} first=${FIRST}.
     78  1.16  rillig 	@echo $@: ${1 2 3:L:@i@${LAST::=$i}@} last=${LAST}.
     79  1.16  rillig 	@echo $@: ${1 2 3:L:@i@${APPENDED::+=$i}@} appended=${APPENDED}.
     80  1.16  rillig 	@echo $@: ${echo.1 echo.2 echo.3:L:@i@${RAN::!=${i:C,.*,&; & 1>\&2,:S,., ,g}}@} ran:${RAN}.
     81  1.16  rillig 	# The assignments happen in the global scope and thus are
     82  1.16  rillig 	# preserved even after the shell command has been run.
     83  1.16  rillig 	@echo $@: global: ${FIRST:Q}, ${LAST:Q}, ${APPENDED:Q}, ${RAN:Q}.
     84  1.17  rillig 
     85  1.18  rillig mod-assign-nested:
     86  1.18  rillig 	@echo $@: ${1:?${THEN1::=then1${IT1::=t1}}:${ELSE1::=else1${IE1::=e1}}}${THEN1}${ELSE1}${IT1}${IE1}
     87  1.18  rillig 	@echo $@: ${0:?${THEN2::=then2${IT2::=t2}}:${ELSE2::=else2${IE2::=e2}}}${THEN2}${ELSE2}${IT2}${IE2}
     88  1.18  rillig 	@echo $@: ${SINK3:Q}
     89  1.18  rillig 	@echo $@: ${SINK4:Q}
     90  1.18  rillig SINK3:=	${1:?${THEN3::=then3${IT3::=t3}}:${ELSE3::=else3${IE3::=e3}}}${THEN3}${ELSE3}${IT3}${IE3}
     91  1.18  rillig SINK4:=	${0:?${THEN4::=then4${IT4::=t4}}:${ELSE4::=else4${IE4::=e4}}}${THEN4}${ELSE4}${IT4}${IE4}
     92  1.18  rillig 
     93  1.17  rillig mod-tu-space:
     94  1.17  rillig 	# The :tu and :tl modifiers operate on the variable value
     95  1.17  rillig 	# as a single string, not as a list of words. Therefore,
     96  1.17  rillig 	# the adjacent spaces are preserved.
     97  1.17  rillig 	@echo $@: ${a   b:L:tu:Q}
     98  1.19  rillig 
     99  1.23  rillig mod-quote:
    100  1.19  rillig 	@echo $@: new${.newline:Q}${.newline:Q}line
    101  1.22  rillig 
    102  1.22  rillig # Cover the bmake_realloc in brk_string.
    103  1.22  rillig mod-break-many-words:
    104  1.22  rillig 	@echo $@: ${UNDEF:U:range=500:[#]}
    105  1.27  rillig 
    106  1.27  rillig # Demonstrate the :_ modifier.
    107  1.27  rillig # In the parameterized form, having the variable name on the right side
    108  1.27  rillig # of the = assignment operator is confusing. Luckily this modifier is
    109  1.27  rillig # only rarely needed.
    110  1.27  rillig mod-remember:
    111  1.27  rillig 	@echo $@: ${1 2 3:L:_:@var@${_}@}
    112  1.27  rillig 	@echo $@: ${1 2 3:L:@var@${var:_=SAVED:}@}, SAVED=${SAVED}
    113  1.28  rillig 
    114  1.28  rillig mod-gmtime:
    115  1.28  rillig 	@echo $@:
    116  1.28  rillig 	@echo ${%Y:L:gmtim=1593536400}		# modifier name too short
    117  1.28  rillig 	@echo ${%Y:L:gmtime=1593536400}		# 2020-07-01T00:00:00Z
    118  1.28  rillig 	@echo ${%Y:L:gmtimer=1593536400}	# modifier name too long
    119  1.29  rillig 	@echo ${%Y:L:gm=gm:M*}
    120  1.28  rillig 
    121  1.36  rillig mod-gmtime-indirect:
    122  1.36  rillig 	@echo $@:
    123  1.36  rillig 	# It's not possible to pass the seconds via a variable expression.
    124  1.36  rillig 	# Parsing of the :gmtime modifier stops at the '$' and returns to
    125  1.36  rillig 	# ApplyModifiers.  There, a colon would be skipped but not a dollar.
    126  1.36  rillig 	# Parsing continues by looking at the next modifier.  Now the ${:U}
    127  1.36  rillig 	# is expanded and interpreted as a variable modifier, which results
    128  1.36  rillig 	# in the error message "Unknown modifier '1'".
    129  1.36  rillig 	@echo ${%Y:L:gmtime=${:U1593536400}}
    130  1.36  rillig 
    131  1.28  rillig mod-localtime:
    132  1.28  rillig 	@echo $@:
    133  1.28  rillig 	@echo ${%Y:L:localtim=1593536400}	# modifier name too short
    134  1.28  rillig 	@echo ${%Y:L:localtime=1593536400}	# 2020-07-01T00:00:00Z
    135  1.28  rillig 	@echo ${%Y:L:localtimer=1593536400}	# modifier name too long
    136  1.28  rillig 
    137  1.28  rillig mod-hash:
    138  1.28  rillig 	@echo $@:
    139  1.28  rillig 	@echo ${12345:L:has}			# modifier name too short
    140  1.28  rillig 	@echo ${12345:L:hash}			# ok
    141  1.28  rillig 	@echo ${12345:L:hash=SHA-256}		# :hash does not accept '='
    142  1.28  rillig 	@echo ${12345:L:hasX}			# misspelled
    143  1.28  rillig 	@echo ${12345:L:hashed}			# modifier name too long
    144  1.28  rillig 
    145  1.28  rillig mod-range:
    146  1.28  rillig 	@echo $@:
    147  1.28  rillig 	@echo ${a b c:L:rang}			# modifier name too short
    148  1.28  rillig 	@echo ${a b c:L:range}			# ok
    149  1.28  rillig 	@echo ${a b c:L:rango}			# misspelled
    150  1.28  rillig 	@echo ${a b c:L:ranger}			# modifier name too long
    151  1.31  rillig 
    152  1.31  rillig # To apply a modifier indirectly via another variable, the whole
    153  1.31  rillig # modifier must be put into a single variable.
    154  1.31  rillig .if ${value:L:${:US}${:U,value,replacement,}} != "S,value,replacement,}"
    155  1.31  rillig .warning unexpected
    156  1.31  rillig .endif
    157  1.31  rillig 
    158  1.31  rillig # Adding another level of indirection (the 2 nested :U expressions) helps.
    159  1.31  rillig .if ${value:L:${:U${:US}${:U,value,replacement,}}} != "replacement"
    160  1.31  rillig .warning unexpected
    161  1.31  rillig .endif
    162  1.31  rillig 
    163  1.31  rillig # Multiple indirect modifiers can be applied one after another as long as
    164  1.31  rillig # they are separated with colons.
    165  1.31  rillig .if ${value:L:${:US,a,A,}:${:US,e,E,}} != "vAluE"
    166  1.31  rillig .warning unexpected
    167  1.31  rillig .endif
    168  1.32  rillig 
    169  1.32  rillig # An indirect variable that evaluates to the empty string is allowed though.
    170  1.32  rillig # This makes it possible to define conditional modifiers, like this:
    171  1.32  rillig #
    172  1.32  rillig # M.little-endian=	S,1234,4321,
    173  1.32  rillig # M.big-endian=		# none
    174  1.32  rillig .if ${value:L:${:Dempty}S,a,A,} != "vAlue"
    175  1.32  rillig .warning unexpected
    176  1.32  rillig .endif
    177  1.40  rillig 
    178  1.40  rillig # begin mod-shell
    179  1.40  rillig 
    180  1.40  rillig .if ${:!echo hello | tr 'l' 'l'!} != "hello"
    181  1.40  rillig .warning unexpected
    182  1.40  rillig .endif
    183  1.40  rillig 
    184  1.40  rillig # The output is truncated at the first null byte.
    185  1.40  rillig # Cmd_Exec returns only a string pointer without length information.
    186  1.40  rillig .if ${:!echo hello | tr 'l' '\0'!} != "he"
    187  1.40  rillig .warning unexpected
    188  1.40  rillig .endif
    189  1.40  rillig 
    190  1.41  rillig .if ${:!echo!} != ""
    191  1.41  rillig .warning A newline at the end of the output must be stripped.
    192  1.41  rillig .endif
    193  1.41  rillig 
    194  1.41  rillig .if ${:!echo;echo!} != " "
    195  1.41  rillig .warning Only a single newline at the end of the output is stripped.
    196  1.41  rillig .endif
    197  1.41  rillig 
    198  1.41  rillig .if ${:!echo;echo;echo;echo!} != "   "
    199  1.41  rillig .warning Other newlines in the output are converted to spaces.
    200  1.41  rillig .endif
    201  1.41  rillig 
    202  1.40  rillig # end mod-shell
    203