summaryrefslogtreecommitdiff
path: root/tests/scripts/features/output-sync
blob: a7534cb7aaf03881fe7016b62c76cf01a766378c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#                                                                    -*-perl-*-

$description = "Test --output-sync (-O) option.";

$details = "Test the synchronization of output from parallel jobs.";

# If we don't have output sync support, never mind.
exists $FEATURES{'output-sync'} or return -1;

# Output sync can't be tested without parallelization
$parallel_jobs or return -1;


if ($vos) {
  $sleep_command = "sleep -seconds";
}
else {
  $sleep_command = "sleep";
}

# The following subdirectories with Makefiles are used in several
# of the following tests.  The model is:
#   foo/Makefile - has a "foo" target that waits for the bar target
#   bar/Makefile - has a "bar" target that runs immediately
#                - has a "baz" target that waits for the foo target
#
# So, you start the two sub-makes in parallel and first the "bar" target is
# built, followed by "foo", followed by "baz".  The trick is that first each
# target prints a "start" statement, then waits (if appropriate), then prints
# an end statement.  Thus we can tell if the -O flag is working, since
# otherwise these statements would be mixed together.

@syncfiles = ();

sub output_sync_clean {
    rmfiles('foo/Makefile', 'bar/Makefile', @syncfiles);
    rmdir('foo');
    rmdir('bar');
}

# We synchronize the different jobs by having them wait for a sentinel file to
# be created, instead of relying on a certain amount of time passing.
# Unfortunately in this test we have to sleep after we see the sync file,
# since we also want to make the obtaining of the write synchronization lock
# reliable.  If things are too fast, then sometimes a different job will steal
# the output sync lock and the output is mis-ordered from what we expect.
sub output_sync_wait {
    return "while [ ! -f ../mksync.$_[0] ]; do :; done; rm -f ../mksync.$_[0].wait; $sleep_command 1";
}
sub output_sync_set {
    return "date > ../mksync.$_[0]";
}

@syncfiles = qw(mksync.foo mksync.foo_start mksync.bar mksync.bar_start);

output_sync_clean();
mkdir('foo', 0777);
mkdir('bar', 0777);

$set_foo = output_sync_set('foo');
$set_bar = output_sync_set('bar');
$set_foo_start = output_sync_set('foo_start');
$set_bar_start = output_sync_set('bar_start');

$wait_foo = output_sync_wait('foo');
$wait_bar = output_sync_wait('bar');
$wait_foo_start = output_sync_set('foo_start');
$wait_bar_start = output_sync_set('bar_start');

open(MAKEFILE,"> foo/Makefile");
print MAKEFILE <<EOF;
all: foo

foo: foo-base ; \@$set_foo

foo-base:
\t\@echo foo: start
\t\@$wait_bar
\t\@echo foo: end

foo-job: foo-job-base ; \@$set_foo

foo-job-base:
\t\@$wait_bar_start
\t\@echo foo: start
\t\@$set_foo_start
\t\@$wait_bar
\t\@echo foo: end

foo-fail:
\t\@echo foo-fail: start
\t\@$wait_bar
\t\@echo foo-fail: end
\t\@exit 1
EOF
close(MAKEFILE);

open(MAKEFILE,"> bar/Makefile");
print MAKEFILE <<EOF;
all: bar baz

bar: bar-base ; \@$set_bar
bar-base:
\t\@echo bar: start
\t\@echo bar: end

bar-job: bar-job-base ; \@$set_bar

bar-job-base:
\t\@echo bar: start
\t\@$set_bar_start
\t\@$wait_foo_start
\t\@echo bar: end

baz: baz-base
baz-base:
\t\@echo baz: start
\t\@$wait_foo
\t\@echo baz: end
EOF
close(MAKEFILE);

