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