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