xref: /freebsd/contrib/bmake/unit-tests/directive-export.mk (revision 8d5c8e21c690b35d0a9a604d6b886fba222cd2fe)
1# $NetBSD: directive-export.mk,v 1.12 2024/06/01 10:06:23 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# Before var.c 1.1117 from 2024-06-01, a plain ".export" without a syntactical
32# argument exported all global variables.  This case could be triggered
33# unintentionally by writing a line of the form ".export ${VARNAMES}" to a
34# makefile, when VARNAMES was an empty list.
35# expect+1: warning: .export requires an argument.
36.export
37
38# An empty argument means no additional variables to export.
39.export ${:U}
40
41
42# Before a child process is started, whether for the '!=' assignment operator
43# or for the ':sh' modifier, all variables that were marked for being exported
44# are expanded and then exported.  If expanding such a variable requires
45# running a child command, the marked-as-exported variables would need to be
46# exported first, ending in an endless loop.  To avoid this endless loop,
47# don't export the variables while preparing a child process, see
48# ExportVarEnv.
49EMPTY_SHELL=	${:sh}
50.export EMPTY_SHELL	# only marked for export at this point
51_!=		:;:	# Force the variable to be actually exported.
52
53
54# If the '.export' directive exports a variable whose value contains a '$',
55# the actual export action is deferred until a subprocess is started, assuming
56# that only subprocesses access the environment variables.  The ':localtime'
57# modifier depends on the 'TZ' environment variable, without any subprocess.
58export TZ=${UTC}
59# expect+1: 00:00:00
60.info ${%T:L:localtime=86400}
61INDIRECT_TZ=	${:UAmerica/Los_Angeles}
62TZ=		${INDIRECT_TZ}
63.export TZ
64# expect+1: 00:00:00
65.info ${%T:L:localtime=86400}
66_!=	echo 'force exporting the environment variables'
67# expect+1: 16:00:00
68.info ${%T:L:localtime=86400}
69
70
71all:
72