-# A buildsystem plugin for handling simple Makefile based projects.
+# A debhelper build system class for handling simple Makefile based projects.
#
# Copyright: © 2008 Joey Hess
# © 2008-2009 Modestas Vainius
package Debian::Debhelper::Buildsystem::makefile;
use strict;
-use Debian::Debhelper::Dh_Lib;
-use Debian::Debhelper::Dh_Buildsystem_Bases;
-use base 'Debian::Debhelper::Dh_Buildsystem_Chdir';
+use Debian::Debhelper::Dh_Lib qw(escape_shell clean_jobserver_makeflags);
+use base 'Debian::Debhelper::Buildsystem';
+
+sub exists_make_target {
+ my ($this, $target) = @_;
-sub _exists_make_target {
- my ($cls, $target) = @_;
# Use make -n to check to see if the target would do
# anything. There's no good way to test if a target exists.
- my $ret=`$ENV{MAKE} -s -n $target 2>/dev/null`;
- chomp $ret;
- return length($ret);
+ my @opts=("-s", "-n", "--no-print-directory");
+ my $buildpath = $this->get_buildpath();
+ unshift @opts, "-C", $buildpath if $buildpath ne ".";
+ open(SAVEDERR, ">&STDERR");
+ open(STDERR, ">/dev/null");
+ open(MAKE, "-|", $this->{makecmd}, @opts, $target);
+ my $output=<MAKE>;
+ chomp $output;
+ close MAKE;
+ open(STDERR, ">&SAVEDERR");
+ return defined $output && length $output;
+}
+
+sub do_make {
+ my $this=shift;
+
+ # Avoid possible warnings about unavailable jobserver,
+ # and force make to start a new jobserver.
+ clean_jobserver_makeflags();
+
+ # Note that this will override any -j settings in MAKEFLAGS.
+ unshift @_, "-j" . ($this->get_parallel() > 0 ? $this->get_parallel() : "");
+
+ $this->doit_in_builddir($this->{makecmd}, @_);
}
-sub _make_first_existing_target {
- my $cls = shift;
- my $targets = shift;
+sub make_first_existing_target {
+ my $this=shift;
+ my $targets=shift;
- $ENV{MAKE}="make" unless exists $ENV{MAKE};
foreach my $target (@$targets) {
- if ($cls->_exists_make_target($target)) {
- doit($ENV{MAKE}, $target, @_);
+ if ($this->exists_make_target($target)) {
+ $this->do_make($target, @_);
return $target;
}
}
}
sub DESCRIPTION {
- "support for building Makefile based packages (make && make install)"
+ "simple Makefile"
+}
+
+sub new {
+ my $class=shift;
+ my $this=$class->SUPER::new(@_);
+ $this->{makecmd} = (exists $ENV{MAKE}) ? $ENV{MAKE} : "make";
+ return $this;
}
-sub is_buildable {
- my $self=shift;
- my ($action) = @_;
- if (grep /^\Q$action\E$/, qw{build test install clean}) {
- return -e $self->get_buildpath("Makefile") ||
- -e $self->get_buildpath("makefile") ||
- -e $self->get_buildpath("GNUmakefile");
- } else {
+sub check_auto_buildable {
+ my $this=shift;
+ my ($step) = @_;
+
+ if (-e $this->get_buildpath("Makefile") ||
+ -e $this->get_buildpath("makefile") ||
+ -e $this->get_buildpath("GNUmakefile"))
+ {
+ # This is always called in the source directory, but generally
+ # Makefiles are created (or live) in the the build directory.
+ return 1;
+ } elsif ($step eq "clean" && defined $this->get_builddir() &&
+ $this->check_auto_buildable("configure"))
+ {
+ # Assume that the package can be cleaned (i.e. the build directory can
+ # be removed) as long as it is built out-of-source tree and can be
+ # configured. This is useful for derivative buildsystems which
+ # generate Makefiles.
return 1;
}
+ return 0;
}
-sub build_impl {
- my $self=shift;
- doit(exists $ENV{MAKE} ? $ENV{MAKE} : "make", @_);
+sub build {
+ my $this=shift;
+ $this->do_make(@_);
}
-sub test_impl {
- my $self=shift;
- $self->_make_first_existing_target(['test', 'check'], @_);
+sub test {
+ my $this=shift;
+ $this->make_first_existing_target(['test', 'check'], @_);
}
-sub install_impl {
- my $self=shift;
+sub install {
+ my $this=shift;
my $destdir=shift;
-
- $ENV{MAKE}="make" unless exists $ENV{MAKE};
- my @params="DESTDIR=$destdir";
-
- # Special case for MakeMaker generated Makefiles.
- if (-e "Makefile" &&
- system('grep -q "generated automatically by MakeMaker" Makefile') == 0) {
- push @params, "PREFIX=/usr";
- }
-
- $self->_make_first_existing_target(['install'], @params, @_);
+ $this->make_first_existing_target(['install'],
+ "DESTDIR=$destdir",
+ "AM_UPDATE_INFO_DIR=no", @_);
}
-sub clean_impl {
- my $self=shift;
- $self->_make_first_existing_target(['distclean', 'realclean', 'clean'], @_);
+sub clean {
+ my $this=shift;
+ if (!$this->rmdir_builddir()) {
+ $this->make_first_existing_target(['distclean', 'realclean', 'clean'], @_);
+ }
}
-1;
+1