xref: /freebsd/contrib/bmake/unit-tests/dep-var.mk (revision 6a7405f5a6b639682cacf01e35d561411ff556aa)
1*6a7405f5SSimon J. Gerraty# $NetBSD: dep-var.mk,v 1.13 2025/01/14 21:23:17 rillig Exp $
22c3632d1SSimon J. Gerraty#
32c3632d1SSimon J. Gerraty# Tests for variable references in dependency declarations.
42c3632d1SSimon J. Gerraty#
52c3632d1SSimon J. Gerraty# Uh oh, this feels so strange that probably nobody uses it. But it seems to
62c3632d1SSimon J. Gerraty# be the only way to reach the lower half of SuffExpandChildren.
72c3632d1SSimon J. Gerraty
88c973ee2SSimon J. Gerraty.MAKEFLAGS: -dv
98c973ee2SSimon J. Gerraty
10*6a7405f5SSimon J. Gerraty# In a dependency line, an undefined expressions expands to an empty string.
11*6a7405f5SSimon J. Gerraty# expect: Var_Parse: ${UNDEF1} (eval)
122c3632d1SSimon J. Gerratyall: ${UNDEF1}
132c3632d1SSimon J. Gerraty
14d5e0a182SSimon J. Gerraty# Using a double dollar in order to circumvent immediate expression expansion
152c3632d1SSimon J. Gerraty# feels like unintended behavior.  At least the manual page says nothing at
162c3632d1SSimon J. Gerraty# all about defined or undefined variables in dependency lines.
172c3632d1SSimon J. Gerraty#
182c3632d1SSimon J. Gerraty# At the point where the expression ${DEF2} is expanded, the variable DEF2
192c3632d1SSimon J. Gerraty# is defined, so everything's fine.
20956e45f6SSimon J. Gerratyall: $${DEF2} a-$${DEF2}-b
212c3632d1SSimon J. Gerraty
22*6a7405f5SSimon J. Gerraty# This variable is neither defined now nor later.
232c3632d1SSimon J. Gerratyall: $${UNDEF3}
242c3632d1SSimon J. Gerraty
25956e45f6SSimon J. Gerraty# Try out how many levels of indirection are really expanded in dependency
26956e45f6SSimon J. Gerraty# lines.
27956e45f6SSimon J. Gerraty#
28956e45f6SSimon J. Gerraty# The first level of indirection is the $$ in the dependency line.
29956e45f6SSimon J. Gerraty# When the dependency line is parsed, it is resolved to the string
30956e45f6SSimon J. Gerraty# "${INDIRECT_1}".  At this point, the dollar is just an ordinary character,
31956e45f6SSimon J. Gerraty# waiting to be expanded at some later point.
32956e45f6SSimon J. Gerraty#
33956e45f6SSimon J. Gerraty# Later, in SuffExpandChildren, that expression is expanded again by calling
34956e45f6SSimon J. Gerraty# Var_Parse, and this time, the result is the string "1-2-${INDIRECT_2}-2-1".
35956e45f6SSimon J. Gerraty#
36956e45f6SSimon J. Gerraty# This string is not expanded anymore by Var_Parse.  But there is another
37956e45f6SSimon J. Gerraty# effect.  Now DirExpandCurly comes into play and expands the curly braces
38956e45f6SSimon J. Gerraty# in this filename pattern, resulting in the string "1-2-$INDIRECT_2-2-1".
39956e45f6SSimon J. Gerraty# As of 2020-09-03, the test dir.mk contains further details on this topic.
40956e45f6SSimon J. Gerraty#
41956e45f6SSimon J. Gerraty# Finally, this string is assigned to the local ${.TARGET} variable.  This
42956e45f6SSimon J. Gerraty# variable is expanded when the shell command is generated.  At that point,
43956e45f6SSimon J. Gerraty# the $I is expanded.  Since the variable I is not defined, it expands to
44956e45f6SSimon J. Gerraty# the empty string.  This way, the final output is the string
45956e45f6SSimon J. Gerraty# "1-2-NDIRECT_2-2-1", which differs from the actual name of the target.
46956e45f6SSimon J. Gerraty# For exactly this reason, it is not recommended to use dollar signs in
47956e45f6SSimon J. Gerraty# target names.
48956e45f6SSimon J. Gerraty#
49956e45f6SSimon J. Gerraty# The number of actual expansions is way more than one might expect,
50956e45f6SSimon J. Gerraty# therefore this feature is probably not widely used.
51956e45f6SSimon J. Gerraty#
52956e45f6SSimon J. Gerratyall: 1-$${INDIRECT_1}-1
53956e45f6SSimon J. GerratyINDIRECT_1=	2-$${INDIRECT_2}-2
54956e45f6SSimon J. GerratyINDIRECT_2=	3-$${INDIRECT_3}-3
55956e45f6SSimon J. GerratyINDIRECT_3=	indirect
56956e45f6SSimon J. Gerraty
572c3632d1SSimon J. GerratyUNDEF1=	undef1
582c3632d1SSimon J. GerratyDEF2=	def2
592c3632d1SSimon J. Gerraty
60d5e0a182SSimon J. Gerraty# Cover the code in SuffExpandChildren that deals with malformed
61956e45f6SSimon J. Gerraty# expressions.
62956e45f6SSimon J. Gerraty#
63956e45f6SSimon J. Gerraty# This seems to be an edge case that never happens in practice, and it would
64956e45f6SSimon J. Gerraty# probably be appropriate to just error out in such a case.
65956e45f6SSimon J. Gerraty#
66956e45f6SSimon J. Gerraty# To trigger this piece of code, the variable name must contain "$)" or "$:"
67956e45f6SSimon J. Gerraty# or "$)" or "$$".  Using "$:" does not work since the dependency line is
68956e45f6SSimon J. Gerraty# fully expanded before parsing, therefore any ':' in a target or source name
69956e45f6SSimon J. Gerraty# would be interpreted as a dependency operator instead.
70956e45f6SSimon J. Gerratyall: $$$$)
71956e45f6SSimon J. Gerraty
72956e45f6SSimon J. Gerraty# The $$INDIRECT in the following line is treated like the dependency of the
73956e45f6SSimon J. Gerraty# "all" target, that is, the "$$I" is first expanded to "$I", and in a second
74956e45f6SSimon J. Gerraty# round of expansion, the "$I" expands to nothing since the variable "I" is
75956e45f6SSimon J. Gerraty# undefined.
76956e45f6SSimon J. Gerraty#
77956e45f6SSimon J. Gerraty# Since 2020-09-13, this generates a parse error in lint mode (-dL), but not
78b0c40a00SSimon J. Gerraty# in normal mode since ParseDependency does not handle any errors after
79956e45f6SSimon J. Gerraty# calling Var_Parse.
80*6a7405f5SSimon J. Gerraty# expect: Var_Parse: ${:U\$)}: (eval)
818d5c8e21SSimon J. Gerraty# expect: Var_Parse: $INDIRECT_2-2-1 $): (parse)
828d5c8e21SSimon J. Gerraty# expect: Var_Parse: $): (parse)
83956e45f6SSimon J. Gerratyundef1 def2 a-def2-b 1-2-$$INDIRECT_2-2-1 ${:U\$)}:
84956e45f6SSimon J. Gerraty	@echo ${.TARGET:Q}
85956e45f6SSimon J. Gerraty
868c973ee2SSimon J. Gerraty.MAKEFLAGS: -d0
87