1 # $NetBSD: var-op-assign.mk,v 1.4 2020/08/25 16:20:32 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.
12 # This condition demonstrates that whitespace around the assignment operator
13 # is discarded. Otherwise the value would start with a single tab.
19 # Whitespace to the left of the assignment operator is ignored as well.
20 # The variable value can contain arbitrary characters.
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.
26 # The '$' needs to be escaped with another '$', otherwise it would refer to
29 VAR =new value and \# some $$ special characters # comment
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"
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.
42 VAR= ${:! echo 'not yet evaluated' 1>&2 !}
43 VAR= ${:! echo 'this will be evaluated later' 1>&2 !}
45 # Now force the variable to be evaluated.
46 # This outputs the line to stderr.
50 # In a variable assignment, the variable name must consist of a single word.
52 VARIABLE NAME= variable value
54 # But if the whitespace appears inside parentheses or braces, everything is
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}.
63 VAR(spaces in parentheses)= ()
64 VAR{spaces in braces}= {}
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.
70 VARNAME_PAREN= VAR(spaces in parentheses)
71 VARNAME_BRACES= VAR{spaces in braces}
73 .if ${${VARNAME_PAREN}} != "()"
77 .if ${${VARNAME_BRACES}} != "{}"
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.
85 # Therefore, just output an info message.
86 .info Parsing still continues until here.