Home | History | Annotate | Line # | Download | only in unit-tests
dep-var.mk revision 1.12.2.1
      1  1.12.2.1  perseant # $NetBSD: dep-var.mk,v 1.12.2.1 2025/08/02 05:58:32 perseant 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.12.2.1  perseant # In a dependency line, an undefined expressions expands to an empty string.
     11  1.12.2.1  perseant # expect: Var_Parse: ${UNDEF1} (eval)
     12       1.1    rillig all: ${UNDEF1}
     13       1.1    rillig 
     14      1.11    rillig # Using a double dollar in order to circumvent immediate expression expansion
     15       1.1    rillig # feels like unintended behavior.  At least the manual page says nothing at
     16       1.1    rillig # all about defined or undefined variables in dependency lines.
     17       1.1    rillig #
     18       1.1    rillig # At the point where the expression ${DEF2} is expanded, the variable DEF2
     19       1.1    rillig # is defined, so everything's fine.
     20       1.2    rillig all: $${DEF2} a-$${DEF2}-b
     21       1.1    rillig 
     22  1.12.2.1  perseant # This variable is neither defined now nor later.
     23       1.1    rillig all: $${UNDEF3}
     24       1.1    rillig 
     25       1.3    rillig # Try out how many levels of indirection are really expanded in dependency
     26       1.3    rillig # lines.
     27       1.3    rillig #
     28       1.3    rillig # The first level of indirection is the $$ in the dependency line.
     29       1.3    rillig # When the dependency line is parsed, it is resolved to the string
     30       1.3    rillig # "${INDIRECT_1}".  At this point, the dollar is just an ordinary character,
     31       1.3    rillig # waiting to be expanded at some later point.
     32       1.3    rillig #
     33       1.3    rillig # Later, in SuffExpandChildren, that expression is expanded again by calling
     34       1.3    rillig # Var_Parse, and this time, the result is the string "1-2-${INDIRECT_2}-2-1".
     35       1.3    rillig #
     36       1.3    rillig # This string is not expanded anymore by Var_Parse.  But there is another
     37       1.3    rillig # effect.  Now DirExpandCurly comes into play and expands the curly braces
     38       1.3    rillig # in this filename pattern, resulting in the string "1-2-$INDIRECT_2-2-1".
     39       1.3    rillig # As of 2020-09-03, the test dir.mk contains further details on this topic.
     40       1.3    rillig #
     41       1.3    rillig # Finally, this string is assigned to the local ${.TARGET} variable.  This
     42       1.3    rillig # variable is expanded when the shell command is generated.  At that point,
     43       1.3    rillig # the $I is expanded.  Since the variable I is not defined, it expands to
     44       1.3    rillig # the empty string.  This way, the final output is the string
     45       1.3    rillig # "1-2-NDIRECT_2-2-1", which differs from the actual name of the target.
     46       1.3    rillig # For exactly this reason, it is not recommended to use dollar signs in
     47       1.3    rillig # target names.
     48       1.3    rillig #
     49       1.3    rillig # The number of actual expansions is way more than one might expect,
     50       1.3    rillig # therefore this feature is probably not widely used.
     51       1.3    rillig #
     52       1.3    rillig all: 1-$${INDIRECT_1}-1
     53       1.3    rillig INDIRECT_1=	2-$${INDIRECT_2}-2
     54       1.3    rillig INDIRECT_2=	3-$${INDIRECT_3}-3
     55       1.3    rillig INDIRECT_3=	indirect
     56       1.3    rillig 
     57       1.1    rillig UNDEF1=	undef1
     58       1.1    rillig DEF2=	def2
     59       1.1    rillig 
     60      1.10    rillig # Cover the code in SuffExpandChildren that deals with malformed
     61       1.4    rillig # expressions.
     62       1.4    rillig #
     63       1.4    rillig # This seems to be an edge case that never happens in practice, and it would
     64       1.4    rillig # probably be appropriate to just error out in such a case.
     65       1.4    rillig #
     66       1.4    rillig # To trigger this piece of code, the variable name must contain "$)" or "$:"
     67       1.4    rillig # or "$)" or "$$".  Using "$:" does not work since the dependency line is
     68       1.4    rillig # fully expanded before parsing, therefore any ':' in a target or source name
     69       1.4    rillig # would be interpreted as a dependency operator instead.
     70       1.4    rillig all: $$$$)
     71       1.4    rillig 
     72       1.5    rillig # The $$INDIRECT in the following line is treated like the dependency of the
     73       1.5    rillig # "all" target, that is, the "$$I" is first expanded to "$I", and in a second
     74       1.5    rillig # round of expansion, the "$I" expands to nothing since the variable "I" is
     75       1.5    rillig # undefined.
     76       1.5    rillig #
     77       1.5    rillig # Since 2020-09-13, this generates a parse error in lint mode (-dL), but not
     78       1.6    rillig # in normal mode since ParseDependency does not handle any errors after
     79       1.5    rillig # calling Var_Parse.
     80  1.12.2.1  perseant # expect: Var_Parse: ${:U\$)}: (eval)
     81      1.12    rillig # expect: Var_Parse: $INDIRECT_2-2-1 $): (parse)
     82      1.12    rillig # expect: Var_Parse: $): (parse)
     83       1.4    rillig undef1 def2 a-def2-b 1-2-$$INDIRECT_2-2-1 ${:U\$)}:
     84       1.3    rillig 	@echo ${.TARGET:Q}
     85       1.4    rillig 
     86       1.7    rillig .MAKEFLAGS: -d0
     87