]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Buildsystem.pm
layout
[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         }
190         elsif (File::Spec->file_name_is_absolute($base)) {
191                 return File::Spec->rel2abs($path, $root);
192         }
193         else {
194                 return File::Spec->abs2rel(
195                         File::Spec->rel2abs($path, $root),
196                         File::Spec->rel2abs($base, $root)
197                 );
198         }
199 }
200
201 # Get path to the source directory
202 # (relative to the current (top) directory)
203 sub get_sourcedir {
204         my $this=shift;
205         return $this->{sourcedir};
206 }
207
208 # Convert path relative to the source directory to the path relative
209 # to the current (top) directory.
210 sub get_sourcepath {
211         my ($this, $path)=@_;
212         return File::Spec->catfile($this->get_sourcedir(), $path);
213 }
214
215 # Get path to the build directory if it was specified
216 # (relative to the current (top) directory). undef if the same
217 # as the source directory.
218 sub get_builddir {
219         my $this=shift;
220         return $this->{builddir};
221 }
222
223 # Convert path that is relative to the build directory to the path
224 # that is relative to the current (top) directory.
225 # If $path is not specified, always returns build directory path
226 # relative to the current (top) directory regardless if builddir was
227 # specified or not.
228 sub get_buildpath {
229         my ($this, $path)=@_;
230         my $builddir = $this->get_builddir() || $this->get_sourcedir();
231         if (defined $path) {
232                 return File::Spec->catfile($builddir, $path);
233         }
234         return $builddir;
235 }
236
237 # When given a relative path to the source directory, converts it
238 # to the path that is relative to the build directory. If $path is
239 # not given, returns a path to the source directory that is relative
240 # to the build directory.
241 sub get_source_rel2builddir {
242         my $this=shift;
243         my $path=shift;
244
245         my $dir = '.';
246         if ($this->get_builddir()) {
247                 $dir = $this->_rel2rel($this->get_sourcedir(), $this->get_builddir());
248         }
249         if (defined $path) {
250                 return File::Spec->catfile($dir, $path);
251         }
252         return $dir;
253 }
254
255 # When given a relative path to the build directory, converts it
256 # to the path that is relative to the source directory. If $path is
257 # not given, returns a path to the build directory that is relative
258 # to the source directory.
259 sub get_build_rel2sourcedir {
260         my $this=shift;
261         my $path=shift;
262
263         my $dir = '.';
264         if ($this->get_builddir()) {
265                 $dir = $this->_rel2rel($this->get_builddir(), $this->get_sourcedir());
266         }
267         if (defined $path) {
268                 return File::Spec->catfile($dir, $path);
269         }
270         return $dir;
271 }
272
273 # Creates a build directory.
274 sub mkdir_builddir {
275         my $this=shift;
276         if ($this->get_builddir()) {
277                 doit("mkdir", "-p", $this->get_builddir());
278         }
279 }
280
281 sub _cd {
282         my ($this, $dir)=@_;
283         if (! $dh{NO_ACT}) {
284                 verbose_print("cd $dir");
285                 chdir $dir or error("error: unable to chdir to $dir");
286         }
287 }
288
289 # Changes working directory to the source directory (if needed),
290 # calls doit(@_) and changes working directory back to the top
291 # directory.
292 sub doit_in_sourcedir {
293         my $this=shift;
294         if ($this->get_sourcedir() ne '.') {
295                 my $sourcedir = $this->get_sourcedir();
296                 $this->_cd($sourcedir);
297                 doit(@_);
298                 $this->_cd($this->_rel2rel($this->{cwd}, $sourcedir));
299         }
300         else {
301                 doit(@_);
302         }
303         return 1;
304 }
305
306 # Changes working directory to the build directory (if needed),
307 # calls doit(@_) and changes working directory back to the top
308 # directory.
309 sub doit_in_builddir {
310         my $this=shift;
311         if ($this->get_buildpath() ne '.') {
312                 my $buildpath = $this->get_buildpath();
313                 $this->_cd($buildpath);
314                 doit(@_);
315                 $this->_cd($this->_rel2rel($this->{cwd}, $buildpath));
316         }
317         else {
318                 doit(@_);
319         }
320         return 1;
321 }
322
323 # In case of out of source tree building, whole build directory
324 # gets wiped (if it exists) and 1 is returned. If build directory
325 # had 2 or more levels, empty parent directories are also deleted.
326 # If build directory does not exist, nothing is done and 0 is returned.
327 sub rmdir_builddir {
328         my $this=shift;
329         my $only_empty=shift;
330         if ($this->get_builddir()) {
331                 my $buildpath = $this->get_buildpath();
332                 if (-d $buildpath && ! $dh{NO_ACT}) {
333                         my @spdir = File::Spec->splitdir($this->get_build_rel2sourcedir());
334                         my $peek;
335                         if (!$only_empty) {
336                                 doit("rm", "-rf", $buildpath);
337                                 pop @spdir;
338                         }
339                         # If build directory is relative and had 2 or more levels, delete
340                         # empty parent directories until the source directory level.
341                         if (not File::Spec->file_name_is_absolute($buildpath)) {
342                                 while (($peek=pop(@spdir)) && $peek ne '.' && $peek ne '..') {
343                                         my $dir = $this->get_sourcepath(File::Spec->catdir(@spdir, $peek));
344                                         verbose_print("rmdir $dir");
345                                         last if ! rmdir($dir);
346                                 }
347                         }
348                 }
349                 return 1;
350         }
351         return 0;
352 }
353
354 # Instance method that is called before performing any step (see below).
355 # Action name is passed as an argument. Derived classes overriding this
356 # method should also call SUPER implementation of it.
357 sub pre_building_step {
358         my $this=shift;
359         my ($step)=@_;
360
361         # Warn if in source building was enforced but build directory was
362         # specified. See enforce_in_source_building().
363         if ($this->{warn_insource}) {
364                 warning("warning: " . $this->NAME() .
365                     " does not support building out of source tree. In source building enforced.");
366                 delete $this->{warn_insource};
367         }
368 }
369
370 # Instance method that is called after performing any step (see below).
371 # Action name is passed as an argument. Derived classes overriding this
372 # method should also call SUPER implementation of it.
373 sub post_building_step {
374         my $this=shift;
375         my ($step)=@_;
376 }
377
378 # The instance methods below provide support for configuring,
379 # building, testing, install and cleaning source packages.
380 # In case of failure, the method may just error() out.
381 #
382 # These methods should be overriden by derived classes to
383 # implement build system specific steps needed to build the
384 # source. Arbitary number of custom step arguments might be
385 # passed. Default implementations do nothing.
386 sub configure {
387         my $this=shift;
388 }
389
390 sub build {
391         my $this=shift;
392 }
393
394 sub test {
395         my $this=shift;
396 }
397
398 # destdir parameter specifies where to install files.
399 sub install {
400         my $this=shift;
401         my $destdir=shift;
402 }
403
404 sub clean {
405         my $this=shift;
406 }
407
408 1