1 # Defines debhelper buildsystem 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 buildsystems.
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 buildsystem 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 # source directory unless it starts with ./, then it is
50 # assumed to be relative to the top directory. If undef or
51 # empty, DEFAULT_BUILD_DIRECTORY relative to the source
52 # directory will be used. If not specified, in source build
54 # - build_step - set this parameter to the name of the build step
55 # if you want the object to determine its is_buidable
56 # status automatically (with check_auto_buildable()).
57 # Do not pass this parameter if is_buildable flag should
58 # be forced to true or set this parameter to undef if
59 # is_buildable flag should be false.
60 # Derived class can override the constructor to initialize common object
61 # parameters and execute commands to configure build environment if
62 # is_buildable flag is set on the object.
64 my ($class, %opts)=@_;
66 my $this = bless({ sourcedir => '.',
68 is_buildable => 1 }, $class);
70 if (exists $opts{sourcedir}) {
71 # Get relative sourcedir abs_path (without symlinks)
72 my $curdir = Cwd::getcwd();
73 my $abspath = Cwd::abs_path($opts{sourcedir});
74 if (! -d $abspath || $abspath !~ /^\Q$curdir\E/) {
75 error("Invalid or non-existing path to the source directory: ".$opts{sourcedir});
77 $this->{sourcedir} = File::Spec->abs2rel($abspath, $curdir);
79 if (exists $opts{builddir}) {
80 $this->_set_builddir($opts{builddir});
82 if (exists $opts{build_step}) {
83 if (defined $opts{build_step}) {
84 $this->{is_buildable} = $this->check_auto_buildable($opts{build_step});
87 $this->{is_buildable} = 0;
93 # Private method to set a build directory. If undef, use default.
94 # Do $this->{builddir} = undef or pass $this->get_sourcedir() to
95 # unset the build directory.
100 if ($builddir =~ m!^\./(.*)!) {
101 # Specified as relative to the current directory
102 $this->{builddir} = $1;
105 # Specified as relative to the source directory
106 $this->{builddir} = $this->get_sourcepath($builddir);
110 # Relative to the source directory by default
111 $this->{builddir} = $this->get_sourcepath($this->DEFAULT_BUILD_DIRECTORY());
114 # Canonicalize. If build directory ends up the same as source directory, drop it
115 if (defined $this->{builddir}) {
116 $this->{builddir} = $this->_canonpath($this->{builddir});
117 if ($this->{builddir} eq $this->get_sourcedir()) {
118 $this->{builddir} = undef;
123 # Test is_buildable flag of the object.
126 return $this->{is_buildable};
129 # This instance method is called to check if the build system is capable
130 # to auto build a source package. Additional argument $step describes
131 # which operation the caller is going to perform (either configure,
132 # build, test, install or clean). You must override this method for the
133 # build system module to be ever picked up automatically. This method is
134 # used in conjuction with @Dh_Buildsystems::BUILDSYSTEMS.
136 # This method is supposed to be called with source root directory being
137 # working directory. Use $this->get_buildpath($path) method to get full
138 # path to the files in the build directory.
139 sub check_auto_buildable {
145 # Derived class can call this method in its constructor
146 # to enforce in source building even if the user requested otherwise.
147 sub enforce_in_source_building {
149 if ($this->{builddir}) {
150 # Do not emit warning unless the object is buildable.
151 if ($this->is_buildable()) {
152 warning("warning: " . $this->NAME() .
153 " does not support building out of source tree. In source building enforced.");
155 $this->{builddir} = undef;
159 # Derived class can call this method in its constructor to enforce
160 # out of source building even if the user didn't request it.
161 sub enforce_out_of_source_building {
162 my ($this, $builddir) = @_;
163 if (!defined $this->get_builddir()) {
164 $this->_set_builddir($builddir);
165 # The build directory might have been dropped if it matched the
166 # source directory. Just set to default in this case.
167 if (!defined $this->get_builddir()) {
168 $this->_set_builddir();
173 # Enhanced version of File::Spec::canonpath. It collapses ..
174 # too so it may return invalid path if symlinks are involved.
175 # On the other hand, it does not need for the path to exist.
177 my ($this, $path)=@_;
180 for my $comp (split(m%/+%, $path)) {
184 elsif ($comp eq '..') {
185 if (@canon > 0) { pop @canon; } else { $back++; }
191 return (@canon + $back > 0) ? join('/', ('..')x$back, @canon) : '.';
194 # Given both $path and $base are relative to the same directory,
195 # converts and returns path of $path being relative the $base.
197 my ($this, $path, $base, $root)=@_;
198 $root = File::Spec->rootdir() if !defined $root;
200 return File::Spec->abs2rel(
201 File::Spec->rel2abs($path, $root),
202 File::Spec->rel2abs($base, $root)
206 # Get path to the source directory
207 # (relative to the current (top) directory)
210 return $this->{sourcedir};
213 # Convert path relative to the source directory to the path relative
214 # to the current (top) directory.
216 my ($this, $path)=@_;
217 return File::Spec->catfile($this->get_sourcedir(), $path);
220 # Get path to the build directory if it was specified
221 # (relative to the current (top) directory). undef otherwise.
224 return $this->{builddir};
227 # Convert path that is relative to the build directory to the path
228 # that is relative to the current (top) directory.
229 # If $path is not specified, always returns build directory path
230 # relative to the current (top) directory regardless if builddir was
233 my ($this, $path)=@_;
234 my $builddir = $this->get_builddir() || $this->get_sourcedir();
236 return File::Spec->catfile($builddir, $path);
241 # When given a relative path to the source directory, converts it
242 # to the path that is relative to the build directory. If $path is
243 # not given, returns a path to the source directory that is relative
244 # to the build directory.
245 sub get_source_rel2builddir {
250 if ($this->get_builddir()) {
251 $dir = $this->_rel2rel($this->get_sourcedir(), $this->get_builddir());
254 return File::Spec->catfile($dir, $path);
259 # When given a relative path to the build directory, converts it
260 # to the path that is relative to the source directory. If $path is
261 # not given, returns a path to the build directory that is relative
262 # to the source directory.
263 sub get_build_rel2sourcedir {
268 if ($this->get_builddir()) {
269 $dir = $this->_rel2rel($this->get_builddir(), $this->get_sourcedir());
272 return File::Spec->catfile($dir, $path);
277 # Creates a build directory.
280 if ($this->get_builddir()) {
281 doit("mkdir", "-p", $this->get_builddir());
288 verbose_print("cd $dir");
289 chdir $dir or error("error: unable to chdir to $dir");
293 # Changes working directory to the source directory (if needed)
294 # calls doit(@_) and changes working directory back to the top
296 sub doit_in_sourcedir {
298 if ($this->get_sourcedir() ne '.') {
299 my $sourcedir = get_sourcedir();
300 my $curdir = Cwd::getcwd();
301 $this->_cd($sourcedir);
303 $this->_cd($this->_rel2rel($curdir, $sourcedir, $curdir));
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 my $curdir = Cwd::getcwd();
319 $this->_cd($buildpath);
321 $this->_cd($this->_rel2rel($curdir, $buildpath, $curdir));
329 # In case of out of source tree building, whole build directory
330 # gets wiped (if it exists) and 1 is returned. If build directory
331 # had 2 or more levels, empty parent directories are also deleted.
332 # If build directory does not exist, nothing is done and 0 is returned.
335 if ($this->get_builddir()) {
336 my $buildpath = $this->get_buildpath();
337 if (-d $buildpath && ! $dh{NO_ACT}) {
338 doit("rm", "-rf", $buildpath);
339 # If build directory had 2 or more levels, delete empty
340 # parent directories until the source directory level.
341 my @spdir = File::Spec->splitdir($this->get_build_rel2sourcedir());
344 while (($peek=pop(@spdir)) && $peek ne '.' && $peek ne '..') {
345 last if ! rmdir($this->get_sourcepath(File::Spec->catdir(@spdir, $peek)));
353 # Instance method that is called before performing any step (see below).
354 # Action name is passed as an argument. Derived classes overriding this
355 # method should also call SUPER implementation of it.
356 sub pre_building_step {
361 # Instance method that is called after performing any step (see below).
362 # Action name is passed as an argument. Derived classes overriding this
363 # method should also call SUPER implementation of it.
364 sub post_building_step {
369 # The instance methods below provide support for configuring,
370 # building, testing, install and cleaning source packages.
371 # In case of failure, the method may just error() out.
373 # These methods should be overriden by derived classes to
374 # implement buildsystem specific steps needed to build the
375 # source. Arbitary number of custom step arguments might be
376 # passed. Default implementations do nothing.
389 # destdir parameter specifies where to install files.