Home | History | Annotate | Line # | Download | only in unit-tests
directive-for.mk revision 1.6
      1 # $NetBSD: directive-for.mk,v 1.6 2020/10/24 08:50:17 rillig Exp $
      2 #
      3 # Tests for the .for directive.
      4 
      5 # Using the .for loop, lists of values can be produced.
      6 # In simple cases, the :@var@${var}@ variable modifier can be used to
      7 # reach the same effects.
      8 #
      9 .undef NUMBERS
     10 .for num in 1 2 3
     11 NUMBERS+=	${num}
     12 .endfor
     13 .if ${NUMBERS} != "1 2 3"
     14 .  error
     15 .endif
     16 
     17 # The .for loop also works for multiple iteration variables.
     18 .for name value in VARNAME value NAME2 value2
     19 ${name}=	${value}
     20 .endfor
     21 .if ${VARNAME} != "value" || ${NAME2} != "value2"
     22 .  error
     23 .endif
     24 
     25 # The .for loop splits the items at whitespace, taking quotes into account,
     26 # just like the :M or :S variable modifiers.
     27 #
     28 # Until 2012-06-03, it had split the items exactly at whitespace, without
     29 # taking the quotes into account.
     30 #
     31 .undef WORDS
     32 .for var in one t\ w\ o "three three" 'four four' `five six`
     33 WORDS+=	counted
     34 .endfor
     35 .if ${WORDS:[#]} != 6
     36 .  error
     37 .endif
     38 
     39 # In the body of the .for loop, the iteration variables can be accessed
     40 # like normal variables, even though they are not really variables.
     41 #
     42 # Instead, the expression ${var} is transformed into ${:U1}, ${:U2} and so
     43 # on, before the loop body is evaluated.
     44 #
     45 # A notable effect of this implementation technique is that the .for
     46 # iteration variables and the normal global variables live in separate
     47 # namespaces and do not influence each other.
     48 #
     49 var=	value before
     50 var2=	value before
     51 .for var var2 in 1 2 3 4
     52 .endfor
     53 .if ${var} != "value before"
     54 .  warning After the .for loop, var must still have its original value.
     55 .endif
     56 .if ${var2} != "value before"
     57 .  warning After the .for loop, var2 must still have its original value.
     58 .endif
     59 
     60 # Everything from the paragraph above also applies if the loop body is
     61 # empty, even if there is no actual iteration since the loop items are
     62 # also empty.
     63 #
     64 var=	value before
     65 var2=	value before
     66 .for var var2 in ${:U}
     67 .endfor
     68 .if ${var} != "value before"
     69 .  warning After the .for loop, var must still have its original value.
     70 .endif
     71 .if ${var2} != "value before"
     72 .  warning After the .for loop, var2 must still have its original value.
     73 .endif
     74 
     75 # Until 2008-12-21, the values of the iteration variables were simply
     76 # inserted as plain text and then parsed as usual, which made it possible
     77 # to achieve all kinds of strange effects.
     78 #
     79 # Before that date, the .for loop expanded to:
     80 #	EXPANSION+= value
     81 # Since that date, the .for loop expands to:
     82 #	EXPANSION${:U+}= value
     83 #
     84 EXPANSION=		before
     85 EXPANSION+ =		before
     86 .for plus in +
     87 EXPANSION${plus}=	value
     88 .endfor
     89 .if ${EXPANSION} != "before"
     90 .  error This must be a make from before 2009.
     91 .endif
     92 .if ${EXPANSION+} != "value"
     93 .  error This must be a make from before 2009.
     94 .endif
     95 
     96 # When the outer .for loop is expanded, it sees the expression ${i} and
     97 # expands it.  The inner loop then has nothing more to expand.
     98 .for i in outer
     99 .  for i in inner
    100 .    info ${i}
    101 .  endfor
    102 .endfor
    103 
    104 # From https://gnats.netbsd.org/29985.
    105 #
    106 # Until 2008-12-21, the .for loop was expanded by replacing the variable
    107 # value literally in the body.  This could lead to situations where the
    108 # characters from the variable value were interpreted as markup rather than
    109 # plain text.
    110 #
    111 # Until 2012-06-03, the .for loop had split the words at whitespace, without
    112 # taking quotes into account.  This made it possible to have variable values
    113 # like "a:\ a:\file.txt" that ended in a single backslash.  Since then, the
    114 # variable values have been replaced with expressions of the form ${:U...},
    115 # which are not interpreted as code anymore.
    116 #
    117 # As of 2020-09-22, a comment in for.c says that it may be possible to
    118 # produce an "unwanted substitution", but there is no demonstration code yet.
    119 #
    120 # The above changes prevent a backslash at the end of a word from being
    121 # interpreted as part of the code.  Because of this, the trailingBackslash
    122 # hack in Var_Subst is no longer needed and as of 2020-09-22, has been
    123 # removed.
    124 .for path in a:\ a:\file.txt d:\\ d:\\file.txt
    125 .  info ${path}
    126 .endfor
    127 
    128 all:
    129 	@:;
    130