xref: /freebsd/contrib/bmake/unit-tests/directive-for.mk (revision 759b177aecbfc49ebc900739954ac56b1aa5fc53)
1*759b177aSSimon J. Gerraty# $NetBSD: directive-for.mk,v 1.30 2025/03/30 16:43:10 rillig Exp $
22c3632d1SSimon J. Gerraty#
32c3632d1SSimon J. Gerraty# Tests for the .for directive.
4e2eeea75SSimon J. Gerraty#
5e2eeea75SSimon J. Gerraty# TODO: Describe naming conventions for the loop variables.
6e2eeea75SSimon J. Gerraty#	.for f in values
7e2eeea75SSimon J. Gerraty#	.for file in values
8e2eeea75SSimon J. Gerraty#	.for _FILE_ in values
9e2eeea75SSimon J. Gerraty#	.for .FILE. in values
10e2eeea75SSimon J. Gerraty#	.for _f_ in values
112c3632d1SSimon J. Gerraty#
12c1d01b5fSSimon J. Gerraty# See also:
13c1d01b5fSSimon J. Gerraty#	varmod-loop.mk		The ':@var@...@' modifier
14c1d01b5fSSimon J. Gerraty
15c1d01b5fSSimon J. Gerraty# A typical use case for a .for loop is to populate a variable with a list of
16c1d01b5fSSimon J. Gerraty# values depending on other variables.  In simple cases, the same effect can
17c1d01b5fSSimon J. Gerraty# be achieved using the ':@var@${var}@' modifier.
182c3632d1SSimon J. Gerraty.undef NUMBERS
192c3632d1SSimon J. Gerraty.for num in 1 2 3
202c3632d1SSimon J. GerratyNUMBERS+=	${num}
212c3632d1SSimon J. Gerraty.endfor
222c3632d1SSimon J. Gerraty.if ${NUMBERS} != "1 2 3"
232c3632d1SSimon J. Gerraty.  error
242c3632d1SSimon J. Gerraty.endif
252c3632d1SSimon J. Gerraty
26c1d01b5fSSimon J. Gerraty
272c3632d1SSimon J. Gerraty# The .for loop also works for multiple iteration variables.
28d5e0a182SSimon J. Gerraty# This is something that the modifier :@ cannot do as easily.
292c3632d1SSimon J. Gerraty.for name value in VARNAME value NAME2 value2
302c3632d1SSimon J. Gerraty${name}=	${value}
312c3632d1SSimon J. Gerraty.endfor
322c3632d1SSimon J. Gerraty.if ${VARNAME} != "value" || ${NAME2} != "value2"
332c3632d1SSimon J. Gerraty.  error
342c3632d1SSimon J. Gerraty.endif
352c3632d1SSimon J. Gerraty
36c1d01b5fSSimon J. Gerraty
372c3632d1SSimon J. Gerraty# The .for loop splits the items at whitespace, taking quotes into account,
38c1d01b5fSSimon J. Gerraty# just like the :M or :S modifiers.
392c3632d1SSimon J. Gerraty#
40c1d01b5fSSimon J. Gerraty# Until 2012-06-03, the .for loop had split the items exactly at whitespace,
41c1d01b5fSSimon J. Gerraty# without taking the quotes into account.  This had resulted in 10 words.
422c3632d1SSimon J. Gerraty.undef WORDS
432c3632d1SSimon J. Gerraty.for var in one t\ w\ o "three three" 'four four' `five six`
442c3632d1SSimon J. GerratyWORDS+=	counted
452c3632d1SSimon J. Gerraty.endfor
462c3632d1SSimon J. Gerraty.if ${WORDS:[#]} != 6
472c3632d1SSimon J. Gerraty.  error
482c3632d1SSimon J. Gerraty.endif
492c3632d1SSimon J. Gerraty
50c1d01b5fSSimon J. Gerraty
512c3632d1SSimon J. Gerraty# In the body of the .for loop, the iteration variables can be accessed
522c3632d1SSimon J. Gerraty# like normal variables, even though they are not really variables.
532c3632d1SSimon J. Gerraty#
54c1d01b5fSSimon J. Gerraty# Instead, before interpreting the body of the .for loop, the body is
55c1d01b5fSSimon J. Gerraty# generated by replacing each expression ${var} with ${:U1}, ${:U2} and so
56c1d01b5fSSimon J. Gerraty# on.
572c3632d1SSimon J. Gerraty#
58c1d01b5fSSimon J. Gerraty# A noticeable effect of this implementation technique is that the .for
592c3632d1SSimon J. Gerraty# iteration variables and the normal global variables live in separate
60c1d01b5fSSimon J. Gerraty# namespaces and do not influence each other.  The "scope" of the .for loop
61c1d01b5fSSimon J. Gerraty# variables is restricted to the current makefile, it does not reach over to
62c1d01b5fSSimon J. Gerraty# any included makefiles.
632c3632d1SSimon J. Gerratyvar=	value before
642c3632d1SSimon J. Gerratyvar2=	value before
652c3632d1SSimon J. Gerraty.for var var2 in 1 2 3 4
662c3632d1SSimon J. Gerraty.endfor
672c3632d1SSimon J. Gerraty.if ${var} != "value before"
682c3632d1SSimon J. Gerraty.  warning After the .for loop, var must still have its original value.
692c3632d1SSimon J. Gerraty.endif
702c3632d1SSimon J. Gerraty.if ${var2} != "value before"
712c3632d1SSimon J. Gerraty.  warning After the .for loop, var2 must still have its original value.
722c3632d1SSimon J. Gerraty.endif
732c3632d1SSimon J. Gerraty
742c3632d1SSimon J. Gerraty# Everything from the paragraph above also applies if the loop body is
75c1d01b5fSSimon J. Gerraty# empty.  In this particular example, the items to be iterated are empty as
76c1d01b5fSSimon J. Gerraty# well.
772c3632d1SSimon J. Gerratyvar=	value before
782c3632d1SSimon J. Gerratyvar2=	value before
792c3632d1SSimon J. Gerraty.for var var2 in ${:U}
802c3632d1SSimon J. Gerraty.endfor
812c3632d1SSimon J. Gerraty.if ${var} != "value before"
822c3632d1SSimon J. Gerraty.  warning After the .for loop, var must still have its original value.
832c3632d1SSimon J. Gerraty.endif
842c3632d1SSimon J. Gerraty.if ${var2} != "value before"
852c3632d1SSimon J. Gerraty.  warning After the .for loop, var2 must still have its original value.
862c3632d1SSimon J. Gerraty.endif
872c3632d1SSimon J. Gerraty
88148ee845SSimon J. Gerraty# Before for.c 1.39 from 2008-12-21, the values of the iteration variables
89148ee845SSimon J. Gerraty# were simply inserted as plain text and then parsed as usual, which made it
90148ee845SSimon J. Gerraty# possible to achieve all kinds of strange effects, such as generating '.if'
91c1d01b5fSSimon J. Gerraty# directives or inserting '$' characters in random places, thereby changing
92c1d01b5fSSimon J. Gerraty# how following '$' are interpreted.
932c3632d1SSimon J. Gerraty#
94c1d01b5fSSimon J. Gerraty# Before that date, the .for loop below expanded to:
952c3632d1SSimon J. Gerraty#	EXPANSION+= value
96c1d01b5fSSimon J. Gerraty# Since that date, the .for loop below expands to:
972c3632d1SSimon J. Gerraty#	EXPANSION${:U+}= value
982c3632d1SSimon J. Gerraty#
992c3632d1SSimon J. GerratyEXPANSION=		before
1002c3632d1SSimon J. GerratyEXPANSION+ =		before
1012c3632d1SSimon J. Gerraty.for plus in +
1022c3632d1SSimon J. GerratyEXPANSION${plus}=	value
1032c3632d1SSimon J. Gerraty.endfor
1042c3632d1SSimon J. Gerraty.if ${EXPANSION} != "before"
1052c3632d1SSimon J. Gerraty.  error This must be a make from before 2009.
1062c3632d1SSimon J. Gerraty.endif
1072c3632d1SSimon J. Gerraty.if ${EXPANSION+} != "value"
1082c3632d1SSimon J. Gerraty.  error This must be a make from before 2009.
1092c3632d1SSimon J. Gerraty.endif
1102c3632d1SSimon J. Gerraty
111956e45f6SSimon J. Gerraty# When the outer .for loop is expanded, it sees the expression ${i} and
112c1d01b5fSSimon J. Gerraty# expands it.  The inner loop then only sees the expression ${:Uouter} and
113c1d01b5fSSimon J. Gerraty# has nothing more to expand.
114956e45f6SSimon J. Gerraty.for i in outer
115956e45f6SSimon J. Gerraty.  for i in inner
116c1d01b5fSSimon J. Gerraty# expect+1: outer
117956e45f6SSimon J. Gerraty.    info ${i}
118956e45f6SSimon J. Gerraty.  endfor
119956e45f6SSimon J. Gerraty.endfor
120956e45f6SSimon J. Gerraty
121c1d01b5fSSimon J. Gerraty
122956e45f6SSimon J. Gerraty# From https://gnats.netbsd.org/29985.
123956e45f6SSimon J. Gerraty#
124956e45f6SSimon J. Gerraty# Until 2008-12-21, the .for loop was expanded by replacing the variable
125956e45f6SSimon J. Gerraty# value literally in the body.  This could lead to situations where the
126956e45f6SSimon J. Gerraty# characters from the variable value were interpreted as markup rather than
127956e45f6SSimon J. Gerraty# plain text.
128956e45f6SSimon J. Gerraty#
129956e45f6SSimon J. Gerraty# Until 2012-06-03, the .for loop had split the words at whitespace, without
130956e45f6SSimon J. Gerraty# taking quotes into account.  This made it possible to have variable values
131956e45f6SSimon J. Gerraty# like "a:\ a:\file.txt" that ended in a single backslash.  Since then, the
132956e45f6SSimon J. Gerraty# variable values have been replaced with expressions of the form ${:U...},
133956e45f6SSimon J. Gerraty# which are not interpreted as code anymore.
134956e45f6SSimon J. Gerraty.for path in a:\ a:\file.txt d:\\ d:\\file.txt
13522619282SSimon J. Gerraty# expect+3: a:\ a:\file.txt
13622619282SSimon J. Gerraty# expect+2: d:\\
13722619282SSimon J. Gerraty# expect+1: d:\\file.txt
138956e45f6SSimon J. Gerraty.  info ${path}
139956e45f6SSimon J. Gerraty.endfor
140c1d01b5fSSimon J. Gerraty
141956e45f6SSimon J. Gerraty
142956e45f6SSimon J. Gerraty# Ensure that braces and parentheses are properly escaped by the .for loop.
143956e45f6SSimon J. Gerraty# Each line must print the same word 3 times.
1449f45a3c8SSimon J. Gerraty# See ForLoop_SubstBody.
145956e45f6SSimon J. Gerraty.for v in ( [ { ) ] } (()) [[]] {{}} )( ][ }{
14622619282SSimon J. Gerraty# expect+12: ( ( (
14722619282SSimon J. Gerraty# expect+11: [ [ [
14822619282SSimon J. Gerraty# expect+10: { { {
14922619282SSimon J. Gerraty# expect+9: ) ) )
15022619282SSimon J. Gerraty# expect+8: ] ] ]
15122619282SSimon J. Gerraty# expect+7: } } }
15222619282SSimon J. Gerraty# expect+6: (()) (()) (())
15322619282SSimon J. Gerraty# expect+5: [[]] [[]] [[]]
15422619282SSimon J. Gerraty# expect+4: {{}} {{}} {{}}
15522619282SSimon J. Gerraty# expect+3: )( )( )(
15622619282SSimon J. Gerraty# expect+2: ][ ][ ][
15722619282SSimon J. Gerraty# expect+1: }{ }{ }{
158956e45f6SSimon J. Gerraty.  info $v ${v} $(v)
159956e45f6SSimon J. Gerraty.endfor
160956e45f6SSimon J. Gerraty
161c1d01b5fSSimon J. Gerraty# Before 2023-05-09, the variable names could contain arbitrary characters,
162c1d01b5fSSimon J. Gerraty# except for whitespace, allowing for creative side effects, as usual for
163c1d01b5fSSimon J. Gerraty# arbitrary code injection.
164956e45f6SSimon J. Gerratyvar=	outer
165c1d01b5fSSimon J. Gerraty# expect+1: invalid character ':' in .for loop variable name
166956e45f6SSimon J. Gerraty.for var:Q in value "quoted"
167c1d01b5fSSimon J. Gerraty.  info <${var}> <${var:Q}> <${var:Q:Q}>
168c1d01b5fSSimon J. Gerraty.endfor
169c1d01b5fSSimon J. Gerraty
170c1d01b5fSSimon J. Gerraty# Before 2023-05-09, when variable names could contain '$', the short
171c1d01b5fSSimon J. Gerraty# expression '$$' was preserved, the long expressions were substituted.
172c1d01b5fSSimon J. Gerraty# expect+1: invalid character '$' in .for loop variable name
173c1d01b5fSSimon J. Gerraty.for $ in value
174c1d01b5fSSimon J. Gerraty.  info <$$> <${$}> <$($)>
175c1d01b5fSSimon J. Gerraty.endfor
176c1d01b5fSSimon J. Gerraty
177c1d01b5fSSimon J. Gerraty
178c1d01b5fSSimon J. Gerraty# https://gnats.netbsd.org/53146 mentions the idea of using a dynamic
179c1d01b5fSSimon J. Gerraty# variable name in .for loops, based on some other variable.  The .for loops
180c1d01b5fSSimon J. Gerraty# are already tricky enough to understand in detail, even without this
181c1d01b5fSSimon J. Gerraty# possibility, therefore the variable names are restricted to using harmless
182c1d01b5fSSimon J. Gerraty# characters only.
183c1d01b5fSSimon J. GerratyINDIRECT=	direct
184c1d01b5fSSimon J. Gerraty# expect+1: invalid character '$' in .for loop variable name
185c1d01b5fSSimon J. Gerraty.for $(INDIRECT) in value
186c1d01b5fSSimon J. Gerraty# If the variable name could be chosen dynamically, the iteration variable
187c1d01b5fSSimon J. Gerraty# might have been 'direct', thereby expanding the expression '${direct}'.
188c1d01b5fSSimon J. Gerraty.  info <$(INDIRECT)> <$(direct)> <$($(INDIRECT))>
189956e45f6SSimon J. Gerraty.endfor
190956e45f6SSimon J. Gerraty
19106b9b3e0SSimon J. Gerraty
192d5e0a182SSimon J. Gerraty# Regular global variables and the "variables" from the .for loop don't
193d5e0a182SSimon J. Gerraty# interfere with each other.  In the following snippet, the variable 'DIRECT'
194d5e0a182SSimon J. Gerraty# is used both as a global variable, as well as an iteration variable in the
195d5e0a182SSimon J. Gerraty# .for loop.  The expression '${INDIRECT}' refers to the global variable, not
196d5e0a182SSimon J. Gerraty# to the one from the .for loop.
197d5e0a182SSimon J. GerratyDIRECT=		global
198d5e0a182SSimon J. GerratyINDIRECT=	${DIRECT}
199d5e0a182SSimon J. Gerraty.for DIRECT in iteration
200d5e0a182SSimon J. Gerraty.  if "${DIRECT} ${INDIRECT}" != "iteration global"
201d5e0a182SSimon J. Gerraty.    error
202d5e0a182SSimon J. Gerraty.  endif
203d5e0a182SSimon J. Gerraty.endfor
204d5e0a182SSimon J. Gerraty
205d5e0a182SSimon J. Gerraty
2069f45a3c8SSimon J. Gerraty# An empty list of variables to the left of the 'in' is a parse error.
2079f45a3c8SSimon J. Gerraty.for in value			# expect+0: no iteration variables in for
208c1d01b5fSSimon J. Gerraty.  error
209c1d01b5fSSimon J. Gerraty.endfor
2109f45a3c8SSimon J. Gerraty
2119f45a3c8SSimon J. Gerraty# An empty list of iteration values to the right of the 'in' is accepted.
2129f45a3c8SSimon J. Gerraty# Unlike in the shell, it is not a parse error.
2139f45a3c8SSimon J. Gerraty.for var in
2149f45a3c8SSimon J. Gerraty.  error
2159f45a3c8SSimon J. Gerraty.endfor
2169f45a3c8SSimon J. Gerraty
2179f45a3c8SSimon J. Gerraty# If the iteration values become empty after expanding the expressions, the
2189f45a3c8SSimon J. Gerraty# body of the loop is not evaluated.  It is not a parse error.
2199f45a3c8SSimon J. Gerraty.for var in ${:U}
2209f45a3c8SSimon J. Gerraty.  error
2219f45a3c8SSimon J. Gerraty.endfor
2229f45a3c8SSimon J. Gerraty
2239f45a3c8SSimon J. Gerraty
2249f45a3c8SSimon J. Gerraty# The loop body can be empty.
2259f45a3c8SSimon J. Gerraty.for var in 1 2 3
2269f45a3c8SSimon J. Gerraty.endfor
2279f45a3c8SSimon J. Gerraty
2289f45a3c8SSimon J. Gerraty
2299f45a3c8SSimon J. Gerraty# A mismatched .if inside a .for loop is detected each time when the loop body
2309f45a3c8SSimon J. Gerraty# is processed.
2319f45a3c8SSimon J. Gerraty.for var in value
2329f45a3c8SSimon J. Gerraty.  if 0
2339f45a3c8SSimon J. Gerraty.endfor				# expect+0: 1 open conditional
2349f45a3c8SSimon J. Gerraty
2359f45a3c8SSimon J. Gerraty# If there are no iteration values, the loop body is not processed, and the
2369f45a3c8SSimon J. Gerraty# check for mismatched conditionals is not performed.
2379f45a3c8SSimon J. Gerraty.for var in ${:U}
2389f45a3c8SSimon J. Gerraty.  if 0
2399f45a3c8SSimon J. Gerraty.endfor
2409f45a3c8SSimon J. Gerraty
2419f45a3c8SSimon J. Gerraty
2429f45a3c8SSimon J. Gerraty# When a .for without the corresponding .endfor occurs in an inactive branch
2439f45a3c8SSimon J. Gerraty# of an .if, the .for directive is just skipped, it does not even need a
2449f45a3c8SSimon J. Gerraty# corresponding .endfor.  In other words, the behavior of the parser depends
2459f45a3c8SSimon J. Gerraty# on the actual values of the conditions in the .if clauses.
2469f45a3c8SSimon J. Gerraty.if 0
2479f45a3c8SSimon J. Gerraty.  for var in value		# does not need a corresponding .endfor
2489f45a3c8SSimon J. Gerraty.endif
2499f45a3c8SSimon J. Gerraty.endfor				# expect+0: for-less endfor
2509f45a3c8SSimon J. Gerraty.endif				# expect+0: if-less endif
2519f45a3c8SSimon J. Gerraty
2529f45a3c8SSimon J. Gerraty
2539f45a3c8SSimon J. Gerraty# When a .for without the corresponding .endfor occurs in an active branch of
2549f45a3c8SSimon J. Gerraty# an .if, the parser just counts the number of .for and .endfor directives,
2559f45a3c8SSimon J. Gerraty# without looking at any other directives.
2569f45a3c8SSimon J. Gerraty.if 1
2579f45a3c8SSimon J. Gerraty.  for var in value
2589f45a3c8SSimon J. Gerraty.    endif			# expect+0: if-less endif
2599f45a3c8SSimon J. Gerraty.  endfor			# no 'for-less endfor'
2609f45a3c8SSimon J. Gerraty.endif				# no 'if-less endif'
2619f45a3c8SSimon J. Gerraty
2629f45a3c8SSimon J. Gerraty
263c1d01b5fSSimon J. Gerraty# Before for.c 1.172 from 2023-05-08, when make parsed a .for loop, it
264c1d01b5fSSimon J. Gerraty# assumed that there was no line continuation between the '.' and the 'for'
265c1d01b5fSSimon J. Gerraty# or 'endfor', as there is no practical reason to break the line at this
266c1d01b5fSSimon J. Gerraty# point.
267c1d01b5fSSimon J. Gerraty#
268c1d01b5fSSimon J. Gerraty# When make scanned the outer .for loop, it did not recognize the inner .for
269c1d01b5fSSimon J. Gerraty# loop as such and instead treated it as an unknown directive.  The body of
270c1d01b5fSSimon J. Gerraty# the outer .for loop thus ended above the '.endfor'.
271c1d01b5fSSimon J. Gerraty#
272c1d01b5fSSimon J. Gerraty# When make scanned the inner .for loop, it did not recognize the inner
273c1d01b5fSSimon J. Gerraty# .endfor as such, which led to a parse error 'Unexpected end of file in .for
274c1d01b5fSSimon J. Gerraty# loop' from the '.endfor' line, followed by a second parse error 'for-less
275c1d01b5fSSimon J. Gerraty# .endfor' from the '.\\n endfor' line.
2769f45a3c8SSimon J. Gerraty.MAKEFLAGS: -df
2779f45a3c8SSimon J. Gerraty.for outer in o
2789f45a3c8SSimon J. Gerraty.\
2799f45a3c8SSimon J. Gerraty   for inner in i
2809f45a3c8SSimon J. Gerraty.\
2819f45a3c8SSimon J. Gerraty   endfor
2829f45a3c8SSimon J. Gerraty.endfor
2839f45a3c8SSimon J. Gerraty.MAKEFLAGS: -d0
2844fde40d9SSimon J. Gerraty
2854fde40d9SSimon J. Gerraty
2864fde40d9SSimon J. Gerraty# When there is a variable definition 'scope=cmdline' from the command line
2874fde40d9SSimon J. Gerraty# (which has higher precedence than global variables) and a .for loop iterates
2884fde40d9SSimon J. Gerraty# over a variable of the same name, the expression '${scope}' expands to the
2894fde40d9SSimon J. Gerraty# value from the .for loop.  This is because when the body of the .for loop is
2904fde40d9SSimon J. Gerraty# expanded, the expression '${scope}' is textually replaced with ${:Uloop}',
2914fde40d9SSimon J. Gerraty# without resolving any other variable names (ForLoop_SubstBody).  Later, when
2924fde40d9SSimon J. Gerraty# the body of the .for loop is actually interpreted, the body text doesn't
2934fde40d9SSimon J. Gerraty# contain the word 'scope' anymore.
2944fde40d9SSimon J. Gerraty.MAKEFLAGS: scope=cmdline
2954fde40d9SSimon J. Gerraty.for scope in loop
2964fde40d9SSimon J. Gerraty.  if ${scope} != "loop"
2974fde40d9SSimon J. Gerraty.    error
2984fde40d9SSimon J. Gerraty.  endif
2994fde40d9SSimon J. Gerraty.endfor
300c1d01b5fSSimon J. Gerraty
301c1d01b5fSSimon J. Gerraty
302c1d01b5fSSimon J. Gerraty# Since at least 1993, iteration stops at the first newline.
303c1d01b5fSSimon J. Gerraty# Back then, the .newline variable didn't exist, therefore it was unlikely
304c1d01b5fSSimon J. Gerraty# that a newline ever occurred.
305c1d01b5fSSimon J. Gerraty.for var in a${.newline}b${.newline}c
30622619282SSimon J. Gerraty# expect+1: newline-item=(a)
307c1d01b5fSSimon J. Gerraty.  info newline-item=(${var})
308c1d01b5fSSimon J. Gerraty.endfor
309