summaryrefslogtreecommitdiffstats
path: root/src/kmk/tests/scripts/functions/foreach
blob: 88ef0a7a7be5cf9a01a378f00e3185d13b5f8a3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#                                                                    -*-perl-*-
# $Id$

$description = "Test the foreach function.";

$details = "This is a test of the foreach function in gnu make.
This function starts with a space separated list of
names and a variable. Each name in the list is subsituted
into the variable and the given text evaluated. The general
form of the command is $(foreach var,$list,$text). Several
types of foreach loops are tested\n";


# TEST 0

# Set an environment variable that we can test in the makefile.
# kmk: CC isn't a default.
$extraENV{FOOFOO} = 'foo foo';
$CC_origin = $is_kmk ? "undefined" : "default";

run_make_test("space = ' '".'
null :=
auto_var = udef space CC null FOOFOO MAKE foo CFLAGS WHITE @ <
foo = bletch null @ garf
av = $(foreach var, $(auto_var), $(origin $(var)) )
override WHITE := BLACK
for_var = $(addsuffix .c,foo $(null) $(foo) $(space) $(av) )
fe = $(foreach var2, $(for_var),$(subst .c,.o, $(var2) ) )
all: auto for2
auto : ; @echo $(av)
for2: ; @echo $(fe)',
              '-j1 -e WHITE=WHITE CFLAGS=',
              "undefined file ". $CC_origin ." file environment default file command line override automatic automatic
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");

delete $extraENV{FOOFOO};

# TEST 1: Test that foreach variables take precedence over global
# variables in a global scope (like inside an eval).  Tests bug #11913

run_make_test('
.PHONY: all target
all: target

x := BAD

define mktarget
target: x := $(x)
target: ; @echo "$(x)"
endef

x := GLOBAL

$(foreach x,FOREACH,$(eval $(value mktarget)))',
              '',
              'FOREACH');

# Allow variable names with trailing space
run_make_test(q!
$(foreach \
  a \
, b c d \
, $(info $a))
all:;@:
!,
              "", "b\nc\nd\n");

# Allow empty variable names.  We still expand the body.

run_make_test('
x = $(foreach ,1 2 3,a)
y := $x

all: ; @echo $y',
              '', "a a a\n");

# Check some error conditions.

run_make_test('
x = $(foreach )
y = $x

all: ; @echo $y',
              '',
              "#MAKEFILE#:2: *** insufficient number of arguments (1) to function 'foreach'.  Stop.",
              512);

run_make_test('
x = $(foreach x,y)
y := $x

all: ; @echo $y',
              '',
              "#MAKEFILE#:2: *** insufficient number of arguments (2) to function 'foreach'.  Stop.",
              512);

1;