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 # 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.
57 my ($class, %opts)=@_;
59 my $this = bless({ sourcedir => '.',
61 cwd => Cwd::getcwd() }, $class);
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});
69 $this->{sourcedir} = File::Spec->abs2rel($abspath, $this->{cwd});
71 if (exists $opts{builddir}) {
72 $this->_set_builddir($opts{builddir});
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.
82 my $builddir=shift || $this->DEFAULT_BUILD_DIRECTORY;
84 if (defined $builddir) {
85 $builddir = $this->canonpath($builddir); # Canonicalize
88 if ($builddir =~ m#^\.\./#) {
89 # We can't handle those as relative. Make them absolute
90 $builddir = File::Spec->catdir($this->{cwd}, $builddir);
92 elsif ($builddir =~ /\Q$this->{cwd}\E/) {
93 $builddir = File::Spec::abs2rel($builddir, $this->{cwd});
96 # If build directory ends up the same as source directory, drop it
97 if ($builddir eq $this->get_sourcedir()) {
101 $this->{builddir} = $builddir;
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.
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 {
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 {
125 if ($this->get_builddir()) {
126 $this->{warn_insource} = 1;
127 $this->{builddir} = undef;
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 {
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");
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 {
156 $this->prefer_out_of_source_building();
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)) {
189 } elsif (File::Spec->file_name_is_absolute($base)) {
190 return File::Spec->rel2abs($path, $root);
192 return File::Spec->abs2rel(
193 File::Spec->rel2abs($path, $root),
194 File::Spec->rel2abs($base, $root)
199 # Get path to the source directory
200 # (relative to the current (top) directory)
203 return $this->{sourcedir};
206 # Convert path relative to the source directory to the path relative
207 # to the current (top) directory.
209 my ($this, $path)=@_;
210 return File::Spec->catfile($this->get_sourcedir(), $path);
213 # Get path to the build directory if it was specified
214 # (relative to the current (top) directory). undef if the same
215 # as the source directory.
218 return $this->{builddir};
221 # Convert path that is relative to the build directory to the path
222 # that is relative to the current (top) directory.
223 # If $path is not specified, always returns build directory path
224 # relative to the current (top) directory regardless if builddir was
227 my ($this, $path)=@_;
228 my $builddir = $this->get_builddir() || $this->get_sourcedir();
230 return File::Spec->catfile($builddir, $path);
235 # When given a relative path to the source directory, converts it
236 # to the path that is relative to the build directory. If $path is
237 # not given, returns a path to the source directory that is relative
238 # to the build directory.
239 sub get_source_rel2builddir {
244 if ($this->get_builddir()) {
245 $dir = $this->_rel2rel($this->get_sourcedir(), $this->get_builddir());
248 return File::Spec->catfile($dir, $path);
253 # When given a relative path to the build directory, converts it
254 # to the path that is relative to the source directory. If $path is
255 # not given, returns a path to the build directory that is relative
256 # to the source directory.
257 sub get_build_rel2sourcedir {
262 if ($this->get_builddir()) {
263 $dir = $this->_rel2rel($this->get_builddir(), $this->get_sourcedir());
266 return File::Spec->catfile($dir, $path);
271 # Creates a build directory.
274 if ($this->get_builddir()) {
275 doit("mkdir", "-p", $this->get_builddir());
282 verbose_print("cd $dir");
283 chdir $dir or error("error: unable to chdir to $dir");
287 # Changes working directory to the source directory (if needed),
288 # calls doit(@_) and changes working directory back to the top
290 sub doit_in_sourcedir {
292 if ($this->get_sourcedir() ne '.') {
293 my $sourcedir = $this->get_sourcedir();
294 $this->_cd($sourcedir);
296 $this->_cd($this->_rel2rel($this->{cwd}, $sourcedir));
304 # Changes working directory to the build directory (if needed),
305 # calls doit(@_) and changes working directory back to the top
307 sub doit_in_builddir {
309 if ($this->get_buildpath() ne '.') {
310 my $buildpath = $this->get_buildpath();
311 $this->_cd($buildpath);
313 $this->_cd($this->_rel2rel($this->{cwd}, $buildpath));
321 # In case of out of source tree building, whole build directory
322 # gets wiped (if it exists) and 1 is returned. If build directory
323 # had 2 or more levels, empty parent directories are also deleted.
324 # If build directory does not exist, nothing is done and 0 is returned.
327 my $only_empty=shift;
328 if ($this->get_builddir()) {
329 my $buildpath = $this->get_buildpath();
330 if (-d $buildpath && ! $dh{NO_ACT}) {
331 my @spdir = File::Spec->splitdir($this->get_build_rel2sourcedir());
334 doit("rm", "-rf", $buildpath);
337 # If build directory is relative and had 2 or more levels, delete
338 # empty parent directories until the source directory level.
339 if (not File::Spec->file_name_is_absolute($buildpath)) {
340 while (($peek=pop(@spdir)) && $peek ne '.' && $peek ne '..') {
341 my $dir = $this->get_sourcepath(File::Spec->catdir(@spdir, $peek));
342 verbose_print("rmdir $dir");
343 last if ! rmdir($dir);
352 # Instance method that is called before performing any step (see below).
353 # Action name is passed as an argument. Derived classes overriding this
354 # method should also call SUPER implementation of it.
355 sub pre_building_step {
359 # Warn if in source building was enforced but build directory was
360 # specified. See enforce_in_source_building().
361 if ($this->{warn_insource}) {
362 warning("warning: " . $this->NAME() .
363 " does not support building out of source tree. In source building enforced.");
364 delete $this->{warn_insource};
368 # Instance method that is called after performing any step (see below).
369 # Action name is passed as an argument. Derived classes overriding this
370 # method should also call SUPER implementation of it.
371 sub post_building_step {
376 # The instance methods below provide support for configuring,
377 # building, testing, install and cleaning source packages.
378 # In case of failure, the method may just error() out.
380 # These methods should be overriden by derived classes to
381 # implement build system specific steps needed to build the
382 # source. Arbitary number of custom step arguments might be
383 # passed. Default implementations do nothing.
396 # destdir parameter specifies where to install files.