1 # Defines debhelper build system class interface and implementation
2 # of common functionality.
4 # Copyright: © 2008-2009 Modestas Vainius
7 package Debian::Debhelper::Buildsystem;
13 use Debian::Debhelper::Dh_Lib;
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");
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
24 my $class = ref($this) || $this;
25 if ($class =~ m/^.+::([^:]+)$/) {
29 error("ınvalid build system class name: $class");
33 # Description of the build system to be shown to the users.
35 error("class lacking a DESCRIPTION");
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;
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 - number of parallel process to be spawned for building
52 # sources. Parallel building needs to be supported by the
53 # underlying build system for this option to be effective.
54 # Defaults to undef (i.e. parallel disabled, but do not try to
55 # enforce this limit by messing with environment).
56 # Derived class can override the constructor to initialize common object
57 # parameters. Do NOT use constructor to execute commands or otherwise
58 # configure/setup build environment. There is absolutely no guarantee the
59 # constructed object will be used to build something. Use pre_building_step(),
60 # $build_step() or post_building_step() methods for this.
62 my ($class, %opts)=@_;
64 my $this = bless({ sourcedir => '.',
67 cwd => Cwd::getcwd() }, $class);
69 if (exists $opts{sourcedir}) {
70 # Get relative sourcedir abs_path (without symlinks)
71 my $abspath = Cwd::abs_path($opts{sourcedir});
72 if (! -d $abspath || $abspath !~ /^\Q$this->{cwd}\E/) {
73 error("invalid or non-existing path to the source directory: ".$opts{sourcedir});
75 $this->{sourcedir} = File::Spec->abs2rel($abspath, $this->{cwd});
77 if (exists $opts{builddir}) {
78 $this->_set_builddir($opts{builddir});
80 if (defined $opts{parallel} && $opts{parallel} >= 1) {
81 $this->{parallel} = $opts{parallel};
86 # Private method to set a build directory. If undef, use default.
87 # Do $this->{builddir} = undef or pass $this->get_sourcedir() to
88 # unset the build directory.
91 my $builddir=shift || $this->DEFAULT_BUILD_DIRECTORY;
93 if (defined $builddir) {
94 $builddir = $this->canonpath($builddir); # Canonicalize
97 if ($builddir =~ m#^\.\./#) {
98 # We can't handle those as relative. Make them absolute
99 $builddir = File::Spec->catdir($this->{cwd}, $builddir);
101 elsif ($builddir =~ /\Q$this->{cwd}\E/) {
102 $builddir = File::Spec::abs2rel($builddir, $this->{cwd});
105 # If build directory ends up the same as source directory, drop it
106 if ($builddir eq $this->get_sourcedir()) {
110 $this->{builddir} = $builddir;
114 # This instance method is called to check if the build system is able
115 # to auto build a source package. Additional argument $step describes
116 # which operation the caller is going to perform (either configure,
117 # build, test, install or clean). You must override this method for the
118 # build system module to be ever picked up automatically. This method is
119 # used in conjuction with @Dh_Buildsystems::BUILDSYSTEMS.
121 # This method is supposed to be called inside the source root directory.
122 # Use $this->get_buildpath($path) method to get full path to the files
123 # in the build directory.
124 sub check_auto_buildable {
130 # Derived class can call this method in its constructor
131 # to enforce in source building even if the user requested otherwise.
132 sub enforce_in_source_building {
134 if ($this->get_builddir()) {
135 $this->{warn_insource} = 1;
136 $this->{builddir} = undef;
140 # Derived class can call this method in its constructor to *prefer*
141 # out of source building. Unless build directory has already been
142 # specified building will proceed in the DEFAULT_BUILD_DIRECTORY or
143 # the one specified in the 'builddir' named parameter (which may
144 # match the source directory). Typically you should pass @_ from
145 # the constructor to this call.
146 sub prefer_out_of_source_building {
149 if (!defined $this->get_builddir()) {
150 if (!$this->_set_builddir($args{builddir}) && !$args{builddir}) {
151 # If we are here, DEFAULT_BUILD_DIRECTORY matches
152 # the source directory, building might fail.
153 error("default build directory is the same as the source directory." .
154 " Please specify a custom build directory");
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.
163 my ($this, $path)=@_;
166 for my $comp (split(m%/+%, $path)) {
170 elsif ($comp eq '..') {
171 if (@canon > 0) { pop @canon; } else { $back++; }
177 return (@canon + $back > 0) ? join('/', ('..')x$back, @canon) : '.';
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.
184 my ($this, $path, $base, $root)=@_;
185 $root = $this->{cwd} unless defined $root;
187 if (File::Spec->file_name_is_absolute($path)) {
190 elsif (File::Spec->file_name_is_absolute($base)) {
191 return File::Spec->rel2abs($path, $root);
194 return File::Spec->abs2rel(
195 File::Spec->rel2abs($path, $root),
196 File::Spec->rel2abs($base, $root)
201 # Get path to the source directory
202 # (relative to the current (top) directory)
205 return $this->{sourcedir};
208 # Convert path relative to the source directory to the path relative
209 # to the current (top) directory.
211 my ($this, $path)=@_;
212 return File::Spec->catfile($this->get_sourcedir(), $path);
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.
220 return $this->{builddir};
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
229 my ($this, $path)=@_;
230 my $builddir = $this->get_builddir() || $this->get_sourcedir();
232 return File::Spec->catfile($builddir, $path);
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 {
246 if ($this->get_builddir()) {
247 $dir = $this->_rel2rel($this->get_sourcedir(), $this->get_builddir());
250 return File::Spec->catfile($dir, $path);
257 return $this->{parallel};
260 # When given a relative path to the build directory, converts it
261 # to the path that is relative to the source directory. If $path is
262 # not given, returns a path to the build directory that is relative
263 # to the source directory.
264 sub get_build_rel2sourcedir {
269 if ($this->get_builddir()) {
270 $dir = $this->_rel2rel($this->get_builddir(), $this->get_sourcedir());
273 return File::Spec->catfile($dir, $path);
278 # Creates a build directory.
281 if ($this->get_builddir()) {
282 doit("mkdir", "-p", $this->get_builddir());
289 verbose_print("cd $dir");
290 chdir $dir or error("error: unable to chdir to $dir");
294 # Changes working directory to the source directory (if needed),
295 # calls doit(@_) and changes working directory back to the top
297 sub doit_in_sourcedir {
299 if ($this->get_sourcedir() ne '.') {
300 my $sourcedir = $this->get_sourcedir();
301 $this->_cd($sourcedir);
303 $this->_cd($this->_rel2rel($this->{cwd}, $sourcedir));
311 # Changes working directory to the build directory (if needed),
312 # calls doit(@_) and changes working directory back to the top
314 sub doit_in_builddir {
316 if ($this->get_buildpath() ne '.') {
317 my $buildpath = $this->get_buildpath();
318 $this->_cd($buildpath);
320 $this->_cd($this->_rel2rel($this->{cwd}, $buildpath));
328 # In case of out of source tree building, whole build directory
329 # gets wiped (if it exists) and 1 is returned. If build directory
330 # had 2 or more levels, empty parent directories are also deleted.
331 # If build directory does not exist, nothing is done and 0 is returned.
334 my $only_empty=shift;
335 if ($this->get_builddir()) {
336 my $buildpath = $this->get_buildpath();
338 my @dir = File::Spec->splitdir($this->get_build_rel2sourcedir());
340 if (not $only_empty) {
341 doit("rm", "-rf", $buildpath);
344 # If build directory is relative and had 2 or more levels, delete
345 # empty parent directories until the source or top directory level.
346 if (not File::Spec->file_name_is_absolute($buildpath)) {
347 while (($peek=pop @dir) && $peek ne '.' && $peek ne '..') {
348 my $dir = $this->get_sourcepath(File::Spec->catdir(@dir, $peek));
349 doit("rmdir", "--ignore-fail-on-non-empty", $dir);
359 # Instance method that is called before performing any step (see below).
360 # Action name is passed as an argument. Derived classes overriding this
361 # method should also call SUPER implementation of it.
362 sub pre_building_step {
366 # Warn if in source building was enforced but build directory was
367 # specified. See enforce_in_source_building().
368 if ($this->{warn_insource}) {
369 warning("warning: " . $this->NAME() .
370 " does not support building out of source tree. In source building enforced.");
371 delete $this->{warn_insource};
375 # Instance method that is called after performing any step (see below).
376 # Action name is passed as an argument. Derived classes overriding this
377 # method should also call SUPER implementation of it.
378 sub post_building_step {
383 # The instance methods below provide support for configuring,
384 # building, testing, install and cleaning source packages.
385 # In case of failure, the method may just error() out.
387 # These methods should be overriden by derived classes to
388 # implement build system specific steps needed to build the
389 # source. Arbitary number of custom step arguments might be
390 # passed. Default implementations do nothing.
403 # destdir parameter specifies where to install files.