| 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 | # kmk: CC isn't a default.
|
|---|
| 18 | $extraENV{FOOFOO} = 'foo foo';
|
|---|
| 19 | $CC_origin = $is_kmk ? "undefined" : "default";
|
|---|
| 20 |
|
|---|
| 21 | run_make_test("space = ' '".'
|
|---|
| 22 | null :=
|
|---|
| 23 | auto_var = udef space CC null FOOFOO MAKE foo CFLAGS WHITE @ <
|
|---|
| 24 | foo = bletch null @ garf
|
|---|
| 25 | av = $(foreach var, $(auto_var), $(origin $(var)) )
|
|---|
| 26 | override WHITE := BLACK
|
|---|
| 27 | for_var = $(addsuffix .c,foo $(null) $(foo) $(space) $(av) )
|
|---|
| 28 | fe = $(foreach var2, $(for_var),$(subst .c,.o, $(var2) ) )
|
|---|
| 29 | all: auto for2
|
|---|
| 30 | auto : ; @echo $(av)
|
|---|
| 31 | for2: ; @echo $(fe)',
|
|---|
| 32 | '-j1 -e WHITE=WHITE CFLAGS=',
|
|---|
| 33 | "undefined file ". $CC_origin ." file environment default file command line override automatic automatic
|
|---|
| 34 | foo.o bletch.o null.o @.o garf.o .o .o undefined.o file.o ". $CC_origin .".o file.o environment.o default.o file.o command.o line.o override.o automatic.o automatic.o");
|
|---|
| 35 |
|
|---|
| 36 | delete $extraENV{FOOFOO};
|
|---|
| 37 |
|
|---|
| 38 | # TEST 1: Test that foreach variables take precedence over global
|
|---|
| 39 | # variables in a global scope (like inside an eval). Tests bug #11913
|
|---|
| 40 |
|
|---|
| 41 | run_make_test('
|
|---|
| 42 | .PHONY: all target
|
|---|
| 43 | all: target
|
|---|
| 44 |
|
|---|
| 45 | x := BAD
|
|---|
| 46 |
|
|---|
| 47 | define mktarget
|
|---|
| 48 | target: x := $(x)
|
|---|
| 49 | target: ; @echo "$(x)"
|
|---|
| 50 | endef
|
|---|
| 51 |
|
|---|
| 52 | x := GLOBAL
|
|---|
| 53 |
|
|---|
| 54 | $(foreach x,FOREACH,$(eval $(value mktarget)))',
|
|---|
| 55 | '',
|
|---|
| 56 | 'FOREACH');
|
|---|
| 57 |
|
|---|
| 58 | # Allow variable names with trailing space
|
|---|
| 59 | run_make_test(q!
|
|---|
| 60 | $(foreach \
|
|---|
| 61 | a \
|
|---|
| 62 | , b c d \
|
|---|
| 63 | , $(info $a))
|
|---|
| 64 | all:;@:
|
|---|
| 65 | !,
|
|---|
| 66 | "", "b\nc\nd\n");
|
|---|
| 67 |
|
|---|
| 68 | # Allow empty variable names. We still expand the body.
|
|---|
| 69 |
|
|---|
| 70 | run_make_test('
|
|---|
| 71 | x = $(foreach ,1 2 3,a)
|
|---|
| 72 | y := $x
|
|---|
| 73 |
|
|---|
| 74 | all: ; @echo $y',
|
|---|
| 75 | '', "a a a\n");
|
|---|
| 76 |
|
|---|
| 77 | # Check some error conditions.
|
|---|
| 78 |
|
|---|
| 79 | run_make_test('
|
|---|
| 80 | x = $(foreach )
|
|---|
| 81 | y = $x
|
|---|
| 82 |
|
|---|
| 83 | all: ; @echo $y',
|
|---|
| 84 | '',
|
|---|
| 85 | "#MAKEFILE#:2: *** insufficient number of arguments (1) to function 'foreach'. Stop.",
|
|---|
| 86 | 512);
|
|---|
| 87 |
|
|---|
| 88 | run_make_test('
|
|---|
| 89 | x = $(foreach x,y)
|
|---|
| 90 | y := $x
|
|---|
| 91 |
|
|---|
| 92 | all: ; @echo $y',
|
|---|
| 93 | '',
|
|---|
| 94 | "#MAKEFILE#:2: *** insufficient number of arguments (2) to function 'foreach'. Stop.",
|
|---|
| 95 | 512);
|
|---|
| 96 |
|
|---|
| 97 | 1;
|
|---|