xref: /netbsd-src/external/gpl2/gmake/dist/tests/scripts/features/targetvars (revision 69606e3f5c9388e52aed8c120ad63c049ca45d8f)
1*69606e3fSchristos#                                                                    -*-perl-*-
2*69606e3fSchristos$description = "Test target-specific variable settings.";
3*69606e3fSchristos
4*69606e3fSchristos$details = "\
5*69606e3fSchristosCreate a makefile containing various flavors of target-specific variable
6*69606e3fSchristosvalues, override and non-override, and using various variable expansion
7*69606e3fSchristosrules, semicolon interference, etc.";
8*69606e3fSchristos
9*69606e3fSchristosopen(MAKEFILE,"> $makefile");
10*69606e3fSchristos
11*69606e3fSchristosprint MAKEFILE <<'EOF';
12*69606e3fSchristosSHELL = /bin/sh
13*69606e3fSchristosexport FOO = foo
14*69606e3fSchristosexport BAR = bar
15*69606e3fSchristosone: override FOO = one
16*69606e3fSchristosone two: ; @echo $(FOO) $(BAR)
17*69606e3fSchristostwo: BAR = two
18*69606e3fSchristosthree: ; BAR=1000
19*69606e3fSchristos	@echo $(FOO) $(BAR)
20*69606e3fSchristos# Some things that shouldn't be target vars
21*69606e3fSchristosfunk : override
22*69606e3fSchristosfunk : override adelic
23*69606e3fSchristosadelic override : ; echo $@
24*69606e3fSchristos# Test per-target recursive variables
25*69606e3fSchristosfour:FOO=x
26*69606e3fSchristosfour:VAR$(FOO)=ok
27*69606e3fSchristosfour: ; @echo '$(FOO) $(VAR$(FOO)) $(VAR) $(VARx)'
28*69606e3fSchristosfive:FOO=x
29*69606e3fSchristosfive six : VAR$(FOO)=good
30*69606e3fSchristosfive six: ;@echo '$(FOO) $(VAR$(FOO)) $(VAR) $(VARx) $(VARfoo)'
31*69606e3fSchristos# Test per-target variable inheritance
32*69606e3fSchristosseven: eight
33*69606e3fSchristosseven eight: ; @echo $@: $(FOO) $(BAR)
34*69606e3fSchristosseven: BAR = seven
35*69606e3fSchristosseven: FOO = seven
36*69606e3fSchristoseight: BAR = eight
37*69606e3fSchristos# Test the export keyword with per-target variables
38*69606e3fSchristosnine: ; @echo $(FOO) $(BAR) $$FOO $$BAR
39*69606e3fSchristosnine: FOO = wallace
40*69606e3fSchristosnine-a: export BAZ = baz
41*69606e3fSchristosnine-a: ; @echo $$BAZ
42*69606e3fSchristos# Test = escaping
43*69606e3fSchristosEQ = =
44*69606e3fSchristosten: one\=two
45*69606e3fSchristosten: one \= two
46*69606e3fSchristosten one$(EQ)two $(EQ):;@echo $@
47*69606e3fSchristos.PHONY: one two three four five six seven eight nine ten $(EQ) one$(EQ)two
48*69606e3fSchristos# Test target-specific vars with pattern/suffix rules
49*69606e3fSchristosQVAR = qvar
50*69606e3fSchristosRVAR = =
51*69606e3fSchristos%.q : ; @echo $(QVAR) $(RVAR)
52*69606e3fSchristosfoo.q : RVAR += rvar
53*69606e3fSchristos# Target-specific vars with multiple LHS pattern rules
54*69606e3fSchristos%.r %.s %.t: ; @echo $(QVAR) $(RVAR) $(SVAR) $(TVAR)
55*69606e3fSchristosfoo.r : RVAR += rvar
56*69606e3fSchristosfoo.t : TVAR := $(QVAR)
57*69606e3fSchristosEOF
58*69606e3fSchristos
59*69606e3fSchristosclose(MAKEFILE);
60*69606e3fSchristos
61*69606e3fSchristos# TEST #1
62*69606e3fSchristos
63*69606e3fSchristos&run_make_with_options($makefile, "one two three", &get_logfile);
64*69606e3fSchristos$answer = "one bar\nfoo two\nBAR=1000\nfoo bar\n";
65*69606e3fSchristos&compare_output($answer,&get_logfile(1));
66*69606e3fSchristos
67*69606e3fSchristos# TEST #2
68*69606e3fSchristos
69*69606e3fSchristos&run_make_with_options($makefile, "one two FOO=1 BAR=2", &get_logfile);
70*69606e3fSchristos$answer = "one 2\n1 2\n";
71*69606e3fSchristos&compare_output($answer,&get_logfile(1));
72*69606e3fSchristos
73*69606e3fSchristos# TEST #3
74*69606e3fSchristos
75*69606e3fSchristos&run_make_with_options($makefile, "four", &get_logfile);
76*69606e3fSchristos$answer = "x ok  ok\n";
77*69606e3fSchristos&compare_output($answer,&get_logfile(1));
78*69606e3fSchristos
79*69606e3fSchristos# TEST #4
80*69606e3fSchristos
81*69606e3fSchristos&run_make_with_options($makefile, "seven", &get_logfile);
82*69606e3fSchristos$answer = "eight: seven eight\nseven: seven seven\n";
83*69606e3fSchristos&compare_output($answer,&get_logfile(1));
84*69606e3fSchristos
85*69606e3fSchristos# TEST #5
86*69606e3fSchristos
87*69606e3fSchristos&run_make_with_options($makefile, "nine", &get_logfile);
88*69606e3fSchristos$answer = "wallace bar wallace bar\n";
89*69606e3fSchristos&compare_output($answer,&get_logfile(1));
90*69606e3fSchristos
91*69606e3fSchristos# TEST #5-a
92*69606e3fSchristos
93*69606e3fSchristos&run_make_with_options($makefile, "nine-a", &get_logfile);
94*69606e3fSchristos$answer = "baz\n";
95*69606e3fSchristos&compare_output($answer,&get_logfile(1));
96*69606e3fSchristos
97*69606e3fSchristos# TEST #6
98*69606e3fSchristos
99*69606e3fSchristos&run_make_with_options($makefile, "ten", &get_logfile);
100*69606e3fSchristos$answer = "one=two\none bar\n=\nfoo two\nten\n";
101*69606e3fSchristos&compare_output($answer,&get_logfile(1));
102*69606e3fSchristos
103*69606e3fSchristos# TEST #6
104*69606e3fSchristos
105*69606e3fSchristos&run_make_with_options($makefile, "foo.q bar.q", &get_logfile);
106*69606e3fSchristos$answer = "qvar = rvar\nqvar =\n";
107*69606e3fSchristos&compare_output($answer,&get_logfile(1));
108*69606e3fSchristos
109*69606e3fSchristos# TEST #7
110*69606e3fSchristos
111*69606e3fSchristos&run_make_with_options($makefile, "foo.t bar.s", &get_logfile);
112*69606e3fSchristos$answer = "qvar = qvar\nqvar =\n";
113*69606e3fSchristos&compare_output($answer,&get_logfile(1));
114*69606e3fSchristos
115*69606e3fSchristos
116*69606e3fSchristos# TEST #8
117*69606e3fSchristos# For PR/1378: Target-specific vars don't inherit correctly
118*69606e3fSchristos
119*69606e3fSchristos$makefile2 = &get_tmpfile;
120*69606e3fSchristos
121*69606e3fSchristosopen(MAKEFILE,"> $makefile2");
122*69606e3fSchristosprint MAKEFILE <<'EOF';
123*69606e3fSchristosfoo: FOO = foo
124*69606e3fSchristosbar: BAR = bar
125*69606e3fSchristosfoo: bar
126*69606e3fSchristosbar: baz
127*69606e3fSchristosbaz: ; @echo $(FOO) $(BAR)
128*69606e3fSchristosEOF
129*69606e3fSchristosclose(MAKEFILE);
130*69606e3fSchristos
131*69606e3fSchristos&run_make_with_options("$makefile2", "", &get_logfile);
132*69606e3fSchristos$answer = "foo bar\n";
133*69606e3fSchristos&compare_output($answer, &get_logfile(1));
134*69606e3fSchristos
135*69606e3fSchristos# TEST #9
136*69606e3fSchristos# For PR/1380: Using += assignment in target-specific variables sometimes fails
137*69606e3fSchristos# Also PR/1831
138*69606e3fSchristos
139*69606e3fSchristos$makefile3 = &get_tmpfile;
140*69606e3fSchristos
141*69606e3fSchristosopen(MAKEFILE,"> $makefile3");
142*69606e3fSchristosprint MAKEFILE <<'EOF';
143*69606e3fSchristos.PHONY: all one
144*69606e3fSchristosall: FOO += baz
145*69606e3fSchristosall: one; @echo $(FOO)
146*69606e3fSchristos
147*69606e3fSchristosFOO = bar
148*69606e3fSchristos
149*69606e3fSchristosone: FOO += biz
150*69606e3fSchristosone: FOO += boz
151*69606e3fSchristosone: ; @echo $(FOO)
152*69606e3fSchristosEOF
153*69606e3fSchristosclose(MAKEFILE);
154*69606e3fSchristos
155*69606e3fSchristos&run_make_with_options("$makefile3", "", &get_logfile);
156*69606e3fSchristos$answer = "bar baz biz boz\nbar baz\n";
157*69606e3fSchristos&compare_output($answer, &get_logfile(1));
158*69606e3fSchristos
159*69606e3fSchristos# Test #10
160*69606e3fSchristos
161*69606e3fSchristos&run_make_with_options("$makefile3", "one", &get_logfile);
162*69606e3fSchristos$answer = "bar biz boz\n";
163*69606e3fSchristos&compare_output($answer, &get_logfile(1));
164*69606e3fSchristos
165*69606e3fSchristos# Test #11
166*69606e3fSchristos# PR/1709: Test semicolons in target-specific variable values
167*69606e3fSchristos
168*69606e3fSchristos$makefile4 = &get_tmpfile;
169*69606e3fSchristos
170*69606e3fSchristosopen(MAKEFILE, "> $makefile4");
171*69606e3fSchristosprint MAKEFILE <<'EOF';
172*69606e3fSchristosfoo : FOO = ; ok
173*69606e3fSchristosfoo : ; @echo '$(FOO)'
174*69606e3fSchristosEOF
175*69606e3fSchristosclose(MAKEFILE);
176*69606e3fSchristos
177*69606e3fSchristos&run_make_with_options("$makefile4", "", &get_logfile);
178*69606e3fSchristos$answer = "; ok\n";
179*69606e3fSchristos&compare_output($answer, &get_logfile(1));
180*69606e3fSchristos
181*69606e3fSchristos# Test #12
182*69606e3fSchristos# PR/2020: More hassles with += target-specific vars.  I _really_ think
183*69606e3fSchristos# I nailed it this time :-/.
184*69606e3fSchristos
185*69606e3fSchristos$makefile5 = &get_tmpfile;
186*69606e3fSchristos
187*69606e3fSchristosopen(MAKEFILE, "> $makefile5");
188*69606e3fSchristosprint MAKEFILE <<'EOF';
189*69606e3fSchristos.PHONY: a
190*69606e3fSchristos
191*69606e3fSchristosBLAH := foo
192*69606e3fSchristosCOMMAND = echo $(BLAH)
193*69606e3fSchristos
194*69606e3fSchristosa: ; @$(COMMAND)
195*69606e3fSchristos
196*69606e3fSchristosa: BLAH := bar
197*69606e3fSchristosa: COMMAND += snafu $(BLAH)
198*69606e3fSchristosEOF
199*69606e3fSchristosclose(MAKEFILE);
200*69606e3fSchristos
201*69606e3fSchristos&run_make_with_options("$makefile5", "", &get_logfile);
202*69606e3fSchristos$answer = "bar snafu bar\n";
203*69606e3fSchristos&compare_output($answer, &get_logfile(1));
204*69606e3fSchristos
205*69606e3fSchristos# Test #13
206*69606e3fSchristos# Test double-colon rules with target-specific variable values
207*69606e3fSchristos
208*69606e3fSchristos$makefile6 = &get_tmpfile;
209*69606e3fSchristos
210*69606e3fSchristosopen(MAKEFILE, "> $makefile6");
211*69606e3fSchristosprint MAKEFILE <<'EOF';
212*69606e3fSchristosW = bad
213*69606e3fSchristosX = bad
214*69606e3fSchristosfoo: W = ok
215*69606e3fSchristosfoo:: ; @echo $(W) $(X) $(Y) $(Z)
216*69606e3fSchristosfoo:: ; @echo $(W) $(X) $(Y) $(Z)
217*69606e3fSchristosfoo: X = ok
218*69606e3fSchristos
219*69606e3fSchristosY = foo
220*69606e3fSchristosbar: foo
221*69606e3fSchristosbar: Y = bar
222*69606e3fSchristos
223*69606e3fSchristosZ = nopat
224*69606e3fSchristosifdef PATTERN
225*69606e3fSchristos  fo% : Z = pat
226*69606e3fSchristosendif
227*69606e3fSchristos
228*69606e3fSchristosEOF
229*69606e3fSchristosclose(MAKEFILE);
230*69606e3fSchristos
231*69606e3fSchristos&run_make_with_options("$makefile6", "foo", &get_logfile);
232*69606e3fSchristos$answer = "ok ok foo nopat\nok ok foo nopat\n";
233*69606e3fSchristos&compare_output($answer, &get_logfile(1));
234*69606e3fSchristos
235*69606e3fSchristos# Test #14
236*69606e3fSchristos# Test double-colon rules with target-specific variable values and
237*69606e3fSchristos# inheritance
238*69606e3fSchristos
239*69606e3fSchristos&run_make_with_options("$makefile6", "bar", &get_logfile);
240*69606e3fSchristos$answer = "ok ok bar nopat\nok ok bar nopat\n";
241*69606e3fSchristos&compare_output($answer, &get_logfile(1));
242*69606e3fSchristos
243*69606e3fSchristos# Test #15
244*69606e3fSchristos# Test double-colon rules with pattern-specific variable values
245*69606e3fSchristos
246*69606e3fSchristos&run_make_with_options("$makefile6", "foo PATTERN=yes", &get_logfile);
247*69606e3fSchristos$answer = "ok ok foo pat\nok ok foo pat\n";
248*69606e3fSchristos&compare_output($answer, &get_logfile(1));
249*69606e3fSchristos
250*69606e3fSchristos
251*69606e3fSchristos# Test #16
252*69606e3fSchristos# Test target-specific variables with very long command line
253*69606e3fSchristos# (> make default buffer length)
254*69606e3fSchristos
255*69606e3fSchristos$makefile7 = &get_tmpfile;
256*69606e3fSchristos
257*69606e3fSchristosopen(MAKEFILE, "> $makefile7");
258*69606e3fSchristosprint MAKEFILE <<'EOF';
259*69606e3fSchristosbase_metals_fmd_reports.sun5 base_metals_fmd_reports CreateRealPositions        CreateMarginFunds deals_changed_since : BUILD_OBJ=$(shell if [ -f               "build_information.generate" ]; then echo "$(OBJ_DIR)/build_information.o"; else echo "no build information"; fi  )
260*69606e3fSchristos
261*69606e3fSchristosdeals_changed_since: ; @echo $(BUILD_OBJ)
262*69606e3fSchristos
263*69606e3fSchristosEOF
264*69606e3fSchristosclose(MAKEFILE);
265*69606e3fSchristos
266*69606e3fSchristos&run_make_with_options("$makefile7", '', &get_logfile);
267*69606e3fSchristos$answer = "no build information\n";
268*69606e3fSchristos&compare_output($answer, &get_logfile(1));
269*69606e3fSchristos
270*69606e3fSchristos# TEST #17
271*69606e3fSchristos
272*69606e3fSchristos# Test a merge of set_lists for files, where one list is much longer
273*69606e3fSchristos# than the other.  See Savannah bug #15757.
274*69606e3fSchristos
275*69606e3fSchristosmkdir('t1', 0777);
276*69606e3fSchristostouch('t1/rules.mk');
277*69606e3fSchristos
278*69606e3fSchristosrun_make_test('
279*69606e3fSchristosVPATH = t1
280*69606e3fSchristosinclude rules.mk
281*69606e3fSchristos.PHONY: all
282*69606e3fSchristosall: foo.x
283*69606e3fSchristosfoo.x : rules.mk ; @echo MYVAR=$(MYVAR) FOOVAR=$(FOOVAR) ALLVAR=$(ALLVAR)
284*69606e3fSchristosall: ALLVAR = xxx
285*69606e3fSchristosfoo.x: FOOVAR = bar
286*69606e3fSchristosrules.mk : MYVAR = foo
287*69606e3fSchristos.INTERMEDIATE: foo.x rules.mk
288*69606e3fSchristos',
289*69606e3fSchristos              '-I t1',
290*69606e3fSchristos              'MYVAR= FOOVAR=bar ALLVAR=xxx');
291*69606e3fSchristos
292*69606e3fSchristosrmfiles('t1/rules.mk');
293*69606e3fSchristosrmdir('t1');
294*69606e3fSchristos
295*69606e3fSchristos# TEST #18
296*69606e3fSchristos
297*69606e3fSchristos# Test appending to a simple variable containing a "$": avoid a
298*69606e3fSchristos# double-expansion.  See Savannah bug #15913.
299*69606e3fSchristos
300*69606e3fSchristosrun_make_test("
301*69606e3fSchristosVAR := \$\$FOO
302*69606e3fSchristosfoo: VAR += BAR
303*69606e3fSchristosfoo: ; \@echo '\$(VAR)'",
304*69606e3fSchristos              '',
305*69606e3fSchristos              '$FOO BAR');
306*69606e3fSchristos
307*69606e3fSchristos1;
308