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