1#                                                                    -*-perl-*-
2$description = "Test the call function.\n";
3
4$details = "Try various uses of call and ensure they all give the correct
5results.\n";
6
7open(MAKEFILE, "> $makefile");
8
9# The Contents of the MAKEFILE ...
10
11print MAKEFILE <<'EOMAKE';
12# Simple, just reverse two things
13#
14reverse = $2 $1
15
16# A complex `map' function, using recursive `call'.
17#
18map = $(foreach a,$2,$(call $1,$a))
19
20# Test using a builtin; this is silly as it's simpler to do without call
21#
22my-notdir = $(call notdir,$(1))
23
24# Test using non-expanded builtins
25#
26my-foreach = $(foreach $(1),$(2),$(3))
27my-if      = $(if $(1),$(2),$(3))
28
29# Test recursive invocations of call with different arguments
30#
31one = $(1) $(2) $(3)
32two = $(call one,$(1),foo,$(2))
33
34# Test recursion on the user-defined function.  As a special case make
35# won't error due to this.
36# Implement transitive closure using $(call ...)
37#
38DEP_foo = bar baz quux
39DEP_baz = quux blarp
40rest = $(wordlist 2,$(words ${1}),${1})
41tclose = $(if $1,$(firstword $1) \
42		$(call tclose,$(sort ${DEP_$(firstword $1)} $(call rest,$1))))
43
44all: ; @echo '$(call reverse,bar,foo)'; \
45        echo '$(call map,origin,MAKE reverse map)'; \
46        echo '$(call my-notdir,a/b   c/d      e/f)'; \
47        echo '$(call my-foreach)'; \
48        echo '$(call my-foreach,a,,,)'; \
49        echo '$(call my-if,a,b,c)'; \
50        echo '$(call two,bar,baz)'; \
51	echo '$(call tclose,foo)'
52
53
54
55EOMAKE
56
57# These won't work until/unless PR/1527 is resolved.
58#        echo '$(call my-foreach,a,x y z,$(a)$(a))'; \
59#        echo '$(call my-if,,$(warning don't print this),ok)'
60#
61# $answer = "xx yy zz\nok\n";
62
63# END of Contents of MAKEFILE
64
65close(MAKEFILE);
66
67&run_make_with_options($makefile, "", &get_logfile);
68$answer = "foo bar\ndefault file file\nb d f\n\n\nb\nbar foo baz\nfoo bar baz blarp quux \n";
69&compare_output($answer, &get_logfile(1));
70
71
72# TEST eclipsing of arguments when invoking sub-calls
73
74$makefile2 = &get_tmpfile;
75
76open(MAKEFILE,"> $makefile2");
77
78print MAKEFILE <<'EOF';
79
80all = $1 $2 $3 $4 $5 $6 $7 $8 $9
81
82level1 = $(call all,$1,$2,$3,$4,$5)
83level2 = $(call level1,$1,$2,$3)
84level3 = $(call level2,$1,$2,$3,$4,$5)
85
86all:
87	@echo $(call all,1,2,3,4,5,6,7,8,9,10,11)
88	@echo $(call level1,1,2,3,4,5,6,7,8)
89	@echo $(call level2,1,2,3,4,5,6,7,8)
90	@echo $(call level3,1,2,3,4,5,6,7,8)
91EOF
92
93close(MAKEFILE);
94
95&run_make_with_options($makefile2, "", &get_logfile);
96$answer = "1 2 3 4 5 6 7 8 9\n1 2 3 4 5\n1 2 3\n1 2 3\n";
97&compare_output($answer,&get_logfile(1));
98
991;
100