Various minor updates and code cleanups.
[make.git] / tests / scripts / features / targetvars
blob3989340c1c30cff40f719dbab78c6d0ab32cc6ea
1 #                                                                    -*-perl-*-
2 $description = "Test target-specific variable settings.";
4 $details = "\
5 Create a makefile containing various flavors of target-specific variable
6 values, override and non-override, and using various variable expansion
7 rules, semicolon interference, etc.";
9 open(MAKEFILE,"> $makefile");
11 print MAKEFILE <<'EOF';
12 SHELL = /bin/sh
13 export FOO = foo
14 export BAR = bar
15 one: override FOO = one
16 one two: ; @echo $(FOO) $(BAR)
17 two: BAR = two
18 three: ; BAR=1000
19         @echo $(FOO) $(BAR)
20 # Some things that shouldn't be target vars
21 funk : override
22 funk : override adelic
23 adelic override : ; echo $@
24 # Test per-target recursive variables
25 four:FOO=x
26 four:VAR$(FOO)=ok
27 four: ; @echo '$(FOO) $(VAR$(FOO)) $(VAR) $(VARx)'
28 five:FOO=x
29 five six : VAR$(FOO)=good
30 five six: ;@echo '$(FOO) $(VAR$(FOO)) $(VAR) $(VARx) $(VARfoo)'
31 # Test per-target variable inheritance
32 seven: eight
33 seven eight: ; @echo $@: $(FOO) $(BAR)
34 seven: BAR = seven
35 seven: FOO = seven
36 eight: BAR = eight
37 # Test the export keyword with per-target variables
38 nine: ; @echo $(FOO) $(BAR) $$FOO $$BAR
39 nine: FOO = wallace
40 nine-a: export BAZ = baz
41 nine-a: ; @echo $$BAZ
42 # Test = escaping
43 EQ = =
44 ten: one\=two
45 ten: one \= two
46 ten 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
49 QVAR = qvar
50 RVAR = =
51 %.q : ; @echo $(QVAR) $(RVAR)
52 foo.q : RVAR += rvar
53 # Target-specific vars with multiple LHS pattern rules
54 %.r %.s %.t: ; @echo $(QVAR) $(RVAR) $(SVAR) $(TVAR)
55 foo.r : RVAR += rvar
56 foo.t : TVAR := $(QVAR)
57 EOF
59 close(MAKEFILE);
61 # TEST #1
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));
67 # TEST #2
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));
73 # TEST #3
75 &run_make_with_options($makefile, "four", &get_logfile);
76 $answer = "x ok  ok\n";
77 &compare_output($answer,&get_logfile(1));
79 # TEST #4
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));
85 # TEST #5
87 &run_make_with_options($makefile, "nine", &get_logfile);
88 $answer = "wallace bar wallace bar\n";
89 &compare_output($answer,&get_logfile(1));
91 # TEST #5-a
93 &run_make_with_options($makefile, "nine-a", &get_logfile);
94 $answer = "baz\n";
95 &compare_output($answer,&get_logfile(1));
97 # TEST #6
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));
103 # TEST #6
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));
109 # TEST #7
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));
116 # TEST #8
117 # For PR/1378: Target-specific vars don't inherit correctly
119 $makefile2 = &get_tmpfile;
121 open(MAKEFILE,"> $makefile2");
122 print MAKEFILE <<'EOF';
123 foo: FOO = foo
124 bar: BAR = bar
125 foo: bar
126 bar: baz
127 baz: ; @echo $(FOO) $(BAR)
129 close(MAKEFILE);
131 &run_make_with_options("$makefile2", "", &get_logfile);
132 $answer = "foo bar\n";
133 &compare_output($answer, &get_logfile(1));
135 # TEST #9
136 # For PR/1380: Using += assignment in target-specific variables sometimes fails
137 # Also PR/1831
139 $makefile3 = &get_tmpfile;
141 open(MAKEFILE,"> $makefile3");
142 print MAKEFILE <<'EOF';
143 .PHONY: all one
144 all: FOO += baz
145 all: one; @echo $(FOO)
147 FOO = bar
149 one: FOO += biz
150 one: FOO += boz
151 one: ; @echo $(FOO)
153 close(MAKEFILE);
155 &run_make_with_options("$makefile3", "", &get_logfile);
156 $answer = "bar baz biz boz\nbar baz\n";
157 &compare_output($answer, &get_logfile(1));
159 # Test #10
161 &run_make_with_options("$makefile3", "one", &get_logfile);
162 $answer = "bar biz boz\n";
163 &compare_output($answer, &get_logfile(1));
165 # Test #11
166 # PR/1709: Test semicolons in target-specific variable values
168 $makefile4 = &get_tmpfile;
170 open(MAKEFILE, "> $makefile4");
171 print MAKEFILE <<'EOF';
172 foo : FOO = ; ok
173 foo : ; @echo '$(FOO)'
175 close(MAKEFILE);
177 &run_make_with_options("$makefile4", "", &get_logfile);
178 $answer = "; ok\n";
179 &compare_output($answer, &get_logfile(1));
181 # Test #12
182 # PR/2020: More hassles with += target-specific vars.  I _really_ think
183 # I nailed it this time :-/.
185 $makefile5 = &get_tmpfile;
187 open(MAKEFILE, "> $makefile5");
188 print MAKEFILE <<'EOF';
189 .PHONY: a
191 BLAH := foo
192 COMMAND = echo $(BLAH)
194 a: ; @$(COMMAND)
196 a: BLAH := bar
197 a: COMMAND += snafu $(BLAH)
199 close(MAKEFILE);
201 &run_make_with_options("$makefile5", "", &get_logfile);
202 $answer = "bar snafu bar\n";
203 &compare_output($answer, &get_logfile(1));
205 # Test #13
206 # Test double-colon rules with target-specific variable values
208 $makefile6 = &get_tmpfile;
210 open(MAKEFILE, "> $makefile6");
211 print MAKEFILE <<'EOF';
212 W = bad
213 X = bad
214 foo: W = ok
215 foo:: ; @echo $(W) $(X) $(Y) $(Z)
216 foo:: ; @echo $(W) $(X) $(Y) $(Z)
217 foo: X = ok
219 Y = foo
220 bar: foo
221 bar: Y = bar
223 Z = nopat
224 ifdef PATTERN
225   fo% : Z = pat
226 endif
229 close(MAKEFILE);
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));
235 # Test #14
236 # Test double-colon rules with target-specific variable values and
237 # inheritance
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));
243 # Test #15
244 # Test double-colon rules with pattern-specific variable values
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));
251 # Test #16
252 # Test target-specific variables with very long command line
253 # (> make default buffer length)
255 $makefile7 = &get_tmpfile;
257 open(MAKEFILE, "> $makefile7");
258 print MAKEFILE <<'EOF';
259 base_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  )
261 deals_changed_since: ; @echo $(BUILD_OBJ)
264 close(MAKEFILE);
266 &run_make_with_options("$makefile7", '', &get_logfile);
267 $answer = "no build information\n";
268 &compare_output($answer, &get_logfile(1));