1 # $NetBSD: var-op-assign.mk,v 1.7 2020/11/15 20:20:58 rillig Exp $
3 # Tests for the = variable assignment operator, which overwrites an existing
4 # variable or creates it.
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. The variable value is stored as-is,
9 # it is not expanded in any way.
13 # This condition demonstrates that whitespace around the assignment operator
14 # is discarded. Otherwise the value would start with a single tab.
20 # Whitespace to the left of the assignment operator is ignored as well.
21 # The variable value can contain arbitrary characters.
23 # The '#' needs to be escaped with a backslash, this happens in a very
24 # early stage of parsing and applies to all line types, except for the
25 # commands, which are indented with a tab.
27 # The '$' needs to be escaped with another '$', otherwise it would refer to
30 VAR= new value and \# some $$ special characters # comment
32 # When a string literal appears in a condition, the escaping rules are
33 # different. Run make with the -dc option to see the details.
34 .if ${VAR} != "new value and \# some \$ special characters"
38 # The variable value may contain references to other variables.
39 # In this example, the reference is to the variable with the empty name,
40 # which is never defined.
42 # This alone would not produce any side-effects, therefore the variable has
43 # a :!...! modifier that executes a shell command. The :!...! modifier turns
44 # an undefined expression into a defined one, see ApplyModifier_ShellCommand,
45 # the call to ApplyModifiersState_Define.
47 # Since the right-hand side of a '=' assignment is not expanded at the time
48 # when the variable is defined, the first command is not run at all.
49 VAR= ${:! echo 'not yet evaluated' 1>&2 !}
50 VAR= ${:! echo 'this will be evaluated later' 1>&2 !}
52 # Now force the variable to be evaluated.
53 # This outputs the line to stderr.
57 # In a variable assignment, the variable name must consist of a single word.
58 # The following line therefore generates a parse error.
59 VARIABLE NAME= variable value
61 # But if the whitespace appears inside parentheses or braces, everything is
64 # XXX: This was not an intentional decision, as variable names typically
65 # neither contain parentheses nor braces. This is only a side-effect from
66 # the implementation of the parser, which cheats when parsing a variable
67 # name. It only counts parentheses and braces instead of properly parsing
68 # nested variable expressions such as VAR.${param}.
70 VAR(spaces in parentheses)= ()
71 VAR{spaces in braces}= {}
73 # Be careful and use indirect variable names here, to prevent accidentally
74 # accepting the test in case the parser just uses "VAR" as the variable name,
75 # ignoring all the rest.
77 VARNAME_PAREN= VAR(spaces in parentheses)
78 VARNAME_BRACES= VAR{spaces in braces}
80 .if ${${VARNAME_PAREN}} != "()"
84 .if ${${VARNAME_BRACES}} != "{}"
88 # In safe mode, parsing would stop immediately after the "VARIABLE NAME="
89 # line, since any commands run after that are probably working with
90 # unexpected variable values.
92 # Therefore, just output an info message.
93 .info Parsing still continues until here.