]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Buildsystem.pm
debhelper modular buildsystems (try 3).
[debhelper.git] / Debian / Debhelper / Dh_Buildsystem.pm
1 # Defines debhelper buildsystem class interface and implementation
2 # of common functionality.
3 #
4 # Copyright: © 2008-2009 Modestas Vainius
5 # License: GPL-2+
6
7 # XXX JEH also it seems the functions in Dh_Buildsystems could be merged
8 #     into this same file.
9 # XXX MDX I disagree. I think that mixing OO class and non-OO functions in the
10 # same file is a bad style. What is more, these two modules have different
11 # purposes (Dh_Buildsystems is an agregator of Dh_Buildsystem and its
12 # derivatives). Moreover, we don't want Dh_Buildsystem to inherit from Exporter
13 # (like Dh_Buildsystems do), do we?
14 package Debian::Debhelper::Dh_Buildsystem;
15
16 use strict;
17 use warnings;
18 use Cwd;
19 use File::Spec;
20 use Debian::Debhelper::Dh_Lib;
21
22 # Cache DEB_BUILD_GNU_TYPE value. Performance hit of multiple
23 # invocations is noticable when listing buildsystems.
24 our $DEB_BUILD_GNU_TYPE = dpkg_architecture_value("DEB_BUILD_GNU_TYPE");
25
26 # Build system name. Defaults to the last component of the class
27 # name. Do not override this method unless you know what you are
28 # doing.
29 sub NAME {
30         my $self=shift;
31         my $cls = ref($self) || $self;
32         if ($cls =~ m/^.+::([^:]+)$/) {
33                 return $1;
34         }
35         else {
36                 error("ınvalid buildsystem class name: $cls");
37         }
38 }
39
40 # Description of the build system to be shown to the users.
41 sub DESCRIPTION {
42         "basic debhelper build system class (please provide description)";
43 }
44
45 # Default build directory. Can be overriden in the derived
46 # class if really needed.
47 sub DEFAULT_BUILD_DIRECTORY {
48         "obj-" . $DEB_BUILD_GNU_TYPE;
49 }
50
51 # Constructs a new build system object. Named parameters:
52 # - builddir -     specifies build directory to use. If not specified,
53 #                  in-source build will be performed. If undef or empty,
54 #                  default DEFAULT_BUILD_DIRECTORY will be used.
55 # - build_action - set this parameter to the name of the build action
56 #                  if you want the object to determine its is_buidable
57 #                  status automatically (with check_auto_buildable()).
58 #                  Do not pass this parameter if is_buildable flag should
59 #                  be forced to true or set this parameter to undef if
60 #                  is_buildable flag should be false.
61 # Derived class can override the constructor to initialize common object
62 # parameters and execute commands to configure build environment if
63 # is_buildable flag is set on the object.
64 #
65 # XXX JEH the above comment begs the question: Why not test
66 # is_auto_buildable in the constructor, and only have the constructor
67 # succeed if it can handle the source? That would also eliminate the 
68 # delayed warning mess in enforce_in_source_building.
69 # XXX MDX Yes, that warning stuff was a mess. I implemented your
70 #         idea partitially.
71 #
72 # XXX JEH (In turn that could be used to remove the pre_action, since that's the
73 # only use of it -- the post_action is currently unused too. It could be
74 # argued that these should be kept in case later buildsystems need them
75 # though.)
76 # XXX MDX Well, I think we could keep them (now both empty) for the reason
77 #         you mention.
78 #
79 # XXX JEH AFAICS, there is only one reason you need an instance of the object
80 # if it can't build -- to list build systems. But that only needs
81 # DESCRIPTION and NAME, which could be considered to be class methods,
82 # rather than object methods -- no need to construct an instance of the
83 # class before calling those.
84 # XXX MDX Well yeah, they used to be (and still can be used) as such. But I
85 #         implemented a new feature to show force/auto_buildable status
86 #         while listing buildsystems. That feature needs an instance.
87
88 # XXX JEH I see that if --buildsystem is manually specified to override,
89 # the is_auto_buildable test is completely skipped. So if this change were
90 # made, you'd not be able to skip the test, and some --buildsystem choices
91 # might cause an error. OTOH, those seem to be cases where it would later
92 # fail anyway. The real use cases for --buildsystem, such as forcing use of
93 # cmake when there are both a CMakeLists.txt and a Makefile, would still
94 # work.
95 # XXX MDX 1) If buildsystem is forced, there might be a good reason for it.
96 #            What is more, that check as it is now is for *auto* stuff only.
97 #            In general, it cannot be used to reliably check if the source
98 #            will be buildable or not.
99 #         2) Your last sentence is not entirely true. Backwards compatibility
100 #            is also a huge limitation. The check_auto_buildable() should always
101 #            fail if it is not possible to add a new buildsystem in the backwards
102 #            compatible manner. See also my comments in the makefile.pm.
103 #         3) What is more, I implemented skipping of the auto buildable check,
104 #            so this is no longer the issue.
105
106 sub new {
107         my ($cls, %opts)=@_;
108
109         my $self = bless({ builddir => undef, is_buildable => 1 }, $cls);
110         if (exists $opts{builddir}) {
111                 if ($opts{builddir}) {
112                         $self->{builddir} = $opts{builddir};
113                 }
114                 else {
115                         $self->{builddir} = $self->DEFAULT_BUILD_DIRECTORY();
116                 }
117         }
118         if (exists $opts{build_action}) {
119                 if (defined $opts{build_action}) {
120                         $self->{is_buildable} = $self->check_auto_buildable($opts{build_action});
121                 }
122                 else {
123                         $self->{is_buildable} = 0;
124                 }
125         }
126         return $self;
127 }
128
129 # Test is_buildable flag of the object.
130 sub is_buildable {
131         my $self=shift;
132         return $self->{is_buildable};
133 }
134
135 # This instance method is called to check if the build system is capable
136 # to auto build a source package. Additional argument $action describes
137 # which operation the caller is going to perform (either configure,
138 # build, test, install or clean). You must override this method for the
139 # build system module to be ever picked up automatically. This method is
140 # used in conjuction with @Dh_Buildsystems::BUILDSYSTEMS.
141 #
142 # This method is supposed to be called with source root directory being
143 # working directory. Use $self->get_buildpath($path) method to get full
144 # path to the files in the build directory.
145 sub check_auto_buildable {
146         my $self=shift;
147         my ($action) = @_;
148         return 0;
149 }
150
151 # Derived class can call this method in its constructor
152 # to enforce in-source building even if the user requested otherwise.
153 sub enforce_in_source_building {
154         my $self=shift;
155         if ($self->{builddir}) {
156                 # Do not emit warning unless the object is buildable.
157                 if ($self->is_buildable()) {
158                         warning("warning: " . $self->NAME() .
159                             " does not support building outside-source. In-source build enforced.");
160                 }
161                 $self->{builddir} = undef;
162         }
163 }
164
165 # Derived class can call this method in its constructor to enforce
166 # outside-source building even if the user didn't request it.
167 sub enforce_outside_source_building {
168         my ($self, $builddir) = @_;
169         if (!defined $self->{builddir}) {
170                 $self->{builddir} = ($builddir && $builddir ne ".") ? $builddir : $self->DEFAULT_BUILD_DIRECTORY();
171         }
172 }
173
174 # Get path to the specified build directory
175 sub get_builddir {
176         my $self=shift;
177         return $self->{builddir};
178 }
179
180 # Construct absolute path to the file from the given path that is relative
181 # to the build directory.
182 sub get_buildpath {
183         my ($self, $path) = @_;
184         if ($self->get_builddir()) {
185                 return File::Spec->catfile($self->get_builddir(), $path);
186         }
187         else {
188                 return File::Spec->catfile('.', $path);
189         }
190 }
191
192 # When given a relative path in the source tree, converts it
193 # to the path that is relative to the build directory.
194 # If $path is not given, returns relative path to the root of the
195 # source tree from the build directory.
196 sub get_rel2builddir_path {
197         my $self=shift;
198         my $path=shift;
199
200         if (defined $path) {
201                 $path = File::Spec->catfile(Cwd::getcwd(), $path);
202         }
203         else {
204                 $path = Cwd::getcwd();
205         }
206         if ($self->get_builddir()) {
207                 return File::Spec->abs2rel($path, Cwd::abs_path($self->get_builddir()));
208         }
209         return $path;
210 }
211
212 sub _mkdir {
213         my ($cls, $dir)=@_;
214         # XXX JEH is there any reason not to just doit("mkdir") ?
215         # XXX MDX Replaced below part. This call is there to be
216         # more verbose about errors (if accidently $dir in
217         # non-dir form and to test for ! -d $dir.
218         if (-e $dir && ! -d $dir) {
219                 error("error: unable to create '$dir': object already exists and is not a directory");
220         }
221         elsif (! -d $dir) {
222                 doit("mkdir", $dir);
223                 return 1;
224         }
225         return 0;
226 }
227
228 sub _cd {
229         my ($cls, $dir)=@_;
230         if (! $dh{NO_ACT}) {
231                 verbose_print("cd $dir");
232                 chdir $dir or error("error: unable to chdir to $dir");
233         }
234 }
235
236 # Creates a build directory. Returns 1 if the directory was created
237 # or 0 if it already exists or there is no need to create it.
238 sub mkdir_builddir {
239         my $self=shift;
240         if ($self->get_builddir()) {
241                 return $self->_mkdir($self->get_builddir());
242         }
243         return 0;
244 }
245
246 # Changes working directory the build directory (if needed), calls doit(@_)
247 # and changes working directory back to the source directory.
248 sub doit_in_builddir {
249         my $self=shift;
250         if ($self->get_builddir()) {
251                 my $builddir = $self->get_builddir();
252                 my $sourcedir = $self->get_rel2builddir_path();
253                 $self->_cd($builddir);
254                 doit(@_);
255                 $self->_cd($sourcedir);
256         }
257         else {
258                 doit(@_);
259         }
260         return 1;
261 }
262
263 # In case of outside-source tree building, whole build directory
264 # gets wiped (if it exists) and 1 is returned. Otherwise, nothing
265 # is done and 0 is returned.
266 # XXX JEH only makefile.pm uses this, move it there?
267 # XXX MDX Well true, but I think this one is good to have for API
268 # completeness near to mkdir_builddir and doit_in_builddir above.
269 # I don't have strong feelings about it, but it looks more common
270 # function than makefile specific to me.
271 sub clean_builddir {
272         my $self=shift;
273         if ($self->get_builddir()) {
274                 if (-d $self->get_builddir()) {
275                         doit("rm", "-rf", $self->get_builddir());
276                 }
277                 return 1;
278         }
279         return 0;
280 }
281
282
283 # Instance method that is called before performing any action (see below).
284 # Action name is passed as an argument. Derived classes overriding this
285 # method should also call SUPER implementation of it.
286 sub pre_action {
287         my $self=shift;
288         my ($action)=@_;
289 }
290
291 # Instance method that is called after performing any action (see below).
292 # Action name is passed as an argument. Derived classes overriding this
293 # method should also call SUPER implementation of it.
294 sub post_action {
295         my $self=shift;
296         my ($action)=@_;
297 }
298
299 # The instance methods below provide support for configuring,
300 # building, testing, install and cleaning source packages.
301 # In case of failure, the method may just error() out.
302 #
303 # These methods should be overriden by derived classes to
304 # implement buildsystem specific actions needed to build the
305 # source. Arbitary number of custom action arguments might be
306 # passed. Default implementations do nothing.
307 sub configure {
308         my $self=shift;
309 }
310
311 sub build {
312         my $self=shift;
313 }
314
315 sub test {
316         my $self=shift;
317 }
318
319 # destdir parameter specifies where to install files.
320 sub install {
321         my $self=shift;
322         my $destdir=shift;
323 }
324
325 sub clean {
326         my $self=shift;
327 }
328
329 1;