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