9badc52e49c0f4de8720ad450899a7c6f1c36a37
[platform/upstream/make.git] / tests / scripts / functions / foreach
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 subsituted
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 $extraENV{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 delete $extraENV{FOOFOO};
35
36 # TEST 1: Test that foreach variables take precedence over global
37 # variables in a global scope (like inside an eval).  Tests bug #11913
38
39 run_make_test('
40 .PHONY: all target
41 all: target
42
43 x := BAD
44
45 define mktarget
46 target: x := $(x)
47 target: ; @echo "$(x)"
48 endef
49
50 x := GLOBAL
51
52 $(foreach x,FOREACH,$(eval $(value mktarget)))',
53               '',
54               'FOREACH');
55
56 # Allow variable names with trailing space
57 run_make_test(q!
58 $(foreach \
59   a \
60 , b c d \
61 , $(info $a))
62 all:;@:
63 !,
64               "", "b\nc\nd\n");
65
66 # Allow empty variable names.  We still expand the body.
67
68 run_make_test('
69 x = $(foreach ,1 2 3,a)
70 y := $x
71
72 all: ; @echo $y',
73               '', "a a a\n");
74
75 # Check some error conditions.
76
77 run_make_test('
78 x = $(foreach )
79 y = $x
80
81 all: ; @echo $y',
82               '',
83               "#MAKEFILE#:2: *** insufficient number of arguments (1) to function 'foreach'.  Stop.",
84               512);
85
86 run_make_test('
87 x = $(foreach x,y)
88 y := $x
89
90 all: ; @echo $y',
91               '',
92               "#MAKEFILE#:2: *** insufficient number of arguments (2) to function 'foreach'.  Stop.",
93               512);
94
95 1;