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 # - builddir - specifies build directory to use. If not specified,
46 # in-source build will be performed. If undef or empty,
47 # DEFAULT_BUILD_DIRECTORY will be used.
48 # - build_action - set this parameter to the name of the build action
49 # if you want the object to determine its is_buidable
50 # status automatically (with check_auto_buildable()).
51 # Do not pass this parameter if is_buildable flag should
52 # be forced to true or set this parameter to undef if
53 # is_buildable flag should be false.
54 # Derived class can override the constructor to initialize common object
55 # parameters and execute commands to configure build environment if
56 # is_buildable flag is set on the object.
58 my ($class, %opts)=@_;
60 my $this = bless({ builddir => undef, is_buildable => 1 }, $class);
61 if (exists $opts{builddir}) {
62 if ($opts{builddir}) {
63 $this->{builddir} = $opts{builddir};
66 $this->{builddir} = $this->DEFAULT_BUILD_DIRECTORY();
69 if (exists $opts{build_action}) {
70 if (defined $opts{build_action}) {
71 $this->{is_buildable} = $this->check_auto_buildable($opts{build_action});
74 $this->{is_buildable} = 0;
80 # Test is_buildable flag of the object.
83 return $this->{is_buildable};
86 # This instance method is called to check if the build system is capable
87 # to auto build a source package. Additional argument $action describes
88 # which operation the caller is going to perform (either configure,
89 # build, test, install or clean). You must override this method for the
90 # build system module to be ever picked up automatically. This method is
91 # used in conjuction with @Dh_Buildsystems::BUILDSYSTEMS.
93 # This method is supposed to be called with source root directory being
94 # working directory. Use $this->get_buildpath($path) method to get full
95 # path to the files in the build directory.
96 sub check_auto_buildable {
102 # Derived class can call this method in its constructor
103 # to enforce in-source building even if the user requested otherwise.
104 sub enforce_in_source_building {
106 if ($this->{builddir}) {
107 # Do not emit warning unless the object is buildable.
108 if ($this->is_buildable()) {
109 warning("warning: " . $this->NAME() .
110 " does not support building outside-source. In-source build enforced.");
112 $this->{builddir} = undef;
116 # Derived class can call this method in its constructor to enforce
117 # outside-source building even if the user didn't request it.
118 sub enforce_outside_source_building {
119 my ($this, $builddir) = @_;
120 if (!defined $this->{builddir}) {
121 $this->{builddir} = ($builddir && $builddir ne ".") ? $builddir : $this->DEFAULT_BUILD_DIRECTORY();
125 # Get path to the specified build directory
128 return $this->{builddir};
131 # Construct absolute path to the file from the given path that is relative
132 # to the build directory.
134 my ($this, $path) = @_;
135 if ($this->get_builddir()) {
136 return File::Spec->catfile($this->get_builddir(), $path);
139 return File::Spec->catfile('.', $path);
143 # When given a relative path in the source tree, converts it
144 # to the path that is relative to the build directory.
145 # If $path is not given, returns relative path to the root of the
146 # source tree from the build directory.
147 sub get_rel2builddir_path {
152 $path = File::Spec->catfile(Cwd::getcwd(), $path);
155 $path = Cwd::getcwd();
157 if ($this->get_builddir()) {
158 return File::Spec->abs2rel($path, Cwd::abs_path($this->get_builddir()));
163 # Creates a build directory.
166 if ($this->get_builddir()) {
167 doit("mkdir", "-p", $this->get_builddir());
174 verbose_print("cd $dir");
175 chdir $dir or error("error: unable to chdir to $dir");
179 # Changes working directory the build directory (if needed), calls doit(@_)
180 # and changes working directory back to the source directory.
181 sub doit_in_builddir {
183 if ($this->get_builddir()) {
184 my $builddir = $this->get_builddir();
185 my $sourcedir = $this->get_rel2builddir_path();
186 $this->_cd($builddir);
188 $this->_cd($sourcedir);
196 # In case of outside-source tree building, whole build directory
197 # gets wiped (if it exists) and 1 is returned. Otherwise, nothing
198 # is done and 0 is returned.
201 if ($this->get_builddir()) {
202 if (-d $this->get_builddir()) {
203 doit("rm", "-rf", $this->get_builddir());
211 # Instance method that is called before performing any action (see below).
212 # Action name is passed as an argument. Derived classes overriding this
213 # method should also call SUPER implementation of it.
219 # Instance method that is called after performing any action (see below).
220 # Action name is passed as an argument. Derived classes overriding this
221 # method should also call SUPER implementation of it.
227 # The instance methods below provide support for configuring,
228 # building, testing, install and cleaning source packages.
229 # In case of failure, the method may just error() out.
231 # These methods should be overriden by derived classes to
232 # implement buildsystem specific actions needed to build the
233 # source. Arbitary number of custom action arguments might be
234 # passed. Default implementations do nothing.
247 # destdir parameter specifies where to install files.