]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Buildsystem.pm
More verbose rmdir_builddir() and more its tests
[debhelper.git] / Debian / Debhelper / Buildsystem.pm
1 # Defines debhelper build system class interface and implementation
2 # of common functionality.
3 #
4 # Copyright: © 2008-2009 Modestas Vainius
5 # License: GPL-2+
6
7 package Debian::Debhelper::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 build systems.
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 $this=shift;
24         my $class = ref($this) || $this;
25         if ($class =~ m/^.+::([^:]+)$/) {
26                 return $1;
27         }
28         else {
29                 error("ınvalid build system class name: $class");
30         }
31 }
32
33 # Description of the build system to be shown to the users.
34 sub DESCRIPTION {
35         error("class lacking a 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 # - sourcedir-     specifies source directory (relative to the current (top)
46 #                  directory) where the sources to be built live. If not
47 #                  specified or empty, defaults to the current directory.
48 # - builddir -     specifies build directory to use. Path is relative to the
49 #                  current (top) directory. If undef or empty,
50 #                  DEFAULT_BUILD_DIRECTORY directory will be used. 
51 # Derived class can override the constructor to initialize common object
52 # parameters. Do NOT use constructor to execute commands or otherwise
53 # configure/setup build environment. There is absolutely no guarantee the
54 # constructed object will be used to build something. Use pre_building_step(),
55 # $build_step() or post_building_step() methods for this.
56 sub new {
57         my ($class, %opts)=@_;
58
59         my $this = bless({ sourcedir => '.',
60                            builddir => undef,
61                            cwd => Cwd::getcwd() }, $class);
62
63         if (exists $opts{sourcedir}) {
64                 # Get relative sourcedir abs_path (without symlinks)
65                 my $abspath = Cwd::abs_path($opts{sourcedir});
66                 if (! -d $abspath || $abspath !~ /^\Q$this->{cwd}\E/) {
67                         error("invalid or non-existing path to the source directory: ".$opts{sourcedir});
68                 }
69                 $this->{sourcedir} = File::Spec->abs2rel($abspath, $this->{cwd});
70         }
71         if (exists $opts{builddir}) {
72                 $this->_set_builddir($opts{builddir});
73         }
74         return $this;
75 }
76
77 # Private method to set a build directory. If undef, use default.
78 # Do $this->{builddir} = undef or pass $this->get_sourcedir() to
79 # unset the build directory.
80 sub _set_builddir {
81         my $this=shift;
82         my $builddir=shift || $this->DEFAULT_BUILD_DIRECTORY;
83
84         if (defined $builddir) {
85                 $builddir = $this->canonpath($builddir); # Canonicalize
86
87                 # Sanitize $builddir
88                 if ($builddir =~ m#^\.\./#) {
89                         # We can't handle those as relative. Make them absolute
90                         $builddir = File::Spec->catdir($this->{cwd}, $builddir);
91                 }
92                 elsif ($builddir =~ /\Q$this->{cwd}\E/) {
93                         $builddir = File::Spec::abs2rel($builddir, $this->{cwd});
94                 }
95
96                 # If build directory ends up the same as source directory, drop it
97                 if ($builddir eq $this->get_sourcedir()) {
98                         $builddir = undef;
99                 }
100         }
101         $this->{builddir} = $builddir;
102         return $builddir;
103 }
104
105 # This instance method is called to check if the build system is able
106 # to auto build a source package. Additional argument $step describes
107 # which operation the caller is going to perform (either configure,
108 # build, test, install or clean). You must override this method for the
109 # build system module to be ever picked up automatically. This method is
110 # used in conjuction with @Dh_Buildsystems::BUILDSYSTEMS.
111 #
112 # This method is supposed to be called inside the source root directory.
113 # Use $this->get_buildpath($path) method to get full path to the files
114 # in the build directory.
115 sub check_auto_buildable {
116         my $this=shift;
117         my ($step) = @_;
118         return 0;
119 }
120
121 # Derived class can call this method in its constructor
122 # to enforce in source building even if the user requested otherwise.
123 sub enforce_in_source_building {
124         my $this=shift;
125         if ($this->get_builddir()) {
126                 $this->{warn_insource} = 1;
127                 $this->{builddir} = undef;
128         }
129 }
130
131 # Derived class can call this method in its constructor to *prefer*
132 # out of source building. Unless build directory has already been
133 # specified building will proceed in the DEFAULT_BUILD_DIRECTORY or
134 # the one specified in the 'builddir' named parameter (which may
135 # match the source directory). Typically you should pass @_ from
136 # the constructor to this call.
137 sub prefer_out_of_source_building {
138         my $this=shift;
139         my %args=@_;
140         if (!defined $this->get_builddir()) {
141                 if (!$this->_set_builddir($args{builddir}) && !$args{builddir}) {
142                         # If we are here, DEFAULT_BUILD_DIRECTORY matches
143                         # the source directory, building might fail.
144                         error("default build directory is the same as the source directory." .
145                               " Please specify a custom build directory");
146                 }
147         }
148 }
149
150 # Derived class can call this method in its constructor to *enforce*
151 # out of source building even if the user didn't request it.
152 # Build directory is set to DEFAULT_BUILD_DIRECTORY or building
153 # fails if it is not possible to set it
154 sub enforce_out_of_source_building {
155         my $this=shift;
156         $this->prefer_out_of_source_building();
157 }
158
159 # Enhanced version of File::Spec::canonpath. It collapses ..
160 # too so it may return invalid path if symlinks are involved.
161 # On the other hand, it does not need for the path to exist.
162 sub canonpath {
163         my ($this, $path)=@_;
164         my @canon;
165         my $back=0;
166         for my $comp (split(m%/+%, $path)) {
167                 if ($comp eq '.') {
168                         next;
169                 }
170                 elsif ($comp eq '..') {
171                         if (@canon > 0) { pop @canon; }  else { $back++; }
172                 }
173                 else {
174                         push @canon, $comp;
175                 }
176         }
177         return (@canon + $back > 0) ? join('/', ('..')x$back, @canon) : '.';
178 }
179
180 # Given both $path and $base are relative to the $root, converts and
181 # returns path of $path being relative to the $base. If either $path or
182 # $base is absolute, returns another $path (converted to) absolute.
183 sub _rel2rel {
184         my ($this, $path, $base, $root)=@_;
185         $root = $this->{cwd} unless defined $root;
186
187         if (File::Spec->file_name_is_absolute($path)) {
188                 return $path;
189         } elsif (File::Spec->file_name_is_absolute($base)) {
190                 return File::Spec->rel2abs($path, $root);
191         } else {
192                 return File::Spec->abs2rel(
193                         File::Spec->rel2abs($path, $root),
194                         File::Spec->rel2abs($base, $root)
195                 );
196         }
197 }
198
199 # Get path to the source directory
200 # (relative to the current (top) directory)
201 sub get_sourcedir {
202         my $this=shift;
203         return $this->{sourcedir};
204 }
205
206 # Convert path relative to the source directory to the path relative
207 # to the current (top) directory.
208 sub get_sourcepath {
209         my ($this, $path)=@_;
210         return File::Spec->catfile($this->get_sourcedir(), $path);
211 }
212
213 # Get path to the build directory if it was specified
214 # (relative to the current (top) directory). undef if the same
215 # as the source directory.
216 sub get_builddir {
217         my $this=shift;
218         return $this->{builddir};
219 }
220
221 # Convert path that is relative to the build directory to the path
222 # that is relative to the current (top) directory.
223 # If $path is not specified, always returns build directory path
224 # relative to the current (top) directory regardless if builddir was
225 # specified or not.
226 sub get_buildpath {
227         my ($this, $path)=@_;
228         my $builddir = $this->get_builddir() || $this->get_sourcedir();
229         if (defined $path) {
230                 return File::Spec->catfile($builddir, $path);
231         }
232         return $builddir;
233 }
234
235 # When given a relative path to the source directory, converts it
236 # to the path that is relative to the build directory. If $path is
237 # not given, returns a path to the source directory that is relative
238 # to the build directory.
239 sub get_source_rel2builddir {
240         my $this=shift;
241         my $path=shift;
242
243         my $dir = '.';
244         if ($this->get_builddir()) {
245                 $dir = $this->_rel2rel($this->get_sourcedir(), $this->get_builddir());
246         }
247         if (defined $path) {
248                 return File::Spec->catfile($dir, $path);
249         }
250         return $dir;
251 }
252
253 # When given a relative path to the build directory, converts it
254 # to the path that is relative to the source directory. If $path is
255 # not given, returns a path to the build directory that is relative
256 # to the source directory.
257 sub get_build_rel2sourcedir {
258         my $this=shift;
259         my $path=shift;
260
261         my $dir = '.';
262         if ($this->get_builddir()) {
263                 $dir = $this->_rel2rel($this->get_builddir(), $this->get_sourcedir());
264         }
265         if (defined $path) {
266                 return File::Spec->catfile($dir, $path);
267         }
268         return $dir;
269 }
270
271 # Creates a build directory.
272 sub mkdir_builddir {
273         my $this=shift;
274         if ($this->get_builddir()) {
275                 doit("mkdir", "-p", $this->get_builddir());
276         }
277 }
278
279 sub _cd {
280         my ($this, $dir)=@_;
281         if (! $dh{NO_ACT}) {
282                 verbose_print("cd $dir");
283                 chdir $dir or error("error: unable to chdir to $dir");
284         }
285 }
286
287 # Changes working directory to the source directory (if needed),
288 # calls doit(@_) and changes working directory back to the top
289 # directory.
290 sub doit_in_sourcedir {
291         my $this=shift;
292         if ($this->get_sourcedir() ne '.') {
293                 my $sourcedir = $this->get_sourcedir();
294                 $this->_cd($sourcedir);
295                 doit(@_);
296                 $this->_cd($this->_rel2rel($this->{cwd}, $sourcedir));
297         }
298         else {
299                 doit(@_);
300         }
301         return 1;
302 }
303
304 # Changes working directory to the build directory (if needed),
305 # calls doit(@_) and changes working directory back to the top
306 # directory.
307 sub doit_in_builddir {
308         my $this=shift;
309         if ($this->get_buildpath() ne '.') {
310                 my $buildpath = $this->get_buildpath();
311                 $this->_cd($buildpath);
312                 doit(@_);
313                 $this->_cd($this->_rel2rel($this->{cwd}, $buildpath));
314         }
315         else {
316                 doit(@_);
317         }
318         return 1;
319 }
320
321 # In case of out of source tree building, whole build directory
322 # gets wiped (if it exists) and 1 is returned. If build directory
323 # had 2 or more levels, empty parent directories are also deleted.
324 # If build directory does not exist, nothing is done and 0 is returned.
325 sub rmdir_builddir {
326         my $this=shift;
327         my $only_empty=shift;
328         if ($this->get_builddir()) {
329                 my $buildpath = $this->get_buildpath();
330                 if (-d $buildpath && ! $dh{NO_ACT}) {
331                         my @spdir = File::Spec->splitdir($this->get_build_rel2sourcedir());
332                         my $peek;
333                         if (!$only_empty) {
334                                 doit("rm", "-rf", $buildpath);
335                                 pop @spdir;
336                         }
337                         # If build directory is relative and had 2 or more levels, delete
338                         # empty parent directories until the source directory level.
339                         if (not File::Spec->file_name_is_absolute($buildpath)) {
340                                 while (($peek=pop(@spdir)) && $peek ne '.' && $peek ne '..') {
341                                         my $dir = $this->get_sourcepath(File::Spec->catdir(@spdir, $peek));
342                                         verbose_print("rmdir $dir");
343                                         last if ! rmdir($dir);
344                                 }
345                         }
346                 }
347                 return 1;
348         }
349         return 0;
350 }
351
352 # Instance method that is called before performing any step (see below).
353 # Action name is passed as an argument. Derived classes overriding this
354 # method should also call SUPER implementation of it.
355 sub pre_building_step {
356         my $this=shift;
357         my ($step)=@_;
358
359         # Warn if in source building was enforced but build directory was
360         # specified. See enforce_in_source_building().
361         if ($this->{warn_insource}) {
362                 warning("warning: " . $this->NAME() .
363                     " does not support building out of source tree. In source building enforced.");
364                 delete $this->{warn_insource};
365         }
366 }
367
368 # Instance method that is called after performing any step (see below).
369 # Action name is passed as an argument. Derived classes overriding this
370 # method should also call SUPER implementation of it.
371 sub post_building_step {
372         my $this=shift;
373         my ($step)=@_;
374 }
375
376 # The instance methods below provide support for configuring,
377 # building, testing, install and cleaning source packages.
378 # In case of failure, the method may just error() out.
379 #
380 # These methods should be overriden by derived classes to
381 # implement build system specific steps needed to build the
382 # source. Arbitary number of custom step arguments might be
383 # passed. Default implementations do nothing.
384 sub configure {
385         my $this=shift;
386 }
387
388 sub build {
389         my $this=shift;
390 }
391
392 sub test {
393         my $this=shift;
394 }
395
396 # destdir parameter specifies where to install files.
397 sub install {
398         my $this=shift;
399         my $destdir=shift;
400 }
401
402 sub clean {
403         my $this=shift;
404 }
405
406 1