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 # - parallel - max number of parallel processes to be spawned for building
52 # sources (-1 = unlimited; 1 = no parallel)
53 # Derived class can override the constructor to initialize common object
54 # parameters. Do NOT use constructor to execute commands or otherwise
55 # configure/setup build environment. There is absolutely no guarantee the
56 # constructed object will be used to build something. Use pre_building_step(),
57 # $build_step() or post_building_step() methods for this.
59 my ($class, %opts)=@_;
61 my $this = bless({ sourcedir => '.',
64 cwd => Cwd::getcwd() }, $class);
66 if (exists $opts{sourcedir}) {
67 # Get relative sourcedir abs_path (without symlinks)
68 my $abspath = Cwd::abs_path($opts{sourcedir});
69 if (! -d $abspath || $abspath !~ /^\Q$this->{cwd}\E/) {
70 error("invalid or non-existing path to the source directory: ".$opts{sourcedir});
72 $this->{sourcedir} = File::Spec->abs2rel($abspath, $this->{cwd});
74 if (exists $opts{builddir}) {
75 $this->_set_builddir($opts{builddir});
77 if (defined $opts{parallel}) {
78 $this->{parallel} = $opts{parallel};
83 # Private method to set a build directory. If undef, use default.
84 # Do $this->{builddir} = undef or pass $this->get_sourcedir() to
85 # unset the build directory.
88 my $builddir=shift || $this->DEFAULT_BUILD_DIRECTORY;
90 if (defined $builddir) {
91 $builddir = $this->canonpath($builddir); # Canonicalize
94 if ($builddir =~ m#^\.\./#) {
95 # We can't handle those as relative. Make them absolute
96 $builddir = File::Spec->catdir($this->{cwd}, $builddir);
98 elsif ($builddir =~ /\Q$this->{cwd}\E/) {
99 $builddir = File::Spec::abs2rel($builddir, $this->{cwd});
102 # If build directory ends up the same as source directory, drop it
103 if ($builddir eq $this->get_sourcedir()) {
107 $this->{builddir} = $builddir;
111 # This instance method is called to check if the build system is able
112 # to auto build a source package. Additional argument $step describes
113 # which operation the caller is going to perform (either configure,
114 # build, test, install or clean). You must override this method for the
115 # build system module to be ever picked up automatically. This method is
116 # used in conjuction with @Dh_Buildsystems::BUILDSYSTEMS.
118 # This method is supposed to be called inside the source root directory.
119 # Use $this->get_buildpath($path) method to get full path to the files
120 # in the build directory.
121 sub check_auto_buildable {
127 # Derived class can call this method in its constructor
128 # to enforce in source building even if the user requested otherwise.
129 sub enforce_in_source_building {
131 if ($this->get_builddir()) {
132 $this->{warn_insource} = 1;
133 $this->{builddir} = undef;
137 # Derived class can call this method in its constructor to *prefer*
138 # out of source building. Unless build directory has already been
139 # specified building will proceed in the DEFAULT_BUILD_DIRECTORY or
140 # the one specified in the 'builddir' named parameter (which may
141 # match the source directory). Typically you should pass @_ from
142 # the constructor to this call.
143 sub prefer_out_of_source_building {
146 if (!defined $this->get_builddir()) {
147 if (!$this->_set_builddir($args{builddir}) && !$args{builddir}) {
148 # If we are here, DEFAULT_BUILD_DIRECTORY matches
149 # the source directory, building might fail.
150 error("default build directory is the same as the source directory." .
151 " Please specify a custom build directory");
156 # Enhanced version of File::Spec::canonpath. It collapses ..
157 # too so it may return invalid path if symlinks are involved.
158 # On the other hand, it does not need for the path to exist.
160 my ($this, $path)=@_;
163 for my $comp (split(m%/+%, $path)) {
167 elsif ($comp eq '..') {
168 if (@canon > 0) { pop @canon; } else { $back++; }
174 return (@canon + $back > 0) ? join('/', ('..')x$back, @canon) : '.';
177 # Given both $path and $base are relative to the $root, converts and
178 # returns path of $path being relative to the $base. If either $path or
179 # $base is absolute, returns another $path (converted to) absolute.
181 my ($this, $path, $base, $root)=@_;
182 $root = $this->{cwd} unless defined $root;
184 if (File::Spec->file_name_is_absolute($path)) {
187 elsif (File::Spec->file_name_is_absolute($base)) {
188 return File::Spec->rel2abs($path, $root);
191 return File::Spec->abs2rel(
192 File::Spec->rel2abs($path, $root),
193 File::Spec->rel2abs($base, $root)
198 # Get path to the source directory
199 # (relative to the current (top) directory)
202 return $this->{sourcedir};
205 # Convert path relative to the source directory to the path relative
206 # to the current (top) directory.
208 my ($this, $path)=@_;
209 return File::Spec->catfile($this->get_sourcedir(), $path);
212 # Get path to the build directory if it was specified
213 # (relative to the current (top) directory). undef if the same
214 # as the source directory.
217 return $this->{builddir};
220 # Convert path that is relative to the build directory to the path
221 # that is relative to the current (top) directory.
222 # If $path is not specified, always returns build directory path
223 # relative to the current (top) directory regardless if builddir was
226 my ($this, $path)=@_;
227 my $builddir = $this->get_builddir() || $this->get_sourcedir();
229 return File::Spec->catfile($builddir, $path);
234 # When given a relative path to the source directory, converts it
235 # to the path that is relative to the build directory. If $path is
236 # not given, returns a path to the source directory that is relative
237 # to the build directory.
238 sub get_source_rel2builddir {
243 if ($this->get_builddir()) {
244 $dir = $this->_rel2rel($this->get_sourcedir(), $this->get_builddir());
247 return File::Spec->catfile($dir, $path);
254 return $this->{parallel};
257 # When given a relative path to the build directory, converts it
258 # to the path that is relative to the source directory. If $path is
259 # not given, returns a path to the build directory that is relative
260 # to the source directory.
261 sub get_build_rel2sourcedir {
266 if ($this->get_builddir()) {
267 $dir = $this->_rel2rel($this->get_builddir(), $this->get_sourcedir());
270 return File::Spec->catfile($dir, $path);
275 # Creates a build directory.
278 if ($this->get_builddir()) {
279 doit("mkdir", "-p", $this->get_builddir());
286 verbose_print("cd $dir");
287 chdir $dir or error("error: unable to chdir to $dir");
291 # Changes working directory to the source directory (if needed),
292 # calls doit(@_) and changes working directory back to the top
294 sub doit_in_sourcedir {
296 if ($this->get_sourcedir() ne '.') {
297 my $sourcedir = $this->get_sourcedir();
298 $this->_cd($sourcedir);
300 $this->_cd($this->_rel2rel($this->{cwd}, $sourcedir));
308 # Changes working directory to the build directory (if needed),
309 # calls doit(@_) and changes working directory back to the top
311 sub doit_in_builddir {
313 if ($this->get_buildpath() ne '.') {
314 my $buildpath = $this->get_buildpath();
315 $this->_cd($buildpath);
317 $this->_cd($this->_rel2rel($this->{cwd}, $buildpath));
325 # In case of out of source tree building, whole build directory
326 # gets wiped (if it exists) and 1 is returned. If build directory
327 # had 2 or more levels, empty parent directories are also deleted.
328 # If build directory does not exist, nothing is done and 0 is returned.
331 my $only_empty=shift;
332 if ($this->get_builddir()) {
333 my $buildpath = $this->get_buildpath();
335 my @dir = File::Spec->splitdir($this->get_build_rel2sourcedir());
337 if (not $only_empty) {
338 doit("rm", "-rf", $buildpath);
341 # If build directory is relative and had 2 or more levels, delete
342 # empty parent directories until the source or top directory level.
343 if (not File::Spec->file_name_is_absolute($buildpath)) {
344 while (($peek=pop @dir) && $peek ne '.' && $peek ne '..') {
345 my $dir = $this->get_sourcepath(File::Spec->catdir(@dir, $peek));
346 doit("rmdir", "--ignore-fail-on-non-empty", $dir);
356 # Instance method that is called before performing any step (see below).
357 # Action name is passed as an argument. Derived classes overriding this
358 # method should also call SUPER implementation of it.
359 sub pre_building_step {
363 # Warn if in source building was enforced but build directory was
364 # specified. See enforce_in_source_building().
365 if ($this->{warn_insource}) {
366 warning("warning: " . $this->NAME() .
367 " does not support building out of source tree. In source building enforced.");
368 delete $this->{warn_insource};
372 # Instance method that is called after performing any step (see below).
373 # Action name is passed as an argument. Derived classes overriding this
374 # method should also call SUPER implementation of it.
375 sub post_building_step {
380 # The instance methods below provide support for configuring,
381 # building, testing, install and cleaning source packages.
382 # In case of failure, the method may just error() out.
384 # These methods should be overriden by derived classes to
385 # implement build system specific steps needed to build the
386 # source. Arbitary number of custom step arguments might be
387 # passed. Default implementations do nothing.
400 # destdir parameter specifies where to install files.