]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Buildsystem/makefile.pm
159f7c1e43f56139bcdd582743270ba1611c7571
[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_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         # Remove unavailable jobserver options from MAKEFLAGS.
38         # Always clean MAKEFLAGS from unavailable jobserver options. If parallel
39         # is enabled, do more extensive clean up from all job control specific
40         # options
41         if (defined $this->get_parallel() || is_make_jobserver_unavailable()) {
42                 clean_makeflags();
43         }
44
45         # Start a new jobserver if parallel building was requested
46         if (defined $this->get_parallel()) {
47                 unshift @_, "-j" . ($this->get_parallel() > 1 ? $this->get_parallel() : 1);
48         }
49
50         $this->doit_in_builddir($this->{makecmd}, @_);
51 }
52
53 sub make_first_existing_target {
54         my $this=shift;
55         my $targets=shift;
56
57         foreach my $target (@$targets) {
58                 if ($this->exists_make_target($target)) {
59                         $this->do_make($target, @_);
60                         return $target;
61                 }
62         }
63         return undef;
64 }
65
66 sub DESCRIPTION {
67         "simple Makefile"
68 }
69
70 sub new {
71         my $class=shift;
72         my $this=$class->SUPER::new(@_);
73         $this->{makecmd} = (exists $ENV{MAKE}) ? $ENV{MAKE} : "make";
74         return $this;
75 }
76
77 sub check_auto_buildable {
78         my $this=shift;
79         my ($step) = @_;
80
81         # Handles build, test, install, clean; configure - next class
82         if (grep /^\Q$step\E$/, qw{build test install clean}) {
83                 # This is always called in the source directory, but generally
84                 # Makefiles are created (or live) in the the build directory.
85                 return -e $this->get_buildpath("Makefile") ||
86                        -e $this->get_buildpath("makefile") ||
87                        -e $this->get_buildpath("GNUmakefile");
88         }
89         return 0;
90 }
91
92 sub build {
93         my $this=shift;
94         $this->do_make(@_);
95 }
96
97 sub test {
98         my $this=shift;
99         $this->make_first_existing_target(['test', 'check'], @_);
100 }
101
102 sub install {
103         my $this=shift;
104         my $destdir=shift;
105         $this->make_first_existing_target(['install'], "DESTDIR=$destdir", @_);
106 }
107
108 sub clean {
109         my $this=shift;
110         if (!$this->rmdir_builddir()) {
111                 $this->make_first_existing_target(['distclean', 'realclean', 'clean'], @_);
112         }
113 }
114
115 1