xref: /freebsd/contrib/bmake/unit-tests/directive-undef.mk (revision 759b177aecbfc49ebc900739954ac56b1aa5fc53)
1*759b177aSSimon J. Gerraty# $NetBSD: directive-undef.mk,v 1.17 2025/03/29 19:08:52 rillig Exp $
22c3632d1SSimon J. Gerraty#
32c3632d1SSimon J. Gerraty# Tests for the .undef directive.
406b9b3e0SSimon J. Gerraty#
506b9b3e0SSimon J. Gerraty# See also:
606b9b3e0SSimon J. Gerraty#	directive-misspellings.mk
72c3632d1SSimon J. Gerraty
806b9b3e0SSimon J. Gerraty# Before var.c 1.737 from 2020-12-19, .undef only undefined the first
906b9b3e0SSimon J. Gerraty# variable, silently skipping all further variable names.
1006b9b3e0SSimon J. Gerraty#
1106b9b3e0SSimon J. Gerraty# Before var.c 1.761 from 2020-12-22, .undef complained about too many
1206b9b3e0SSimon J. Gerraty# arguments.
1306b9b3e0SSimon J. Gerraty#
1406b9b3e0SSimon J. Gerraty# Since var.c 1.761 from 2020-12-22, .undef handles multiple variable names
1506b9b3e0SSimon J. Gerraty# just like the .export directive.
162c3632d1SSimon J. Gerraty1=		1
172c3632d1SSimon J. Gerraty2=		2
182c3632d1SSimon J. Gerraty3=		3
192c3632d1SSimon J. Gerraty.undef 1 2 3
2006b9b3e0SSimon J. Gerraty.if ${1:U_}${2:U_}${3:U_} != ___
212c3632d1SSimon J. Gerraty.  warning $1$2$3
222c3632d1SSimon J. Gerraty.endif
232c3632d1SSimon J. Gerraty
2406b9b3e0SSimon J. Gerraty
2506b9b3e0SSimon J. Gerraty# Without any arguments, until var.c 1.736 from 2020-12-19, .undef tried
2606b9b3e0SSimon J. Gerraty# to delete the variable with the empty name, which never exists; see
2706b9b3e0SSimon J. Gerraty# varname-empty.mk.  Since var.c 1.737 from 2020-12-19, .undef complains
2806b9b3e0SSimon J. Gerraty# about a missing argument.
29148ee845SSimon J. Gerraty# expect+1: The .undef directive requires an argument
3006b9b3e0SSimon J. Gerraty.undef
3106b9b3e0SSimon J. Gerraty
3206b9b3e0SSimon J. Gerraty
3306b9b3e0SSimon J. Gerraty# Trying to delete the variable with the empty name is ok, it just won't
3406b9b3e0SSimon J. Gerraty# ever do anything since that variable is never defined.
3506b9b3e0SSimon J. Gerraty.undef ${:U}
3606b9b3e0SSimon J. Gerraty
3706b9b3e0SSimon J. Gerraty
3806b9b3e0SSimon J. Gerraty# The argument of .undef is first expanded exactly once and then split into
3906b9b3e0SSimon J. Gerraty# words, just like everywhere else.  This prevents variables whose names
4006b9b3e0SSimon J. Gerraty# contain spaces or unbalanced 'single' or "double" quotes from being
4106b9b3e0SSimon J. Gerraty# undefined, but these characters do not appear in variables names anyway.
4206b9b3e0SSimon J. Gerraty1=		1
4306b9b3e0SSimon J. Gerraty2=		2
4406b9b3e0SSimon J. Gerraty3=		3
4506b9b3e0SSimon J. Gerraty${:U1 2 3}=	one two three
4606b9b3e0SSimon J. GerratyVARNAMES=	1 2 3
471d3f2ddcSSimon J. Gerraty.undef ${VARNAMES}		# undefines the variables "1", "2" and "3"
481d3f2ddcSSimon J. Gerraty.if ${${:U1 2 3}} != "one two three"	# still there
4906b9b3e0SSimon J. Gerraty.  error
5006b9b3e0SSimon J. Gerraty.endif
511d3f2ddcSSimon J. Gerraty.if ${1:U_}${2:U_}${3:U_} != "___"	# these have been undefined
5206b9b3e0SSimon J. Gerraty.  error
5306b9b3e0SSimon J. Gerraty.endif
5406b9b3e0SSimon J. Gerraty
5506b9b3e0SSimon J. Gerraty
5606b9b3e0SSimon J. Gerraty# A variable named " " cannot be undefined.  There's no practical use case
5706b9b3e0SSimon J. Gerraty# for such variables anyway.
5806b9b3e0SSimon J. GerratySPACE=		${:U }
5906b9b3e0SSimon J. Gerraty${SPACE}=	space
6006b9b3e0SSimon J. Gerraty.if !defined(${SPACE})
6106b9b3e0SSimon J. Gerraty.  error
6206b9b3e0SSimon J. Gerraty.endif
6306b9b3e0SSimon J. Gerraty.undef ${SPACE}
6406b9b3e0SSimon J. Gerraty.if !defined(${SPACE})
6506b9b3e0SSimon J. Gerraty.  error
6606b9b3e0SSimon J. Gerraty.endif
6706b9b3e0SSimon J. Gerraty
6806b9b3e0SSimon J. Gerraty
6906b9b3e0SSimon J. Gerraty# A variable named "$" can be undefined since the argument to .undef is
7006b9b3e0SSimon J. Gerraty# expanded exactly once, before being split into words.
7106b9b3e0SSimon J. GerratyDOLLAR=		$$
7206b9b3e0SSimon J. Gerraty${DOLLAR}=	dollar
7306b9b3e0SSimon J. Gerraty.if !defined(${DOLLAR})
7406b9b3e0SSimon J. Gerraty.  error
7506b9b3e0SSimon J. Gerraty.endif
7606b9b3e0SSimon J. Gerraty.undef ${DOLLAR}
7706b9b3e0SSimon J. Gerraty.if defined(${DOLLAR})
7806b9b3e0SSimon J. Gerraty.  error
7906b9b3e0SSimon J. Gerraty.endif
8006b9b3e0SSimon J. Gerraty
8106b9b3e0SSimon J. Gerraty
8206b9b3e0SSimon J. Gerraty# Since var.c 1.762 from 2020-12-22, parse errors in the argument should be
8306b9b3e0SSimon J. Gerraty# properly detected and should stop the .undef directive from doing any work.
8406b9b3e0SSimon J. Gerraty#
8506b9b3e0SSimon J. Gerraty# As of var.c 1.762, this doesn't happen though because the error handling
8606b9b3e0SSimon J. Gerraty# in Var_Parse and Var_Subst is not done properly.
87*759b177aSSimon J. Gerraty# expect+1: Unknown modifier ":Z"
8806b9b3e0SSimon J. Gerraty.undef ${VARNAMES:L:Z}
8906b9b3e0SSimon J. Gerraty
90e2eeea75SSimon J. Gerraty
91b0c40a00SSimon J. GerratyUT_EXPORTED=	exported-value
92b0c40a00SSimon J. Gerraty.export UT_EXPORTED
93b0c40a00SSimon J. Gerraty.if ${:!echo "\${UT_EXPORTED:-not-exported}"!} != "exported-value"
94b0c40a00SSimon J. Gerraty.  error
95b0c40a00SSimon J. Gerraty.endif
96b0c40a00SSimon J. Gerraty.if !${.MAKE.EXPORTED:MUT_EXPORTED}
97b0c40a00SSimon J. Gerraty.  error
98b0c40a00SSimon J. Gerraty.endif
99b0c40a00SSimon J. Gerraty.undef UT_EXPORTED		# XXX: does not update .MAKE.EXPORTED
100b0c40a00SSimon J. Gerraty.if ${:!echo "\${UT_EXPORTED:-not-exported}"!} != "not-exported"
101b0c40a00SSimon J. Gerraty.  error
102b0c40a00SSimon J. Gerraty.endif
103b0c40a00SSimon J. Gerraty.if ${.MAKE.EXPORTED:MUT_EXPORTED}
104148ee845SSimon J. Gerraty# expect+1: warning: UT_EXPORTED is still listed in .MAKE.EXPORTED even though spaceit is not exported anymore.
105b0c40a00SSimon J. Gerraty.  warning UT_EXPORTED is still listed in .MAKE.EXPORTED even though $\
106b0c40a00SSimon J. Gerraty	   it is not exported anymore.
107b0c40a00SSimon J. Gerraty.endif
108b0c40a00SSimon J. Gerraty
109b0c40a00SSimon J. Gerraty
1101d3f2ddcSSimon J. Gerraty# When an exported variable is undefined, the variable is removed both from
1111d3f2ddcSSimon J. Gerraty# the global scope as well as from the environment.
1121d3f2ddcSSimon J. GerratyDIRECT=		direct
1131d3f2ddcSSimon J. GerratyINDIRECT=	in-${DIRECT}
1141d3f2ddcSSimon J. Gerraty.export DIRECT INDIRECT
1151d3f2ddcSSimon J. Gerraty.if ${DIRECT} != "direct"
1161d3f2ddcSSimon J. Gerraty.  error
1171d3f2ddcSSimon J. Gerraty.endif
1181d3f2ddcSSimon J. Gerraty.if ${INDIRECT} != "in-direct"
1191d3f2ddcSSimon J. Gerraty.  error
1201d3f2ddcSSimon J. Gerraty.endif
1211d3f2ddcSSimon J. Gerraty
1221d3f2ddcSSimon J. Gerraty# Deletes the variables from the global scope and also from the environment.
1231d3f2ddcSSimon J. Gerraty# This applies to both variables, even though 'INDIRECT' is not actually
1241d3f2ddcSSimon J. Gerraty# exported yet since it refers to another variable.
1251d3f2ddcSSimon J. Gerraty.undef DIRECT			# Separate '.undef' directives,
1261d3f2ddcSSimon J. Gerraty.undef INDIRECT			# for backwards compatibility.
1271d3f2ddcSSimon J. Gerraty
1281d3f2ddcSSimon J. Gerraty.if ${DIRECT:Uundefined} != "undefined"
1291d3f2ddcSSimon J. Gerraty.  error
1301d3f2ddcSSimon J. Gerraty.endif
1311d3f2ddcSSimon J. Gerraty.if ${INDIRECT:Uundefined} != "undefined"
1321d3f2ddcSSimon J. Gerraty.  error
1331d3f2ddcSSimon J. Gerraty.endif
1341d3f2ddcSSimon J. Gerraty
1351d3f2ddcSSimon J. Gerraty
1361d3f2ddcSSimon J. Gerraty# Since var.c 1.570 from 2020-10-06 and before var.c 1.1014 from 2022-03-26,
1371d3f2ddcSSimon J. Gerraty# make ran into an assertion failure when trying to undefine a variable that
1381d3f2ddcSSimon J. Gerraty# was based on an environment variable.
1391d3f2ddcSSimon J. Gerraty.if ${ENV_VAR} != "env-value"	# see ./Makefile, ENV.directive-undef
1401d3f2ddcSSimon J. Gerraty.  error
1411d3f2ddcSSimon J. Gerraty.endif
1421d3f2ddcSSimon J. GerratyENV_VAR+=	appended	# moves the short-lived variable to the
1431d3f2ddcSSimon J. Gerraty				# global scope
1441d3f2ddcSSimon J. Gerraty.undef ENV_VAR			# removes the variable from both the global
1451d3f2ddcSSimon J. Gerraty				# scope and from the environment
1461d3f2ddcSSimon J. Gerraty
1471d3f2ddcSSimon J. Gerraty
1482c3632d1SSimon J. Gerratyall:
149