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 => '.',
61 cwd => Cwd::getcwd() }, $class);
63 if (exists $opts{sourcedir}) {
64 # Get relative sourcedir abs_path (without symlinks)
65 my $abspath = Cwd::abs_path($opts{sourcedir});
66 if (! -d $abspath || $abspath !~ /^\Q$this->{cwd}\E/) {
67 error("invalid or non-existing path to the source directory: ".$opts{sourcedir});
69 $this->{sourcedir} = File::Spec->abs2rel($abspath, $this->{cwd});
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.
82 my $builddir=shift || $this->DEFAULT_BUILD_DIRECTORY;
84 if (defined $builddir) {
85 $builddir = $this->canonpath($builddir); # Canonicalize
88 if ($builddir =~ m#^\.\./#) {
89 # We can't handle those as relative. Make them absolute
90 $builddir = File::Spec->catdir($this->{cwd}, $builddir);
92 elsif ($builddir =~ /\Q$this->{cwd}\E/) {
93 $builddir = File::Spec::abs2rel($builddir, $this->{cwd});
96 # If build directory ends up the same as source directory, drop it
97 if ($builddir eq $this->get_sourcedir()) {
101 $this->{builddir} = $builddir;
105 # This instance method is called to check if the build system is able
106 # to auto build a source package. Additional argument $step describes
107 # which operation the caller is going to perform (either configure,
108 # build, test, install or clean). You must override this method for the
109 # build system module to be ever picked up automatically. This method is
110 # used in conjuction with @Dh_Buildsystems::BUILDSYSTEMS.
112 # This method is supposed to be called inside the source root directory.
113 # Use $this->get_buildpath($path) method to get full path to the files
114 # in the build directory.
115 sub check_auto_buildable {
121 # Derived class can call this method in its constructor
122 # to enforce in source building even if the user requested otherwise.
123 sub enforce_in_source_building {
125 if ($this->get_builddir()) {
126 $this->{warn_insource} = 1;
127 $this->{builddir} = undef;
131 # Derived class can call this method in its constructor to *prefer*
132 # out of source building. Unless build directory has already been
133 # specified building will proceed in the DEFAULT_BUILD_DIRECTORY or
134 # the one specified in the 'builddir' named parameter (which may
135 # match the source directory). Typically you should pass @_ from
136 # the constructor to this call.
137 sub prefer_out_of_source_building {
140 if (!defined $this->get_builddir()) {
141 if (!$this->_set_builddir($args{builddir}) && !$args{builddir}) {
142 # If we are here, DEFAULT_BUILD_DIRECTORY matches
143 # the source directory, building might fail.
144 error("default build directory is the same as the source directory." .
145 " Please specify a custom build directory");
150 # Enhanced version of File::Spec::canonpath. It collapses ..
151 # too so it may return invalid path if symlinks are involved.
152 # On the other hand, it does not need for the path to exist.
154 my ($this, $path)=@_;
157 for my $comp (split(m%/+%, $path)) {
161 elsif ($comp eq '..') {
162 if (@canon > 0) { pop @canon; } else { $back++; }
168 return (@canon + $back > 0) ? join('/', ('..')x$back, @canon) : '.';
171 # Given both $path and $base are relative to the $root, converts and
172 # returns path of $path being relative to the $base. If either $path or
173 # $base is absolute, returns another $path (converted to) absolute.
175 my ($this, $path, $base, $root)=@_;
176 $root = $this->{cwd} unless defined $root;
178 if (File::Spec->file_name_is_absolute($path)) {
181 elsif (File::Spec->file_name_is_absolute($base)) {
182 return File::Spec->rel2abs($path, $root);
185 return File::Spec->abs2rel(
186 File::Spec->rel2abs($path, $root),
187 File::Spec->rel2abs($base, $root)
192 # Get path to the source directory
193 # (relative to the current (top) directory)
196 return $this->{sourcedir};
199 # Convert path relative to the source directory to the path relative
200 # to the current (top) directory.
202 my ($this, $path)=@_;
203 return File::Spec->catfile($this->get_sourcedir(), $path);
206 # Get path to the build directory if it was specified
207 # (relative to the current (top) directory). undef if the same
208 # as the source directory.
211 return $this->{builddir};
214 # Convert path that is relative to the build directory to the path
215 # that is relative to the current (top) directory.
216 # If $path is not specified, always returns build directory path
217 # relative to the current (top) directory regardless if builddir was
220 my ($this, $path)=@_;
221 my $builddir = $this->get_builddir() || $this->get_sourcedir();
223 return File::Spec->catfile($builddir, $path);
228 # When given a relative path to the source directory, converts it
229 # to the path that is relative to the build directory. If $path is
230 # not given, returns a path to the source directory that is relative
231 # to the build directory.
232 sub get_source_rel2builddir {
237 if ($this->get_builddir()) {
238 $dir = $this->_rel2rel($this->get_sourcedir(), $this->get_builddir());
241 return File::Spec->catfile($dir, $path);
246 # When given a relative path to the build directory, converts it
247 # to the path that is relative to the source directory. If $path is
248 # not given, returns a path to the build directory that is relative
249 # to the source directory.
250 sub get_build_rel2sourcedir {
255 if ($this->get_builddir()) {
256 $dir = $this->_rel2rel($this->get_builddir(), $this->get_sourcedir());
259 return File::Spec->catfile($dir, $path);
264 # Creates a build directory.
267 if ($this->get_builddir()) {
268 doit("mkdir", "-p", $this->get_builddir());
275 verbose_print("cd $dir");
276 chdir $dir or error("error: unable to chdir to $dir");
280 # Changes working directory to the source directory (if needed),
281 # calls doit(@_) and changes working directory back to the top
283 sub doit_in_sourcedir {
285 if ($this->get_sourcedir() ne '.') {
286 my $sourcedir = $this->get_sourcedir();
287 $this->_cd($sourcedir);
289 $this->_cd($this->_rel2rel($this->{cwd}, $sourcedir));
297 # Changes working directory to the build directory (if needed),
298 # calls doit(@_) and changes working directory back to the top
300 sub doit_in_builddir {
302 if ($this->get_buildpath() ne '.') {
303 my $buildpath = $this->get_buildpath();
304 $this->_cd($buildpath);
306 $this->_cd($this->_rel2rel($this->{cwd}, $buildpath));
314 # In case of out of source tree building, whole build directory
315 # gets wiped (if it exists) and 1 is returned. If build directory
316 # had 2 or more levels, empty parent directories are also deleted.
317 # If build directory does not exist, nothing is done and 0 is returned.
320 my $only_empty=shift;
321 if ($this->get_builddir()) {
322 my $buildpath = $this->get_buildpath();
324 my @dir = File::Spec->splitdir($this->get_build_rel2sourcedir());
326 if (not $only_empty) {
327 doit("rm", "-rf", $buildpath);
330 # If build directory is relative and had 2 or more levels, delete
331 # empty parent directories until the source or top directory level.
332 if (not File::Spec->file_name_is_absolute($buildpath)) {
333 while (($peek=pop @dir) && $peek ne '.' && $peek ne '..') {
334 my $dir = $this->get_sourcepath(File::Spec->catdir(@dir, $peek));
335 doit("rmdir", "--ignore-fail-on-non-empty", $dir);
345 # Instance method that is called before performing any step (see below).
346 # Action name is passed as an argument. Derived classes overriding this
347 # method should also call SUPER implementation of it.
348 sub pre_building_step {
352 # Warn if in source building was enforced but build directory was
353 # specified. See enforce_in_source_building().
354 if ($this->{warn_insource}) {
355 warning("warning: " . $this->NAME() .
356 " does not support building out of source tree. In source building enforced.");
357 delete $this->{warn_insource};
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 build system 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.