directive-undef.mk revision 1.12 1 1.12 rillig # $NetBSD: directive-undef.mk,v 1.12 2022/03/26 12:44:57 rillig Exp $
2 1.1 rillig #
3 1.2 rillig # Tests for the .undef directive.
4 1.6 rillig #
5 1.6 rillig # See also:
6 1.6 rillig # directive-misspellings.mk
7 1.1 rillig
8 1.9 rillig # Before var.c 1.737 from 2020-12-19, .undef only undefined the first
9 1.9 rillig # variable, silently skipping all further variable names.
10 1.9 rillig #
11 1.9 rillig # Before var.c 1.761 from 2020-12-22, .undef complained about too many
12 1.9 rillig # arguments.
13 1.9 rillig #
14 1.9 rillig # Since var.c 1.761 from 2020-12-22, .undef handles multiple variable names
15 1.9 rillig # just like the .export directive.
16 1.3 rillig 1= 1
17 1.3 rillig 2= 2
18 1.3 rillig 3= 3
19 1.3 rillig .undef 1 2 3
20 1.9 rillig .if ${1:U_}${2:U_}${3:U_} != ___
21 1.4 rillig . warning $1$2$3
22 1.3 rillig .endif
23 1.1 rillig
24 1.8 rillig
25 1.8 rillig # Without any arguments, until var.c 1.736 from 2020-12-19, .undef tried
26 1.8 rillig # to delete the variable with the empty name, which never exists; see
27 1.8 rillig # varname-empty.mk. Since var.c 1.737 from 2020-12-19, .undef complains
28 1.8 rillig # about a missing argument.
29 1.8 rillig .undef
30 1.8 rillig
31 1.8 rillig
32 1.8 rillig # Trying to delete the variable with the empty name is ok, it just won't
33 1.8 rillig # ever do anything since that variable is never defined.
34 1.8 rillig .undef ${:U}
35 1.8 rillig
36 1.8 rillig
37 1.9 rillig # The argument of .undef is first expanded exactly once and then split into
38 1.9 rillig # words, just like everywhere else. This prevents variables whose names
39 1.9 rillig # contain spaces or unbalanced 'single' or "double" quotes from being
40 1.9 rillig # undefined, but these characters do not appear in variables names anyway.
41 1.8 rillig 1= 1
42 1.8 rillig 2= 2
43 1.8 rillig 3= 3
44 1.8 rillig ${:U1 2 3}= one two three
45 1.8 rillig VARNAMES= 1 2 3
46 1.11 rillig .undef ${VARNAMES} # undefines the variables "1", "2" and "3"
47 1.11 rillig .if ${${:U1 2 3}} != "one two three" # still there
48 1.8 rillig . error
49 1.8 rillig .endif
50 1.11 rillig .if ${1:U_}${2:U_}${3:U_} != "___" # these have been undefined
51 1.8 rillig . error
52 1.8 rillig .endif
53 1.5 rillig
54 1.7 rillig
55 1.9 rillig # A variable named " " cannot be undefined. There's no practical use case
56 1.9 rillig # for such variables anyway.
57 1.7 rillig SPACE= ${:U }
58 1.7 rillig ${SPACE}= space
59 1.7 rillig .if !defined(${SPACE})
60 1.7 rillig . error
61 1.7 rillig .endif
62 1.7 rillig .undef ${SPACE}
63 1.9 rillig .if !defined(${SPACE})
64 1.7 rillig . error
65 1.7 rillig .endif
66 1.7 rillig
67 1.7 rillig
68 1.9 rillig # A variable named "$" can be undefined since the argument to .undef is
69 1.9 rillig # expanded exactly once, before being split into words.
70 1.7 rillig DOLLAR= $$
71 1.7 rillig ${DOLLAR}= dollar
72 1.7 rillig .if !defined(${DOLLAR})
73 1.7 rillig . error
74 1.7 rillig .endif
75 1.7 rillig .undef ${DOLLAR}
76 1.7 rillig .if defined(${DOLLAR})
77 1.7 rillig . error
78 1.7 rillig .endif
79 1.7 rillig
80 1.7 rillig
81 1.9 rillig # Since var.c 1.762 from 2020-12-22, parse errors in the argument should be
82 1.9 rillig # properly detected and should stop the .undef directive from doing any work.
83 1.9 rillig #
84 1.9 rillig # As of var.c 1.762, this doesn't happen though because the error handling
85 1.9 rillig # in Var_Parse and Var_Subst is not done properly.
86 1.9 rillig .undef ${VARNAMES:L:Z}
87 1.9 rillig
88 1.9 rillig
89 1.10 rillig UT_EXPORTED= exported-value
90 1.10 rillig .export UT_EXPORTED
91 1.10 rillig .if ${:!echo "\${UT_EXPORTED:-not-exported}"!} != "exported-value"
92 1.10 rillig . error
93 1.10 rillig .endif
94 1.10 rillig .if !${.MAKE.EXPORTED:MUT_EXPORTED}
95 1.10 rillig . error
96 1.10 rillig .endif
97 1.10 rillig .undef UT_EXPORTED # XXX: does not update .MAKE.EXPORTED
98 1.10 rillig .if ${:!echo "\${UT_EXPORTED:-not-exported}"!} != "not-exported"
99 1.10 rillig . error
100 1.10 rillig .endif
101 1.10 rillig .if ${.MAKE.EXPORTED:MUT_EXPORTED}
102 1.10 rillig . warning UT_EXPORTED is still listed in .MAKE.EXPORTED even though $\
103 1.10 rillig it is not exported anymore.
104 1.10 rillig .endif
105 1.10 rillig
106 1.10 rillig
107 1.11 rillig # When an exported variable is undefined, the variable is removed both from
108 1.11 rillig # the global scope as well as from the environment.
109 1.11 rillig DIRECT= direct
110 1.11 rillig INDIRECT= in-${DIRECT}
111 1.11 rillig .export DIRECT INDIRECT
112 1.11 rillig .if ${DIRECT} != "direct"
113 1.11 rillig . error
114 1.11 rillig .endif
115 1.11 rillig .if ${INDIRECT} != "in-direct"
116 1.11 rillig . error
117 1.11 rillig .endif
118 1.11 rillig
119 1.11 rillig # Deletes the variables from the global scope and also from the environment.
120 1.11 rillig # This applies to both variables, even though 'INDIRECT' is not actually
121 1.11 rillig # exported yet since it refers to another variable.
122 1.11 rillig .undef DIRECT # Separate '.undef' directives,
123 1.11 rillig .undef INDIRECT # for backwards compatibility.
124 1.11 rillig
125 1.11 rillig .if ${DIRECT:Uundefined} != "undefined"
126 1.11 rillig . error
127 1.11 rillig .endif
128 1.11 rillig .if ${INDIRECT:Uundefined} != "undefined"
129 1.11 rillig . error
130 1.11 rillig .endif
131 1.11 rillig
132 1.11 rillig
133 1.12 rillig # Since var.c 1.570 from 2020-10-06 and before var.c 1.1014 from 2022-03-26,
134 1.12 rillig # make ran into an assertion failure when trying to undefine a variable that
135 1.12 rillig # was based on an environment variable.
136 1.12 rillig .if ${ENV_VAR} != "env-value" # see ./Makefile, ENV.directive-undef
137 1.12 rillig . error
138 1.12 rillig .endif
139 1.12 rillig ENV_VAR+= appended # moves the short-lived variable to the
140 1.12 rillig # global scope
141 1.12 rillig .undef ENV_VAR # removes the variable from both the global
142 1.12 rillig # scope and from the environment
143 1.12 rillig
144 1.12 rillig
145 1.1 rillig all:
146