]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Buildsystem/makefile.pm
Merge branch 'master' into buildsystems
[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 =head1 NAME
10
11 B<makefile> - make (Makefile)
12
13 =head1 SYNOPSIS
14
15 B<dh_auto_*> [B<--buildsystem>=I<makefile>] ...
16
17 =head1 DESCRIPTION
18
19 Makefile based projects use C<make> to control build process. C<make> utility
20 is the most popular tool on *NIX for building & installing packages from
21 source. It is also a basis for most other popular build systems. For example,
22 GNU Autoconf (autoconf) or CMake (cmake) generate F<Makefile>s during I<configure>
23 step and leave the rest of build process for C<make> to handle.
24
25 =head1 DH_AUTO NOTES
26
27 Since C<make> itself does not strictly define standard target names, a couple
28 of the most popular targets are tried for each building step. Whichever first
29 of them is discovered to exist, it is run. If neither of the tried targets
30 exist in the actual, the building step is assumed to have completed
31 successfully. However, if executed C<make> target fails, the respective dh_auto
32 program will fail too.
33
34 If MAKE environment variable is set, its value is executed rather than default
35 C<make> command.
36
37 Both in source (default) and out of source tree building modes are supported.
38 Either F<Makefile>, F<makefile> or F<GNUmakefile> file should be present in the
39 build directory for this debhelper build system to work.
40
41 =head1 BUILD PROCESS
42
43 =head2 Configure step
44
45 =over 4
46
47 =item I<Behaviour>
48
49 Do nothing (auto-selection continues).
50
51 =item I<Auto-selection>
52
53 It will never be auto-selected at this step.
54
55 =back
56
57 =cut
58
59 use strict;
60 use Debian::Debhelper::Dh_Lib qw(escape_shell);
61 use base 'Debian::Debhelper::Buildsystem';
62
63 sub get_makecmd_C {
64         my $this=shift;
65         my $buildpath = $this->get_buildpath();
66         if ($buildpath ne '.') {
67                 return $this->{makecmd} . " -C " . escape_shell($buildpath);
68         }
69         return $this->{makecmd};
70 }
71
72 sub exists_make_target {
73         my ($this, $target) = @_;
74         my $makecmd=$this->get_makecmd_C();
75
76         # Use make -n to check to see if the target would do
77         # anything. There's no good way to test if a target exists.
78         my $ret=`$makecmd -s -n --no-print-directory $target 2>/dev/null`;
79         chomp $ret;
80         return length($ret);
81 }
82
83 sub make_first_existing_target {
84         my $this=shift;
85         my $targets=shift;
86
87         foreach my $target (@$targets) {
88                 if ($this->exists_make_target($target)) {
89                         $this->doit_in_builddir($this->{makecmd}, $target, @_);
90                         return $target;
91                 }
92         }
93         return undef;
94 }
95
96 sub DESCRIPTION {
97         "simple Makefile"
98 }
99
100 sub new {
101         my $class=shift;
102         my $this=$class->SUPER::new(@_);
103         $this->{makecmd} = (exists $ENV{MAKE}) ? $ENV{MAKE} : "make";
104         return $this;
105 }
106
107 sub check_auto_buildable {
108         my $this=shift;
109         my ($step) = @_;
110
111         # Handles build, test, install, clean; configure - next class
112         if (grep /^\Q$step\E$/, qw{build test install clean}) {
113                 # This is always called in the source directory, but generally
114                 # Makefiles are created (or live) in the the build directory.
115                 return -e $this->get_buildpath("Makefile") ||
116                        -e $this->get_buildpath("makefile") ||
117                        -e $this->get_buildpath("GNUmakefile");
118         }
119         return 0;
120 }
121
122 =head2 Build step
123
124 =over 4
125
126 =item I<Behaviour>
127
128 Execute C<make> (without arguments) with working directory changed to the build
129 directory.
130
131 =item I<Auto-selection>
132
133 If either F<Makefile>, F<makefile> or F<GNUmakefile> exists in the build
134 directory, but F<Makefile.PL> does not exist in the source directory.
135
136 =back
137
138 =cut
139 sub build {
140         my $this=shift;
141         $this->doit_in_builddir($this->{makecmd}, @_);
142 }
143
144 =head2 Test step
145
146 =over 4
147
148 =item I<Behaviour>
149
150 Try to C<make> either I<test> or I<check> target (the first existing one) with
151 working directory changed to the build directory.
152
153 =item I<Auto-selection>
154
155 If either F<Makefile>, F<makefile> or F<GNUmakefile> exists in the build
156 directory, but F<Makefile.PL> does not exist in the source directory.
157
158 =back
159
160 =cut
161 sub test {
162         my $this=shift;
163         $this->make_first_existing_target(['test', 'check'], @_);
164 }
165
166 =head2 Install step
167
168 =over 4
169
170 =item I<Behaviour>
171
172 Try to run C<make install DESTDIR=$destdir> with working directory changed to
173 the build directory. $desdir is the path to the appropriate temporary
174 installation directory under debian/ (see L<dh_auto_install(1)>).
175
176 =item I<Auto-selection>
177
178 If either F<Makefile>, F<makefile> or F<GNUmakefile> exists in the build
179 directory, but F<Makefile.PL> does not exist in the source directory.
180
181 =back
182
183 =cut
184 sub install {
185         my $this=shift;
186         my $destdir=shift;
187         $this->make_first_existing_target(['install'], "DESTDIR=$destdir", @_);
188 }
189
190 =head2 Clean step
191
192 =over 4
193
194 =item I<Behaviour>
195
196 When building in source, try to C<make> either I<distclean>, I<realclean> or
197 I<clean> target (the first existing one) in the source directory. When building
198 out of source tree, recursively remove the whole build directory.
199
200 =item I<Auto-selection>
201
202 If either F<Makefile>, F<makefile> or F<GNUmakefile> exists in the build
203 directory, but F<Makefile.PL> does not exist in the source directory.
204
205 =back
206
207 =cut
208 sub clean {
209         my $this=shift;
210         if (!$this->rmdir_builddir()) {
211                 $this->make_first_existing_target(['distclean', 'realclean', 'clean'], @_);
212         }
213 }
214
215 =head1 SEE ALSO
216
217 L<dh_auto(7)>
218
219 =head1 AUTHORS
220
221  Joey Hess <joeyh@debian.org>
222  Modestas Vainius <modestas@vainius.eu>
223
224 =cut
225
226 1;