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