summaryrefslogtreecommitdiff
path: root/t/buildsystems/buildsystem_tests
blob: 41c0f977ecfb930674a18b9d78b3816479417fd3 (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
#!/usr/bin/perl

use Test::More tests => 273;

use strict;
use warnings;
use IPC::Open2;
use Cwd ();
use File::Temp qw(tempfile tempdir);
use File::Basename ();

# Let the tests to be run from anywhere but currect directory
# is expected to be the one where this test lives in.
chdir File::Basename::dirname($0) or die "Unable to chdir to ".File::Basename::dirname($0);

use_ok( 'Debian::Debhelper::Dh_Lib' );
use_ok( 'Debian::Debhelper::Buildsystem' );
use_ok( 'Debian::Debhelper::Dh_Buildsystems' );

my $TOPDIR = "../..";
my @STEPS = qw(configure build test install clean);
my $BS_CLASS = 'Debian::Debhelper::Buildsystem';

my ($bs, @bs, %bs);
my ($tmp, @tmp, %tmp);
my ($tmpdir, $builddir, $default_builddir);

### Common subs ####
sub touch {
	my $file=shift;
	my $chmod=shift;
	open FILE, ">", $file and close FILE or die "Unable to touch $file";
	chmod $chmod, $file if defined $chmod;
}

sub cleandir {
	my $dir=shift;
	system ("find", $dir, "-type", "f", "-delete");
}
sub readlines {
	my $h=shift;
	my @lines = <$h>;
	close $h;
	chop @lines;
	return \@lines;
}

sub process_stdout {
	my ($cmdline, $stdin) = @_;
	my ($reader, $writer);

	my $pid = open2($reader, $writer, $cmdline) or die "Unable to exec $cmdline";
	print $writer $stdin if $stdin;
	close $writer;
	waitpid($pid, 0);
	$? = $? >> 8; # exit status
	return readlines($reader);
}

sub write_debian_rules {
	my $contents=shift;
	my $backup;

	if (-f "debian/rules") {
		(undef, $backup) = tempfile(DIR => ".", OPEN => 0);
		rename "debian/rules", $backup;
	}
	# Write debian/rules if requested
	if ($contents) {
		open(my $f, ">", "debian/rules");
		print $f $contents;;
		close($f);
		chmod 0755, "debian/rules";
	}
	return $backup;
}

### Test Buildsystem class API methods
is( $BS_CLASS->canonpath("path/to/the/./nowhere/../../somewhere"),
    "path/to/somewhere", "canonpath no1" );
is( $BS_CLASS->canonpath("path/to/../forward/../../somewhere"),
    "somewhere","canonpath no2" );
is( $BS_CLASS->canonpath("path/to/../../../somewhere"),
    "../somewhere","canonpath no3" );
is( $BS_CLASS->canonpath("./"), ".", "canonpath no4" );
is( $BS_CLASS->canonpath("/absolute/path/./somewhere/../to/nowhere"),
    "/absolute/path/to/nowhere", "canonpath no5" );
is( $BS_CLASS->_rel2rel("path/my/file", "path/my", "/tmp"),
    "file", "_rel2rel no1" );
is( $BS_CLASS->_rel2rel("path/dir/file", "path/my", "/tmp"),
    "../dir/file", "_rel2rel no2" );
is( $BS_CLASS->_rel2rel("file", "/root/path/my", "/root"),
    "/root/file", "_rel2rel abs no3" );
is( $BS_CLASS->_rel2rel(".", ".", "/tmp"), ".", "_rel2rel no4" );
is( $BS_CLASS->_rel2rel("path", "path/", "/tmp"), ".", "_rel2rel no5" );
is( $BS_CLASS->_rel2rel("/absolute/path", "anybase", "/tmp"),
    "/absolute/path", "_rel2rel abs no6");
is( $BS_CLASS->_rel2rel("relative/path", "/absolute/base", "/tmp"),
    "/tmp/relative/path", "_rel2rel abs no7");

### Test Buildsystem class path API methods under different configurations
sub test_buildsystem_paths_api {
	my ($bs, $config, $expected)=@_;

	my $api_is = sub {
		my ($got, $name)=@_;
		is( $got, $expected->{$name}, "paths API ($config): $name")
	};

	&$api_is( $bs->get_sourcedir(), 'get_sourcedir()' );
	&$api_is( $bs->get_sourcepath("a/b"), 'get_sourcepath(a/b)' );
	&$api_is( $bs->get_builddir(), 'get_builddir()' );
	&$api_is( $bs->get_buildpath(), 'get_buildpath()' );
	&$api_is( $bs->get_buildpath("a/b"), 'get_buildpath(a/b)' );
	&$api_is( $bs->get_source_rel2builddir(), 'get_source_rel2builddir()' );
	&$api_is( $bs->get_source_rel2builddir("a/b"), 'get_source_rel2builddir(a/b)' );
	&$api_is( $bs->get_build_rel2sourcedir(), 'get_build_rel2sourcedir()' );
	&$api_is( $bs->get_build_rel2sourcedir("a/b"), 'get_build_rel2sourcedir(a/b)' );
}

# Defaults
$bs = $BS_CLASS->new();
$default_builddir = $bs->DEFAULT_BUILD_DIRECTORY();
%tmp = (
	"get_sourcedir()" => ".",
	"get_sourcepath(a/b)" => "./a/b",
	"get_builddir()" => undef,
	"get_buildpath()" => ".",
	"get_buildpath(a/b)" =>  "./a/b",
	"get_source_rel2builddir()" => ".",
	"get_source_rel2builddir(a/b)" => "./a/b",
	"get_build_rel2sourcedir()" => ".",
	"get_build_rel2sourcedir(a/b)" => "./a/b",
);
test_buildsystem_paths_api($bs, "no builddir, no sourcedir", \%tmp);

# builddir=bld/dir
$bs = $BS_CLASS->new(builddir => "bld/dir");
%tmp = (
	"get_sourcedir()" => ".",
	"get_sourcepath(a/b)" => "./a/b",
	"get_builddir()" => "bld/dir",
	"get_buildpath()" => "bld/dir",
	"get_buildpath(a/b)" =>  "bld/dir/a/b",
	"get_source_rel2builddir()" => "../..",
	"get_source_rel2builddir(a/b)" => "../../a/b",
	"get_build_rel2sourcedir()" => "bld/dir",
	"get_build_rel2sourcedir(a/b)" => "bld/dir/a/b",
);
test_buildsystem_paths_api($bs, "builddir=bld/dir, no sourcedir", \%tmp);

# Default builddir, sourcedir=autoconf
$bs = $BS_CLASS->new(builddir => undef, sourcedir => "autoconf");
%tmp = (
	"get_sourcedir()" => "autoconf",
	"get_sourcepath(a/b)" => "autoconf/a/b",
	"get_builddir()" => "$default_builddir",
	"get_buildpath()" => "$default_builddir",
	"get_buildpath(a/b)" =>  "$default_builddir/a/b",
	"get_source_rel2builddir()" => "../autoconf",
	"get_source_rel2builddir(a/b)" => "../autoconf/a/b",
	"get_build_rel2sourcedir()" => "../$default_builddir",
	"get_build_rel2sourcedir(a/b)" => "../$default_builddir/a/b",
);
test_buildsystem_paths_api($bs, "default builddir, sourcedir=autoconf", \%tmp);

# sourcedir=autoconf (builddir should be dropped)
$bs = $BS_CLASS->new(builddir => "autoconf", sourcedir => "autoconf");
%tmp = (
	"get_sourcedir()" => "autoconf",
	"get_sourcepath(a/b)" => "autoconf/a/b",
	"get_builddir()" => undef,
	"get_buildpath()" => "autoconf",
	"get_buildpath(a/b)" =>  "autoconf/a/b",
	"get_source_rel2builddir()" => ".",
	"get_source_rel2builddir(a/b)" => "./a/b",
	"get_build_rel2sourcedir()" => ".",
	"get_build_rel2sourcedir(a/b)" => "./a/b",
);
test_buildsystem_paths_api($bs, "no builddir, sourcedir=autoconf", \%tmp);

# Prefer out of source tree building when
# sourcedir=builddir=autoconf hence builddir should be dropped.
$bs->prefer_out_of_source_building(builddir => "autoconf");
test_buildsystem_paths_api($bs, "out of source prefered, sourcedir=builddir", \%tmp);

# builddir=bld/dir, sourcedir=autoconf. Should be the same as sourcedir=autoconf.
$bs = $BS_CLASS->new(builddir => "bld/dir", sourcedir => "autoconf");
$bs->enforce_in_source_building();
test_buildsystem_paths_api($bs, "in source enforced, sourcedir=autoconf", \%tmp);

# builddir=../bld/dir (relative to the curdir)
$bs = $BS_CLASS->new(builddir => "bld/dir/", sourcedir => "autoconf");
%tmp = (
	"get_sourcedir()" => "autoconf",
	"get_sourcepath(a/b)" => "autoconf/a/b",
	"get_builddir()" => "bld/dir",
	"get_buildpath()" => "bld/dir",
	"get_buildpath(a/b)" =>  "bld/dir/a/b",
	"get_source_rel2builddir()" => "../../autoconf",
	"get_source_rel2builddir(a/b)" => "../../autoconf/a/b",
	"get_build_rel2sourcedir()" => "../bld/dir",
	"get_build_rel2sourcedir(a/b)" => "../bld/dir/a/b",
);
test_buildsystem_paths_api($bs, "builddir=../bld/dir, sourcedir=autoconf", \%tmp);

### Test if all buildsystems can be loaded
@bs = load_all_buildsystems([ $INC[0] ]);
@tmp = map { $_->NAME() } @bs;
ok(@Debian::Debhelper::Dh_Buildsystems::BUILDSYSTEMS >= 1, "some build systems are built in" );
is_deeply( \@tmp, \@Debian::Debhelper::Dh_Buildsystems::BUILDSYSTEMS, "load_all_buildsystems() loads all built-in buildsystems" );

### Test check_auto_buildable() of each buildsystem
sub test_check_auto_buildable {
	my $bs=shift;
	my $config=shift;
	my $expected=shift;
	my @steps=@_ || @STEPS;

	if (! ref $expected) {
		my %all_steps;
		$all_steps{$_} = $expected foreach (@steps);
		$expected = \%all_steps;
	}
	for my $step (@steps) {
		my $e = 0;
		if (exists $expected->{$step}) {
			$e = $expected->{$step};
		} elsif (exists $expected->{default}) {
			$e = $expected->{default};
		}
		if ($e) {
			ok( $bs->check_auto_buildable($step),
			    $bs->NAME() . "($config): check_auto_buildable($step)" );
		}
		else {
			ok( ! $bs->check_auto_buildable($step),
			    $bs->NAME() . "($config): ! check_auto_buildable($step)" );
		}
	}
}

$tmpdir = tempdir("tmp.XXXXXX");
$builddir = "$tmpdir/builddir";
mkdir $builddir;
%tmp = (
	builddir => "$tmpdir/builddir",
	sourcedir => $tmpdir
);

$bs{autoconf} = load_buildsystem("autoconf", undef, %tmp);
$bs{cmake} = load_buildsystem("cmake", undef, %tmp);
$bs{perl_mm} = load_buildsystem("perl_makemaker", undef, %tmp);
$bs = load_buildsystem("makefile", undef, %tmp);

test_check_auto_buildable($bs{autoconf}, "no configure", 0);
test_check_auto_buildable($bs{cmake}, "no CMakeLists.txt", 0);
test_check_auto_buildable($bs{perl_mm}, "no Makefile.PL", 0);
test_check_auto_buildable($bs, "no Makefile", 0);

touch "$tmpdir/configure", 0755;
test_check_auto_buildable($bs{autoconf}, "configure", { configure => 1 });

touch "$tmpdir/CMakeLists.txt";
test_check_auto_buildable($bs{cmake}, "CMakeLists.txt", { configure => 1 });

touch "$tmpdir/Makefile.PL";
test_check_auto_buildable($bs{perl_mm}, "Makefile.PL",
    { configure => 1, install => 1 });

# With Makefile
touch "$builddir/Makefile";
test_check_auto_buildable($bs, "Makefile", { configure => 0, default => 1 });
test_check_auto_buildable($bs{autoconf}, "configure+Makefile", { configure => 1 });
test_check_auto_buildable($bs{cmake}, "CMakeLists.txt+Makefile", 1);

# Makefile.PL forces in-source
#(see note in check_auto_buildable() why always 1 here)
unlink "$builddir/Makefile";
touch "$tmpdir/Makefile";
test_check_auto_buildable($bs{perl_mm}, "Makefile.PL+Makefile", 1);

# Perl Build.PL - handles always
$bs = load_buildsystem("perl_build", undef, %tmp);
test_check_auto_buildable($bs, "no Build.PL", 0);
touch "$tmpdir/Build.PL";
test_check_auto_buildable($bs, "Build.PL", { configure => 1 });
touch "$tmpdir/Build"; # forced in source
test_check_auto_buildable($bs, "Build.PL+Build", 1);

# Python Distutils
$bs = load_buildsystem("python_distutils", undef, %tmp);
test_check_auto_buildable($bs, "no setup.py", 0);
touch "$tmpdir/setup.py";
test_check_auto_buildable($bs, "setup.py", 1);

cleandir($tmpdir);

### Now test if it can autoselect a proper buildsystem for a typical package
sub test_autoselection {
	my $system=shift;
	my $expected=shift;
	for my $step (@STEPS) {
		my $bs = load_buildsystem(undef, $step, @_);
		my $e = $expected;
		$e = $expected->{$step} if ref $expected;
		if (defined $bs) {
			is( $bs->NAME(), $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
		}
		else {
			is ( undef, $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
		}
	}
}

# Autoconf
touch "$tmpdir/configure", 0755;
touch "$builddir/Makefile";
test_autoselection("autoconf",
    { configure => "autoconf", build => "makefile",
      test => "makefile", install => "makefile", clean => "makefile" }, %tmp);
cleandir $tmpdir;

# Perl Makemaker (build, test, clean fail with builddir set [not supported])
touch "$tmpdir/Makefile.PL";
touch "$tmpdir/Makefile";
test_autoselection("perl_makemaker", "perl_makemaker", %tmp);
cleandir $tmpdir;

# Makefile
touch "$builddir/Makefile";
test_autoselection("makefile", { build => "makefile", test => "makefile",
		install => "makefile", clean => "makefile" }, %tmp);
cleandir $tmpdir;

# Python Distutils
touch "$tmpdir/setup.py";
test_autoselection("python_distutils", "python_distutils", %tmp);
cleandir $tmpdir;

# Perl Build
touch "$tmpdir/Build.PL";
touch "$tmpdir/Build";
test_autoselection("perl_build", "perl_build", %tmp);
cleandir $tmpdir;

# CMake
touch "$tmpdir/CMakeLists.txt";
touch "$builddir/Makefile";
test_autoselection("cmake",
    { configure => "cmake", build => "makefile",
      test => "makefile", install => "makefile", clean => "makefile" }, %tmp);
cleandir $tmpdir;

### Test Buildsystem::rmdir_builddir()
sub do_rmdir_builddir {
	my $builddir=shift;
	my $system;
	$system = $BS_CLASS->new(builddir => $builddir, sourcedir => $tmpdir);
	$system->mkdir_builddir();
	$system->rmdir_builddir();
}

$builddir = "$tmpdir/builddir";
do_rmdir_builddir($builddir);
ok ( ! -e $builddir, "testing rmdir_builddir() 1: builddir parent '$builddir' deleted" );
ok ( -d $tmpdir, "testing rmdir_builddir() 1: sourcedir '$tmpdir' remains" );

$builddir = "$tmpdir/bld";
do_rmdir_builddir("$builddir/dir");
ok ( ! -e $builddir, "testing rmdir_builddir() 2: builddir parent '$builddir' deleted" );
ok ( -d $tmpdir, "testing rmdir_builddir() 2: sourcedir '$tmpdir' remains" );

$builddir = "$tmpdir/bld";
mkdir "$builddir";
touch "$builddir/afile";
mkdir "$builddir/dir";
touch "$builddir/dir/afile2";
do_rmdir_builddir("$builddir/dir");
ok ( ! -e "$builddir/dir", "testing rmdir_builddir() 3: builddir '$builddir/dir' not empty, but deleted" );
ok ( -d $builddir, "testing rmdir_builddir() 3: builddir parent '$builddir' not empty, remains" );

cleandir $tmpdir;

### Test buildsystems_init() and commandline/env argument handling
sub get_load_bs_source {
	my ($system, $step)=@_;
	$step = (defined $step) ? "'$step'" : 'undef';
	$system = (defined $system) ? "'$system'" : 'undef';

return <<EOF;
use strict;
use warnings;
use Debian::Debhelper::Dh_Buildsystems;

buildsystems_init();
my \$bs = load_buildsystem($system, $step);
if (defined \$bs) {
	print 'NAME=', \$bs->NAME(), "\\n";
	print \$_, "=", (defined \$bs->{\$_}) ? \$bs->{\$_} : 'undef', "\\n"
	    foreach (sort keys \%\$bs);
}
EOF
}

$tmp = Cwd::getcwd();
# NOTE: disabling parallel building explicitly (it might get automatically
# enabled if run under dpkg-buildpackage -jX) to make output deterministic.
is_deeply( process_stdout("$^X -- - --builddirectory='autoconf/bld dir' --sourcedirectory autoconf --parallel=1",
                          get_load_bs_source(undef, "configure")),
    [ 'NAME=autoconf', 'builddir=autoconf/bld dir', "cwd=$tmp",  'makecmd=make', 'parallel=1', 'sourcedir=autoconf' ],
    "autoconf autoselection and sourcedir/builddir" );

is_deeply( process_stdout("$^X -- - -Sautoconf -D autoconf --parallel=1", get_load_bs_source("autoconf", "build")),
    [ 'NAME=autoconf', 'builddir=undef', "cwd=$tmp", 'makecmd=make', 'parallel=1', 'sourcedir=autoconf' ],
    "forced autoconf and sourcedir" );

is_deeply( process_stdout("$^X -- - -B -Sautoconf --parallel=1", get_load_bs_source("autoconf", "build")),
    [ 'NAME=autoconf', "builddir=$default_builddir", "cwd=$tmp", 'makecmd=make', 'parallel=1', 'sourcedir=.' ],
    "forced autoconf and default build directory" );

# Build the autoconf test package
sub dh_auto_do_autoconf {
	my $sourcedir=shift;
	my $builddir=shift;
	my %args=@_;

	my (@lines, @extra_args);
	my $buildpath = $sourcedir;
	my @dh_auto_args = ("-D", $sourcedir);
	my $dh_auto_str = "-D $sourcedir";
	if ($builddir) {
		push @dh_auto_args, "-B", $builddir;
		$dh_auto_str .= " -B $builddir";
		$buildpath = $builddir;
	}

	my $do_dh_auto = sub {
		my $step=shift;
		my @extra_args;
		my $extra_str = "";
		if (exists $args{"${step}_args"}) {
			push @extra_args, @{$args{"${step}_args"}};
			$extra_str .= " $_" foreach (@extra_args);
		}
		is ( system("$TOPDIR/dh_auto_$step", @dh_auto_args, "--", @extra_args), 0,
			 "dh_auto_$step $dh_auto_str$extra_str" );
		return @extra_args;
	};
	
	@extra_args = &$do_dh_auto('configure');
	ok ( -f "$buildpath/Makefile", "$buildpath/Makefile exists" );
	@lines=();
	if (ok( open(FILE, "$buildpath/stamp_configure"), "$buildpath/stamp_configure exists") ) {
		@lines = @{readlines(\*FILE)};
	}
	is_deeply( \@lines, \@extra_args, "$buildpath/stamp_configure contains extra args" );

	&$do_dh_auto('build');
	ok ( -f "$buildpath/stamp_build", "$buildpath/stamp_build exists" );
	&$do_dh_auto('test');
	ok ( -f "$buildpath/stamp_test", "$buildpath/stamp_test exists" );
	&$do_dh_auto('install');
	@lines=();
	if ( ok(open(FILE, "$buildpath/stamp_install"), "$buildpath/stamp_install exists") ) {
		@lines = @{readlines(\*FILE)};
	} 
	is_deeply( \@lines, [ "DESTDIR=".Cwd::getcwd()."/debian/testpackage" ],
	    "$buildpath/stamp_install contains DESTDIR" );
	&$do_dh_auto('clean');
	if ($builddir) {
		ok ( ! -e "$buildpath", "builddir $buildpath was removed" );
	}
	else {
		ok ( ! -e "$buildpath/Makefile" && ! -e "$buildpath/stamp_configure", "Makefile and stamps gone" );
	}
	ok ( -x "$sourcedir/configure", "configure script renamins after clean" );
}

dh_auto_do_autoconf('autoconf');
dh_auto_do_autoconf('autoconf', 'bld/dir', configure_args => [ "--extra-autoconf-configure-arg" ]);
ok ( ! -e 'bld', "bld got deleted too" );

#### Test parallel building and related options / routines
@tmp = ( $ENV{MAKEFLAGS}, $ENV{DEB_BUILD_OPTIONS} );

# Test get_make_jobserver_status() sub

$ENV{MAKEFLAGS} = "--jobserver-fds=103,104 -j";
is_deeply( [ get_make_jobserver_status() ], [ "jobserver-unavailable", undef ],
	"get_make_jobserver_status(): unavailable jobserver, unset makeflags" );

$ENV{MAKEFLAGS} = "-a --jobserver-fds=103,104 -j -b";
is_deeply( [ get_make_jobserver_status() ], [ "jobserver-unavailable", "-a -b" ],
	"get_make_jobserver_status(): unavailable jobserver, clean makeflags" );

$ENV{MAKEFLAGS} = " --jobserver-fds=1,2 -j  ";
is_deeply( [ get_make_jobserver_status() ], [ "jobserver", undef ],
	"get_make_jobserver_status(): jobserver (available), clean makeflags" );

$ENV{MAKEFLAGS} = "-a -j -b";
is_deeply( [ get_make_jobserver_status() ], [ "jobs-0", "-a -b" ],
	"get_make_jobserver_status(): -j" );

$ENV{MAKEFLAGS} = "-a --jobs -b";
is_deeply( [ get_make_jobserver_status() ], [ "jobs-0", "-a -b" ],
	"get_make_jobserver_status(): --jobs" );

$ENV{MAKEFLAGS} = "-j6";
is_deeply( [ get_make_jobserver_status() ], [ "jobs-6", undef ],
	"get_make_jobserver_status(): -j6" );

$ENV{MAKEFLAGS} = "-a -j6 --jobs=7";
is_deeply( [ get_make_jobserver_status() ], [ "jobs-7", "-a" ],
	"get_make_jobserver_status(): -j6 --jobs=7" );

$ENV{MAKEFLAGS} = "-j6 --jobserver-fds=5,6 --jobs=8";
is_deeply( [ get_make_jobserver_status() ], [ "jobserver-unavailable", "-j6 --jobs=8" ],
	"get_make_jobserver_status(): mixed jobserver and -j/--jobs" );

# Test parallel building with makefile build system.
$ENV{MAKEFLAGS} = "";
$ENV{DEB_BUILD_OPTIONS} = "";

sub do_parallel_mk {
	my $dh_opts=shift || "";
	my $make_opts=shift || "";
	return process_stdout(
		"LANG=C LC_ALL=C LC_MESSAGES=C $TOPDIR/dh_auto_build -Smakefile $dh_opts " .
		"-- -s -f parallel.mk $make_opts 2>&1 >/dev/null", "");
}

sub test_isnt_parallel {
	my ($got, $desc) = @_;
	my @makemsgs = grep /^make[\d\[\]]*:/, @$got;
	if (@makemsgs) {
		like( $makemsgs[0], qr/Error 10/, $desc );
	}
	else {
		ok( scalar(@makemsgs) > 0, $desc );
	}
}

sub test_is_parallel {
	my ($got, $desc) = @_;
	is_deeply( $got, [] , $desc );
	is( $?, 0, "(exit status=0) $desc");
}

test_isnt_parallel( do_parallel_mk(),
	"No parallel by default" );
test_isnt_parallel( do_parallel_mk("--parallel"),
	"No parallel by default with --parallel" );

$ENV{DEB_BUILD_OPTIONS}="parallel=5";
test_isnt_parallel( do_parallel_mk(),
	"DEB_BUILD_OPTIONS=parallel=5 without --parallel" );
test_is_parallel( do_parallel_mk("--parallel"),
	"DEB_BUILD_OPTIONS=parallel=5 with --parallel" );
test_is_parallel( do_parallel_mk("--parallel=2"),
	"DEB_BUILD_OPTIONS=parallel=5 with --parallel=2" );
test_isnt_parallel( do_parallel_mk("--parallel=1"),
	"DEB_BUILD_OPTIONS=parallel=5 with --parallel=1 (off)" );

$ENV{MAKEFLAGS} = "--jobserver-fds=105,106 -j";
$ENV{DEB_BUILD_OPTIONS}="";
test_isnt_parallel( do_parallel_mk(),
	"makefile.pm (no parallel): no make warnings about unavailable jobserver" );
$ENV{DEB_BUILD_OPTIONS}="parallel=5";
test_is_parallel( do_parallel_mk("--parallel"),
	"DEB_BUILD_OPTIONS=parallel=5 with --parallel: no make warnings about unavail parent jobserver" );

$ENV{MAKEFLAGS} = "-j2";
$ENV{DEB_BUILD_OPTIONS}="";
test_is_parallel( do_parallel_mk(),
	"MAKEFLAGS=-j2 without --parallel: dh_auto_build honours MAKEFLAGS" );
test_isnt_parallel( do_parallel_mk("--parallel=1"),
	"MAKEFLAGS=-j2 with --parallel=1: dh_auto_build enforces -j1" );

# Test dh dpkg-buildpackage -jX detection
sub do_rules_for_parallel {
	my $cmdline=shift || "";
	my $stdin=shift || "";
	return process_stdout("LANG=C LC_ALL=C LC_MESSAGES=C PATH=$TOPDIR:\$PATH " .
		"make -f - $cmdline 2>&1 >/dev/null", $stdin);
}

# Simulate dpkg-buildpackage -j5
doit("ln", "-s", "parallel.mk", "Makefile");

sub test_dh_parallel {
	my $extra_dsc=shift || "";
	my $debian_rules=shift || "";
	my $rules;
	my $tmpfile;

	$ENV{MAKEFLAGS} = "-j5";
	$ENV{DEB_BUILD_OPTIONS} = "parallel=5";

	# Write debian/rules if requested
	$tmpfile = write_debian_rules($debian_rules);

	$rules = <<'EOF';
%:
	@dh_clean > /dev/null 2>&1
	@dh --buildsystem=makefile --after=dh_auto_configure --until=dh_auto_build $@
	@dh_clean > /dev/null 2>&1
EOF
	test_is_parallel( do_rules_for_parallel("build", $rules),
		"dh adds --parallel implicitly under dpkg-buildpackage -j5 $extra_dsc");

	$ENV{MAKEFLAGS} = "";
	test_isnt_parallel( do_rules_for_parallel("build", $rules),
		"DEB_BUILD_OPTIONS=parallel=5 without MAKEFLAGS=-jX via dh $extra_dsc" );

	$ENV{MAKEFLAGS} = "-j5";
	$rules = <<'EOF';
%:
	@dh_clean > /dev/null 2>&1
	@dh -j1 --buildsystem=makefile --after=dh_auto_configure --until=dh_auto_build $@
	@dh_clean > /dev/null 2>&1
EOF
	test_isnt_parallel( do_rules_for_parallel("build", $rules),
		"dh -j1 disables implicit parallel under dpkg-buildpackage -j5 $extra_dsc");

	$rules = <<'EOF';
%:
	@dh_clean > /dev/null 2>&1
	@dh -j --buildsystem=makefile --after=dh_auto_configure --until=dh_auto_build $@
	@dh_clean > /dev/null 2>&1
EOF
	test_is_parallel( do_rules_for_parallel("build", $rules),
		"dh -j under dpkg-buildpackage -j5 is parallel $extra_dsc");
	$ENV{MAKEFLAGS} = "";
	test_is_parallel( do_rules_for_parallel("build", $rules),
		"dh -j is parallel only with DEB_BUILD_OPTIONS=parallel=5 $extra_dsc");

	if (defined $tmpfile) {
		rename($tmpfile, "debian/rules");
	}
	elsif ($debian_rules) {
		unlink("debian/rules");
	}
}

# dh should pass the same tests with and without overrides
test_dh_parallel();
test_dh_parallel("(with overrides)", <<'EOF');
#!/usr/bin/make -f
override_dh_auto_build:
	@dh_auto_build -- -f parallel.mk
EOF

# Test if legacy punctuation hacks (+) work as before
$ENV{MAKEFLAGS} = "-j5";
$ENV{DEB_BUILD_OPTIONS} = "parallel=5";
$tmp = write_debian_rules(<<'EOF');
#!/usr/bin/make -f
%:
	@dh_clean > /dev/null 2>&1
	@+dh --buildsystem=makefile --after=dh_auto_configure --until=dh_auto_build $@
	@dh_clean > /dev/null 2>&1
EOF
test_is_parallel( do_rules_for_parallel("build", "include debian/rules"),
	"legacy punctuation hacks: +dh, no override" );
unlink "debian/rules";

write_debian_rules(<<'EOF');
#!/usr/bin/make -f
override_dh_auto_build:
	dh_auto_build
%:
	@dh_clean > /dev/null 2>&1
	@+dh --buildsystem=makefile --after=dh_auto_configure --until=dh_auto_build $@
	@dh_clean > /dev/null 2>&1
EOF
test_isnt_parallel( do_rules_for_parallel("build", "include debian/rules"),
	"legacy punctuation hacks: +dh, override without +, no parallel, no make warnings" );
unlink "debian/rules";

write_debian_rules(<<'EOF');
#!/usr/bin/make -f
override_dh_auto_build:
	+dh_auto_build
%:
	@dh_clean > /dev/null 2>&1
	@+dh --buildsystem=makefile --after=dh_auto_configure --until=dh_auto_build $@
	@dh_clean > /dev/null 2>&1
EOF
test_is_parallel( do_rules_for_parallel("build", "include debian/rules"),
	"legacy punctuation hacks: +dh, override with +" );
unlink "debian/rules";

write_debian_rules(<<'EOF');
#!/usr/bin/make -f
override_dh_auto_build:
	$(MAKE)
%:
	@dh_clean > /dev/null 2>&1
	@+dh --buildsystem=makefile --after=dh_auto_configure --until=dh_auto_build $@
	@dh_clean > /dev/null 2>&1
EOF
test_is_parallel( do_rules_for_parallel("build", "include debian/rules"),
	"legacy punctuation hacks: +dh, override with \$(MAKE)" );
unlink "debian/rules";

if (defined $tmp) {
	rename($tmp, "debian/rules");
}
else {
	unlink("debian/rules");
}

# Clean up after parallel testing
END {
	system("rm", "-f", "Makefile");
}
$ENV{MAKEFLAGS} = $tmp[0] if defined $tmp[0];
$ENV{DEB_BUILD_OPTIONS} = $tmp[1] if defined $tmp[1];

END {
	system("rm", "-rf", $tmpdir);
	system("$TOPDIR/dh_clean");
}