xref: /freebsd/contrib/bmake/unit-tests/varmisc.mk (revision e17f5b1d307b7b8910d67883e57a9604305906d5)
1# $Id: varmisc.mk,v 1.11 2020/07/02 15:43:43 sjg Exp $
2#
3# Miscellaneous variable tests.
4
5all: unmatched_var_paren D_true U_true D_false U_false Q_lhs Q_rhs NQ_none \
6	strftime cmpv manok
7
8unmatched_var_paren:
9	@echo ${foo::=foo-text}
10
11True = ${echo true >&2:L:sh}TRUE
12False= ${echo false >&2:L:sh}FALSE
13
14VSET= is set
15.undef UNDEF
16
17U_false:
18	@echo :U skipped when var set
19	@echo ${VSET:U${False}}
20
21D_false:
22	@echo :D skipped if var undef
23	@echo ${UNDEF:D${False}}
24
25U_true:
26	@echo :U expanded when var undef
27	@echo ${UNDEF:U${True}}
28
29D_true:
30	@echo :D expanded when var set
31	@echo ${VSET:D${True}}
32
33Q_lhs:
34	@echo :? only lhs when value true
35	@echo ${1:L:?${True}:${False}}
36
37Q_rhs:
38	@echo :? only rhs when value false
39	@echo ${0:L:?${True}:${False}}
40
41NQ_none:
42	@echo do not evaluate or expand :? if discarding
43	@echo ${VSET:U${1:L:?${True}:${False}}}
44
45April1= 1459494000
46
47# slightly contorted syntax to use utc via variable
48strftime:
49	@echo ${year=%Y month=%m day=%d:L:gmtime=1459494000}
50	@echo date=${%Y%m%d:L:${gmtime=${April1}:L}}
51
52# big jumps to handle 3 digits per step
53M_cmpv.units = 1 1000 1000000
54M_cmpv = S,., ,g:_:range:@i@+ $${_:[-$$i]} \* $${M_cmpv.units:[$$i]}@:S,^,expr 0 ,1:sh
55
56Version = 123.456.789
57cmpv.only = target specific vars
58
59cmpv:
60	@echo Version=${Version} == ${Version:${M_cmpv}}
61	@echo Literal=3.4.5 == ${3.4.5:L:${M_cmpv}}
62	@echo We have ${${.TARGET:T}.only}
63
64# catch misshandling of nested vars in .for loop
65MAN=
66MAN1= make.1
67.for s in 1 2
68.if defined(MAN$s) && !empty(MAN$s)
69MAN+= ${MAN$s}
70.endif
71.endfor
72
73manok:
74	@echo MAN=${MAN}
75
76# This is an expanded variant of the above .for loop.
77# Between 2020-08-28 and 2020-07-02 this paragraph generated a wrong
78# error message "Variable VARNAME is recursive".
79# When evaluating the !empty expression, the ${:U1} was not expanded and
80# thus resulted in the seeming definition VARNAME=${VARNAME}, which is
81# obviously recursive.
82VARNAME=	${VARNAME${:U1}}
83.if defined(VARNAME${:U2}) && !empty(VARNAME${:U2})
84.endif
85