xref: /netbsd-src/external/gpl2/gmake/dist/tests/scripts/functions/eval (revision 69606e3f5c9388e52aed8c120ad63c049ca45d8f)
1*69606e3fSchristos#                                                                    -*-perl-*-
2*69606e3fSchristos
3*69606e3fSchristos$description = "Test the eval function.";
4*69606e3fSchristos
5*69606e3fSchristos$details = "This is a test of the eval function in GNU make.
6*69606e3fSchristosThis function will evaluate inline makefile syntax and incorporate the
7*69606e3fSchristosresults into its internal database.\n";
8*69606e3fSchristos
9*69606e3fSchristosopen(MAKEFILE,"> $makefile");
10*69606e3fSchristos
11*69606e3fSchristosprint MAKEFILE <<'EOF';
12*69606e3fSchristosdefine Y
13*69606e3fSchristos  all:: ; @echo $AA
14*69606e3fSchristos  A = B
15*69606e3fSchristosendef
16*69606e3fSchristos
17*69606e3fSchristosX = $(eval $(value Y))
18*69606e3fSchristos
19*69606e3fSchristos$(eval $(shell echo A = A))
20*69606e3fSchristos$(eval $(Y))
21*69606e3fSchristos$(eval A = C)
22*69606e3fSchristos$(eval $(X))
23*69606e3fSchristosEOF
24*69606e3fSchristos
25*69606e3fSchristosclose(MAKEFILE);
26*69606e3fSchristos
27*69606e3fSchristos&run_make_with_options($makefile, "", &get_logfile);
28*69606e3fSchristos
29*69606e3fSchristos# Create the answer to what should be produced by this Makefile
30*69606e3fSchristos$answer = "AA\nBA\n";
31*69606e3fSchristos
32*69606e3fSchristos&compare_output($answer,&get_logfile(1));
33*69606e3fSchristos
34*69606e3fSchristos# Test to make sure defining variables when we have extra scope pushed works
35*69606e3fSchristos# as expected.
36*69606e3fSchristos
37*69606e3fSchristos$makefile2 = &get_tmpfile;
38*69606e3fSchristos
39*69606e3fSchristosopen(MAKEFILE,"> $makefile2");
40*69606e3fSchristos
41*69606e3fSchristosprint MAKEFILE <<'EOF';
42*69606e3fSchristosVARS = A B
43*69606e3fSchristos
44*69606e3fSchristosVARSET = $(1) = $(2)
45*69606e3fSchristos
46*69606e3fSchristos$(foreach v,$(VARS),$(eval $(call VARSET,$v,$v)))
47*69606e3fSchristos
48*69606e3fSchristosall: ; @echo A = $(A) B = $(B)
49*69606e3fSchristosEOF
50*69606e3fSchristos
51*69606e3fSchristosclose(MAKEFILE);
52*69606e3fSchristos
53*69606e3fSchristos&run_make_with_options($makefile2, "", &get_logfile);
54*69606e3fSchristos
55*69606e3fSchristos# Create the answer to what should be produced by this Makefile
56*69606e3fSchristos$answer = "A = A B = B\n";
57*69606e3fSchristos
58*69606e3fSchristos&compare_output($answer,&get_logfile(1));
59*69606e3fSchristos
60*69606e3fSchristos# Test to make sure eval'ing inside conditionals works properly
61*69606e3fSchristos
62*69606e3fSchristos$makefile3 = &get_tmpfile;
63*69606e3fSchristos
64*69606e3fSchristosopen(MAKEFILE,"> $makefile3");
65*69606e3fSchristos
66*69606e3fSchristosprint MAKEFILE <<'EOF';
67*69606e3fSchristosFOO = foo
68*69606e3fSchristos
69*69606e3fSchristosall:: ; @echo it
70*69606e3fSchristos
71*69606e3fSchristosdefine Y
72*69606e3fSchristos  all:: ; @echo worked
73*69606e3fSchristosendef
74*69606e3fSchristos
75*69606e3fSchristosifdef BAR
76*69606e3fSchristos$(eval $(Y))
77*69606e3fSchristosendif
78*69606e3fSchristos
79*69606e3fSchristosEOF
80*69606e3fSchristos
81*69606e3fSchristosclose(MAKEFILE);
82*69606e3fSchristos
83*69606e3fSchristos&run_make_with_options($makefile3, "", &get_logfile);
84*69606e3fSchristos$answer = "it\n";
85*69606e3fSchristos&compare_output($answer,&get_logfile(1));
86*69606e3fSchristos
87*69606e3fSchristos&run_make_with_options($makefile3, "BAR=1", &get_logfile);
88*69606e3fSchristos$answer = "it\nworked\n";
89*69606e3fSchristos&compare_output($answer,&get_logfile(1));
90*69606e3fSchristos
91*69606e3fSchristos
92*69606e3fSchristos# TEST very recursive invocation of eval
93*69606e3fSchristos
94*69606e3fSchristos$makefile3 = &get_tmpfile;
95*69606e3fSchristos
96*69606e3fSchristosopen(MAKEFILE,"> $makefile3");
97*69606e3fSchristos
98*69606e3fSchristosprint MAKEFILE <<'EOF';
99*69606e3fSchristos..9 := 0 1 2 3 4 5 6 7 8 9
100*69606e3fSchristosrev=$(eval res:=)$(foreach word,$1,$(eval res:=${word} ${res}))${res}
101*69606e3fSchristosa:=$(call rev,${..9})
102*69606e3fSchristosall: ; @echo '[$(a)]'
103*69606e3fSchristos
104*69606e3fSchristosEOF
105*69606e3fSchristos
106*69606e3fSchristosclose(MAKEFILE);
107*69606e3fSchristos
108*69606e3fSchristos&run_make_with_options($makefile3, "", &get_logfile);
109*69606e3fSchristos$answer = "[         9 8 7 6 5 4 3 2 1 0 ]\n";
110*69606e3fSchristos&compare_output($answer,&get_logfile(1));
111*69606e3fSchristos
112*69606e3fSchristos
113*69606e3fSchristos# TEST eval with no filename context.
114*69606e3fSchristos# The trick here is that because EVAR is taken from the environment, it must
115*69606e3fSchristos# be evaluated before every command is invoked.  Make sure that works, when
116*69606e3fSchristos# we have no file context for reading_file (bug # 6195)
117*69606e3fSchristos
118*69606e3fSchristos$makefile4 = &get_tmpfile;
119*69606e3fSchristos
120*69606e3fSchristosopen(MAKEFILE,"> $makefile4");
121*69606e3fSchristos
122*69606e3fSchristosprint MAKEFILE <<'EOF';
123*69606e3fSchristosEVAR = $(eval FOBAR = 1)
124*69606e3fSchristosall: ; @echo "OK"
125*69606e3fSchristos
126*69606e3fSchristosEOF
127*69606e3fSchristos
128*69606e3fSchristosclose(MAKEFILE);
129*69606e3fSchristos
130*69606e3fSchristos$extraENV{EVAR} = '1';
131*69606e3fSchristos&run_make_with_options($makefile4, "", &get_logfile);
132*69606e3fSchristos$answer = "OK\n";
133*69606e3fSchristos&compare_output($answer,&get_logfile(1));
134*69606e3fSchristos
135*69606e3fSchristos
136*69606e3fSchristos# Clean out previous information to allow new run_make_test() interface.
137*69606e3fSchristos# If we ever convert all the above to run_make_test() we can remove this line.
138*69606e3fSchristos$makefile = undef;
139*69606e3fSchristos
140*69606e3fSchristos# Test handling of backslashes in strings to be evaled.
141*69606e3fSchristos
142*69606e3fSchristosrun_make_test('
143*69606e3fSchristosdefine FOO
144*69606e3fSchristosall: ; @echo hello \
145*69606e3fSchristosworld
146*69606e3fSchristosendef
147*69606e3fSchristos$(eval $(FOO))
148*69606e3fSchristos', '', 'hello world');
149*69606e3fSchristos
150*69606e3fSchristosrun_make_test('
151*69606e3fSchristosdefine FOO
152*69606e3fSchristosall: ; @echo '."'".'he\llo'."'".'
153*69606e3fSchristos	@echo world
154*69606e3fSchristosendef
155*69606e3fSchristos$(eval $(FOO))
156*69606e3fSchristos', '', 'he\llo
157*69606e3fSchristosworld');
158*69606e3fSchristos
159*69606e3fSchristos
160*69606e3fSchristos# We don't allow new target/prerequisite relationships to be defined within a
161*69606e3fSchristos# command script, because these are evaluated after snap_deps() and that
162*69606e3fSchristos# causes lots of problems (like core dumps!)
163*69606e3fSchristos# See Savannah bug # 12124.
164*69606e3fSchristos
165*69606e3fSchristosrun_make_test('deps: ; $(eval deps: foo)', '',
166*69606e3fSchristos              '#MAKEFILE#:1: *** prerequisites cannot be defined in command scripts.  Stop.',
167*69606e3fSchristos              512);
168*69606e3fSchristos
169*69606e3fSchristos1;
170