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