xref: /freebsd/contrib/bmake/unit-tests/var-op-assign.mk (revision 36d6566e5985030fd2f1100bd9c1387bbe0bd290)
1# $NetBSD: var-op-assign.mk,v 1.6 2020/10/24 08:50:17 rillig Exp $
2#
3# Tests for the = variable assignment operator, which overwrites an existing
4# variable or creates it.
5
6# This is a simple variable assignment.
7# To the left of the assignment operator '=' there is the variable name,
8# and to the right is the variable value.
9#
10VAR=	value
11
12# This condition demonstrates that whitespace around the assignment operator
13# is discarded.  Otherwise the value would start with a single tab.
14#
15.if ${VAR} != "value"
16.  error
17.endif
18
19# Whitespace to the left of the assignment operator is ignored as well.
20# The variable value can contain arbitrary characters.
21#
22# The '#' needs to be escaped with a backslash, this happens in a very
23# early stage of parsing and applies to all line types, except for the
24# commands, which are indented with a tab.
25#
26# The '$' needs to be escaped with another '$', otherwise it would refer to
27# another variable.
28#
29VAR=	new value and \# some $$ special characters	# comment
30
31# When a string literal appears in a condition, the escaping rules are
32# different.  Run make with the -dc option to see the details.
33.if ${VAR} != "new value and \# some \$ special characters"
34.  error ${VAR}
35.endif
36
37# The variable value may contain references to other variables.
38# In this example, the reference is to the variable with the empty name,
39# which always expands to an empty string.  This alone would not produce
40# any side-effects, therefore the variable has a :!...! modifier that
41# executes a shell command.
42VAR=	${:! echo 'not yet evaluated' 1>&2 !}
43VAR=	${:! echo 'this will be evaluated later' 1>&2 !}
44
45# Now force the variable to be evaluated.
46# This outputs the line to stderr.
47.if ${VAR}
48.endif
49
50# In a variable assignment, the variable name must consist of a single word.
51#
52VARIABLE NAME=	variable value
53
54# But if the whitespace appears inside parentheses or braces, everything is
55# fine.
56#
57# XXX: This was not an intentional decision, as variable names typically
58# neither contain parentheses nor braces.  This is only a side-effect from
59# the implementation of the parser, which cheats when parsing a variable
60# name.  It only counts parentheses and braces instead of properly parsing
61# nested variable expressions such as VAR.${param}.
62#
63VAR(spaces in parentheses)=	()
64VAR{spaces in braces}=		{}
65
66# Be careful and use indirect variable names here, to prevent accidentally
67# accepting the test in case the parser just uses "VAR" as the variable name,
68# ignoring all the rest.
69#
70VARNAME_PAREN=	VAR(spaces in parentheses)
71VARNAME_BRACES=	VAR{spaces in braces}
72
73.if ${${VARNAME_PAREN}} != "()"
74.  error
75.endif
76
77.if ${${VARNAME_BRACES}} != "{}"
78.  error
79.endif
80
81# In safe mode, parsing would stop immediately after the "VARIABLE NAME="
82# line, since any commands run after that are probably working with
83# unexpected variable values.
84#
85# Therefore, just output an info message.
86.info Parsing still continues until here.
87
88all:
89	@:;
90