]> git.donarmstrong.com Git - debhelper.git/blob - t/buildsystems/buildsystem_tests
41c0f977ecfb930674a18b9d78b3816479417fd3
[debhelper.git] / t / buildsystems / buildsystem_tests
1 #!/usr/bin/perl
2
3 use Test::More tests => 273;
4
5 use strict;
6 use warnings;
7 use IPC::Open2;
8 use Cwd ();
9 use File::Temp qw(tempfile tempdir);
10 use File::Basename ();
11
12 # Let the tests to be run from anywhere but currect directory
13 # is expected to be the one where this test lives in.
14 chdir File::Basename::dirname($0) or die "Unable to chdir to ".File::Basename::dirname($0);
15
16 use_ok( 'Debian::Debhelper::Dh_Lib' );
17 use_ok( 'Debian::Debhelper::Buildsystem' );
18 use_ok( 'Debian::Debhelper::Dh_Buildsystems' );
19
20 my $TOPDIR = "../..";
21 my @STEPS = qw(configure build test install clean);
22 my $BS_CLASS = 'Debian::Debhelper::Buildsystem';
23
24 my ($bs, @bs, %bs);
25 my ($tmp, @tmp, %tmp);
26 my ($tmpdir, $builddir, $default_builddir);
27
28 ### Common subs ####
29 sub touch {
30         my $file=shift;
31         my $chmod=shift;
32         open FILE, ">", $file and close FILE or die "Unable to touch $file";
33         chmod $chmod, $file if defined $chmod;
34 }
35
36 sub cleandir {
37         my $dir=shift;
38         system ("find", $dir, "-type", "f", "-delete");
39 }
40 sub readlines {
41         my $h=shift;
42         my @lines = <$h>;
43         close $h;
44         chop @lines;
45         return \@lines;
46 }
47
48 sub process_stdout {
49         my ($cmdline, $stdin) = @_;
50         my ($reader, $writer);
51
52         my $pid = open2($reader, $writer, $cmdline) or die "Unable to exec $cmdline";
53         print $writer $stdin if $stdin;
54         close $writer;
55         waitpid($pid, 0);
56         $? = $? >> 8; # exit status
57         return readlines($reader);
58 }
59
60 sub write_debian_rules {
61         my $contents=shift;
62         my $backup;
63
64         if (-f "debian/rules") {
65                 (undef, $backup) = tempfile(DIR => ".", OPEN => 0);
66                 rename "debian/rules", $backup;
67         }
68         # Write debian/rules if requested
69         if ($contents) {
70                 open(my $f, ">", "debian/rules");
71                 print $f $contents;;
72                 close($f);
73                 chmod 0755, "debian/rules";
74         }
75         return $backup;
76 }
77
78 ### Test Buildsystem class API methods
79 is( $BS_CLASS->canonpath("path/to/the/./nowhere/../../somewhere"),
80     "path/to/somewhere", "canonpath no1" );
81 is( $BS_CLASS->canonpath("path/to/../forward/../../somewhere"),
82     "somewhere","canonpath no2" );
83 is( $BS_CLASS->canonpath("path/to/../../../somewhere"),
84     "../somewhere","canonpath no3" );
85 is( $BS_CLASS->canonpath("./"), ".", "canonpath no4" );
86 is( $BS_CLASS->canonpath("/absolute/path/./somewhere/../to/nowhere"),
87     "/absolute/path/to/nowhere", "canonpath no5" );
88 is( $BS_CLASS->_rel2rel("path/my/file", "path/my", "/tmp"),
89     "file", "_rel2rel no1" );
90 is( $BS_CLASS->_rel2rel("path/dir/file", "path/my", "/tmp"),
91     "../dir/file", "_rel2rel no2" );
92 is( $BS_CLASS->_rel2rel("file", "/root/path/my", "/root"),
93     "/root/file", "_rel2rel abs no3" );
94 is( $BS_CLASS->_rel2rel(".", ".", "/tmp"), ".", "_rel2rel no4" );
95 is( $BS_CLASS->_rel2rel("path", "path/", "/tmp"), ".", "_rel2rel no5" );
96 is( $BS_CLASS->_rel2rel("/absolute/path", "anybase", "/tmp"),
97     "/absolute/path", "_rel2rel abs no6");
98 is( $BS_CLASS->_rel2rel("relative/path", "/absolute/base", "/tmp"),
99     "/tmp/relative/path", "_rel2rel abs no7");
100
101 ### Test Buildsystem class path API methods under different configurations
102 sub test_buildsystem_paths_api {
103         my ($bs, $config, $expected)=@_;
104
105         my $api_is = sub {
106                 my ($got, $name)=@_;
107                 is( $got, $expected->{$name}, "paths API ($config): $name")
108         };
109
110         &$api_is( $bs->get_sourcedir(), 'get_sourcedir()' );
111         &$api_is( $bs->get_sourcepath("a/b"), 'get_sourcepath(a/b)' );
112         &$api_is( $bs->get_builddir(), 'get_builddir()' );
113         &$api_is( $bs->get_buildpath(), 'get_buildpath()' );
114         &$api_is( $bs->get_buildpath("a/b"), 'get_buildpath(a/b)' );
115         &$api_is( $bs->get_source_rel2builddir(), 'get_source_rel2builddir()' );
116         &$api_is( $bs->get_source_rel2builddir("a/b"), 'get_source_rel2builddir(a/b)' );
117         &$api_is( $bs->get_build_rel2sourcedir(), 'get_build_rel2sourcedir()' );
118         &$api_is( $bs->get_build_rel2sourcedir("a/b"), 'get_build_rel2sourcedir(a/b)' );
119 }
120
121 # Defaults
122 $bs = $BS_CLASS->new();
123 $default_builddir = $bs->DEFAULT_BUILD_DIRECTORY();
124 %tmp = (
125         "get_sourcedir()" => ".",
126         "get_sourcepath(a/b)" => "./a/b",
127         "get_builddir()" => undef,
128         "get_buildpath()" => ".",
129         "get_buildpath(a/b)" =>  "./a/b",
130         "get_source_rel2builddir()" => ".",
131         "get_source_rel2builddir(a/b)" => "./a/b",
132         "get_build_rel2sourcedir()" => ".",
133         "get_build_rel2sourcedir(a/b)" => "./a/b",
134 );
135 test_buildsystem_paths_api($bs, "no builddir, no sourcedir", \%tmp);
136
137 # builddir=bld/dir
138 $bs = $BS_CLASS->new(builddir => "bld/dir");
139 %tmp = (
140         "get_sourcedir()" => ".",
141         "get_sourcepath(a/b)" => "./a/b",
142         "get_builddir()" => "bld/dir",
143         "get_buildpath()" => "bld/dir",
144         "get_buildpath(a/b)" =>  "bld/dir/a/b",
145         "get_source_rel2builddir()" => "../..",
146         "get_source_rel2builddir(a/b)" => "../../a/b",
147         "get_build_rel2sourcedir()" => "bld/dir",
148         "get_build_rel2sourcedir(a/b)" => "bld/dir/a/b",
149 );
150 test_buildsystem_paths_api($bs, "builddir=bld/dir, no sourcedir", \%tmp);
151
152 # Default builddir, sourcedir=autoconf
153 $bs = $BS_CLASS->new(builddir => undef, sourcedir => "autoconf");
154 %tmp = (
155         "get_sourcedir()" => "autoconf",
156         "get_sourcepath(a/b)" => "autoconf/a/b",
157         "get_builddir()" => "$default_builddir",
158         "get_buildpath()" => "$default_builddir",
159         "get_buildpath(a/b)" =>  "$default_builddir/a/b",
160         "get_source_rel2builddir()" => "../autoconf",
161         "get_source_rel2builddir(a/b)" => "../autoconf/a/b",
162         "get_build_rel2sourcedir()" => "../$default_builddir",
163         "get_build_rel2sourcedir(a/b)" => "../$default_builddir/a/b",
164 );
165 test_buildsystem_paths_api($bs, "default builddir, sourcedir=autoconf", \%tmp);
166
167 # sourcedir=autoconf (builddir should be dropped)
168 $bs = $BS_CLASS->new(builddir => "autoconf", sourcedir => "autoconf");
169 %tmp = (
170         "get_sourcedir()" => "autoconf",
171         "get_sourcepath(a/b)" => "autoconf/a/b",
172         "get_builddir()" => undef,
173         "get_buildpath()" => "autoconf",
174         "get_buildpath(a/b)" =>  "autoconf/a/b",
175         "get_source_rel2builddir()" => ".",
176         "get_source_rel2builddir(a/b)" => "./a/b",
177         "get_build_rel2sourcedir()" => ".",
178         "get_build_rel2sourcedir(a/b)" => "./a/b",
179 );
180 test_buildsystem_paths_api($bs, "no builddir, sourcedir=autoconf", \%tmp);
181
182 # Prefer out of source tree building when
183 # sourcedir=builddir=autoconf hence builddir should be dropped.
184 $bs->prefer_out_of_source_building(builddir => "autoconf");
185 test_buildsystem_paths_api($bs, "out of source prefered, sourcedir=builddir", \%tmp);
186
187 # builddir=bld/dir, sourcedir=autoconf. Should be the same as sourcedir=autoconf.
188 $bs = $BS_CLASS->new(builddir => "bld/dir", sourcedir => "autoconf");
189 $bs->enforce_in_source_building();
190 test_buildsystem_paths_api($bs, "in source enforced, sourcedir=autoconf", \%tmp);
191
192 # builddir=../bld/dir (relative to the curdir)
193 $bs = $BS_CLASS->new(builddir => "bld/dir/", sourcedir => "autoconf");
194 %tmp = (
195         "get_sourcedir()" => "autoconf",
196         "get_sourcepath(a/b)" => "autoconf/a/b",
197         "get_builddir()" => "bld/dir",
198         "get_buildpath()" => "bld/dir",
199         "get_buildpath(a/b)" =>  "bld/dir/a/b",
200         "get_source_rel2builddir()" => "../../autoconf",
201         "get_source_rel2builddir(a/b)" => "../../autoconf/a/b",
202         "get_build_rel2sourcedir()" => "../bld/dir",
203         "get_build_rel2sourcedir(a/b)" => "../bld/dir/a/b",
204 );
205 test_buildsystem_paths_api($bs, "builddir=../bld/dir, sourcedir=autoconf", \%tmp);
206
207 ### Test if all buildsystems can be loaded
208 @bs = load_all_buildsystems([ $INC[0] ]);
209 @tmp = map { $_->NAME() } @bs;
210 ok(@Debian::Debhelper::Dh_Buildsystems::BUILDSYSTEMS >= 1, "some build systems are built in" );
211 is_deeply( \@tmp, \@Debian::Debhelper::Dh_Buildsystems::BUILDSYSTEMS, "load_all_buildsystems() loads all built-in buildsystems" );
212
213 ### Test check_auto_buildable() of each buildsystem
214 sub test_check_auto_buildable {
215         my $bs=shift;
216         my $config=shift;
217         my $expected=shift;
218         my @steps=@_ || @STEPS;
219
220         if (! ref $expected) {
221                 my %all_steps;
222                 $all_steps{$_} = $expected foreach (@steps);
223                 $expected = \%all_steps;
224         }
225         for my $step (@steps) {
226                 my $e = 0;
227                 if (exists $expected->{$step}) {
228                         $e = $expected->{$step};
229                 } elsif (exists $expected->{default}) {
230                         $e = $expected->{default};
231                 }
232                 if ($e) {
233                         ok( $bs->check_auto_buildable($step),
234                             $bs->NAME() . "($config): check_auto_buildable($step)" );
235                 }
236                 else {
237                         ok( ! $bs->check_auto_buildable($step),
238                             $bs->NAME() . "($config): ! check_auto_buildable($step)" );
239                 }
240         }
241 }
242
243 $tmpdir = tempdir("tmp.XXXXXX");
244 $builddir = "$tmpdir/builddir";
245 mkdir $builddir;
246 %tmp = (
247         builddir => "$tmpdir/builddir",
248         sourcedir => $tmpdir
249 );
250
251 $bs{autoconf} = load_buildsystem("autoconf", undef, %tmp);
252 $bs{cmake} = load_buildsystem("cmake", undef, %tmp);
253 $bs{perl_mm} = load_buildsystem("perl_makemaker", undef, %tmp);
254 $bs = load_buildsystem("makefile", undef, %tmp);
255
256 test_check_auto_buildable($bs{autoconf}, "no configure", 0);
257 test_check_auto_buildable($bs{cmake}, "no CMakeLists.txt", 0);
258 test_check_auto_buildable($bs{perl_mm}, "no Makefile.PL", 0);
259 test_check_auto_buildable($bs, "no Makefile", 0);
260
261 touch "$tmpdir/configure", 0755;
262 test_check_auto_buildable($bs{autoconf}, "configure", { configure => 1 });
263
264 touch "$tmpdir/CMakeLists.txt";
265 test_check_auto_buildable($bs{cmake}, "CMakeLists.txt", { configure => 1 });
266
267 touch "$tmpdir/Makefile.PL";
268 test_check_auto_buildable($bs{perl_mm}, "Makefile.PL",
269     { configure => 1, install => 1 });
270
271 # With Makefile
272 touch "$builddir/Makefile";
273 test_check_auto_buildable($bs, "Makefile", { configure => 0, default => 1 });
274 test_check_auto_buildable($bs{autoconf}, "configure+Makefile", { configure => 1 });
275 test_check_auto_buildable($bs{cmake}, "CMakeLists.txt+Makefile", 1);
276
277 # Makefile.PL forces in-source
278 #(see note in check_auto_buildable() why always 1 here)
279 unlink "$builddir/Makefile";
280 touch "$tmpdir/Makefile";
281 test_check_auto_buildable($bs{perl_mm}, "Makefile.PL+Makefile", 1);
282
283 # Perl Build.PL - handles always
284 $bs = load_buildsystem("perl_build", undef, %tmp);
285 test_check_auto_buildable($bs, "no Build.PL", 0);
286 touch "$tmpdir/Build.PL";
287 test_check_auto_buildable($bs, "Build.PL", { configure => 1 });
288 touch "$tmpdir/Build"; # forced in source
289 test_check_auto_buildable($bs, "Build.PL+Build", 1);
290
291 # Python Distutils
292 $bs = load_buildsystem("python_distutils", undef, %tmp);
293 test_check_auto_buildable($bs, "no setup.py", 0);
294 touch "$tmpdir/setup.py";
295 test_check_auto_buildable($bs, "setup.py", 1);
296
297 cleandir($tmpdir);
298
299 ### Now test if it can autoselect a proper buildsystem for a typical package
300 sub test_autoselection {
301         my $system=shift;
302         my $expected=shift;
303         for my $step (@STEPS) {
304                 my $bs = load_buildsystem(undef, $step, @_);
305                 my $e = $expected;
306                 $e = $expected->{$step} if ref $expected;
307                 if (defined $bs) {
308                         is( $bs->NAME(), $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
309                 }
310                 else {
311                         is ( undef, $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
312                 }
313         }
314 }
315
316 # Autoconf
317 touch "$tmpdir/configure", 0755;
318 touch "$builddir/Makefile";
319 test_autoselection("autoconf",
320     { configure => "autoconf", build => "makefile",
321       test => "makefile", install => "makefile", clean => "makefile" }, %tmp);
322 cleandir $tmpdir;
323
324 # Perl Makemaker (build, test, clean fail with builddir set [not supported])
325 touch "$tmpdir/Makefile.PL";
326 touch "$tmpdir/Makefile";
327 test_autoselection("perl_makemaker", "perl_makemaker", %tmp);
328 cleandir $tmpdir;
329
330 # Makefile
331 touch "$builddir/Makefile";
332 test_autoselection("makefile", { build => "makefile", test => "makefile",
333                 install => "makefile", clean => "makefile" }, %tmp);
334 cleandir $tmpdir;
335
336 # Python Distutils
337 touch "$tmpdir/setup.py";
338 test_autoselection("python_distutils", "python_distutils", %tmp);
339 cleandir $tmpdir;
340
341 # Perl Build
342 touch "$tmpdir/Build.PL";
343 touch "$tmpdir/Build";
344 test_autoselection("perl_build", "perl_build", %tmp);
345 cleandir $tmpdir;
346
347 # CMake
348 touch "$tmpdir/CMakeLists.txt";
349 touch "$builddir/Makefile";
350 test_autoselection("cmake",
351     { configure => "cmake", build => "makefile",
352       test => "makefile", install => "makefile", clean => "makefile" }, %tmp);
353 cleandir $tmpdir;
354
355 ### Test Buildsystem::rmdir_builddir()
356 sub do_rmdir_builddir {
357         my $builddir=shift;
358         my $system;
359         $system = $BS_CLASS->new(builddir => $builddir, sourcedir => $tmpdir);
360         $system->mkdir_builddir();
361         $system->rmdir_builddir();
362 }
363
364 $builddir = "$tmpdir/builddir";
365 do_rmdir_builddir($builddir);
366 ok ( ! -e $builddir, "testing rmdir_builddir() 1: builddir parent '$builddir' deleted" );
367 ok ( -d $tmpdir, "testing rmdir_builddir() 1: sourcedir '$tmpdir' remains" );
368
369 $builddir = "$tmpdir/bld";
370 do_rmdir_builddir("$builddir/dir");
371 ok ( ! -e $builddir, "testing rmdir_builddir() 2: builddir parent '$builddir' deleted" );
372 ok ( -d $tmpdir, "testing rmdir_builddir() 2: sourcedir '$tmpdir' remains" );
373
374 $builddir = "$tmpdir/bld";
375 mkdir "$builddir";
376 touch "$builddir/afile";
377 mkdir "$builddir/dir";
378 touch "$builddir/dir/afile2";
379 do_rmdir_builddir("$builddir/dir");
380 ok ( ! -e "$builddir/dir", "testing rmdir_builddir() 3: builddir '$builddir/dir' not empty, but deleted" );
381 ok ( -d $builddir, "testing rmdir_builddir() 3: builddir parent '$builddir' not empty, remains" );
382
383 cleandir $tmpdir;
384
385 ### Test buildsystems_init() and commandline/env argument handling
386 sub get_load_bs_source {
387         my ($system, $step)=@_;
388         $step = (defined $step) ? "'$step'" : 'undef';
389         $system = (defined $system) ? "'$system'" : 'undef';
390
391 return <<EOF;
392 use strict;
393 use warnings;
394 use Debian::Debhelper::Dh_Buildsystems;
395
396 buildsystems_init();
397 my \$bs = load_buildsystem($system, $step);
398 if (defined \$bs) {
399         print 'NAME=', \$bs->NAME(), "\\n";
400         print \$_, "=", (defined \$bs->{\$_}) ? \$bs->{\$_} : 'undef', "\\n"
401             foreach (sort keys \%\$bs);
402 }
403 EOF
404 }
405
406 $tmp = Cwd::getcwd();
407 # NOTE: disabling parallel building explicitly (it might get automatically
408 # enabled if run under dpkg-buildpackage -jX) to make output deterministic.
409 is_deeply( process_stdout("$^X -- - --builddirectory='autoconf/bld dir' --sourcedirectory autoconf --parallel=1",
410                           get_load_bs_source(undef, "configure")),
411     [ 'NAME=autoconf', 'builddir=autoconf/bld dir', "cwd=$tmp",  'makecmd=make', 'parallel=1', 'sourcedir=autoconf' ],
412     "autoconf autoselection and sourcedir/builddir" );
413
414 is_deeply( process_stdout("$^X -- - -Sautoconf -D autoconf --parallel=1", get_load_bs_source("autoconf", "build")),
415     [ 'NAME=autoconf', 'builddir=undef', "cwd=$tmp", 'makecmd=make', 'parallel=1', 'sourcedir=autoconf' ],
416     "forced autoconf and sourcedir" );
417
418 is_deeply( process_stdout("$^X -- - -B -Sautoconf --parallel=1", get_load_bs_source("autoconf", "build")),
419     [ 'NAME=autoconf', "builddir=$default_builddir", "cwd=$tmp", 'makecmd=make', 'parallel=1', 'sourcedir=.' ],
420     "forced autoconf and default build directory" );
421
422 # Build the autoconf test package
423 sub dh_auto_do_autoconf {
424         my $sourcedir=shift;
425         my $builddir=shift;
426         my %args=@_;
427
428         my (@lines, @extra_args);
429         my $buildpath = $sourcedir;
430         my @dh_auto_args = ("-D", $sourcedir);
431         my $dh_auto_str = "-D $sourcedir";
432         if ($builddir) {
433                 push @dh_auto_args, "-B", $builddir;
434                 $dh_auto_str .= " -B $builddir";
435                 $buildpath = $builddir;
436         }
437
438         my $do_dh_auto = sub {
439                 my $step=shift;
440                 my @extra_args;
441                 my $extra_str = "";
442                 if (exists $args{"${step}_args"}) {
443                         push @extra_args, @{$args{"${step}_args"}};
444                         $extra_str .= " $_" foreach (@extra_args);
445                 }
446                 is ( system("$TOPDIR/dh_auto_$step", @dh_auto_args, "--", @extra_args), 0,
447                          "dh_auto_$step $dh_auto_str$extra_str" );
448                 return @extra_args;
449         };
450         
451         @extra_args = &$do_dh_auto('configure');
452         ok ( -f "$buildpath/Makefile", "$buildpath/Makefile exists" );
453         @lines=();
454         if (ok( open(FILE, "$buildpath/stamp_configure"), "$buildpath/stamp_configure exists") ) {
455                 @lines = @{readlines(\*FILE)};
456         }
457         is_deeply( \@lines, \@extra_args, "$buildpath/stamp_configure contains extra args" );
458
459         &$do_dh_auto('build');
460         ok ( -f "$buildpath/stamp_build", "$buildpath/stamp_build exists" );
461         &$do_dh_auto('test');
462         ok ( -f "$buildpath/stamp_test", "$buildpath/stamp_test exists" );
463         &$do_dh_auto('install');
464         @lines=();
465         if ( ok(open(FILE, "$buildpath/stamp_install"), "$buildpath/stamp_install exists") ) {
466                 @lines = @{readlines(\*FILE)};
467         } 
468         is_deeply( \@lines, [ "DESTDIR=".Cwd::getcwd()."/debian/testpackage" ],
469             "$buildpath/stamp_install contains DESTDIR" );
470         &$do_dh_auto('clean');
471         if ($builddir) {
472                 ok ( ! -e "$buildpath", "builddir $buildpath was removed" );
473         }
474         else {
475                 ok ( ! -e "$buildpath/Makefile" && ! -e "$buildpath/stamp_configure", "Makefile and stamps gone" );
476         }
477         ok ( -x "$sourcedir/configure", "configure script renamins after clean" );
478 }
479
480 dh_auto_do_autoconf('autoconf');
481 dh_auto_do_autoconf('autoconf', 'bld/dir', configure_args => [ "--extra-autoconf-configure-arg" ]);
482 ok ( ! -e 'bld', "bld got deleted too" );
483
484 #### Test parallel building and related options / routines
485 @tmp = ( $ENV{MAKEFLAGS}, $ENV{DEB_BUILD_OPTIONS} );
486
487 # Test get_make_jobserver_status() sub
488
489 $ENV{MAKEFLAGS} = "--jobserver-fds=103,104 -j";
490 is_deeply( [ get_make_jobserver_status() ], [ "jobserver-unavailable", undef ],
491         "get_make_jobserver_status(): unavailable jobserver, unset makeflags" );
492
493 $ENV{MAKEFLAGS} = "-a --jobserver-fds=103,104 -j -b";
494 is_deeply( [ get_make_jobserver_status() ], [ "jobserver-unavailable", "-a -b" ],
495         "get_make_jobserver_status(): unavailable jobserver, clean makeflags" );
496
497 $ENV{MAKEFLAGS} = " --jobserver-fds=1,2 -j  ";
498 is_deeply( [ get_make_jobserver_status() ], [ "jobserver", undef ],
499         "get_make_jobserver_status(): jobserver (available), clean makeflags" );
500
501 $ENV{MAKEFLAGS} = "-a -j -b";
502 is_deeply( [ get_make_jobserver_status() ], [ "jobs-0", "-a -b" ],
503         "get_make_jobserver_status(): -j" );
504
505 $ENV{MAKEFLAGS} = "-a --jobs -b";
506 is_deeply( [ get_make_jobserver_status() ], [ "jobs-0", "-a -b" ],
507         "get_make_jobserver_status(): --jobs" );
508
509 $ENV{MAKEFLAGS} = "-j6";
510 is_deeply( [ get_make_jobserver_status() ], [ "jobs-6", undef ],
511         "get_make_jobserver_status(): -j6" );
512
513 $ENV{MAKEFLAGS} = "-a -j6 --jobs=7";
514 is_deeply( [ get_make_jobserver_status() ], [ "jobs-7", "-a" ],
515         "get_make_jobserver_status(): -j6 --jobs=7" );
516
517 $ENV{MAKEFLAGS} = "-j6 --jobserver-fds=5,6 --jobs=8";
518 is_deeply( [ get_make_jobserver_status() ], [ "jobserver-unavailable", "-j6 --jobs=8" ],
519         "get_make_jobserver_status(): mixed jobserver and -j/--jobs" );
520
521 # Test parallel building with makefile build system.
522 $ENV{MAKEFLAGS} = "";
523 $ENV{DEB_BUILD_OPTIONS} = "";
524
525 sub do_parallel_mk {
526         my $dh_opts=shift || "";
527         my $make_opts=shift || "";
528         return process_stdout(
529                 "LANG=C LC_ALL=C LC_MESSAGES=C $TOPDIR/dh_auto_build -Smakefile $dh_opts " .
530                 "-- -s -f parallel.mk $make_opts 2>&1 >/dev/null", "");
531 }
532
533 sub test_isnt_parallel {
534         my ($got, $desc) = @_;
535         my @makemsgs = grep /^make[\d\[\]]*:/, @$got;
536         if (@makemsgs) {
537                 like( $makemsgs[0], qr/Error 10/, $desc );
538         }
539         else {
540                 ok( scalar(@makemsgs) > 0, $desc );
541         }
542 }
543
544 sub test_is_parallel {
545         my ($got, $desc) = @_;
546         is_deeply( $got, [] , $desc );
547         is( $?, 0, "(exit status=0) $desc");
548 }
549
550 test_isnt_parallel( do_parallel_mk(),
551         "No parallel by default" );
552 test_isnt_parallel( do_parallel_mk("--parallel"),
553         "No parallel by default with --parallel" );
554
555 $ENV{DEB_BUILD_OPTIONS}="parallel=5";
556 test_isnt_parallel( do_parallel_mk(),
557         "DEB_BUILD_OPTIONS=parallel=5 without --parallel" );
558 test_is_parallel( do_parallel_mk("--parallel"),
559         "DEB_BUILD_OPTIONS=parallel=5 with --parallel" );
560 test_is_parallel( do_parallel_mk("--parallel=2"),
561         "DEB_BUILD_OPTIONS=parallel=5 with --parallel=2" );
562 test_isnt_parallel( do_parallel_mk("--parallel=1"),
563         "DEB_BUILD_OPTIONS=parallel=5 with --parallel=1 (off)" );
564
565 $ENV{MAKEFLAGS} = "--jobserver-fds=105,106 -j";
566 $ENV{DEB_BUILD_OPTIONS}="";
567 test_isnt_parallel( do_parallel_mk(),
568         "makefile.pm (no parallel): no make warnings about unavailable jobserver" );
569 $ENV{DEB_BUILD_OPTIONS}="parallel=5";
570 test_is_parallel( do_parallel_mk("--parallel"),
571         "DEB_BUILD_OPTIONS=parallel=5 with --parallel: no make warnings about unavail parent jobserver" );
572
573 $ENV{MAKEFLAGS} = "-j2";
574 $ENV{DEB_BUILD_OPTIONS}="";
575 test_is_parallel( do_parallel_mk(),
576         "MAKEFLAGS=-j2 without --parallel: dh_auto_build honours MAKEFLAGS" );
577 test_isnt_parallel( do_parallel_mk("--parallel=1"),
578         "MAKEFLAGS=-j2 with --parallel=1: dh_auto_build enforces -j1" );
579
580 # Test dh dpkg-buildpackage -jX detection
581 sub do_rules_for_parallel {
582         my $cmdline=shift || "";
583         my $stdin=shift || "";
584         return process_stdout("LANG=C LC_ALL=C LC_MESSAGES=C PATH=$TOPDIR:\$PATH " .
585                 "make -f - $cmdline 2>&1 >/dev/null", $stdin);
586 }
587
588 # Simulate dpkg-buildpackage -j5
589 doit("ln", "-s", "parallel.mk", "Makefile");
590
591 sub test_dh_parallel {
592         my $extra_dsc=shift || "";
593         my $debian_rules=shift || "";
594         my $rules;
595         my $tmpfile;
596
597         $ENV{MAKEFLAGS} = "-j5";
598         $ENV{DEB_BUILD_OPTIONS} = "parallel=5";
599
600         # Write debian/rules if requested
601         $tmpfile = write_debian_rules($debian_rules);
602
603         $rules = <<'EOF';
604 %:
605         @dh_clean > /dev/null 2>&1
606         @dh --buildsystem=makefile --after=dh_auto_configure --until=dh_auto_build $@
607         @dh_clean > /dev/null 2>&1
608 EOF
609         test_is_parallel( do_rules_for_parallel("build", $rules),
610                 "dh adds --parallel implicitly under dpkg-buildpackage -j5 $extra_dsc");
611
612         $ENV{MAKEFLAGS} = "";
613         test_isnt_parallel( do_rules_for_parallel("build", $rules),
614                 "DEB_BUILD_OPTIONS=parallel=5 without MAKEFLAGS=-jX via dh $extra_dsc" );
615
616         $ENV{MAKEFLAGS} = "-j5";
617         $rules = <<'EOF';
618 %:
619         @dh_clean > /dev/null 2>&1
620         @dh -j1 --buildsystem=makefile --after=dh_auto_configure --until=dh_auto_build $@
621         @dh_clean > /dev/null 2>&1
622 EOF
623         test_isnt_parallel( do_rules_for_parallel("build", $rules),
624                 "dh -j1 disables implicit parallel under dpkg-buildpackage -j5 $extra_dsc");
625
626         $rules = <<'EOF';
627 %:
628         @dh_clean > /dev/null 2>&1
629         @dh -j --buildsystem=makefile --after=dh_auto_configure --until=dh_auto_build $@
630         @dh_clean > /dev/null 2>&1
631 EOF
632         test_is_parallel( do_rules_for_parallel("build", $rules),
633                 "dh -j under dpkg-buildpackage -j5 is parallel $extra_dsc");
634         $ENV{MAKEFLAGS} = "";
635         test_is_parallel( do_rules_for_parallel("build", $rules),
636                 "dh -j is parallel only with DEB_BUILD_OPTIONS=parallel=5 $extra_dsc");
637
638         if (defined $tmpfile) {
639                 rename($tmpfile, "debian/rules");
640         }
641         elsif ($debian_rules) {
642                 unlink("debian/rules");
643         }
644 }
645
646 # dh should pass the same tests with and without overrides
647 test_dh_parallel();
648 test_dh_parallel("(with overrides)", <<'EOF');
649 #!/usr/bin/make -f
650 override_dh_auto_build:
651         @dh_auto_build -- -f parallel.mk
652 EOF
653
654 # Test if legacy punctuation hacks (+) work as before
655 $ENV{MAKEFLAGS} = "-j5";
656 $ENV{DEB_BUILD_OPTIONS} = "parallel=5";
657 $tmp = write_debian_rules(<<'EOF');
658 #!/usr/bin/make -f
659 %:
660         @dh_clean > /dev/null 2>&1
661         @+dh --buildsystem=makefile --after=dh_auto_configure --until=dh_auto_build $@
662         @dh_clean > /dev/null 2>&1
663 EOF
664 test_is_parallel( do_rules_for_parallel("build", "include debian/rules"),
665         "legacy punctuation hacks: +dh, no override" );
666 unlink "debian/rules";
667
668 write_debian_rules(<<'EOF');
669 #!/usr/bin/make -f
670 override_dh_auto_build:
671         dh_auto_build
672 %:
673         @dh_clean > /dev/null 2>&1
674         @+dh --buildsystem=makefile --after=dh_auto_configure --until=dh_auto_build $@
675         @dh_clean > /dev/null 2>&1
676 EOF
677 test_isnt_parallel( do_rules_for_parallel("build", "include debian/rules"),
678         "legacy punctuation hacks: +dh, override without +, no parallel, no make warnings" );
679 unlink "debian/rules";
680
681 write_debian_rules(<<'EOF');
682 #!/usr/bin/make -f
683 override_dh_auto_build:
684         +dh_auto_build
685 %:
686         @dh_clean > /dev/null 2>&1
687         @+dh --buildsystem=makefile --after=dh_auto_configure --until=dh_auto_build $@
688         @dh_clean > /dev/null 2>&1
689 EOF
690 test_is_parallel( do_rules_for_parallel("build", "include debian/rules"),
691         "legacy punctuation hacks: +dh, override with +" );
692 unlink "debian/rules";
693
694 write_debian_rules(<<'EOF');
695 #!/usr/bin/make -f
696 override_dh_auto_build:
697         $(MAKE)
698 %:
699         @dh_clean > /dev/null 2>&1
700         @+dh --buildsystem=makefile --after=dh_auto_configure --until=dh_auto_build $@
701         @dh_clean > /dev/null 2>&1
702 EOF
703 test_is_parallel( do_rules_for_parallel("build", "include debian/rules"),
704         "legacy punctuation hacks: +dh, override with \$(MAKE)" );
705 unlink "debian/rules";
706
707 if (defined $tmp) {
708         rename($tmp, "debian/rules");
709 }
710 else {
711         unlink("debian/rules");
712 }
713
714 # Clean up after parallel testing
715 END {
716         system("rm", "-f", "Makefile");
717 }
718 $ENV{MAKEFLAGS} = $tmp[0] if defined $tmp[0];
719 $ENV{DEB_BUILD_OPTIONS} = $tmp[1] if defined $tmp[1];
720
721 END {
722         system("rm", "-rf", $tmpdir);
723         system("$TOPDIR/dh_clean");
724 }