]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Buildsystem/makefile.pm
reduce amount of MAKEFLAGS cleaning
[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 is_make_jobserver_unavailable
11         clean_jobserver_makeflags);
12 use base 'Debian::Debhelper::Buildsystem';
13
14 sub get_makecmd_C {
15         my $this=shift;
16         my $buildpath = $this->get_buildpath();
17         if ($buildpath ne '.') {
18                 return $this->{makecmd} . " -C " . escape_shell($buildpath);
19         }
20         return $this->{makecmd};
21 }
22
23 sub exists_make_target {
24         my ($this, $target) = @_;
25         my $makecmd=$this->get_makecmd_C();
26
27         # Use make -n to check to see if the target would do
28         # anything. There's no good way to test if a target exists.
29         my $ret=`$makecmd -s -n --no-print-directory $target 2>/dev/null`;
30         chomp $ret;
31         return length($ret);
32 }
33
34 sub do_make {
35         my $this=shift;
36
37         # Avoid warnings about unavailable jobserver.
38         if (is_make_jobserver_unavailable()) {
39                 clean_jobserver_makeflags();
40         }
41
42         if (defined $this->get_parallel()) {
43                 # Note that this will override any -j settings in MAKEFLAGS.
44                 unshift @_, "-j" . ($this->get_parallel() > 1 ? $this->get_parallel() : 1);
45                 # Force make to start a new jobserver.
46                 clean_jobserver_makeflags();
47         }
48
49         $this->doit_in_builddir($this->{makecmd}, @_);
50 }
51
52 sub make_first_existing_target {
53         my $this=shift;
54         my $targets=shift;
55
56         foreach my $target (@$targets) {
57                 if ($this->exists_make_target($target)) {
58                         $this->do_make($target, @_);
59                         return $target;
60                 }
61         }
62         return undef;
63 }
64
65 sub DESCRIPTION {
66         "simple Makefile"
67 }
68
69 sub new {
70         my $class=shift;
71         my $this=$class->SUPER::new(@_);
72         $this->{makecmd} = (exists $ENV{MAKE}) ? $ENV{MAKE} : "make";
73         return $this;
74 }
75
76 sub check_auto_buildable {
77         my $this=shift;
78         my ($step) = @_;
79
80         # Handles build, test, install, clean; configure - next class
81         if (grep /^\Q$step\E$/, qw{build test install clean}) {
82                 # This is always called in the source directory, but generally
83                 # Makefiles are created (or live) in the the build directory.
84                 return -e $this->get_buildpath("Makefile") ||
85                        -e $this->get_buildpath("makefile") ||
86                        -e $this->get_buildpath("GNUmakefile");
87         }
88         return 0;
89 }
90
91 sub build {
92         my $this=shift;
93         $this->do_make(@_);
94 }
95
96 sub test {
97         my $this=shift;
98         $this->make_first_existing_target(['test', 'check'], @_);
99 }
100
101 sub install {
102         my $this=shift;
103         my $destdir=shift;
104         $this->make_first_existing_target(['install'], "DESTDIR=$destdir", @_);
105 }
106
107 sub clean {
108         my $this=shift;
109         if (!$this->rmdir_builddir()) {
110                 $this->make_first_existing_target(['distclean', 'realclean', 'clean'], @_);
111         }
112 }
113
114 1