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