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 => '.',
60 builddir => undef, }, $class);
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});
69 $this->{sourcedir} = File::Spec->abs2rel($abspath, $curdir);
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.
83 $this->{builddir} = ($builddir) ? $builddir : $this->DEFAULT_BUILD_DIRECTORY;
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;
92 return $this->{builddir};
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.
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 {
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 {
115 if ($this->get_builddir()) {
116 $this->{warn_insource} = 1;
117 $this->{builddir} = undef;
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 {
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");
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 {
146 $this->prefer_out_of_source_building();
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.
153 my ($this, $path)=@_;
156 for my $comp (split(m%/+%, $path)) {
160 elsif ($comp eq '..') {
161 if (@canon > 0) { pop @canon; } else { $back++; }
167 return (@canon + $back > 0) ? join('/', ('..')x$back, @canon) : '.';
170 # Given both $path and $base are relative to the same directory,
171 # converts and returns path of $path being relative the $base.
173 my ($this, $path, $base, $root)=@_;
174 $root = "/tmp" if !defined $root;
176 return File::Spec->abs2rel(
177 File::Spec->rel2abs($path, $root),
178 File::Spec->rel2abs($base, $root)
182 # Get path to the source directory
183 # (relative to the current (top) directory)
186 return $this->{sourcedir};
189 # Convert path relative to the source directory to the path relative
190 # to the current (top) directory.
192 my ($this, $path)=@_;
193 return File::Spec->catfile($this->get_sourcedir(), $path);
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.
201 return $this->{builddir};
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
210 my ($this, $path)=@_;
211 my $builddir = $this->get_builddir() || $this->get_sourcedir();
213 return File::Spec->catfile($builddir, $path);
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 {
227 if ($this->get_builddir()) {
228 $dir = $this->_rel2rel($this->get_sourcedir(), $this->get_builddir());
231 return File::Spec->catfile($dir, $path);
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 {
245 if ($this->get_builddir()) {
246 $dir = $this->_rel2rel($this->get_builddir(), $this->get_sourcedir());
249 return File::Spec->catfile($dir, $path);
254 # Creates a build directory.
257 if ($this->get_builddir()) {
258 doit("mkdir", "-p", $this->get_builddir());
265 verbose_print("cd $dir");
266 chdir $dir or error("error: unable to chdir to $dir");
270 # Changes working directory to the source directory (if needed),
271 # calls doit(@_) and changes working directory back to the top
273 sub doit_in_sourcedir {
275 if ($this->get_sourcedir() ne '.') {
276 my $sourcedir = $this->get_sourcedir();
277 my $curdir = Cwd::getcwd();
278 $this->_cd($sourcedir);
280 $this->_cd($this->_rel2rel($curdir, $sourcedir, $curdir));
288 # Changes working directory to the build directory (if needed),
289 # calls doit(@_) and changes working directory back to the top
291 sub doit_in_builddir {
293 if ($this->get_buildpath() ne '.') {
294 my $buildpath = $this->get_buildpath();
295 my $curdir = Cwd::getcwd();
296 $this->_cd($buildpath);
298 $this->_cd($this->_rel2rel($curdir, $buildpath, $curdir));
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.
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());
319 doit("rm", "-rf", $buildpath);
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)));
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 {
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};
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 {
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.
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.
377 # destdir parameter specifies where to install files.