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