Sun Dec 27 21:19:13 2020 UTC ()
make(1): add more tests for ':=' assignments


(rillig)
diff -r1.5 -r1.6 src/usr.bin/make/unit-tests/var-op-expand.mk

cvs diff -r1.5 -r1.6 src/usr.bin/make/unit-tests/var-op-expand.mk (expand / switch to unified diff)

--- src/usr.bin/make/unit-tests/var-op-expand.mk 2020/12/27 20:45:52 1.5
+++ src/usr.bin/make/unit-tests/var-op-expand.mk 2020/12/27 21:19:13 1.6
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1# $NetBSD: var-op-expand.mk,v 1.5 2020/12/27 20:45:52 rillig Exp $ 1# $NetBSD: var-op-expand.mk,v 1.6 2020/12/27 21:19:13 rillig Exp $
2# 2#
3# Tests for the := variable assignment operator, which expands its 3# Tests for the := variable assignment operator, which expands its
4# right-hand side. 4# right-hand side.
5 5
6 6
7# If the right-hand side does not contain a dollar sign, the ':=' assignment 7# If the right-hand side does not contain a dollar sign, the ':=' assignment
8# operator has the same effect as the '=' assignment operator. 8# operator has the same effect as the '=' assignment operator.
9VAR:= value 9VAR:= value
10.if ${VAR} != "value" 10.if ${VAR} != "value"
11. error 11. error
12.endif 12.endif
13 13
14# When a ':=' assignment is performed, its right-hand side is evaluated and 14# When a ':=' assignment is performed, its right-hand side is evaluated and
@@ -76,26 +76,56 @@ UNDEF= Uwas undefined @@ -76,26 +76,56 @@ UNDEF= Uwas undefined
76# code that takes care of this global variable. 76# code that takes care of this global variable.
77.undef UNDEF 77.undef UNDEF
78REF= U${UNDEF} 78REF= U${UNDEF}
79#.MAKEFLAGS: -dv 79#.MAKEFLAGS: -dv
80VAR:= ${:${REF}} 80VAR:= ${:${REF}}
81#.MAKEFLAGS: -d0 81#.MAKEFLAGS: -d0
82REF= too late 82REF= too late
83UNDEF= Uwas undefined 83UNDEF= Uwas undefined
84.if ${VAR} != "" 84.if ${VAR} != ""
85. error 85. error
86.endif 86.endif
87 87
88 88
 89# In variable assignments using the ':=' operator, undefined variables are
 90# preserved, no matter how indirectly they are referenced.
 91.undef REF3
 92REF2= <${REF3}>
 93REF= ${REF2}
 94VAR:= ${REF}
 95REF3= too late
 96.if ${VAR} != "<too late>"
 97. error
 98.endif
 99
 100
 101# In variable assignments using the ':=' operator, '$$' are preserved, no
 102# matter how indirectly they are referenced.
 103REF2= REF2:$$ $$$$
 104REF= REF:$$ $$$$ ${REF2}
 105VAR:= VAR:$$ $$$$ ${REF}
 106.if ${VAR} != "VAR:\$ \$\$ REF:\$ \$\$ REF2:\$ \$\$"
 107. error
 108.endif
 109
 110
 111# In variable assignments using the ':=' operator, '$$' are preserved in the
 112# expressions of the top level, but not in expressions that are nested.
 113VAR:= top:$$ ${:Unest1\:\$\$} ${:Unest2${:U\:\$\$}}
 114.if ${VAR} != "top:\$ nest1:\$ nest2:\$"
 115. error
 116.endif
 117
 118
89# XXX: edge case: When a variable name refers to an undefined variable, the 119# XXX: edge case: When a variable name refers to an undefined variable, the
90# behavior differs between the '=' and the ':=' assignment operators. 120# behavior differs between the '=' and the ':=' assignment operators.
91# This bug exists since var.c 1.42 from 2000-05-11. 121# This bug exists since var.c 1.42 from 2000-05-11.
92# 122#
93# The '=' operator expands the undefined variable to an empty string, thus 123# The '=' operator expands the undefined variable to an empty string, thus
94# assigning to VAR_ASSIGN_. In the name of variables to be set, it should 124# assigning to VAR_ASSIGN_. In the name of variables to be set, it should
95# really be forbidden to refer to undefined variables. 125# really be forbidden to refer to undefined variables.
96# 126#
97# The ':=' operator expands the variable name twice. In one of these 127# The ':=' operator expands the variable name twice. In one of these
98# expansions, the undefined variable expression is preserved (controlled by 128# expansions, the undefined variable expression is preserved (controlled by
99# preserveUndefined in VarAssign_EvalSubst), in the other expansion it expands 129# preserveUndefined in VarAssign_EvalSubst), in the other expansion it expands
100# to an empty string. This way, 2 variables are created using a single 130# to an empty string. This way, 2 variables are created using a single
101# variable assignment. It's magic. :-/ 131# variable assignment. It's magic. :-/