]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Buildsystem/makefile.pm
2c14c15184b92bcd26301676a8e33648509b6cf9
[debhelper.git] / Debian / Debhelper / Buildsystem / makefile.pm
1 # A debhelper build system class 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 qw(escape_shell clean_jobserver_makeflags);
11 use base 'Debian::Debhelper::Buildsystem';
12
13 sub exists_make_target {
14         my ($this, $target) = @_;
15
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 @opts=("-s", "-n", "--no-print-directory");
19         my $buildpath = $this->get_buildpath();
20         unshift @opts, "-C", $buildpath if $buildpath ne ".";
21         open(SAVEDERR, ">&STDERR");
22         close STDERR;
23         open(MAKE, "-|", $this->{makecmd}, @opts, $target);
24         my $output=<MAKE>;
25         close MAKE;
26         open(STDERR, ">&SAVEDERR");
27         return defined $output && length $output;
28 }
29
30 sub do_make {
31         my $this=shift;
32
33         # Avoid possible warnings about unavailable jobserver,
34         # and force make to start a new jobserver.
35         clean_jobserver_makeflags();
36
37         # Note that this will override any -j settings in MAKEFLAGS.
38         unshift @_, "-j" . ($this->get_parallel() > 0 ? $this->get_parallel() : "");
39
40         $this->doit_in_builddir($this->{makecmd}, @_);
41 }
42
43 sub make_first_existing_target {
44         my $this=shift;
45         my $targets=shift;
46
47         foreach my $target (@$targets) {
48                 if ($this->exists_make_target($target)) {
49                         $this->do_make($target, @_);
50                         return $target;
51                 }
52         }
53         return undef;
54 }
55
56 sub DESCRIPTION {
57         "simple Makefile"
58 }
59
60 sub new {
61         my $class=shift;
62         my $this=$class->SUPER::new(@_);
63         $this->{makecmd} = (exists $ENV{MAKE}) ? $ENV{MAKE} : "make";
64         return $this;
65 }
66
67 sub check_auto_buildable {
68         my $this=shift;
69         my ($step) = @_;
70
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 $this->get_buildpath("Makefile") ||
74                 -e $this->get_buildpath("makefile") ||
75                 -e $this->get_buildpath("GNUmakefile")) ? 1 : 0;
76 }
77
78 sub build {
79         my $this=shift;
80         $this->do_make(@_);
81 }
82
83 sub test {
84         my $this=shift;
85         $this->make_first_existing_target(['test', 'check'], @_);
86 }
87
88 sub install {
89         my $this=shift;
90         my $destdir=shift;
91         $this->make_first_existing_target(['install'], "DESTDIR=$destdir", @_);
92 }
93
94 sub clean {
95         my $this=shift;
96         if (!$this->rmdir_builddir()) {
97                 $this->make_first_existing_target(['distclean', 'realclean', 'clean'], @_);
98         }
99 }
100
101 1