| @@ -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. |
9 | VAR:= value | | 9 | VAR:= 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 |
78 | REF= U${UNDEF} | | 78 | REF= U${UNDEF} |
79 | #.MAKEFLAGS: -dv | | 79 | #.MAKEFLAGS: -dv |
80 | VAR:= ${:${REF}} | | 80 | VAR:= ${:${REF}} |
81 | #.MAKEFLAGS: -d0 | | 81 | #.MAKEFLAGS: -d0 |
82 | REF= too late | | 82 | REF= too late |
83 | UNDEF= Uwas undefined | | 83 | UNDEF= 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 |
| | | 92 | REF2= <${REF3}> |
| | | 93 | REF= ${REF2} |
| | | 94 | VAR:= ${REF} |
| | | 95 | REF3= 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. |
| | | 103 | REF2= REF2:$$ $$$$ |
| | | 104 | REF= REF:$$ $$$$ ${REF2} |
| | | 105 | VAR:= 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. |
| | | 113 | VAR:= 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. :-/ |