dep-var.mk revision 1.8 1 1.8 rillig # $NetBSD: dep-var.mk,v 1.8 2023/05/10 15:53:32 rillig Exp $
2 1.1 rillig #
3 1.1 rillig # Tests for variable references in dependency declarations.
4 1.1 rillig #
5 1.1 rillig # Uh oh, this feels so strange that probably nobody uses it. But it seems to
6 1.1 rillig # be the only way to reach the lower half of SuffExpandChildren.
7 1.1 rillig
8 1.7 rillig .MAKEFLAGS: -dv
9 1.7 rillig
10 1.7 rillig # expect: Var_Parse: ${UNDEF1} (eval-defined)
11 1.7 rillig # Even though undefined expressions should lead to errors, no error message is
12 1.7 rillig # generated for this line. The variable expression ${UNDEF1} simply expands
13 1.7 rillig # to an empty string.
14 1.1 rillig all: ${UNDEF1}
15 1.1 rillig
16 1.1 rillig # Using a double dollar in order to circumvent immediate variable expansion
17 1.1 rillig # feels like unintended behavior. At least the manual page says nothing at
18 1.1 rillig # all about defined or undefined variables in dependency lines.
19 1.1 rillig #
20 1.1 rillig # At the point where the expression ${DEF2} is expanded, the variable DEF2
21 1.1 rillig # is defined, so everything's fine.
22 1.2 rillig all: $${DEF2} a-$${DEF2}-b
23 1.1 rillig
24 1.1 rillig # This variable is not defined at all.
25 1.7 rillig # XXX: The -dv log says later when expanding the sources of 'all':
26 1.7 rillig # Var_Parse: ${UNDEF3} (eval-defined)
27 1.1 rillig # but no error message is generated for this line, just like for UNDEF1.
28 1.1 rillig # The variable expression ${UNDEF3} simply expands to an empty string.
29 1.1 rillig all: $${UNDEF3}
30 1.1 rillig
31 1.3 rillig # Try out how many levels of indirection are really expanded in dependency
32 1.3 rillig # lines.
33 1.3 rillig #
34 1.3 rillig # The first level of indirection is the $$ in the dependency line.
35 1.3 rillig # When the dependency line is parsed, it is resolved to the string
36 1.3 rillig # "${INDIRECT_1}". At this point, the dollar is just an ordinary character,
37 1.3 rillig # waiting to be expanded at some later point.
38 1.3 rillig #
39 1.3 rillig # Later, in SuffExpandChildren, that expression is expanded again by calling
40 1.3 rillig # Var_Parse, and this time, the result is the string "1-2-${INDIRECT_2}-2-1".
41 1.3 rillig #
42 1.3 rillig # This string is not expanded anymore by Var_Parse. But there is another
43 1.3 rillig # effect. Now DirExpandCurly comes into play and expands the curly braces
44 1.3 rillig # in this filename pattern, resulting in the string "1-2-$INDIRECT_2-2-1".
45 1.3 rillig # As of 2020-09-03, the test dir.mk contains further details on this topic.
46 1.3 rillig #
47 1.3 rillig # Finally, this string is assigned to the local ${.TARGET} variable. This
48 1.3 rillig # variable is expanded when the shell command is generated. At that point,
49 1.3 rillig # the $I is expanded. Since the variable I is not defined, it expands to
50 1.3 rillig # the empty string. This way, the final output is the string
51 1.3 rillig # "1-2-NDIRECT_2-2-1", which differs from the actual name of the target.
52 1.3 rillig # For exactly this reason, it is not recommended to use dollar signs in
53 1.3 rillig # target names.
54 1.3 rillig #
55 1.3 rillig # The number of actual expansions is way more than one might expect,
56 1.3 rillig # therefore this feature is probably not widely used.
57 1.3 rillig #
58 1.3 rillig all: 1-$${INDIRECT_1}-1
59 1.3 rillig INDIRECT_1= 2-$${INDIRECT_2}-2
60 1.3 rillig INDIRECT_2= 3-$${INDIRECT_3}-3
61 1.3 rillig INDIRECT_3= indirect
62 1.3 rillig
63 1.1 rillig UNDEF1= undef1
64 1.1 rillig DEF2= def2
65 1.1 rillig
66 1.4 rillig # Cover the code in SuffExpandChildren that deals with malformed variable
67 1.4 rillig # expressions.
68 1.4 rillig #
69 1.4 rillig # This seems to be an edge case that never happens in practice, and it would
70 1.4 rillig # probably be appropriate to just error out in such a case.
71 1.4 rillig #
72 1.4 rillig # To trigger this piece of code, the variable name must contain "$)" or "$:"
73 1.4 rillig # or "$)" or "$$". Using "$:" does not work since the dependency line is
74 1.4 rillig # fully expanded before parsing, therefore any ':' in a target or source name
75 1.4 rillig # would be interpreted as a dependency operator instead.
76 1.4 rillig all: $$$$)
77 1.4 rillig
78 1.5 rillig # The $$INDIRECT in the following line is treated like the dependency of the
79 1.5 rillig # "all" target, that is, the "$$I" is first expanded to "$I", and in a second
80 1.5 rillig # round of expansion, the "$I" expands to nothing since the variable "I" is
81 1.5 rillig # undefined.
82 1.5 rillig #
83 1.5 rillig # Since 2020-09-13, this generates a parse error in lint mode (-dL), but not
84 1.6 rillig # in normal mode since ParseDependency does not handle any errors after
85 1.5 rillig # calling Var_Parse.
86 1.7 rillig # expect: Var_Parse: ${:U\$)}: (eval-defined)
87 1.7 rillig # expect: Var_Parse: $INDIRECT_2-2-1 $): (parse-only)
88 1.7 rillig # expect: Var_Parse: $): (parse-only)
89 1.4 rillig undef1 def2 a-def2-b 1-2-$$INDIRECT_2-2-1 ${:U\$)}:
90 1.3 rillig @echo ${.TARGET:Q}
91 1.4 rillig
92 1.7 rillig .MAKEFLAGS: -d0
93 1.7 rillig
94 1.8 rillig # XXX: The exit status is still 0, even though Parse_Error is called with
95 1.8 rillig # PARSE_FATAL in SuffExpandChildren. The exit status is only affected by
96 1.8 rillig # parse errors when they occur in the parsing phase, see Parse_File.
97