Home | History | Annotate | Line # | Download | only in unit-tests
varmod-loop-varname.mk revision 1.1
      1  1.1  rillig # $NetBSD: varmod-loop-varname.mk,v 1.1 2021/04/04 13:20:52 rillig Exp $
      2  1.1  rillig #
      3  1.1  rillig # Tests for the first part of the variable modifier ':@var (a] ...@', which
      4  1.1  rillig # contains the variable name to use during the loop.
      5  1.1  rillig 
      6  1.1  rillig .MAKE.SAVE_DOLLARS=	yes
      7  1.1  rillig 
      8  1.1  rillig 
      9  1.1  rillig # In the :@ modifier, the name of the loop variable can be generated
     10  1.1  rillig # dynamically.  There's no practical use-case for this, and hopefully nobody
     11  1.1  rillig # will ever depend on this, but technically it's possible.
     12  1.1  rillig # Therefore, in -dL mode, this is forbidden, see lint.mk.
     13  1.1  rillig .if ${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@} != "+one+ +two+ +three+"
     14  1.1  rillig .  error
     15  1.1  rillig .endif
     16  1.1  rillig 
     17  1.1  rillig 
     18  1.1  rillig # ":::" is a very creative variable name, unlikely to occur in practice.
     19  1.1  rillig # The expression ${\:\:\:} would not work since backslashes can only
     20  1.1  rillig # be escaped in the modifiers, but not in the variable name, therefore
     21  1.1  rillig # the extra indirection via the modifier ':U'.
     22  1.1  rillig .if ${:U1 2 3:@:::@x${${:U\:\:\:}}y@} != "x1y x2y x3y"
     23  1.1  rillig .  error
     24  1.1  rillig .endif
     25  1.1  rillig 
     26  1.1  rillig 
     27  1.1  rillig # "@@" is another creative variable name.
     28  1.1  rillig .if ${:U1 2 3:@\@\@@x${@@}y@} != "x1y x2y x3y"
     29  1.1  rillig .  error
     30  1.1  rillig .endif
     31  1.1  rillig 
     32  1.1  rillig 
     33  1.1  rillig # In extreme cases, even the backslash can be used as variable name.
     34  1.1  rillig # It needs to be doubled though.
     35  1.1  rillig .if ${:U1 2 3:@\\@x${${:Ux:S,x,\\,}}y@} != "x1y x2y x3y"
     36  1.1  rillig .  error
     37  1.1  rillig .endif
     38  1.1  rillig 
     39  1.1  rillig 
     40  1.1  rillig # The variable name can technically be empty, and in this situation
     41  1.1  rillig # the variable value cannot be accessed since the empty "variable"
     42  1.1  rillig # is protected to always return an empty string.
     43  1.1  rillig .if ${:U1 2 3:@@x${}y@} != "xy xy xy"
     44  1.1  rillig .  error
     45  1.1  rillig .endif
     46  1.1  rillig 
     47  1.1  rillig 
     48  1.1  rillig # The :@ modifier resolves the variables from the replacement text once more
     49  1.1  rillig # than expected.  In particular, it resolves _all_ variables from the scope,
     50  1.1  rillig # and not only the loop variable (in this case v).
     51  1.1  rillig SRCS=		source
     52  1.1  rillig CFLAGS.source=	before
     53  1.1  rillig ALL_CFLAGS:=	${SRCS:@src@${CFLAGS.${src}}@}	# note the ':='
     54  1.1  rillig CFLAGS.source+=	after
     55  1.1  rillig .if ${ALL_CFLAGS} != "before"
     56  1.1  rillig .  error
     57  1.1  rillig .endif
     58  1.1  rillig 
     59  1.1  rillig 
     60  1.1  rillig # In the following example, the modifier ':@' expands the '$$' to '$'.  This
     61  1.1  rillig # means that when the resulting expression is evaluated, these resulting '$'
     62  1.1  rillig # will be interpreted as starting a subexpression.
     63  1.1  rillig #
     64  1.1  rillig # The d means direct reference, the i means indirect reference.
     65  1.1  rillig RESOLVE=	${RES1} $${RES1}
     66  1.1  rillig RES1=		1d${RES2} 1i$${RES2}
     67  1.1  rillig RES2=		2d${RES3} 2i$${RES3}
     68  1.1  rillig RES3=		3
     69  1.1  rillig 
     70  1.1  rillig .if ${RESOLVE:@v@w${v}w@} != "w1d2d3w w2i3w w1i2d3 2i\${RES3}w w1d2d3 2i\${RES3} 1i\${RES2}w"
     71  1.1  rillig .  error
     72  1.1  rillig .endif
     73  1.1  rillig 
     74  1.1  rillig 
     75  1.1  rillig # Until 2020-07-20, the variable name of the :@ modifier could end with one
     76  1.1  rillig # or two dollar signs, which were silently ignored.
     77  1.1  rillig # There's no point in allowing a dollar sign in that position.
     78  1.1  rillig .if ${1 2 3:L:@v$@($v)@} != "(1) (2) (3)"
     79  1.1  rillig .  error
     80  1.1  rillig .endif
     81  1.1  rillig .if ${1 2 3:L:@v$$@($v)@} != "() () ()"
     82  1.1  rillig .  error
     83  1.1  rillig .endif
     84  1.1  rillig .if ${1 2 3:L:@v$$$@($v)@} != "() () ()"
     85  1.1  rillig .  error
     86  1.1  rillig .endif
     87  1.1  rillig 
     88  1.1  rillig 
     89  1.1  rillig # It may happen that there are nested :@ modifiers that use the same name for
     90  1.1  rillig # for the loop variable.  These modifiers influence each other.
     91  1.1  rillig #
     92  1.1  rillig # As of 2020-10-18, the :@ modifier is implemented by actually setting a
     93  1.1  rillig # variable in the scope of the expression and deleting it again after the
     94  1.1  rillig # loop.  This is different from the .for loops, which substitute the variable
     95  1.1  rillig # expression with ${:Uvalue}, leading to different unwanted side effects.
     96  1.1  rillig #
     97  1.1  rillig # To make the behavior more predictable, the :@ modifier should restore the
     98  1.1  rillig # loop variable to the value it had before the loop.  This would result in
     99  1.1  rillig # the string "1a b c1 2a b c2 3a b c3", making the two loops independent.
    100  1.1  rillig .if ${:U1 2 3:@i@$i${:Ua b c:@i@$i@}${i:Uu}@} != "1a b cu 2a b cu 3a b cu"
    101  1.1  rillig .  error
    102  1.1  rillig .endif
    103  1.1  rillig 
    104  1.1  rillig # During the loop, the variable is actually defined and nonempty.
    105  1.1  rillig # If the loop were implemented in the same way as the .for loop, the variable
    106  1.1  rillig # would be neither defined nor nonempty since all expressions of the form
    107  1.1  rillig # ${var} would have been replaced with ${:Uword} before evaluating them.
    108  1.1  rillig .if defined(var)
    109  1.1  rillig .  error
    110  1.1  rillig .endif
    111  1.1  rillig .if ${:Uword:@var@${defined(var):?def:undef} ${empty(var):?empty:nonempty}@} \
    112  1.1  rillig     != "def nonempty"
    113  1.1  rillig .  error
    114  1.1  rillig .endif
    115  1.1  rillig .if defined(var)
    116  1.1  rillig .  error
    117  1.1  rillig .endif
    118  1.1  rillig 
    119  1.1  rillig all: .PHONY
    120