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 build a source package. It will be called during the build
113 # system auto-selection process, inside the root directory of the debian
114 # source package. The current build step is passed as an argument.
115 # Return 0 if the source is not buildable, or a positive integer
118 # Generally, it is enough to look for invariant unique build system
119 # files shipped with clean source to determine if the source might
120 # be buildable or not. However, if the build system is derived from
121 # another other auto-buildable build system, this method
122 # may also check if the source has already been built with this build
123 # system partitially by looking for temporary files or other common
124 # results the build system produces during the build process. The
125 # latter checks must be unique to the current build system and must
126 # be very unlikely to be true for either its parent or other build
127 # systems. If it is determined that the source has already built
128 # partitially with this build system, the value returned must be
129 # greater than the one of the SUPER call.
130 sub check_auto_buildable {
136 # Derived class can call this method in its constructor
137 # to enforce in source building even if the user requested otherwise.
138 sub enforce_in_source_building {
140 if ($this->get_builddir()) {
141 $this->{warn_insource} = 1;
142 $this->{builddir} = undef;
146 # Derived class can call this method in its constructor to *prefer*
147 # out of source building. Unless build directory has already been
148 # specified building will proceed in the DEFAULT_BUILD_DIRECTORY or
149 # the one specified in the 'builddir' named parameter (which may
150 # match the source directory). Typically you should pass @_ from
151 # the constructor to this call.
152 sub prefer_out_of_source_building {
155 if (!defined $this->get_builddir()) {
156 if (!$this->_set_builddir($args{builddir}) && !$args{builddir}) {
157 # If we are here, DEFAULT_BUILD_DIRECTORY matches
158 # the source directory, building might fail.
159 error("default build directory is the same as the source directory." .
160 " Please specify a custom build directory");
165 # Enhanced version of File::Spec::canonpath. It collapses ..
166 # too so it may return invalid path if symlinks are involved.
167 # On the other hand, it does not need for the path to exist.
169 my ($this, $path)=@_;
172 foreach my $comp (split(m%/+%, $path)) {
176 elsif ($comp eq '..') {
177 if (@canon > 0) { pop @canon; } else { $back++; }
183 return (@canon + $back > 0) ? join('/', ('..')x$back, @canon) : '.';
186 # Given both $path and $base are relative to the $root, converts and
187 # returns path of $path being relative to the $base. If either $path or
188 # $base is absolute, returns another $path (converted to) absolute.
190 my ($this, $path, $base, $root)=@_;
191 $root = $this->{cwd} unless defined $root;
193 if (File::Spec->file_name_is_absolute($path)) {
196 elsif (File::Spec->file_name_is_absolute($base)) {
197 return File::Spec->rel2abs($path, $root);
200 return File::Spec->abs2rel(
201 File::Spec->rel2abs($path, $root),
202 File::Spec->rel2abs($base, $root)
207 # Get path to the source directory
208 # (relative to the current (top) directory)
211 return $this->{sourcedir};
214 # Convert path relative to the source directory to the path relative
215 # to the current (top) directory.
217 my ($this, $path)=@_;
218 return File::Spec->catfile($this->get_sourcedir(), $path);
221 # Get path to the build directory if it was specified
222 # (relative to the current (top) directory). undef if the same
223 # as the source directory.
226 return $this->{builddir};
229 # Convert path that is relative to the build directory to the path
230 # that is relative to the current (top) directory.
231 # If $path is not specified, always returns build directory path
232 # relative to the current (top) directory regardless if builddir was
235 my ($this, $path)=@_;
236 my $builddir = $this->get_builddir() || $this->get_sourcedir();
238 return File::Spec->catfile($builddir, $path);
243 # When given a relative path to the source directory, converts it
244 # to the path that is relative to the build directory. If $path is
245 # not given, returns a path to the source directory that is relative
246 # to the build directory.
247 sub get_source_rel2builddir {
252 if ($this->get_builddir()) {
253 $dir = $this->_rel2rel($this->get_sourcedir(), $this->get_builddir());
256 return File::Spec->catfile($dir, $path);
263 return $this->{parallel};
266 # When given a relative path to the build directory, converts it
267 # to the path that is relative to the source directory. If $path is
268 # not given, returns a path to the build directory that is relative
269 # to the source directory.
270 sub get_build_rel2sourcedir {
275 if ($this->get_builddir()) {
276 $dir = $this->_rel2rel($this->get_builddir(), $this->get_sourcedir());
279 return File::Spec->catfile($dir, $path);
284 # Creates a build directory.
287 if ($this->get_builddir()) {
288 doit("mkdir", "-p", $this->get_builddir());
295 verbose_print("cd $dir");
296 chdir $dir or error("error: unable to chdir to $dir");
300 # Changes working directory to the source directory (if needed),
301 # calls doit(@_) and changes working directory back to the top
303 sub doit_in_sourcedir {
305 if ($this->get_sourcedir() ne '.') {
306 my $sourcedir = $this->get_sourcedir();
307 $this->_cd($sourcedir);
309 $this->_cd($this->_rel2rel($this->{cwd}, $sourcedir));
317 # Changes working directory to the build directory (if needed),
318 # calls doit(@_) and changes working directory back to the top
320 sub doit_in_builddir {
322 if ($this->get_buildpath() ne '.') {
323 my $buildpath = $this->get_buildpath();
324 $this->_cd($buildpath);
326 $this->_cd($this->_rel2rel($this->{cwd}, $buildpath));
334 # In case of out of source tree building, whole build directory
335 # gets wiped (if it exists) and 1 is returned. If build directory
336 # had 2 or more levels, empty parent directories are also deleted.
337 # If build directory does not exist, nothing is done and 0 is returned.
340 my $only_empty=shift;
341 if ($this->get_builddir()) {
342 my $buildpath = $this->get_buildpath();
344 my @dir = File::Spec->splitdir($this->get_build_rel2sourcedir());
346 if (not $only_empty) {
347 doit("rm", "-rf", $buildpath);
350 # If build directory is relative and had 2 or more levels, delete
351 # empty parent directories until the source or top directory level.
352 if (not File::Spec->file_name_is_absolute($buildpath)) {
353 while (($peek=pop @dir) && $peek ne '.' && $peek ne '..') {
354 my $dir = $this->get_sourcepath(File::Spec->catdir(@dir, $peek));
355 doit("rmdir", "--ignore-fail-on-non-empty", $dir);
365 # Instance method that is called before performing any step (see below).
366 # Action name is passed as an argument. Derived classes overriding this
367 # method should also call SUPER implementation of it.
368 sub pre_building_step {
372 # Warn if in source building was enforced but build directory was
373 # specified. See enforce_in_source_building().
374 if ($this->{warn_insource}) {
375 warning("warning: " . $this->NAME() .
376 " does not support building out of source tree. In source building enforced.");
377 delete $this->{warn_insource};
381 # Instance method that is called after performing any step (see below).
382 # Action name is passed as an argument. Derived classes overriding this
383 # method should also call SUPER implementation of it.
384 sub post_building_step {
389 # The instance methods below provide support for configuring,
390 # building, testing, install and cleaning source packages.
391 # In case of failure, the method may just error() out.
393 # These methods should be overriden by derived classes to
394 # implement build system specific steps needed to build the
395 # source. Arbitary number of custom step arguments might be
396 # passed. Default implementations do nothing.
409 # destdir parameter specifies where to install files.