1# $NetBSD: directive-export.mk,v 1.10 2023/11/19 09:45:19 rillig Exp $
2#
3# Tests for the .export directive.
4#
5# See also:
6#	directive-misspellings.mk
7
8# TODO: Implementation
9
10INDIRECT=	indirect
11VAR=		value $$ ${INDIRECT}
12
13# Before 2020-12-13, this unusual expression invoked undefined behavior since
14# it accessed out-of-bounds memory via Var_Export -> ExportVar -> MayExport.
15.export ${:U }
16
17# A variable is exported using the .export directive.
18# During that, its value is expanded, just like almost everywhere else.
19.export VAR
20.if ${:!env | grep '^VAR'!} != "VAR=value \$ indirect"
21.  error
22.endif
23
24# Undefining a variable that has been exported implicitly removes it from
25# the environment of all child processes.
26.undef VAR
27.if ${:!env | grep '^VAR' || true!} != ""
28.  error
29.endif
30
31# No syntactical argument means to export all variables.
32.export
33
34# An empty argument means no additional variables to export.
35.export ${:U}
36
37
38# Before a child process is started, whether for the '!=' assignment operator
39# or for the ':sh' modifier, all variables that were marked for being exported
40# are expanded and then exported.  If expanding such a variable requires
41# running a child command, the marked-as-exported variables would need to be
42# exported first, ending in an endless loop.  To avoid this endless loop,
43# don't export the variables while preparing a child process, see
44# ExportVarEnv.
45EMPTY_SHELL=	${:sh}
46.export EMPTY_SHELL	# only marked for export at this point
47_!=		:;:	# Force the variable to be actually exported.
48
49
50# If the '.export' directive exports a variable whose value contains a '$',
51# the actual export action is deferred until a subprocess is started, assuming
52# that only subprocesses access the environment variables.  The ':localtime'
53# modifier depends on the 'TZ' environment variable, without any subprocess.
54export TZ=${UTC}
55# expect+1: 00:00:00
56.info ${%T:L:localtime=86400}
57INDIRECT_TZ=	${:UAmerica/Los_Angeles}
58TZ=		${INDIRECT_TZ}
59.export TZ
60# expect+1: 00:00:00
61.info ${%T:L:localtime=86400}
62_!=	echo 'force exporting the environment variables'
63# expect+1: 16:00:00
64.info ${%T:L:localtime=86400}
65
66
67all:
68