]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Buildsystem/makefile.pm
Modular object-orientied buildsystem implementation (try 2).
[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 # XXX JEH setting this env var is dodgy,
14 # probably better to test if it exists with a default value.
15 # (Factor out to helper function?)
16 # XXX MDX Done. See new().
17
18 sub get_makecmd_C {
19         my $self=shift;
20         if ($self->get_builddir()) {
21                 return $self->{makecmd} . " -C " . $self->get_builddir();
22         }
23         return $self->{makecmd};
24 }
25
26 # XXX JEH I *like* this. Yay for factoring out ugly ugly stuff!
27 # XXX MDX TODO: this could use dh debian/rules parser.
28 sub exists_make_target {
29         my ($self, $target) = @_;
30         my $makecmd=$self->get_makecmd_C();
31
32         # Use make -n to check to see if the target would do
33         # anything. There's no good way to test if a target exists.
34         my $ret=`$makecmd -s -n $target 2>/dev/null`;
35         chomp $ret;
36         return length($ret);
37 }
38
39 sub make_first_existing_target {
40         my $self=shift;
41         my $targets=shift;
42
43         foreach my $target (@$targets) {
44                 if ($self->exists_make_target($target)) {
45                         $self->doit_in_builddir($self->{makecmd}, $target, @_);
46                         return $target;
47                 }
48         }
49         return undef;
50 }
51
52 sub DESCRIPTION {
53         "support for building Makefile based packages (make && make install)"
54 }
55
56 sub new {
57         my $cls=shift;
58         my $self=$cls->SUPER::new(@_);
59         $self->{makecmd} = (exists $ENV{MAKE}) ? $ENV{MAKE} : "make";
60         return $self;
61 }
62
63 sub is_auto_buildable {
64         my $self=shift;
65         my ($action) = @_;
66
67         # Handles build, test, install, clean; configure - next class
68         if (grep /^\Q$action\E$/, qw{build test install clean}) {
69                 # This is always called in the source directory, but generally
70                 # Makefiles are created (or live) in the the build directory.
71                 return -e $self->get_buildpath("Makefile") ||
72                        -e $self->get_buildpath("makefile") ||
73                        -e $self->get_buildpath("GNUmakefile");
74         }
75         return 0;
76 }
77
78 sub build {
79         my $self=shift;
80         $self->doit_in_builddir($self->{makecmd}, @_);
81 }
82
83 sub test {
84         my $self=shift;
85         $self->make_first_existing_target(['test', 'check'], @_);
86 }
87
88 sub install {
89         my $self=shift;
90         my $destdir=shift;
91         $self->make_first_existing_target(['install'], "DESTDIR=$destdir", @_);
92 }
93
94 sub clean {
95         my $self=shift;
96         if (!$self->clean_builddir()) {
97                 $self->make_first_existing_target(['distclean', 'realclean', 'clean'], @_);
98         }
99 }
100
101 1;