Some bug fixes and document updates.
[make.git] / tests / scripts / features / targetvars
bloba70d3ab6506cc6bb507ad53199e14a6bafba6224
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 # Test = escaping
41 EQ = =
42 ten: one\=two
43 ten: one \= two
44 ten one$(EQ)two $(EQ):;@echo $@
45 .PHONY: one two three four five six seven eight nine ten $(EQ) one$(EQ)two
46 # Test target-specific vars with pattern/suffix rules
47 QVAR = qvar
48 RVAR = =
49 %.q : ; @echo $(QVAR) $(RVAR)
50 foo.q : RVAR += rvar
51 # Target-specific vars with multiple LHS pattern rules
52 %.r %.s %.t: ; @echo $(QVAR) $(RVAR) $(SVAR) $(TVAR)
53 foo.r : RVAR += rvar
54 foo.t : TVAR := $(QVAR)
55 EOF
57 close(MAKEFILE);
59 # TEST #1
61 &run_make_with_options($makefile, "one two three", &get_logfile);
62 $answer = "one bar\nfoo two\nBAR=1000\nfoo bar\n";
63 &compare_output($answer,&get_logfile(1));
65 # TEST #2
67 &run_make_with_options($makefile, "one two FOO=1 BAR=2", &get_logfile);
68 $answer = "one 2\n1 2\n";
69 &compare_output($answer,&get_logfile(1));
71 # TEST #3
73 &run_make_with_options($makefile, "four", &get_logfile);
74 $answer = "x ok  ok\n";
75 &compare_output($answer,&get_logfile(1));
77 # TEST #4
79 &run_make_with_options($makefile, "seven", &get_logfile);
80 $answer = "eight: seven eight\nseven: seven seven\n";
81 &compare_output($answer,&get_logfile(1));
83 # TEST #5
85 &run_make_with_options($makefile, "nine", &get_logfile);
86 $answer = "wallace bar wallace bar\n";
87 &compare_output($answer,&get_logfile(1));
89 # TEST #6
91 &run_make_with_options($makefile, "ten", &get_logfile);
92 $answer = "one=two\none bar\n=\nfoo two\nten\n";
93 &compare_output($answer,&get_logfile(1));
95 # TEST #6
97 &run_make_with_options($makefile, "foo.q bar.q", &get_logfile);
98 $answer = "qvar = rvar\nqvar =\n";
99 &compare_output($answer,&get_logfile(1));
101 # TEST #7
103 &run_make_with_options($makefile, "foo.t bar.s", &get_logfile);
104 $answer = "qvar = qvar\nqvar =\n";
105 &compare_output($answer,&get_logfile(1));
108 # TEST #8
109 # For PR/1378: Target-specific vars don't inherit correctly
111 $makefile2 = &get_tmpfile;
113 open(MAKEFILE,"> $makefile2");
114 print MAKEFILE <<'EOF';
115 foo: FOO = foo
116 bar: BAR = bar
117 foo: bar
118 bar: baz
119 baz: ; @echo $(FOO) $(BAR)
121 close(MAKEFILE);
123 &run_make_with_options("$makefile2", "", &get_logfile);
124 $answer = "foo bar\n";
125 &compare_output($answer, &get_logfile(1));
127 # TEST #9
128 # For PR/1380: Using += assignment in target-specific variables sometimes fails
129 # Also PR/1831
131 $makefile3 = &get_tmpfile;
133 open(MAKEFILE,"> $makefile3");
134 print MAKEFILE <<'EOF';
135 .PHONY: all one
136 all: FOO += baz
137 all: one; @echo $(FOO)
139 FOO = bar
141 one: FOO += biz
142 one: FOO += boz
143 one: ; @echo $(FOO)
145 close(MAKEFILE);
147 &run_make_with_options("$makefile3", "", &get_logfile);
148 $answer = "bar baz biz boz\nbar baz\n";
149 &compare_output($answer, &get_logfile(1));
151 # Test #10
153 &run_make_with_options("$makefile3", "one", &get_logfile);
154 $answer = "bar biz boz\n";
155 &compare_output($answer, &get_logfile(1));
157 # Test #11
158 # PR/1709: Test semicolons in target-specific variable values
160 $makefile4 = &get_tmpfile;
162 open(MAKEFILE, "> $makefile4");
163 print MAKEFILE <<'EOF';
164 foo : FOO = ; ok
165 foo : ; @echo '$(FOO)'
167 close(MAKEFILE);
169 &run_make_with_options("$makefile4", "", &get_logfile);
170 $answer = "; ok\n";
171 &compare_output($answer, &get_logfile(1));
173 # Test #12
174 # PR/2020: More hassles with += target-specific vars.  I _really_ think
175 # I nailed it this time :-/.
177 $makefile5 = &get_tmpfile;
179 open(MAKEFILE, "> $makefile5");
180 print MAKEFILE <<'EOF';
181 .PHONY: a
183 BLAH := foo
184 COMMAND = echo $(BLAH)
186 a: ; @$(COMMAND)
188 a: BLAH := bar
189 a: COMMAND += snafu $(BLAH)
191 close(MAKEFILE);
193 &run_make_with_options("$makefile5", "", &get_logfile);
194 $answer = "bar snafu bar\n";
195 &compare_output($answer, &get_logfile(1));