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