Debian Codegraph @

tests/scripts/functions/foreach

raw
1 # -*-perl-*-
2 # $Id$
3
4 $description = "Test the foreach function.";
5
6 $details = "This is a test of the foreach function in gnu make.
7 This function starts with a space separated list of
8 names and a variable. Each name in the list is substituted
9 into the variable and the given text evaluated. The general
10 form of the command is $(foreach var,\$list,\$text). Several
11 types of foreach loops are tested\n";
12
13
14 # TEST 0
15
16 # Set an environment variable that we can test in the makefile.
17 $ENV{FOOFOO} = 'foo foo';
18
19 run_make_test("space = ' '".'
20 null :=
21 auto_var = udef space CC null FOOFOO MAKE foo CFLAGS WHITE @ <
22 foo = bletch null @ garf
23 av = $(foreach var, $(auto_var), $(origin $(var)) )
24 override WHITE := BLACK
25 for_var = $(addsuffix .c,foo $(null) $(foo) $(space) $(av) )
26 fe = $(foreach var2, $(for_var),$(subst .c,.o, $(var2) ) )
27 all: auto for2
28 auto : ; @echo $(av)
29 for2: ; @echo $(fe)',
30 '-e WHITE=WHITE CFLAGS=',
31 "undefined file default file environment default file command line override automatic automatic
32 foo.o bletch.o null.o @.o garf.o .o .o undefined.o file.o default.o file.o environment.o default.o file.o command.o line.o override.o automatic.o automatic.o");
33
34 # TEST 1: Test that foreach variables take precedence over global
35 # variables in a global scope (like inside an eval). Tests bug #11913
36
37 run_make_test('
38 .PHONY: all target
39 all: target
40
41 x := BAD
42
43 define mktarget
44 target: x := $(x)
45 target: ; @echo "$(x)"
46 endef
47
48 x := GLOBAL
49
50 $(foreach x,FOREACH,$(eval $(value mktarget)))',
51 '',
52 'FOREACH');
53
54 # Allow variable names with trailing space
55 run_make_test(q!
56 $(foreach \
57 a \
58 , b c d \
59 , $(info $a))
60 all:;@:
61 !,
62 "", "b\nc\nd\n");
63
64 # Allow empty variable names. We still expand the body.
65
66 run_make_test('
67 x = $(foreach ,1 2 3,a)
68 y := $x
69
70 all: ; @echo $y',
71 '', "a a a\n");
72
73 # Check some error conditions.
74
75 run_make_test('
76 x = $(foreach )
77 y = $x
78
79 all: ; @echo $y',
80 '',
81 "#MAKEFILE#:2: *** insufficient number of arguments (1) to function 'foreach'. Stop.",
82 512);
83
84 run_make_test('
85 x = $(foreach x,y)
86 y := $x
87
88 all: ; @echo $y',
89 '',
90 "#MAKEFILE#:2: *** insufficient number of arguments (2) to function 'foreach'. Stop.",
91 512);
92
93 1;