]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Buildsystem/makefile.pm
code review, added comments
[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 # XXX JEH I *like* this. Yay for factoring out ugly ugly stuff!
15 sub _exists_make_target {
16         my ($cls, $target) = @_;
17         # Use make -n to check to see if the target would do
18         # anything. There's no good way to test if a target exists.
19         my $ret=`$ENV{MAKE} -s -n $target 2>/dev/null`;
20         chomp $ret;
21         return length($ret);
22 }
23
24 sub _make_first_existing_target {
25         my $cls = shift;
26         my $targets = shift;
27
28         # XXX JEH setting this env var is dodgy,
29         # probably better to test if it exists with a default value.
30         # (Factor out to helper function?)
31         $ENV{MAKE}="make" unless exists $ENV{MAKE};
32         foreach my $target (@$targets) {
33                 if ($cls->_exists_make_target($target)) {
34                         doit($ENV{MAKE}, $target, @_);
35                         return $target;
36                 }
37         }
38         return undef;
39 }
40
41 sub DESCRIPTION {
42         "support for building Makefile based packages (make && make install)"
43 }
44
45 sub is_buildable {
46         my $self=shift;
47         my ($action) = @_;
48         if (grep /^\Q$action\E$/, qw{build test install clean}) {
49                 # XXX JEH why does get_buildpath need to be used 
50                 # here? is_buildable is run at the top of the source
51                 # directory, so -e 'Makefile' should be the same
52                 return -e $self->get_buildpath("Makefile") ||
53                        -e $self->get_buildpath("makefile") ||
54                        -e $self->get_buildpath("GNUmakefile");
55         } else {
56                 # XXX JEH why return 1 here?
57                 return 1;
58         }
59 }
60
61 sub build_impl {
62         my $self=shift;
63         doit(exists $ENV{MAKE} ? $ENV{MAKE} : "make", @_);
64 }
65
66 sub test_impl {
67         my $self=shift;
68         $self->_make_first_existing_target(['test', 'check'], @_);
69 }
70
71 sub install_impl {
72         my $self=shift;
73         my $destdir=shift;
74
75         # XXX JEH again with the setting the env var, see above..
76         $ENV{MAKE}="make" unless exists $ENV{MAKE};
77         my @params="DESTDIR=$destdir";
78
79         # Special case for MakeMaker generated Makefiles.
80         # XXX JEH This is a really unfortunate breaking of the
81         # encapsulation of the perl_makefile module. Perhaps it would be
82         # better for that module to contain some hack that injects that
83         # test into this one?
84         if (-e "Makefile" &&
85             system('grep -q "generated automatically by MakeMaker" Makefile') == 0) {
86                 push @params, "PREFIX=/usr";
87         }
88
89         $self->_make_first_existing_target(['install'], @params, @_);
90 }
91
92 sub clean_impl {
93         my $self=shift;
94         $self->_make_first_existing_target(['distclean', 'realclean', 'clean'], @_);
95 }
96
97 1;