]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Buildsystem/makefile.pm
91a6341cfc6b35e3eab4eed912837862eba3ca86
[debhelper.git] / Debian / Debhelper / Buildsystem / makefile.pm
1 # A buildsystem plugin for handling simple Makefile based projects.
2 #
3 # Copyright: © 2008 Joey Hess
4 #            © 2008-2009 Modestas Vainius
5 # License: GPL-2+
6
7 package Debian::Debhelper::Buildsystem::makefile;
8
9 use strict;
10 use Debian::Debhelper::Dh_Lib;
11 use Debian::Debhelper::Dh_Buildsystem_Bases;
12 use base 'Debian::Debhelper::Dh_Buildsystem_Chdir';
13
14 sub _exists_make_target {
15         my ($cls, $target) = @_;
16         # Use make -n to check to see if the target would do
17         # anything. There's no good way to test if a target exists.
18         my $ret=`$ENV{MAKE} -s -n $target 2>/dev/null`;
19         chomp $ret;
20         return length($ret);
21 }
22
23 sub _make_first_existing_target {
24         my $cls = shift;
25         my $targets = shift;
26
27         $ENV{MAKE}="make" unless exists $ENV{MAKE};
28         foreach my $target (@$targets) {
29                 if ($cls->_exists_make_target($target)) {
30                         doit($ENV{MAKE}, $target, @_);
31                         return $target;
32                 }
33         }
34         return undef;
35 }
36
37 sub DESCRIPTION {
38         "support for building Makefile based packages (make && make install)"
39 }
40
41 sub is_buildable {
42         my $self=shift;
43         my ($action) = @_;
44         if (grep /^\Q$action\E$/, qw{build test install clean}) {
45                 return -e $self->get_buildpath("Makefile") ||
46                        -e $self->get_buildpath("makefile") ||
47                        -e $self->get_buildpath("GNUmakefile");
48         } else {
49                 return 1;
50         }
51 }
52
53 sub build_impl {
54         my $self=shift;
55         doit(exists $ENV{MAKE} ? $ENV{MAKE} : "make", @_);
56 }
57
58 sub test_impl {
59         my $self=shift;
60         $self->_make_first_existing_target(['test', 'check'], @_);
61 }
62
63 sub install_impl {
64         my $self=shift;
65         my $destdir=shift;
66
67         $ENV{MAKE}="make" unless exists $ENV{MAKE};
68         my @params="DESTDIR=$destdir";
69
70         # Special case for MakeMaker generated Makefiles.
71         if (-e "Makefile" &&
72             system('grep -q "generated automatically by MakeMaker" Makefile') == 0) {
73                 push @params, "PREFIX=/usr";
74         }
75
76         $self->_make_first_existing_target(['install'], @params, @_);
77 }
78
79 sub clean_impl {
80         my $self=shift;
81         $self->_make_first_existing_target(['distclean', 'realclean', 'clean'], @_);
82 }
83
84 1;