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