]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Buildsystem_Basic.pm
more comments
[debhelper.git] / Debian / Debhelper / Dh_Buildsystem_Basic.pm
1 # Defines basic debhelper buildsystem class interface.
2 #
3 # Copyright: © 2008-2009 Modestas Vainius
4 # License: GPL-2+
5
6 # XXX JEH maybe rename this class to Debian::Debhelper::Dh_Buildsystem?
7 # XXX JEH also it seems the functions in Dh_Buildsystems could be merged
8 #     into this same file.
9 package Debian::Debhelper::Dh_Buildsystem_Basic;
10
11 use strict;
12 use warnings;
13 use Cwd;
14 use File::Spec;
15 use Debian::Debhelper::Dh_Lib;
16
17 # Build system name. Defaults to the last component of the class
18 # name. Do not override this method unless you know what you are
19 # doing.
20 sub NAME {
21         my $self=shift;
22         my $cls = ref($self) || $self;
23         if ($cls =~ m/^.+::([^:]+)$/) {
24                 return $1;
25         }
26         else {
27                 error("ınvalid buildsystem class name: $cls");
28         }
29 }
30
31 # Description of the build system to be shown to the users.
32 sub DESCRIPTION {
33         "basic debhelper build system class (please provide description)";
34 }
35
36 # Default build directory. Can be overriden in the derived
37 # class if really needed.
38 sub DEFAULT_BUILD_DIRECTORY {
39         "obj-" . dpkg_architecture_value("DEB_BUILD_GNU_TYPE");
40 }
41
42 # Constructs a new build system object. Named parameters:
43 # - builddir - specifies build directory to use. If not specified,
44 #              in-source build will be performed. If undef or empty,
45 #              default DEFAULT_BUILD_DIRECTORY will be used.
46 # - is_auto -  might be used by the derived classes to determine if
47 #              the build system has been picked up automatically.
48 # Derived class can override the constructor to initialize common parameters.
49 # Constructor SHOULD NOT be used to initialize build environment because
50 # constructed class may not be eventually used to build the package (if e.g.
51 # is_auto_buildable() returns 0).
52 #
53 # XXX JEH the above comment begs the question: Why not test
54 # is_auto_buildable in the constructor, and only have the constructor
55 # succeed if it can handle the source? That would also eliminate the 
56 # delayed warning mess in enforce_in_source_building.
57 #
58 # (In turn that could be used to remove the pre_action, since that's the
59 # only use of it -- the post_action is currently unused too. It could be
60 # argued that these should be kept in case later buildsystems need them
61 # though.)
62 #
63 # AFAICS, there is only one reason you need an instance of the object
64 # if it can't build -- to list build systems. But that only needs
65 # DESCRIPTION and NAME, which could be considered to be class methods,
66 # rather than object methods -- no need to construct an instance of the
67 # class before calling those.
68 #
69 # I see that if --buildsystem is manually specified to override,
70 # the is_auto_buildable test is completely skipped. So if this change were
71 # made, you'd not be able to skip the test, and some --buildsystem choices
72 # might cause an error. OTOH, those seem to be cases where it would later
73 # fail anyway. The real use cases for --buildsystem, such as forcing use of
74 # cmake when there are both a CMakeLists.txt and a Makefile, would still
75 # work.
76 sub new {
77         my ($cls, %opts)=@_;
78
79         my $self = bless({ builddir => undef, is_auto => $opts{is_auto} }, $cls);
80         if (exists $opts{builddir}) {
81                 if ($opts{builddir}) {
82                         $self->{builddir} = $opts{builddir};
83                 }
84                 else {
85                         $self->{builddir} = $self->DEFAULT_BUILD_DIRECTORY();
86                 }
87         }
88         return $self;
89 }
90
91 # This instance method is called to check if the build system is capable
92 # to auto build a source package. Additional argument $action describes
93 # which operation the caller is going to perform (either configure,
94 # build, test, install or clean). You must override this method for the
95 # build system module to be ever picked up automatically. This method is
96 # used in conjuction with @Dh_Buildsystems::BUILDSYSTEMS.
97 #
98 # This method is supposed to be called with source root directory being
99 # working directory. Use $self->get_buildpath($path) method to get full
100 # path to the files in the build directory.
101 sub is_auto_buildable {
102         my $self=shift;
103         my ($action) = @_;
104         return 0;
105 }
106
107 # Derived class can call this method in its constructor
108 # to enforce in-source building even if the user requested otherwise.
109 sub enforce_in_source_building {
110         my $self=shift;
111         if ($self->{builddir}) {
112                 # Since this method is called in the constructor, emitting
113                 # warnings immediatelly may cause too much noise when
114                 # scanning for auto buildsystems or listing them.
115                 push @{$self->{warnings}},
116                     $self->NAME()." buildsystem does not support building outside-source. In-source build enforced.";
117                 $self->{builddir} = undef;
118         }
119 }
120
121 # Derived class can call this method in its constructor to enforce
122 # outside-source building even if the user didn't request it.
123 sub enforce_outside_source_building {
124         my ($self, $builddir) = @_;
125         if (!defined $self->{builddir}) {
126                 $self->{builddir} = ($builddir && $builddir ne ".") ? $builddir : $self->DEFAULT_BUILD_DIRECTORY();
127         }
128 }
129
130 # Get path to the specified build directory
131 sub get_builddir {
132         my $self=shift;
133         return $self->{builddir};
134 }
135
136 # Construct absolute path to the file from the given path that is relative
137 # to the build directory.
138 sub get_buildpath {
139         my ($self, $path) = @_;
140         if ($self->get_builddir()) {
141                 return File::Spec->catfile($self->get_builddir(), $path);
142         }
143         else {
144                 return File::Spec->catfile('.', $path);
145         }
146 }
147
148 # When given a relative path in the source tree, converts it
149 # to the path that is relative to the build directory.
150 # If $path is not given, returns relative path to the root of the
151 # source tree from the build directory.
152 sub get_rel2builddir_path {
153         my $self=shift;
154         my $path=shift;
155
156         if (defined $path) {
157                 $path = File::Spec->catfile(Cwd::getcwd(), $path);
158         }
159         else {
160                 $path = Cwd::getcwd();
161         }
162         if ($self->get_builddir()) {
163                 return File::Spec->abs2rel($path, Cwd::abs_path($self->get_builddir()));
164         }
165         return $path;
166 }
167
168 sub _mkdir {
169         my ($cls, $dir)=@_;
170         # XXX JEH is there any reason not to just doit("mkdir") ?
171         if (-e $dir && ! -d $dir) {
172                 error("error: unable to create '$dir': object already exists and is not a directory");
173         }
174         elsif (! -d $dir) {
175                 verbose_print("mkdir '$dir'");
176                 if (! $dh{NO_ACT}) {
177                         mkdir($dir, 0755) or error("error: unable to create '$dir': $!");
178                 }
179                 return 1;
180         }
181         return 0;
182 }
183
184 sub _cd {
185         my ($cls, $dir)=@_;
186         # XXX JEH I think this should verbose_print("cd $dir")
187         # then the verbose_prints in doit_in_builddir are unnecessary.
188         if (! $dh{NO_ACT}) {
189                 chdir $dir or error("error: unable to chdir to $dir");
190         }
191 }
192
193 # Creates a build directory. Returns 1 if the directory was created
194 # or 0 if it already exists or there is no need to create it.
195 sub mkdir_builddir {
196         my $self=shift;a
197         if ($self->get_builddir()) {
198                 return $self->_mkdir($self->get_builddir());
199         }
200         return 0;
201 }
202
203 # Changes working directory the build directory (if needed), calls doit(@_)
204 # and changes working directory back to the source directory.
205 sub doit_in_builddir {
206         my $self=shift;
207         if ($self->get_builddir()) {
208                 my $builddir = $self->get_builddir();
209                 my $sourcedir = $self->get_rel2builddir_path();
210                 verbose_print("cd to the build directory: $builddir");
211                 $self->_cd($builddir);
212                 doit(@_);
213                 verbose_print("cd back to the source directory: $sourcedir");
214                 $self->_cd($sourcedir);
215         }
216         else {
217                 doit(@_);
218         }
219         return 1;
220 }
221
222 # In case of outside-source tree building, whole build directory
223 # gets wiped (if it exists) and 1 is returned. Otherwise, nothing
224 # is done and 0 is returned.
225 # XXX JEH only makefile.pm uses this, move it there?
226 sub clean_builddir {
227         my $self=shift;
228         if ($self->get_builddir()) {
229                 if (-d $self->get_builddir()) {
230                         doit("rm", "-rf", $self->get_builddir());
231                 }
232                 return 1;
233         }
234         return 0;
235 }
236
237
238 # Instance method that is called before performing any action (see below).
239 # Action name is passed as an argument. Derived classes overriding this
240 # method should also call SUPER implementation of it.
241 sub pre_action {
242         my $self=shift;
243         my $action=shift;
244
245         # Emit warnings pre action.
246         if (exists $self->{warnings}) {
247                 for my $msg (@{$self->{warnings}}) {
248                         warning("warning: " . $msg);
249                 }
250         }
251 }
252
253 # Instance method that is called after performing any action (see below).
254 # Action name is passed as an argument. Derived classes overriding this
255 # method should also call SUPER implementation of it.
256 sub post_action {
257         my $self=shift;
258         my $action=shift;
259 }
260
261 # The instance methods below provide support for configuring,
262 # building, testing, install and cleaning source packages.
263 # In case of failure, the method may just error() out.
264 #
265 # These methods should be overriden by derived classes to
266 # implement buildsystem specific actions needed to build the
267 # source. Arbitary number of custom action arguments might be
268 # passed. Default implementations do nothing.
269 sub configure {
270         my $self=shift;
271 }
272
273 sub build {
274         my $self=shift;
275 }
276
277 sub test {
278         my $self=shift;
279 }
280
281 # destdir parameter specifies where to install files.
282 sub install {
283         my $self=shift;
284         my $destdir=shift;
285 }
286
287 sub clean {
288         my $self=shift;
289 }
290
291 1;