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 # 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 is specified, accept it even if it matches the source
123 # directory (soft mode).
124 sub enforce_out_of_source_building {
125 my ($this, $builddir) = @_;
126 if (!defined $this->get_builddir()) {
127 $this->_set_builddir($builddir);
128 if (!defined $this->get_builddir() && !$builddir) {
129 # If we are here, DEFAULT_BUILD_DIRECTORY matches
130 # the source directory, building might fail.
131 error("default build directory is the same as the source directory." .
132 " Please specify a custom build directory");
137 # Enhanced version of File::Spec::canonpath. It collapses ..
138 # too so it may return invalid path if symlinks are involved.
139 # On the other hand, it does not need for the path to exist.
141 my ($this, $path)=@_;
144 for my $comp (split(m%/+%, $path)) {
148 elsif ($comp eq '..') {
149 if (@canon > 0) { pop @canon; } else { $back++; }
155 return (@canon + $back > 0) ? join('/', ('..')x$back, @canon) : '.';
158 # Given both $path and $base are relative to the same directory,
159 # converts and returns path of $path being relative the $base.
161 my ($this, $path, $base, $root)=@_;
162 $root = "/tmp" if !defined $root;
164 return File::Spec->abs2rel(
165 File::Spec->rel2abs($path, $root),
166 File::Spec->rel2abs($base, $root)
170 # Get path to the source directory
171 # (relative to the current (top) directory)
174 return $this->{sourcedir};
177 # Convert path relative to the source directory to the path relative
178 # to the current (top) directory.
180 my ($this, $path)=@_;
181 return File::Spec->catfile($this->get_sourcedir(), $path);
184 # Get path to the build directory if it was specified
185 # (relative to the current (top) directory). undef if the same
186 # as the source directory.
189 return $this->{builddir};
192 # Convert path that is relative to the build directory to the path
193 # that is relative to the current (top) directory.
194 # If $path is not specified, always returns build directory path
195 # relative to the current (top) directory regardless if builddir was
198 my ($this, $path)=@_;
199 my $builddir = $this->get_builddir() || $this->get_sourcedir();
201 return File::Spec->catfile($builddir, $path);
206 # When given a relative path to the source directory, converts it
207 # to the path that is relative to the build directory. If $path is
208 # not given, returns a path to the source directory that is relative
209 # to the build directory.
210 sub get_source_rel2builddir {
215 if ($this->get_builddir()) {
216 $dir = $this->_rel2rel($this->get_sourcedir(), $this->get_builddir());
219 return File::Spec->catfile($dir, $path);
224 # When given a relative path to the build directory, converts it
225 # to the path that is relative to the source directory. If $path is
226 # not given, returns a path to the build directory that is relative
227 # to the source directory.
228 sub get_build_rel2sourcedir {
233 if ($this->get_builddir()) {
234 $dir = $this->_rel2rel($this->get_builddir(), $this->get_sourcedir());
237 return File::Spec->catfile($dir, $path);
242 # Creates a build directory.
245 if ($this->get_builddir()) {
246 doit("mkdir", "-p", $this->get_builddir());
253 verbose_print("cd $dir");
254 chdir $dir or error("error: unable to chdir to $dir");
258 # Changes working directory to the source directory (if needed)
259 # calls doit(@_) and changes working directory back to the top
261 sub doit_in_sourcedir {
263 if ($this->get_sourcedir() ne '.') {
264 my $sourcedir = $this->get_sourcedir();
265 my $curdir = Cwd::getcwd();
266 $this->_cd($sourcedir);
268 $this->_cd($this->_rel2rel($curdir, $sourcedir, $curdir));
276 # Changes working directory to the build directory (if needed),
277 # calls doit(@_) and changes working directory back to the top
279 sub doit_in_builddir {
281 if ($this->get_buildpath() ne '.') {
282 my $buildpath = $this->get_buildpath();
283 my $curdir = Cwd::getcwd();
284 $this->_cd($buildpath);
286 $this->_cd($this->_rel2rel($curdir, $buildpath, $curdir));
294 # In case of out of source tree building, whole build directory
295 # gets wiped (if it exists) and 1 is returned. If build directory
296 # had 2 or more levels, empty parent directories are also deleted.
297 # If build directory does not exist, nothing is done and 0 is returned.
300 my $only_empty=shift;
301 if ($this->get_builddir()) {
302 my $buildpath = $this->get_buildpath();
303 if (-d $buildpath && ! $dh{NO_ACT}) {
304 my @spdir = File::Spec->splitdir($this->get_build_rel2sourcedir());
307 doit("rm", "-rf", $buildpath);
310 # If build directory had 2 or more levels, delete empty
311 # parent directories until the source directory level.
312 while (($peek=pop(@spdir)) && $peek ne '.' && $peek ne '..') {
313 last if ! rmdir($this->get_sourcepath(File::Spec->catdir(@spdir, $peek)));
321 # Instance method that is called before performing any step (see below).
322 # Action name is passed as an argument. Derived classes overriding this
323 # method should also call SUPER implementation of it.
324 sub pre_building_step {
328 # Warn if in source building was enforced but build directory was
329 # specified. See enforce_in_source_building().
330 if ($this->{warn_insource}) {
331 warning("warning: " . $this->NAME() .
332 " does not support building out of source tree. In source building enforced.");
333 delete $this->{warn_insource};
337 # Instance method that is called after performing any step (see below).
338 # Action name is passed as an argument. Derived classes overriding this
339 # method should also call SUPER implementation of it.
340 sub post_building_step {
345 # The instance methods below provide support for configuring,
346 # building, testing, install and cleaning source packages.
347 # In case of failure, the method may just error() out.
349 # These methods should be overriden by derived classes to
350 # implement buildsystem specific steps needed to build the
351 # source. Arbitary number of custom step arguments might be
352 # passed. Default implementations do nothing.
365 # destdir parameter specifies where to install files.