]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Buildsystem/makefile.pm
more 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 base 'Debian::Debhelper::Dh_Buildsystem_Basic';
12
13 sub get_makecmd_C {
14         my $self=shift;
15         if ($self->get_builddir()) {
16                 return $self->{makecmd} . " -C " . $self->get_builddir();
17         }
18         return $self->{makecmd};
19 }
20
21 # XXX JEH I *like* this. Yay for factoring out ugly ugly stuff!
22 # XXX MDX TODO: this could use dh debian/rules parser.
23 # XXX JEH That one checks for explicit only targets, while we want
24 #         implicit targets here too. I think the current code is ok;
25 #         it's a bonus that it checks if the target it empty.
26 #         Hmm, one problem is that if a target exists but will run no
27 #         commands since it's already built, the approach below will return
28 #         nothing and assume it doesn't exist.
29 sub exists_make_target {
30         my ($self, $target) = @_;
31         my $makecmd=$self->get_makecmd_C();
32
33         # Use make -n to check to see if the target would do
34         # anything. There's no good way to test if a target exists.
35         my $ret=`$makecmd -s -n $target 2>/dev/null`;
36         chomp $ret;
37         return length($ret);
38 }
39
40 sub make_first_existing_target {
41         my $self=shift;
42         my $targets=shift;
43
44         foreach my $target (@$targets) {
45                 if ($self->exists_make_target($target)) {
46                         $self->doit_in_builddir($self->{makecmd}, $target, @_);
47                         return $target;
48                 }
49         }
50         return undef;
51 }
52
53 sub DESCRIPTION {
54         "support for building Makefile based packages (make && make install)"
55 }
56
57 sub new {
58         my $cls=shift;
59         my $self=$cls->SUPER::new(@_);
60         $self->{makecmd} = (exists $ENV{MAKE}) ? $ENV{MAKE} : "make";
61         return $self;
62 }
63
64 sub is_auto_buildable {
65         my $self=shift;
66         my ($action) = @_;
67
68         # Handles build, test, install, clean; configure - next class
69         # XXX JEH shouldn't it also handle configure, just as a no-op?
70         if (grep /^\Q$action\E$/, qw{build test install clean}) {
71                 # This is always called in the source directory, but generally
72                 # Makefiles are created (or live) in the the build directory.
73                 return -e $self->get_buildpath("Makefile") ||
74                        -e $self->get_buildpath("makefile") ||
75                        -e $self->get_buildpath("GNUmakefile");
76         }
77         return 0;
78 }
79
80 sub build {
81         my $self=shift;
82         $self->doit_in_builddir($self->{makecmd}, @_);
83 }
84
85 sub test {
86         my $self=shift;
87         $self->make_first_existing_target(['test', 'check'], @_);
88 }
89
90 sub install {
91         my $self=shift;
92         my $destdir=shift;
93         $self->make_first_existing_target(['install'], "DESTDIR=$destdir", @_);
94 }
95
96 sub clean {
97         my $self=shift;
98         if (!$self->clean_builddir()) {
99                 $self->make_first_existing_target(['distclean', 'realclean', 'clean'], @_);
100         }
101 }
102
103 1;