Imported Upstream version 4.0
[platform/upstream/make.git] / tests / scripts / features / output-sync
1 #                                                                    -*-perl-*-
2
3 $description = "Test --output-sync (-O) option.";
4
5 $details = "Test the synchronization of output from parallel jobs.";
6
7 # If we don't have output sync support, never mind.
8 exists $FEATURES{'output-sync'} or return -1;
9
10 # Output sync can't be tested without parallelization
11 $parallel_jobs or return -1;
12
13
14 if ($vos) {
15   $sleep_command = "sleep -seconds";
16 }
17 else {
18   $sleep_command = "sleep";
19 }
20
21 # The following subdirectories with Makefiles are used in several
22 # of the following tests.  The model is:
23 #   foo/Makefile - has a "foo" target that waits for the bar target
24 #   bar/Makefile - has a "bar" target that runs immediately
25 #                - has a "baz" target that waits for the foo target
26 #
27 # So, you start the two sub-makes in parallel and first the "bar" target is
28 # built, followed by "foo", followed by "baz".  The trick is that first each
29 # target prints a "start" statement, then waits (if appropriate), then prints
30 # an end statement.  Thus we can tell if the -O flag is working, since
31 # otherwise these statements would be mixed together.
32
33 @syncfiles = ();
34
35 sub output_sync_clean {
36     rmfiles('foo/Makefile', 'bar/Makefile', @syncfiles);
37     rmdir('foo');
38     rmdir('bar');
39 }
40
41 # We synchronize the different jobs by having them wait for a sentinel file to
42 # be created, instead of relying on a certain amount of time passing.
43 # Unfortunately in this test we have to sleep after we see the sync file,
44 # since we also want to make the obtaining of the write synchronization lock
45 # reliable.  If things are too fast, then sometimes a different job will steal
46 # the output sync lock and the output is mis-ordered from what we expect.
47 sub output_sync_wait {
48     return "while [ ! -f ../mksync.$_[0] ]; do :; done; rm -f ../mksync.$_[0].wait; $sleep_command 1";
49 }
50 sub output_sync_set {
51     return "date > ../mksync.$_[0]";
52 }
53
54 @syncfiles = qw(mksync.foo mksync.foo_start mksync.bar mksync.bar_start);
55
56 output_sync_clean();
57 mkdir('foo', 0777);
58 mkdir('bar', 0777);
59
60 $set_foo = output_sync_set('foo');
61 $set_bar = output_sync_set('bar');
62 $set_foo_start = output_sync_set('foo_start');
63 $set_bar_start = output_sync_set('bar_start');
64
65 $wait_foo = output_sync_wait('foo');
66 $wait_bar = output_sync_wait('bar');
67 $wait_foo_start = output_sync_set('foo_start');
68 $wait_bar_start = output_sync_set('bar_start');
69
70 open(MAKEFILE,"> foo/Makefile");
71 print MAKEFILE <<EOF;
72 all: foo
73
74 foo: foo-base ; \@$set_foo
75
76 foo-base:
77 \t\@echo foo: start
78 \t\@$wait_bar
79 \t\@echo foo: end
80
81 foo-job: foo-job-base ; \@$set_foo
82
83 foo-job-base:
84 \t\@$wait_bar_start
85 \t\@echo foo: start
86 \t\@$set_foo_start
87 \t\@$wait_bar
88 \t\@echo foo: end
89
90 foo-fail:
91 \t\@echo foo-fail: start
92 \t\@$wait_bar
93 \t\@echo foo-fail: end
94 \t\@exit 1
95 EOF
96 close(MAKEFILE);
97
98 open(MAKEFILE,"> bar/Makefile");
99 print MAKEFILE <<EOF;
100 all: bar baz
101
102 bar: bar-base ; \@$set_bar
103 bar-base:
104 \t\@echo bar: start
105 \t\@echo bar: end
106
107 bar-job: bar-job-base ; \@$set_bar
108
109 bar-job-base:
110 \t\@echo bar: start
111 \t\@$set_bar_start
112 \t\@$wait_foo_start
113 \t\@echo bar: end
114
115 baz: baz-base
116 baz-base:
117 \t\@echo baz: start
118 \t\@$wait_foo
119 \t\@echo baz: end
120 EOF
121 close(MAKEFILE);
122
123 # Test per-make synchronization.
124 unlink(@syncfiles);
125 run_make_test(qq!
126 all: make-foo make-bar
127
128 make-foo: ; \$(MAKE) -C foo
129
130 make-bar: ; \$(MAKE) -C bar!,
131               '-j -Orecurse',
132 "#MAKEPATH# -C foo
133 #MAKE#[1]: Entering directory '#PWD#/foo'
134 foo: start
135 foo: end
136 #MAKE#[1]: Leaving directory '#PWD#/foo'
137 #MAKEPATH# -C bar
138 #MAKE#[1]: Entering directory '#PWD#/bar'
139 bar: start
140 bar: end
141 baz: start
142 baz: end
143 #MAKE#[1]: Leaving directory '#PWD#/bar'\n", 0, 6);
144
145 # Test per-target synchronization.
146 # Note we have to sleep again here after starting the foo makefile before
147 # starting the bar makefile, otherwise the "entering/leaving" messages for the
148 # submakes might be ordered differently than we expect.
149
150 unlink(@syncfiles);
151 run_make_test(qq!
152 x=1
153 \$xMAKEFLAGS += --no-print-directory
154
155 all: make-foo make-bar
156
157 make-foo: ; \$(MAKE) -C foo
158
159 make-bar: ; $sleep_command 1 ; \$(MAKE) -C bar!,
160               '-j --output-sync=target',
161 "#MAKEPATH# -C foo
162 $sleep_command 1 ; #MAKEPATH# -C bar
163 #MAKE#[1]: Entering directory '#PWD#/bar'
164 bar: start
165 bar: end
166 #MAKE#[1]: Leaving directory '#PWD#/bar'
167 #MAKE#[1]: Entering directory '#PWD#/foo'
168 foo: start
169 foo: end
170 #MAKE#[1]: Leaving directory '#PWD#/foo'
171 #MAKE#[1]: Entering directory '#PWD#/bar'
172 baz: start
173 baz: end
174 #MAKE#[1]: Leaving directory '#PWD#/bar'\n", 0, 6);
175
176 # Rerun but this time suppress the directory tracking
177 unlink(@syncfiles);
178 run_make_test(undef, '-j --output-sync=target x=',
179               "#MAKEPATH# -C foo
180 $sleep_command 1 ; #MAKEPATH# -C bar
181 bar: start
182 bar: end
183 foo: start
184 foo: end
185 baz: start
186 baz: end\n", 0, 6);
187
188 # Test that messages from make itself are enclosed with
189 # "Entering/Leaving directory" messages.
190 unlink(@syncfiles);
191 run_make_test(qq!
192 all: make-foo-fail make-bar-bar
193
194 make-foo-fail: ; \$(MAKE) -C foo foo-fail
195
196 make-bar-bar: ; $sleep_command 1 ; \$(MAKE) -C bar bar!,
197               '-j -O',
198 "#MAKEPATH# -C foo foo-fail
199 $sleep_command 1 ; #MAKEPATH# -C bar bar
200 #MAKE#[1]: Entering directory '#PWD#/bar'
201 bar: start
202 bar: end
203 #MAKE#[1]: Leaving directory '#PWD#/bar'
204 #MAKE#[1]: Entering directory '#PWD#/foo'
205 foo-fail: start
206 foo-fail: end
207 Makefile:20: recipe for target 'foo-fail' failed
208 #MAKE#[1]: *** [foo-fail] Error 1
209 #MAKE#[1]: Leaving directory '#PWD#/foo'
210 #MAKEFILE#:4: recipe for target 'make-foo-fail' failed
211 #MAKE#: *** [make-foo-fail] Error 2\n",
212 512);
213
214 # Test the per-job synchronization.
215 # For this we'll have bar-job:
216 #   print start, invoke bar-start, wait for foo-start, print end, print-bar-end
217 # And foo-job:
218 #   wait for bar-start, print foo-start, wait for bar-end, print end
219
220 unlink(@syncfiles);
221 run_make_test(qq!
222 all: make-foo make-bar
223
224 make-foo: ; \$(MAKE) -C foo foo-job
225
226 make-bar: ; $sleep_command 1 ; \$(MAKE) -C bar bar-job!,
227               '-j --output-sync=line',
228 "#MAKEPATH# -C foo foo-job
229 $sleep_command 1 ; #MAKEPATH# -C bar bar-job
230 #MAKE#[1]: Entering directory '#PWD#/foo'
231 foo: start
232 #MAKE#[1]: Leaving directory '#PWD#/foo'
233 #MAKE#[1]: Entering directory '#PWD#/bar'
234 bar: start
235 #MAKE#[1]: Leaving directory '#PWD#/bar'
236 #MAKE#[1]: Entering directory '#PWD#/bar'
237 bar: end
238 #MAKE#[1]: Leaving directory '#PWD#/bar'
239 #MAKE#[1]: Entering directory '#PWD#/foo'
240 foo: end
241 #MAKE#[1]: Leaving directory '#PWD#/foo'\n", 0, 6);
242
243
244 # Remove temporary directories and contents.
245 output_sync_clean();
246
247 # Ensure recursion doesn't mis-order or double-print output
248 run_make_test(qq!
249 all:
250 \t\@echo foo
251 \t\@+echo bar
252 !,
253               '-j -Oline', "foo\nbar\n");
254
255 run_make_test(undef, '-j -Otarget', "foo\nbar\n");
256
257 # Ensure when make writes out command it's not misordered
258 run_make_test(qq!
259 all:
260 \t\@echo foobar
261 \ttrue
262 !,
263               '-j -Oline', "foobar\ntrue\n");
264
265 run_make_test(undef, '-j -Otarget', "foobar\ntrue\n");
266
267 # Ensure that shell functions inside recipes write stderr to the sync file
268 run_make_test(q!
269 all: ; @: $(shell echo foo 1>&2)
270 !,
271               '-w -Oline', "#MAKE#: Entering directory '#PWD#'\nfoo\n#MAKE#: Leaving directory '#PWD#'\n");
272
273 # Ensure that output generated while parsing makefiles is synced
274 # when appropriate.
275 run_make_test(q!
276 $(shell echo foo 1>&2)
277 all: ; echo bar
278 !,
279               '-s -w -Otarget', "#MAKE#: Entering directory '#PWD#'\nfoo\n#MAKE#: Leaving directory '#PWD#'\n#MAKE#: Entering directory '#PWD#'\nbar\n#MAKE#: Leaving directory '#PWD#'\n");
280
281 # Test recursion
282 $m1 = get_tmpfile();
283 $m2 = get_tmpfile();
284
285 open(M1, "> $m1");
286 print M1 <<'EOF';
287 $(shell echo d1 stderr 1>&2)
288 $(info d1 stdout)
289 all:; @:
290 EOF
291 close(M1);
292
293 open(M2, "> $m2");
294 print M2 <<'EOF';
295 $(shell echo d2 stderr 1>&2)
296 $(info d2 stdout)
297 all:; @:
298 # Force an ordering on the output
299 $(shell sleep 1)
300 EOF
301 close(M2);
302
303 run_make_test(qq!
304 all: t1 t2
305 t1: ; \@\$(MAKE) -f $m1
306 t2: ; \@\$(MAKE) -f $m2
307 !,
308               "-j -Oline", "#MAKE#[1]: Entering directory '#PWD#'\nd1 stderr\nd1 stdout\n#MAKE#[1]: Leaving directory '#PWD#'\n#MAKE#[1]: Entering directory '#PWD#'\nd2 stderr\nd2 stdout\n#MAKE#[1]: Leaving directory '#PWD#'\n");
309
310 rmfiles($m1, $m2);
311
312 # Ensure that output generated while parsing makefiles is synced
313 # when appropriate.
314 $m1 = get_tmpfile();
315
316 open(M1, "> $m1");
317 print M1 <<'EOF';
318 $(shell echo d1 stderr 1>&2)
319 $(info d1 stdout)
320 $(error d1 failed)
321 all:; @:
322 EOF
323 close(M1);
324
325 run_make_test(qq!
326 all: t1
327 t1: ; -\@\$(MAKE) -f $m1
328 !,
329               "-j -Oline", "#MAKE#[1]: Entering directory '#PWD#'\nd1 stderr\nd1 stdout\n$m1:3: *** d1 failed.  Stop.\n#MAKE#[1]: Leaving directory '#PWD#'\n#MAKEFILE#:3: recipe for target 't1' failed\n#MAKE#: [t1] Error 2 (ignored)\n");
330
331 rmfiles($m1);
332
333 # This tells the test driver that the perl test script executed properly.
334 1;