xref: /freebsd/contrib/bmake/unit-tests/directive-export.mk (revision 8d5c8e21c690b35d0a9a604d6b886fba222cd2fe)
1*8d5c8e21SSimon J. Gerraty# $NetBSD: directive-export.mk,v 1.12 2024/06/01 10:06:23 rillig Exp $
22c3632d1SSimon J. Gerraty#
32c3632d1SSimon J. Gerraty# Tests for the .export directive.
406b9b3e0SSimon J. Gerraty#
506b9b3e0SSimon J. Gerraty# See also:
606b9b3e0SSimon J. Gerraty#	directive-misspellings.mk
72c3632d1SSimon J. Gerraty
82c3632d1SSimon J. Gerraty# TODO: Implementation
92c3632d1SSimon J. Gerraty
10956e45f6SSimon J. GerratyINDIRECT=	indirect
11956e45f6SSimon J. GerratyVAR=		value $$ ${INDIRECT}
12956e45f6SSimon J. Gerraty
1306b9b3e0SSimon J. Gerraty# Before 2020-12-13, this unusual expression invoked undefined behavior since
1406b9b3e0SSimon J. Gerraty# it accessed out-of-bounds memory via Var_Export -> ExportVar -> MayExport.
1506b9b3e0SSimon J. Gerraty.export ${:U }
1606b9b3e0SSimon J. Gerraty
17956e45f6SSimon J. Gerraty# A variable is exported using the .export directive.
18956e45f6SSimon J. Gerraty# During that, its value is expanded, just like almost everywhere else.
19956e45f6SSimon J. Gerraty.export VAR
20956e45f6SSimon J. Gerraty.if ${:!env | grep '^VAR'!} != "VAR=value \$ indirect"
21956e45f6SSimon J. Gerraty.  error
22956e45f6SSimon J. Gerraty.endif
23956e45f6SSimon J. Gerraty
24956e45f6SSimon J. Gerraty# Undefining a variable that has been exported implicitly removes it from
25956e45f6SSimon J. Gerraty# the environment of all child processes.
26956e45f6SSimon J. Gerraty.undef VAR
27956e45f6SSimon J. Gerraty.if ${:!env | grep '^VAR' || true!} != ""
28956e45f6SSimon J. Gerraty.  error
29956e45f6SSimon J. Gerraty.endif
30956e45f6SSimon J. Gerraty
31*8d5c8e21SSimon J. Gerraty# Before var.c 1.1117 from 2024-06-01, a plain ".export" without a syntactical
32*8d5c8e21SSimon J. Gerraty# argument exported all global variables.  This case could be triggered
33*8d5c8e21SSimon J. Gerraty# unintentionally by writing a line of the form ".export ${VARNAMES}" to a
34*8d5c8e21SSimon J. Gerraty# makefile, when VARNAMES was an empty list.
35*8d5c8e21SSimon J. Gerraty# expect+1: warning: .export requires an argument.
3606b9b3e0SSimon J. Gerraty.export
37e2eeea75SSimon J. Gerraty
38b0c40a00SSimon J. Gerraty# An empty argument means no additional variables to export.
39b0c40a00SSimon J. Gerraty.export ${:U}
40b0c40a00SSimon J. Gerraty
41b0c40a00SSimon J. Gerraty
42d5e0a182SSimon J. Gerraty# Before a child process is started, whether for the '!=' assignment operator
43d5e0a182SSimon J. Gerraty# or for the ':sh' modifier, all variables that were marked for being exported
44d5e0a182SSimon J. Gerraty# are expanded and then exported.  If expanding such a variable requires
45d5e0a182SSimon J. Gerraty# running a child command, the marked-as-exported variables would need to be
46d5e0a182SSimon J. Gerraty# exported first, ending in an endless loop.  To avoid this endless loop,
47d5e0a182SSimon J. Gerraty# don't export the variables while preparing a child process, see
48d5e0a182SSimon J. Gerraty# ExportVarEnv.
49b0c40a00SSimon J. GerratyEMPTY_SHELL=	${:sh}
50b0c40a00SSimon J. Gerraty.export EMPTY_SHELL	# only marked for export at this point
51b0c40a00SSimon J. Gerraty_!=		:;:	# Force the variable to be actually exported.
52b0c40a00SSimon J. Gerraty
53b0c40a00SSimon J. Gerraty
5498875883SSimon J. Gerraty# If the '.export' directive exports a variable whose value contains a '$',
5598875883SSimon J. Gerraty# the actual export action is deferred until a subprocess is started, assuming
5698875883SSimon J. Gerraty# that only subprocesses access the environment variables.  The ':localtime'
5798875883SSimon J. Gerraty# modifier depends on the 'TZ' environment variable, without any subprocess.
5898875883SSimon J. Gerratyexport TZ=${UTC}
5998875883SSimon J. Gerraty# expect+1: 00:00:00
6098875883SSimon J. Gerraty.info ${%T:L:localtime=86400}
6198875883SSimon J. GerratyINDIRECT_TZ=	${:UAmerica/Los_Angeles}
6298875883SSimon J. GerratyTZ=		${INDIRECT_TZ}
6398875883SSimon J. Gerraty.export TZ
6498875883SSimon J. Gerraty# expect+1: 00:00:00
6598875883SSimon J. Gerraty.info ${%T:L:localtime=86400}
6698875883SSimon J. Gerraty_!=	echo 'force exporting the environment variables'
6798875883SSimon J. Gerraty# expect+1: 16:00:00
6898875883SSimon J. Gerraty.info ${%T:L:localtime=86400}
6998875883SSimon J. Gerraty
7098875883SSimon J. Gerraty
712c3632d1SSimon J. Gerratyall:
72