1 # $NetBSD: directive-for.mk,v 1.9 2020/11/15 20:20:58 rillig Exp $
3 # Tests for the .for directive.
5 # TODO: Describe naming conventions for the loop variables.
8 # .for _FILE_ in values
9 # .for .FILE. in values
12 # Using the .for loop, lists of values can be produced.
13 # In simple cases, the :@var@${var}@ variable modifier can be used to
14 # reach the same effects.
20 .if ${NUMBERS} != "1 2 3"
24 # The .for loop also works for multiple iteration variables.
25 # This is something that the variable modifier :@ cannot do.
26 .for name value in VARNAME value NAME2 value2
29 .if ${VARNAME} != "value" || ${NAME2} != "value2"
33 # The .for loop splits the items at whitespace, taking quotes into account,
34 # just like the :M or :S variable modifiers.
36 # Until 2012-06-03, it had split the items exactly at whitespace, without
37 # taking the quotes into account. This had resulted in 10 words.
40 .for var in one t\ w\ o "three three" 'four four' `five six`
47 # In the body of the .for loop, the iteration variables can be accessed
48 # like normal variables, even though they are not really variables.
50 # Instead, the expression ${var} is transformed into ${:U1}, ${:U2} and so
51 # on, before the loop body is evaluated.
53 # A notable effect of this implementation technique is that the .for
54 # iteration variables and the normal global variables live in separate
55 # namespaces and do not influence each other.
59 .for var var2 in 1 2 3 4
61 .if ${var} != "value before"
62 . warning After the .for loop, var must still have its original value.
64 .if ${var2} != "value before"
65 . warning After the .for loop, var2 must still have its original value.
68 # Everything from the paragraph above also applies if the loop body is
69 # empty, even if there is no actual iteration since the loop items are
74 .for var var2 in ${:U}
76 .if ${var} != "value before"
77 . warning After the .for loop, var must still have its original value.
79 .if ${var2} != "value before"
80 . warning After the .for loop, var2 must still have its original value.
83 # Until 2008-12-21, the values of the iteration variables were simply
84 # inserted as plain text and then parsed as usual, which made it possible
85 # to achieve all kinds of strange effects.
87 # Before that date, the .for loop expanded to:
89 # Since that date, the .for loop expands to:
90 # EXPANSION${:U+}= value
95 EXPANSION${plus}= value
97 .if ${EXPANSION} != "before"
98 . error This must be a make from before 2009.
100 .if ${EXPANSION+} != "value"
101 . error This must be a make from before 2009.
104 # When the outer .for loop is expanded, it sees the expression ${i} and
105 # expands it. The inner loop then has nothing more to expand.
112 # From https://gnats.netbsd.org/29985.
114 # Until 2008-12-21, the .for loop was expanded by replacing the variable
115 # value literally in the body. This could lead to situations where the
116 # characters from the variable value were interpreted as markup rather than
119 # Until 2012-06-03, the .for loop had split the words at whitespace, without
120 # taking quotes into account. This made it possible to have variable values
121 # like "a:\ a:\file.txt" that ended in a single backslash. Since then, the
122 # variable values have been replaced with expressions of the form ${:U...},
123 # which are not interpreted as code anymore.
125 # As of 2020-09-22, a comment in for.c says that it may be possible to
126 # produce an "unwanted substitution", but there is no demonstration code yet.
128 # The above changes prevent a backslash at the end of a word from being
129 # interpreted as part of the code. Because of this, the trailingBackslash
130 # hack in Var_Subst is no longer needed and as of 2020-09-22, has been
132 .for path in a:\ a:\file.txt d:\\ d:\\file.txt
136 # Ensure that braces and parentheses are properly escaped by the .for loop.
137 # Each line must print the same word 3 times.
139 .for v in ( [ { ) ] } (()) [[]] {{}} )( ][ }{
143 # As of 2020-10-25, the variable names may contain arbitrary characters,
144 # except for whitespace. This allows for creative side effects. Hopefully
145 # nobody is misusing this "feature".
147 .for var:Q in value "quoted"
148 . info ${var} ${var:Q} ${var:Q:Q}