xref: /freebsd/contrib/bmake/unit-tests/directive-export-gmake.mk (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1# $NetBSD: directive-export-gmake.mk,v 1.9 2023/12/17 09:44:00 rillig Exp $
2#
3# Tests for the export directive (without leading dot), as in GNU make.
4
5# The "export" directive only affects the environment of the make process
6# and its child processes.  It does not affect the global variables or any
7# other variables.
8VAR=	before
9export VAR=exported
10.if ${VAR} != "before"
11.  error
12.endif
13
14# Ensure that the name-value pair is actually exported.
15.if ${:!echo "\$VAR"!} != "exported"
16.  error
17.endif
18
19# This line looks like it would export 2 variables, but it doesn't.
20# It only exports VAR and appends everything else as the variable value.
21export VAR=exported VAR2=exported-as-well
22.if ${:!echo "\$VAR"!} != "exported VAR2=exported-as-well"
23.  error ${:!echo "\$VAR"!}
24.endif
25
26# Contrary to the usual variable assignments, spaces are significant
27# after the '=' sign and are prepended to the value of the environment
28# variable.
29export VAR=  leading spaces
30.if ${:!echo "\$VAR"!} != "  leading spaces"
31.  error
32.endif
33
34# Contrary to the usual variable assignments, spaces are significant
35# before the '=' sign and are appended to the name of the environment
36# variable.
37#
38# Depending on the shell, environment variables with such exotic names
39# may be silently discarded.  One such shell is dash, which is the default
40# shell on Ubuntu and Debian.
41export VAR =trailing space in varname
42.if ${:!env | grep trailing || true!} != "VAR =trailing space in varname"
43.  if ${:!env | grep trailing || true!} != "" # for dash
44.    error
45.  endif
46.endif
47
48# The right-hand side of the exported variable is expanded exactly once.
49TWICE=	expanded twice
50ONCE=	expanded once, leaving $${TWICE} as-is
51export VAR=${ONCE}
52.if ${:!echo "\$VAR"!} != "expanded once, leaving \${TWICE} as-is"
53.  error
54.endif
55
56# Undefined variables are allowed on the right-hand side, they expand
57# to an empty string, as usual.
58export VAR=an ${UNDEF} variable
59.if ${:!echo "\$VAR"!} != "an  variable"
60.  error
61.endif
62
63
64# The body of the .for loop expands to 'export VAR=${:U1}', and the 'export'
65# directive is only recognized if the line does not contain a ':', to allow
66# 'export' to be a regular target.
67.for value in 1
68# XXX: The ':' in this line is inside an expression and should thus not be
69# interpreted as a dependency operator.
70# expect+1: Invalid line 'export VAR=${:U1}', expanded to 'export VAR=1'
71export VAR=${value}
72.endfor
73
74
75# The 'export' directive expands expressions, but the expressions must not
76# contain a ':', due to the overly strict parser.  The indirect expressions
77# may contain a ':', though.
78#
79# As a side effect, this test demonstrates that the 'export' directive exports
80# the environment variable immediately, other than the '.export' directive,
81# which defers that action if the variable value contains a '$'.
82INDIRECT_TZ=	${:UAmerica/Los_Angeles}
83export TZ=${INDIRECT_TZ}
84# expect+1: 16:00:00
85.info ${%T:L:localtime=86400}
86
87
88# The '=' must be present in the unexpanded line, it cannot be generated by
89# an expression.
90EQ=	=
91# expect+1: Variable/Value missing from "export"
92export EQ_VAR${EQ}eq-value
93.if ${:!env!:MEQ_VAR=*}
94.  error
95.endif
96
97
98# The variable name must be given directly, it is not expanded.  The name of
99# the exported variable thus starts with a '$', and that name may be filtered
100# out by the platform.
101INDIRECT_NAME=	I_NAME
102INDIRECT_VALUE=	indirect value
103export ${INDIRECT_NAME}=${INDIRECT_VALUE}
104.if ${:!env!:MI_NAME=*}
105.  error
106.endif
107