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;
94 # This instance method is called to check if the build system is capable
95 # to auto build a source package. Additional argument $step describes
96 # which operation the caller is going to perform (either configure,
97 # build, test, install or clean). You must override this method for the
98 # build system module to be ever picked up automatically. This method is
99 # used in conjuction with @Dh_Buildsystems::BUILDSYSTEMS.
101 # This method is supposed to be called with source root directory being
102 # working directory. Use $this->get_buildpath($path) method to get full
103 # path to the files in the build directory.
104 sub check_auto_buildable {
110 # Derived class can call this method in its constructor
111 # to enforce in source building even if the user requested otherwise.
112 sub enforce_in_source_building {
114 if ($this->get_builddir()) {
115 $this->{warn_insource} = 1;
116 $this->{builddir} = undef;
120 # Derived class can call this method in its constructor to enforce
121 # out of source building even if the user didn't request it. However,
122 # if 'builddir' named parameter is passed, accept its value as the
123 # build directory even if it matches the source directory (meaning out
124 # of source is only prefered to in source, not enforced).
125 sub enforce_out_of_source_building {
128 if (!defined $this->get_builddir()) {
129 $this->_set_builddir($args{builddir});
130 if (!defined $this->get_builddir() && !$args{builddir}) {
131 # If we are here, DEFAULT_BUILD_DIRECTORY matches
132 # the source directory, building might fail.
133 error("default build directory is the same as the source directory." .
134 " Please specify a custom build directory");
139 # Enhanced version of File::Spec::canonpath. It collapses ..
140 # too so it may return invalid path if symlinks are involved.
141 # On the other hand, it does not need for the path to exist.
143 my ($this, $path)=@_;
146 for my $comp (split(m%/+%, $path)) {
150 elsif ($comp eq '..') {
151 if (@canon > 0) { pop @canon; } else { $back++; }
157 return (@canon + $back > 0) ? join('/', ('..')x$back, @canon) : '.';
160 # Given both $path and $base are relative to the same directory,
161 # converts and returns path of $path being relative the $base.
163 my ($this, $path, $base, $root)=@_;
164 $root = "/tmp" if !defined $root;
166 return File::Spec->abs2rel(
167 File::Spec->rel2abs($path, $root),
168 File::Spec->rel2abs($base, $root)
172 # Get path to the source directory
173 # (relative to the current (top) directory)
176 return $this->{sourcedir};
179 # Convert path relative to the source directory to the path relative
180 # to the current (top) directory.
182 my ($this, $path)=@_;
183 return File::Spec->catfile($this->get_sourcedir(), $path);
186 # Get path to the build directory if it was specified
187 # (relative to the current (top) directory). undef if the same
188 # as the source directory.
191 return $this->{builddir};
194 # Convert path that is relative to the build directory to the path
195 # that is relative to the current (top) directory.
196 # If $path is not specified, always returns build directory path
197 # relative to the current (top) directory regardless if builddir was
200 my ($this, $path)=@_;
201 my $builddir = $this->get_builddir() || $this->get_sourcedir();
203 return File::Spec->catfile($builddir, $path);
208 # When given a relative path to the source directory, converts it
209 # to the path that is relative to the build directory. If $path is
210 # not given, returns a path to the source directory that is relative
211 # to the build directory.
212 sub get_source_rel2builddir {
217 if ($this->get_builddir()) {
218 $dir = $this->_rel2rel($this->get_sourcedir(), $this->get_builddir());
221 return File::Spec->catfile($dir, $path);
226 # When given a relative path to the build directory, converts it
227 # to the path that is relative to the source directory. If $path is
228 # not given, returns a path to the build directory that is relative
229 # to the source directory.
230 sub get_build_rel2sourcedir {
235 if ($this->get_builddir()) {
236 $dir = $this->_rel2rel($this->get_builddir(), $this->get_sourcedir());
239 return File::Spec->catfile($dir, $path);
244 # Creates a build directory.
247 if ($this->get_builddir()) {
248 doit("mkdir", "-p", $this->get_builddir());
255 verbose_print("cd $dir");
256 chdir $dir or error("error: unable to chdir to $dir");
260 # Changes working directory to the source directory (if needed)
261 # calls doit(@_) and changes working directory back to the top
263 sub doit_in_sourcedir {
265 if ($this->get_sourcedir() ne '.') {
266 my $sourcedir = $this->get_sourcedir();
267 my $curdir = Cwd::getcwd();
268 $this->_cd($sourcedir);
270 $this->_cd($this->_rel2rel($curdir, $sourcedir, $curdir));
278 # Changes working directory to the build directory (if needed),
279 # calls doit(@_) and changes working directory back to the top
281 sub doit_in_builddir {
283 if ($this->get_buildpath() ne '.') {
284 my $buildpath = $this->get_buildpath();
285 my $curdir = Cwd::getcwd();
286 $this->_cd($buildpath);
288 $this->_cd($this->_rel2rel($curdir, $buildpath, $curdir));
296 # In case of out of source tree building, whole build directory
297 # gets wiped (if it exists) and 1 is returned. If build directory
298 # had 2 or more levels, empty parent directories are also deleted.
299 # If build directory does not exist, nothing is done and 0 is returned.
302 my $only_empty=shift;
303 if ($this->get_builddir()) {
304 my $buildpath = $this->get_buildpath();
305 if (-d $buildpath && ! $dh{NO_ACT}) {
306 my @spdir = File::Spec->splitdir($this->get_build_rel2sourcedir());
309 doit("rm", "-rf", $buildpath);
312 # If build directory had 2 or more levels, delete empty
313 # parent directories until the source directory level.
314 while (($peek=pop(@spdir)) && $peek ne '.' && $peek ne '..') {
315 last if ! rmdir($this->get_sourcepath(File::Spec->catdir(@spdir, $peek)));
323 # Instance method that is called before performing any step (see below).
324 # Action name is passed as an argument. Derived classes overriding this
325 # method should also call SUPER implementation of it.
326 sub pre_building_step {
330 # Warn if in source building was enforced but build directory was
331 # specified. See enforce_in_source_building().
332 if ($this->{warn_insource}) {
333 warning("warning: " . $this->NAME() .
334 " does not support building out of source tree. In source building enforced.");
335 delete $this->{warn_insource};
339 # Instance method that is called after performing any step (see below).
340 # Action name is passed as an argument. Derived classes overriding this
341 # method should also call SUPER implementation of it.
342 sub post_building_step {
347 # The instance methods below provide support for configuring,
348 # building, testing, install and cleaning source packages.
349 # In case of failure, the method may just error() out.
351 # These methods should be overriden by derived classes to
352 # implement build system specific steps needed to build the
353 # source. Arbitary number of custom step arguments might be
354 # passed. Default implementations do nothing.
367 # destdir parameter specifies where to install files.