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 # source directory unless it starts with ./, then it is
50 # assumed to be relative to the top directory. If undef or
51 # empty, DEFAULT_BUILD_DIRECTORY relative to the source
52 # directory will be used. If not specified, in source build
54 # Derived class can override the constructor to initialize common object
55 # parameters. Do NOT use constructor to execute commands or otherwise
56 # configure/setup build environment. There is absolutely no guarantee the
57 # constructed object will be used to build something. Use pre_building_step(),
58 # $build_step() or post_building_step() methods for this.
60 my ($class, %opts)=@_;
62 my $this = bless({ sourcedir => '.',
63 builddir => undef, }, $class);
65 if (exists $opts{sourcedir}) {
66 # Get relative sourcedir abs_path (without symlinks)
67 my $curdir = Cwd::getcwd();
68 my $abspath = Cwd::abs_path($opts{sourcedir});
69 if (! -d $abspath || $abspath !~ /^\Q$curdir\E/) {
70 error("Invalid or non-existing path to the source directory: ".$opts{sourcedir});
72 $this->{sourcedir} = File::Spec->abs2rel($abspath, $curdir);
74 if (exists $opts{builddir}) {
75 $this->_set_builddir($opts{builddir});
80 # Private method to set a build directory. If undef, use default.
81 # Do $this->{builddir} = undef or pass $this->get_sourcedir() to
82 # unset the build directory.
87 if ($builddir =~ m!^\./(.*)!) {
88 # Specified as relative to the current directory
89 $this->{builddir} = $1;
92 # Specified as relative to the source directory
93 $this->{builddir} = $this->get_sourcepath($builddir);
97 # Relative to the source directory by default
98 $this->{builddir} = $this->get_sourcepath($this->DEFAULT_BUILD_DIRECTORY());
101 # Canonicalize. If build directory ends up the same as source directory, drop it
102 if (defined $this->{builddir}) {
103 $this->{builddir} = $this->_canonpath($this->{builddir});
104 if ($this->{builddir} eq $this->get_sourcedir()) {
105 $this->{builddir} = undef;
110 # This instance method is called to check if the build system is capable
111 # to auto build a source package. Additional argument $step describes
112 # which operation the caller is going to perform (either configure,
113 # build, test, install or clean). You must override this method for the
114 # build system module to be ever picked up automatically. This method is
115 # used in conjuction with @Dh_Buildsystems::BUILDSYSTEMS.
117 # This method is supposed to be called with source root directory being
118 # working directory. Use $this->get_buildpath($path) method to get full
119 # path to the files in the build directory.
120 sub check_auto_buildable {
126 # Derived class can call this method in its constructor
127 # to enforce in source building even if the user requested otherwise.
128 sub enforce_in_source_building {
130 if ($this->get_builddir()) {
131 $this->{warn_insource} = 1;
132 $this->{builddir} = undef;
136 # Derived class can call this method in its constructor to enforce
137 # out of source building even if the user didn't request it.
138 sub enforce_out_of_source_building {
139 my ($this, $builddir) = @_;
140 if (!defined $this->get_builddir()) {
141 $this->_set_builddir($builddir);
142 # The build directory might have been dropped if it matched the
143 # source directory. Just set to default in this case.
144 if (!defined $this->get_builddir()) {
145 $this->_set_builddir();
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 same directory,
172 # converts and returns path of $path being relative the $base.
174 my ($this, $path, $base, $root)=@_;
175 $root = File::Spec->rootdir() if !defined $root;
177 return File::Spec->abs2rel(
178 File::Spec->rel2abs($path, $root),
179 File::Spec->rel2abs($base, $root)
183 # Get path to the source directory
184 # (relative to the current (top) directory)
187 return $this->{sourcedir};
190 # Convert path relative to the source directory to the path relative
191 # to the current (top) directory.
193 my ($this, $path)=@_;
194 return File::Spec->catfile($this->get_sourcedir(), $path);
197 # Get path to the build directory if it was specified
198 # (relative to the current (top) directory). undef otherwise.
201 return $this->{builddir};
204 # Convert path that is relative to the build directory to the path
205 # that is relative to the current (top) directory.
206 # If $path is not specified, always returns build directory path
207 # relative to the current (top) directory regardless if builddir was
210 my ($this, $path)=@_;
211 my $builddir = $this->get_builddir() || $this->get_sourcedir();
213 return File::Spec->catfile($builddir, $path);
218 # When given a relative path to the source directory, converts it
219 # to the path that is relative to the build directory. If $path is
220 # not given, returns a path to the source directory that is relative
221 # to the build directory.
222 sub get_source_rel2builddir {
227 if ($this->get_builddir()) {
228 $dir = $this->_rel2rel($this->get_sourcedir(), $this->get_builddir());
231 return File::Spec->catfile($dir, $path);
236 # When given a relative path to the build directory, converts it
237 # to the path that is relative to the source directory. If $path is
238 # not given, returns a path to the build directory that is relative
239 # to the source directory.
240 sub get_build_rel2sourcedir {
245 if ($this->get_builddir()) {
246 $dir = $this->_rel2rel($this->get_builddir(), $this->get_sourcedir());
249 return File::Spec->catfile($dir, $path);
254 # Creates a build directory.
257 if ($this->get_builddir()) {
258 doit("mkdir", "-p", $this->get_builddir());
265 verbose_print("cd $dir");
266 chdir $dir or error("error: unable to chdir to $dir");
270 # Changes working directory to the source directory (if needed)
271 # calls doit(@_) and changes working directory back to the top
273 sub doit_in_sourcedir {
275 if ($this->get_sourcedir() ne '.') {
276 my $sourcedir = get_sourcedir();
277 my $curdir = Cwd::getcwd();
278 $this->_cd($sourcedir);
280 $this->_cd($this->_rel2rel($curdir, $sourcedir, $curdir));
288 # Changes working directory to the build directory (if needed),
289 # calls doit(@_) and changes working directory back to the top
291 sub doit_in_builddir {
293 if ($this->get_buildpath() ne '.') {
294 my $buildpath = $this->get_buildpath();
295 my $curdir = Cwd::getcwd();
296 $this->_cd($buildpath);
298 $this->_cd($this->_rel2rel($curdir, $buildpath, $curdir));
306 # In case of out of source tree building, whole build directory
307 # gets wiped (if it exists) and 1 is returned. If build directory
308 # had 2 or more levels, empty parent directories are also deleted.
309 # If build directory does not exist, nothing is done and 0 is returned.
312 if ($this->get_builddir()) {
313 my $buildpath = $this->get_buildpath();
314 if (-d $buildpath && ! $dh{NO_ACT}) {
315 doit("rm", "-rf", $buildpath);
316 # If build directory had 2 or more levels, delete empty
317 # parent directories until the source directory level.
318 my @spdir = File::Spec->splitdir($this->get_build_rel2sourcedir());
321 while (($peek=pop(@spdir)) && $peek ne '.' && $peek ne '..') {
322 last if ! rmdir($this->get_sourcepath(File::Spec->catdir(@spdir, $peek)));
330 # Instance method that is called before performing any step (see below).
331 # Action name is passed as an argument. Derived classes overriding this
332 # method should also call SUPER implementation of it.
333 sub pre_building_step {
337 # Warn if in source building was enforced but build directory was
338 # specified. See enforce_in_source_building().
339 if ($this->{warn_insource}) {
340 warning("warning: " . $this->NAME() .
341 " does not support building out of source tree. In source building enforced.");
342 delete $this->{warn_insource};
346 # Instance method that is called after performing any step (see below).
347 # Action name is passed as an argument. Derived classes overriding this
348 # method should also call SUPER implementation of it.
349 sub post_building_step {
354 # The instance methods below provide support for configuring,
355 # building, testing, install and cleaning source packages.
356 # In case of failure, the method may just error() out.
358 # These methods should be overriden by derived classes to
359 # implement buildsystem specific steps needed to build the
360 # source. Arbitary number of custom step arguments might be
361 # passed. Default implementations do nothing.
374 # destdir parameter specifies where to install files.