]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Buildsystem/makefile.pm
merge master
[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::Buildsystem';
12
13 sub get_makecmd_C {
14         my $this=shift;
15         if ($this->get_builddir()) {
16                 return $this->{makecmd} . " -C " . $this->get_builddir();
17         }
18         return $this->{makecmd};
19 }
20
21 sub exists_make_target {
22         my ($this, $target) = @_;
23         my $makecmd=$this->get_makecmd_C();
24
25         # Use make -n to check to see if the target would do
26         # anything. There's no good way to test if a target exists.
27         my $ret=`$makecmd -s -n $target 2>/dev/null`;
28         chomp $ret;
29         return length($ret);
30 }
31
32 sub make_first_existing_target {
33         my $this=shift;
34         my $targets=shift;
35
36         foreach my $target (@$targets) {
37                 if ($this->exists_make_target($target)) {
38                         $this->doit_in_builddir($this->{makecmd}, $target, @_);
39                         return $target;
40                 }
41         }
42         return undef;
43 }
44
45 sub DESCRIPTION {
46         "simple Makefile"
47 }
48
49 sub new {
50         my $class=shift;
51         my $this=$class->SUPER::new(@_);
52         $this->{makecmd} = (exists $ENV{MAKE}) ? $ENV{MAKE} : "make";
53         return $this;
54 }
55
56 sub check_auto_buildable {
57         my $this=shift;
58         my ($action) = @_;
59
60         # Handles build, test, install, clean; configure - next class
61         if (grep /^\Q$action\E$/, qw{build test install clean}) {
62                 # This is always called in the source directory, but generally
63                 # Makefiles are created (or live) in the the build directory.
64                 return -e $this->get_buildpath("Makefile") ||
65                        -e $this->get_buildpath("makefile") ||
66                        -e $this->get_buildpath("GNUmakefile");
67         }
68         return 0;
69 }
70
71 sub build {
72         my $this=shift;
73         $this->doit_in_builddir($this->{makecmd}, @_);
74 }
75
76 sub test {
77         my $this=shift;
78         $this->make_first_existing_target(['test', 'check'], @_);
79 }
80
81 sub install {
82         my $this=shift;
83         my $destdir=shift;
84         $this->make_first_existing_target(['install'], "DESTDIR=$destdir", @_);
85 }
86
87 sub clean {
88         my $this=shift;
89         if (!$this->clean_builddir()) {
90                 $this->make_first_existing_target(['distclean', 'realclean', 'clean'], @_);
91         }
92 }
93
94 1;