]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Buildsystem.pm
remove discussion
[debhelper.git] / Debian / Debhelper / Dh_Buildsystem.pm
1 # Defines debhelper buildsystem class interface and implementation
2 # of common functionality.
3 #
4 # Copyright: © 2008-2009 Modestas Vainius
5 # License: GPL-2+
6
7 package Debian::Debhelper::Dh_Buildsystem;
8
9 use strict;
10 use warnings;
11 use Cwd;
12 use File::Spec;
13 use Debian::Debhelper::Dh_Lib;
14
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");
18
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
21 # doing.
22 sub NAME {
23         my $self=shift;
24         my $cls = ref($self) || $self;
25         if ($cls =~ m/^.+::([^:]+)$/) {
26                 return $1;
27         }
28         else {
29                 error("ınvalid buildsystem class name: $cls");
30         }
31 }
32
33 # Description of the build system to be shown to the users.
34 sub DESCRIPTION {
35         "basic debhelper build system class (please provide description)";
36 }
37
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;
42 }
43
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.
57 sub new {
58         my ($cls, %opts)=@_;
59
60         my $self = bless({ builddir => undef, is_buildable => 1 }, $cls);
61         if (exists $opts{builddir}) {
62                 if ($opts{builddir}) {
63                         $self->{builddir} = $opts{builddir};
64                 }
65                 else {
66                         $self->{builddir} = $self->DEFAULT_BUILD_DIRECTORY();
67                 }
68         }
69         if (exists $opts{build_action}) {
70                 if (defined $opts{build_action}) {
71                         $self->{is_buildable} = $self->check_auto_buildable($opts{build_action});
72                 }
73                 else {
74                         $self->{is_buildable} = 0;
75                 }
76         }
77         return $self;
78 }
79
80 # Test is_buildable flag of the object.
81 sub is_buildable {
82         my $self=shift;
83         return $self->{is_buildable};
84 }
85
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.
92 #
93 # This method is supposed to be called with source root directory being
94 # working directory. Use $self->get_buildpath($path) method to get full
95 # path to the files in the build directory.
96 sub check_auto_buildable {
97         my $self=shift;
98         my ($action) = @_;
99         return 0;
100 }
101
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 {
105         my $self=shift;
106         if ($self->{builddir}) {
107                 # Do not emit warning unless the object is buildable.
108                 if ($self->is_buildable()) {
109                         warning("warning: " . $self->NAME() .
110                             " does not support building outside-source. In-source build enforced.");
111                 }
112                 $self->{builddir} = undef;
113         }
114 }
115
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 ($self, $builddir) = @_;
120         if (!defined $self->{builddir}) {
121                 $self->{builddir} = ($builddir && $builddir ne ".") ? $builddir : $self->DEFAULT_BUILD_DIRECTORY();
122         }
123 }
124
125 # Get path to the specified build directory
126 sub get_builddir {
127         my $self=shift;
128         return $self->{builddir};
129 }
130
131 # Construct absolute path to the file from the given path that is relative
132 # to the build directory.
133 sub get_buildpath {
134         my ($self, $path) = @_;
135         if ($self->get_builddir()) {
136                 return File::Spec->catfile($self->get_builddir(), $path);
137         }
138         else {
139                 return File::Spec->catfile('.', $path);
140         }
141 }
142
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 {
148         my $self=shift;
149         my $path=shift;
150
151         if (defined $path) {
152                 $path = File::Spec->catfile(Cwd::getcwd(), $path);
153         }
154         else {
155                 $path = Cwd::getcwd();
156         }
157         if ($self->get_builddir()) {
158                 return File::Spec->abs2rel($path, Cwd::abs_path($self->get_builddir()));
159         }
160         return $path;
161 }
162
163 sub _mkdir {
164         my ($cls, $dir)=@_;
165         # XXX JEH is there any reason not to just doit("mkdir") ?
166         # XXX MDX Replaced below part. This call is there to be
167         # more verbose about errors (if accidently $dir in
168         # non-dir form and to test for ! -d $dir.
169         if (-e $dir && ! -d $dir) {
170                 error("error: unable to create '$dir': object already exists and is not a directory");
171         }
172         elsif (! -d $dir) {
173                 doit("mkdir", $dir);
174                 return 1;
175         }
176         return 0;
177 }
178
179 sub _cd {
180         my ($cls, $dir)=@_;
181         if (! $dh{NO_ACT}) {
182                 verbose_print("cd $dir");
183                 chdir $dir or error("error: unable to chdir to $dir");
184         }
185 }
186
187 # Creates a build directory. Returns 1 if the directory was created
188 # or 0 if it already exists or there is no need to create it.
189 sub mkdir_builddir {
190         my $self=shift;
191         if ($self->get_builddir()) {
192                 return $self->_mkdir($self->get_builddir());
193         }
194         return 0;
195 }
196
197 # Changes working directory the build directory (if needed), calls doit(@_)
198 # and changes working directory back to the source directory.
199 sub doit_in_builddir {
200         my $self=shift;
201         if ($self->get_builddir()) {
202                 my $builddir = $self->get_builddir();
203                 my $sourcedir = $self->get_rel2builddir_path();
204                 $self->_cd($builddir);
205                 doit(@_);
206                 $self->_cd($sourcedir);
207         }
208         else {
209                 doit(@_);
210         }
211         return 1;
212 }
213
214 # In case of outside-source tree building, whole build directory
215 # gets wiped (if it exists) and 1 is returned. Otherwise, nothing
216 # is done and 0 is returned.
217 sub clean_builddir {
218         my $self=shift;
219         if ($self->get_builddir()) {
220                 if (-d $self->get_builddir()) {
221                         doit("rm", "-rf", $self->get_builddir());
222                 }
223                 return 1;
224         }
225         return 0;
226 }
227
228
229 # Instance method that is called before performing any action (see below).
230 # Action name is passed as an argument. Derived classes overriding this
231 # method should also call SUPER implementation of it.
232 sub pre_action {
233         my $self=shift;
234         my ($action)=@_;
235 }
236
237 # Instance method that is called after performing any action (see below).
238 # Action name is passed as an argument. Derived classes overriding this
239 # method should also call SUPER implementation of it.
240 sub post_action {
241         my $self=shift;
242         my ($action)=@_;
243 }
244
245 # The instance methods below provide support for configuring,
246 # building, testing, install and cleaning source packages.
247 # In case of failure, the method may just error() out.
248 #
249 # These methods should be overriden by derived classes to
250 # implement buildsystem specific actions needed to build the
251 # source. Arbitary number of custom action arguments might be
252 # passed. Default implementations do nothing.
253 sub configure {
254         my $self=shift;
255 }
256
257 sub build {
258         my $self=shift;
259 }
260
261 sub test {
262         my $self=shift;
263 }
264
265 # destdir parameter specifies where to install files.
266 sub install {
267         my $self=shift;
268         my $destdir=shift;
269 }
270
271 sub clean {
272         my $self=shift;
273 }
274
275 1;