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