directive-for.mk revision 1.3 1 1.3 rillig # $NetBSD: directive-for.mk,v 1.3 2020/09/14 18:49:24 rillig Exp $
2 1.1 rillig #
3 1.1 rillig # Tests for the .for directive.
4 1.1 rillig
5 1.1 rillig # Using the .for loop, lists of values can be produced.
6 1.1 rillig # In simple cases, the :@var@${var}@ variable modifier can be used to
7 1.1 rillig # reach the same effects.
8 1.1 rillig #
9 1.1 rillig .undef NUMBERS
10 1.1 rillig .for num in 1 2 3
11 1.1 rillig NUMBERS+= ${num}
12 1.1 rillig .endfor
13 1.1 rillig .if ${NUMBERS} != "1 2 3"
14 1.1 rillig . error
15 1.1 rillig .endif
16 1.1 rillig
17 1.1 rillig # The .for loop also works for multiple iteration variables.
18 1.1 rillig .for name value in VARNAME value NAME2 value2
19 1.1 rillig ${name}= ${value}
20 1.1 rillig .endfor
21 1.1 rillig .if ${VARNAME} != "value" || ${NAME2} != "value2"
22 1.1 rillig . error
23 1.1 rillig .endif
24 1.1 rillig
25 1.1 rillig # The .for loop splits the items at whitespace, taking quotes into account,
26 1.1 rillig # just like the :M or :S variable modifiers.
27 1.1 rillig #
28 1.1 rillig # Until 2012-06-03, it had split the items exactly at whitespace, without
29 1.1 rillig # taking the quotes into account.
30 1.1 rillig #
31 1.1 rillig .undef WORDS
32 1.1 rillig .for var in one t\ w\ o "three three" 'four four' `five six`
33 1.1 rillig WORDS+= counted
34 1.1 rillig .endfor
35 1.1 rillig .if ${WORDS:[#]} != 6
36 1.1 rillig . error
37 1.1 rillig .endif
38 1.1 rillig
39 1.1 rillig # In the body of the .for loop, the iteration variables can be accessed
40 1.1 rillig # like normal variables, even though they are not really variables.
41 1.1 rillig #
42 1.1 rillig # Instead, the expression ${var} is transformed into ${:U1}, ${:U2} and so
43 1.1 rillig # on, before the loop body is evaluated.
44 1.1 rillig #
45 1.1 rillig # A notable effect of this implementation technique is that the .for
46 1.1 rillig # iteration variables and the normal global variables live in separate
47 1.1 rillig # namespaces and do not influence each other.
48 1.1 rillig #
49 1.1 rillig var= value before
50 1.1 rillig var2= value before
51 1.1 rillig .for var var2 in 1 2 3 4
52 1.1 rillig .endfor
53 1.1 rillig .if ${var} != "value before"
54 1.2 rillig . warning After the .for loop, var must still have its original value.
55 1.1 rillig .endif
56 1.1 rillig .if ${var2} != "value before"
57 1.2 rillig . warning After the .for loop, var2 must still have its original value.
58 1.1 rillig .endif
59 1.1 rillig
60 1.1 rillig # Everything from the paragraph above also applies if the loop body is
61 1.1 rillig # empty, even if there is no actual iteration since the loop items are
62 1.1 rillig # also empty.
63 1.1 rillig #
64 1.1 rillig var= value before
65 1.1 rillig var2= value before
66 1.1 rillig .for var var2 in ${:U}
67 1.1 rillig .endfor
68 1.1 rillig .if ${var} != "value before"
69 1.2 rillig . warning After the .for loop, var must still have its original value.
70 1.1 rillig .endif
71 1.1 rillig .if ${var2} != "value before"
72 1.2 rillig . warning After the .for loop, var2 must still have its original value.
73 1.1 rillig .endif
74 1.1 rillig
75 1.1 rillig # Until 2008-12-21, the values of the iteration variables were simply
76 1.1 rillig # inserted as plain text and then parsed as usual, which made it possible
77 1.1 rillig # to achieve all kinds of strange effects.
78 1.1 rillig #
79 1.1 rillig # Before that date, the .for loop expanded to:
80 1.1 rillig # EXPANSION+= value
81 1.1 rillig # Since that date, the .for loop expands to:
82 1.1 rillig # EXPANSION${:U+}= value
83 1.1 rillig #
84 1.1 rillig EXPANSION= before
85 1.1 rillig EXPANSION+ = before
86 1.1 rillig .for plus in +
87 1.1 rillig EXPANSION${plus}= value
88 1.1 rillig .endfor
89 1.1 rillig .if ${EXPANSION} != "before"
90 1.1 rillig . error This must be a make from before 2009.
91 1.1 rillig .endif
92 1.1 rillig .if ${EXPANSION+} != "value"
93 1.1 rillig . error This must be a make from before 2009.
94 1.1 rillig .endif
95 1.1 rillig
96 1.3 rillig # When the outer .for loop is expanded, it sees the expression ${i} and
97 1.3 rillig # expands it. The inner loop then has nothing more to expand.
98 1.3 rillig .for i in outer
99 1.3 rillig . for i in inner
100 1.3 rillig . info ${i}
101 1.3 rillig . endfor
102 1.3 rillig .endfor
103 1.3 rillig
104 1.1 rillig all:
105 1.1 rillig @:;
106