xref: /freebsd/contrib/bmake/unit-tests/varname-dot-make-save_dollars.mk (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1# $NetBSD: varname-dot-make-save_dollars.mk,v 1.7 2021/12/03 18:43:52 rillig Exp $
2#
3# Tests for the special .MAKE.SAVE_DOLLARS variable, which controls whether
4# the assignment operator ':=' converts '$$' to a single '$' or keeps it
5# as-is.
6#
7# See also:
8#	var-op-expand.mk	for ':=' in general
9#	varmisc.mk		for parsing the boolean values
10
11# Initially, the variable .MAKE.SAVE_DOLLARS is undefined. At this point the
12# behavior of the assignment operator ':=' depends.  NetBSD's usr.bin/make
13# preserves the '$$' as-is, while the bmake distribution replaces '$$' with
14# '$'.
15.if ${.MAKE.SAVE_DOLLARS:Uundefined} != "undefined"
16.  error
17.endif
18
19
20# When dollars are preserved, this setting not only applies to literal
21# dollars, but also to those that come indirectly from other expressions.
22DOLLARS=		$$$$$$$$
23.MAKE.SAVE_DOLLARS=	yes
24VAR:=			${DOLLARS}
25# The reduction from 8 '$' to 4 '$' happens when ${VAR} is evaluated in the
26# condition; .MAKE.SAVE_DOLLARS only applies at the moment where the
27# assignment is performed using ':='.
28.if ${VAR} != "\$\$\$\$"
29.  error
30.endif
31
32# When dollars are preserved, this setting not only applies to literal
33# dollars, but also to those that come indirectly from other expressions.
34DOLLARS=		$$$$$$$$
35.MAKE.SAVE_DOLLARS=	no
36VAR:=			${DOLLARS}
37.if ${VAR} != "\$\$"
38.  error
39.endif
40
41# The 'yes' preserves the dollars from the literal.
42.MAKE.SAVE_DOLLARS=	yes
43VAR:=			$$$$$$$$
44.if ${VAR} != "\$\$\$\$"
45.  error
46.endif
47
48# The 'no' converts each '$$' to '$'.
49.MAKE.SAVE_DOLLARS=	no
50VAR:=			$$$$$$$$
51.if ${VAR} != "\$\$"
52.  error
53.endif
54
55# It's even possible to change the dollar interpretation in the middle of
56# evaluating an expression, but there is no practical need for it.
57.MAKE.SAVE_DOLLARS=	no
58VAR:=		$$$$-${.MAKE.SAVE_DOLLARS::=yes}-$$$$
59.if ${VAR} != "\$--\$\$"
60.  error
61.endif
62
63# The '$' from the ':U' expressions do not appear as literal '$$' to the
64# parser (no matter whether directly or indirectly), they only appear as '$$'
65# in the value of an expression, therefore .MAKE.SAVE_DOLLARS doesn't apply
66# here.
67.MAKE.SAVE_DOLLARS=	no
68VAR:=		${:U\$\$\$\$}-${.MAKE.SAVE_DOLLARS::=yes}-${:U\$\$\$\$}
69.if ${VAR} != "\$\$--\$\$"
70.  error
71.endif
72
73# Undefining .MAKE.SAVE_DOLLARS does not have any effect, in particular it
74# does not restore the default behavior.
75.MAKE.SAVE_DOLLARS=	no
76.undef .MAKE.SAVE_DOLLARS
77VAR:=		$$$$$$$$
78.if ${VAR} != "\$\$"
79.  error
80.endif
81
82# Undefining .MAKE.SAVE_DOLLARS does not have any effect, in particular it
83# does not restore the default behavior.
84.MAKE.SAVE_DOLLARS=	yes
85.undef .MAKE.SAVE_DOLLARS
86VAR:=		$$$$$$$$
87.if ${VAR} != "\$\$\$\$"
88.  error
89.endif
90
91# The variable '.MAKE.SAVE_DOLLARS' not only affects literal '$$' on the
92# right-hand side of the assignment operator ':=', it also affects dollars
93# in indirect expressions.
94#
95# In this example, it affects the command in CMD itself, not the result of
96# running that command.
97.MAKE.SAVE_DOLLARS=	no
98CMD=			echo '$$$$$$$$'
99VAR:=			${CMD:sh}
100.if ${VAR} != "\$\$"
101.  error
102.endif
103
104.MAKE.SAVE_DOLLARS=	yes
105CMD=			echo '$$$$$$$$'
106VAR:=			${CMD:sh}
107.if ${VAR} != "\$\$\$\$"
108.  error
109.endif
110
111
112# In the modifier ':@var@body@', .MAKE.SAVE_DOLLARS does not affect the body.
113# In both cases, each '$$' is replaced with a single '$', no matter whether
114# directly or indirectly via another expression.
115.MAKE.SAVE_DOLLARS=	no
116DOLLARS=		$$$$$$$$
117VAR:=			${word:L:@word@$$$$$$$$-${DOLLARS}@}
118.if ${VAR} != "\$\$-\$\$"
119.  error
120.endif
121
122.MAKE.SAVE_DOLLARS=	yes
123DOLLARS=		$$$$$$$$
124VAR:=			${word:L:@word@$$$$$$$$-${DOLLARS}@}
125.if ${VAR} != "\$\$-\$\$"
126.  error
127.endif
128
129
130all:
131