# Test per-make synchronization.
unlink(@syncfiles);
run_make_test(qq!
all: make-foo make-bar

make-foo: ; \$(MAKE) -C foo

make-bar: ; \$(MAKE) -C bar!,
              '-j -Orecurse',
"#MAKEPATH# -C foo
#MAKE#[1]: Entering directory '#PWD#/foo'
foo: start
foo: end
#MAKE#[1]: Leaving directory '#PWD#/foo'
#MAKEPATH# -C bar
#MAKE#[1]: Entering directory '#PWD#/bar'
bar: start
bar: end
baz: start
baz: end
#MAKE#[1]: Leaving directory '#PWD#/bar'\n", 0, 6);

# Test per-target synchronization.
# Note we have to sleep again here after starting the foo makefile before
# starting the bar makefile, otherwise the "entering/leaving" messages for the
# submakes might be ordered differently than we expect.

unlink(@syncfiles);
run_make_test(qq!
x=1
\$xMAKEFLAGS += --no-print-directory

all: make-foo make-bar

make-foo: ; \$(MAKE) -C foo

make-bar: ; $sleep_command 1 ; \$(MAKE) -C bar!,
              '-j --output-sync=target',
"#MAKEPATH# -C foo
$sleep_command 1 ; #MAKEPATH# -C bar
#MAKE#[1]: Entering directory '#PWD#/bar'
bar: start
bar: end
#MAKE#[1]: Leaving directory '#PWD#/bar'
#MAKE#[1]: Entering directory '#PWD#/foo'
foo: start
foo: end
#MAKE#[1]: Leaving directory '#PWD#/foo'
#MAKE#[1]: Entering directory '#PWD#/bar'
baz: start
baz: end
#MAKE#[1]: Leaving directory '#PWD#/bar'\n", 0, 6);

# Rerun but this time suppress the directory tracking
unlink(@syncfiles);
run_make_test(undef, '-j --output-sync=target x=',
              "#MAKEPATH# -C foo
$sleep_command 1 ; #MAKEPATH# -C bar
bar: start
bar: end
foo: start
foo: end
baz: start
baz: end\n", 0, 6);

# Test that messages from make itself are enclosed with
# "Entering/Leaving directory" messages.
unlink(@syncfiles);
run_make_test(qq!
all: make-foo-fail make-bar-bar

make-foo-fail: ; \$(MAKE) -C foo foo-fail

make-bar-bar: ; $sleep_command 1 ; \$(MAKE) -C bar bar!,
              '-j -O',
"#MAKEPATH# -C foo foo-fail
$sleep_command 1 ; #MAKEPATH# -C bar bar
#MAKE#[1]: Entering directory '#PWD#/bar'
bar: start
bar: end
#MAKE#[1]: Leaving directory '#PWD#/bar'
#MAKE#[1]: Entering directory '#PWD#/foo'
foo-fail: start
foo-fail: end
Makefile:20: recipe for target 'foo-fail' failed
#MAKE#[1]: *** [foo-fail] Error 1
#MAKE#[1]: Leaving directory '#PWD#/foo'
#MAKEFILE#:4: recipe for target 'make-foo-fail' failed
#MAKE#: *** [make-foo-fail] Error 2\n",
512);

# Test the per-job synchronization.
# For this we'll have bar-job:
#   print start, invoke bar-start, wait for foo-start, print end, print-bar-end
# And foo-job:
#   wait for bar-start, print foo-start, wait for bar-end, print end

unlink(@syncfiles);
run_make_test(qq!
all: make-foo make-bar

make-foo: ; \$(MAKE) -C foo foo-job

make-bar: ; $sleep_command 1 ; \$(MAKE) -C bar bar-job!,
              '-j --output-sync=line',
"#MAKEPATH# -C foo foo-job
$sleep_command 1 ; #MAKEPATH# -C bar bar-job
#MAKE#[1]: Entering directory '#PWD#/foo'
foo: start
#MAKE#[1]: Leaving directory '#PWD#/foo'
#MAKE#[1]: Entering directory '#PWD#/bar'
bar: start
#MAKE#[1]: Leaving directory '#PWD#/bar'
#MAKE#[1]: Entering directory '#PWD#/bar'
bar: end
#MAKE#[1]: Leaving directory '#PWD#/bar'
#MAKE#[1]: Entering directory '#PWD#/foo'
foo: end
#MAKE#[1]: Leaving directory '#PWD#/foo'\n", 0, 6);


# Remove temporary directories and contents.
output_sync_clean();

# Ensure recursion doesn't mis-order or double-print output
run_make_test(qq!
all:
\t\@echo foo
\t\@+echo bar
!,
              '-j -Oline', "foo\nbar\n");

run_make_test(undef, '-j -Otarget', "foo\nbar\n");

# Ensure when make writes out command it's not misordered
run_make_test(qq!
all:
\t\@echo foobar
\ttrue
!,
              '-j -Oline', "foobar\ntrue\n");

run_make_test(undef, '-j -Otarget', "foobar\ntrue\n");

# Ensure that shell functions inside recipes write stderr to the sync file
run_make_test(q!
all: ; @: $(shell echo foo 1>&2)
!,
              '-w -Oline', "#MAKE#: Entering directory '#PWD#'\nfoo\n#MAKE#: Leaving directory '#PWD#'\n");

# Ensure that output generated while parsing makefiles is synced
# when appropriate.
run_make_test(q!
$(shell echo foo 1>&2)
all: ; echo bar
!,
              '-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");

# Test recursion
$m1 = get_tmpfile();
$m2 = get_tmpfile();

open(M1, "> $m1");
print M1 <<'EOF';
$(shell echo d1 stderr 1>&2)
$(info d1 stdout)
all:; @:
EOF
close(M1);

open(M2, "> $m2");
print M2 <<'EOF';
$(shell echo d2 stderr 1>&2)
$(info d2 stdout)
all:; @:
# Force an ordering on the output
$(shell sleep 1)
EOF
close(M2);

run_make_test(qq!
all: t1 t2
t1: ; \@\$(MAKE) -f $m1
t2: ; \@\$(MAKE) -f $m2
!,
              "-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");

rmfiles($m1, $m2);

# Ensure that output generated while parsing makefiles is synced
# when appropriate.
$m1 = get_tmpfile();

open(M1, "> $m1");
print M1 <<'EOF';
$(shell echo d1 stderr 1>&2)
$(info d1 stdout)
$(error d1 failed)
all:; @:
EOF
close(M1);

run_make_test(qq!
all: t1
t1: ; -\@\$(MAKE) -f $m1
!,
              "-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");

rmfiles($m1);

# Test $(error ...) functions in recipes

run_make_test(q!
foo: $(OBJS) ; echo $(or $(filter %.o,$^),$(error fail))
!,
              '-O', "#MAKEFILE#:2: *** fail.  Stop.\n", 512);

# This tells the test driver that the perl test script executed properly.
1;