#!/usr/bin/perl
-use Test::More tests => 224;
+use Test::More tests => 281;
use strict;
use warnings;
my $TOPDIR = "../..";
my @STEPS = qw(configure build test install clean);
-my @BUILDSYSTEMS = qw(autoconf perl_makemaker makefile python_distutils perl_build cmake);
my $BS_CLASS = 'Debian::Debhelper::Buildsystem';
my ($bs, @bs, %bs);
my ($cmdline, $stdin) = @_;
my ($reader, $writer);
- open2($reader, $writer, $cmdline) or die "Unable to exec $cmdline";
+ 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->_rel2rel("path/my/file", "path/my"),
+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"),
+is( $BS_CLASS->_rel2rel("path/dir/file", "path/my", "/tmp"),
"../dir/file", "_rel2rel no2" );
is( $BS_CLASS->_rel2rel("file", "/root/path/my", "/root"),
- "../../file", "_rel2rel no3" );
+ "/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 {
%tmp = (
"get_sourcedir()" => "autoconf",
"get_sourcepath(a/b)" => "autoconf/a/b",
- "get_builddir()" => "autoconf/$default_builddir",
- "get_buildpath()" => "autoconf/$default_builddir",
- "get_buildpath(a/b)" => "autoconf/$default_builddir/a/b",
- "get_source_rel2builddir()" => "..",
- "get_source_rel2builddir(a/b)" => "../a/b",
- "get_build_rel2sourcedir()" => "$default_builddir",
- "get_build_rel2sourcedir(a/b)" => "$default_builddir/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);
-# Enforced out of source tree building
-# sourcedir=builddir=autoconf hence default builddir is implied
-$bs = $BS_CLASS->new(builddir => "./autoconf", sourcedir => "autoconf/");
-$bs->enforce_out_of_source_building();
-test_buildsystem_paths_api($bs, "out of source enforced, sourcedir=autoconf/", \%tmp);
-
# sourcedir=autoconf (builddir should be dropped)
-$bs = $BS_CLASS->new(builddir => ".", sourcedir => "autoconf");
+$bs = $BS_CLASS->new(builddir => "autoconf", sourcedir => "autoconf");
%tmp = (
"get_sourcedir()" => "autoconf",
"get_sourcepath(a/b)" => "autoconf/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 sourcedir)
-$bs = $BS_CLASS->new(builddir => "../bld/dir/", sourcedir => "autoconf");
+# 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",
);
test_buildsystem_paths_api($bs, "builddir=../bld/dir, sourcedir=autoconf", \%tmp);
-# Builddir relative to the pwd (same path as above).
-$bs = $BS_CLASS->new(builddir => "./bld/dir", sourcedir => "autoconf");
-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;
-is_deeply( \@tmp, \@BUILDSYSTEMS, "load_all_buildsystems() loads all built-in buildsystems" );
+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 {
$builddir = "$tmpdir/builddir";
mkdir $builddir;
%tmp = (
- builddir => 'builddir',
+ builddir => "$tmpdir/builddir",
sourcedir => $tmpdir
);
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)=@_;
EOF
}
-is_deeply( process_stdout("DH_AUTO_OPTIONS='--builddirectory=bld\\ dir --sourcedirectory autoconf' $^X -- -",
+$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', 'makecmd=make', 'sourcedir=autoconf' ],
- "dh_auto_options w/space, autoconf autoselection and sourcedir/builddir" );
+ [ 'NAME=autoconf', 'builddir=autoconf/bld dir', "cwd=$tmp", 'makecmd=make', 'parallel=1', 'sourcedir=autoconf' ],
+ "autoconf autoselection and sourcedir/builddir" );
-is_deeply( process_stdout("$^X -- - -cautoconf -d autoconf", get_load_bs_source("autoconf", "build")),
- [ 'NAME=autoconf', 'builddir=undef', 'makecmd=make', 'sourcedir=autoconf' ],
+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 -cautoconf", get_load_bs_source("autoconf", "build")),
- [ 'NAME=autoconf', "builddir=$default_builddir", 'makecmd=make', '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
my (@lines, @extra_args);
my $buildpath = $sourcedir;
- my @dh_auto_args = ("-d", $sourcedir);
- my $dh_auto_str = "-d $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";
+ push @dh_auto_args, "-B", $builddir;
+ $dh_auto_str .= " -B $builddir";
+ $buildpath = $builddir;
}
my $do_dh_auto = sub {
dh_auto_do_autoconf('autoconf');
dh_auto_do_autoconf('autoconf', 'bld/dir', configure_args => [ "--extra-autoconf-configure-arg" ]);
-ok ( ! -e 'autoconf/bld', "autoconf/bld got deleted too" );
+ok ( ! -e 'bld', "bld got deleted too" );
+
+#### Test parallel building and related options / routines
+@tmp = ( $ENV{MAKEFLAGS}, $ENV{DEB_BUILD_OPTIONS} );
+
+# Test is_make_jobserver_unavailable and clean_jobserver_makeflags.
+
+$ENV{MAKEFLAGS} = "--jobserver-fds=103,104 -j";
+ok(is_make_jobserver_unavailable(), "unavailable jobserver" );
+clean_jobserver_makeflags();
+ok(! exists $ENV{MAKEFLAGS}, "unset makeflags");
+
+$ENV{MAKEFLAGS} = "-a --jobserver-fds=103,104 -j -b";
+ok(is_make_jobserver_unavailable(), "unavailable jobserver" );
+clean_jobserver_makeflags();
+is($ENV{MAKEFLAGS}, "-a -b", "clean makeflags");
+
+$ENV{MAKEFLAGS} = " --jobserver-fds=1,2 -j ";
+ok(! is_make_jobserver_unavailable(), "available jobserver" );
+clean_jobserver_makeflags();
+ok(! exists $ENV{MAKEFLAGS}, "unset makeflags");
+
+$ENV{MAKEFLAGS} = "-a -j -b";
+ok(! is_make_jobserver_unavailable(), "no specified jobserver");
+clean_jobserver_makeflags();
+is($ENV{MAKEFLAGS}, "-a -j -b", "clean makeflags does not remove -j");
+
+$ENV{MAKEFLAGS} = "-a --jobs -b";
+ok(! is_make_jobserver_unavailable(), "no specified jobserver");
+clean_jobserver_makeflags();
+is($ENV{MAKEFLAGS}, "-a --jobs -b", "clean makeflags does not remove --jobs");
+
+$ENV{MAKEFLAGS} = "-j6";
+ok(! is_make_jobserver_unavailable(), "no specified jobserver");
+clean_jobserver_makeflags();
+is($ENV{MAKEFLAGS}, "-j6", "clean makeflags does not remove -j6");
+
+$ENV{MAKEFLAGS} = "-a -j6 --jobs=7";
+ok(! is_make_jobserver_unavailable(), "no specified jobserver");
+clean_jobserver_makeflags();
+is($ENV{MAKEFLAGS}, "-a -j6 --jobs=7", "clean makeflags does not remove -j or --jobs");
+
+$ENV{MAKEFLAGS} = "-j6 --jobserver-fds=5,6 --jobs=8";
+ok(is_make_jobserver_unavailable(), "unavailable jobserver");
+clean_jobserver_makeflags();
+is($ENV{MAKEFLAGS}, "-j6 --jobs=8", "jobserver options removed");
+
+# 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);