1 # Defines basic debhelper buildsystem class interface.
3 # Copyright: © 2008-2009 Modestas Vainius
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
9 package Debian::Debhelper::Dh_Buildsystem_Basic;
15 use Debian::Debhelper::Dh_Lib;
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
22 my $cls = ref($self) || $self;
23 if ($cls =~ m/^.+::([^:]+)$/) {
27 error("ınvalid buildsystem class name: $cls");
31 # Description of the build system to be shown to the users.
33 "basic debhelper build system class (please provide description)";
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");
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).
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.
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
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.
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
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};
85 $self->{builddir} = $self->DEFAULT_BUILD_DIRECTORY();
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.
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 {
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 {
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;
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();
130 # Get path to the specified build directory
133 return $self->{builddir};
136 # Construct absolute path to the file from the given path that is relative
137 # to the build directory.
139 my ($self, $path) = @_;
140 if ($self->get_builddir()) {
141 return File::Spec->catfile($self->get_builddir(), $path);
144 return File::Spec->catfile('.', $path);
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 {
157 $path = File::Spec->catfile(Cwd::getcwd(), $path);
160 $path = Cwd::getcwd();
162 if ($self->get_builddir()) {
163 return File::Spec->abs2rel($path, Cwd::abs_path($self->get_builddir()));
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");
175 verbose_print("mkdir '$dir'");
177 mkdir($dir, 0755) or error("error: unable to create '$dir': $!");
186 # XXX JEH I think this should verbose_print("cd $dir")
187 # then the verbose_prints in doit_in_builddir are unnecessary.
189 chdir $dir or error("error: unable to chdir to $dir");
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.
197 if ($self->get_builddir()) {
198 return $self->_mkdir($self->get_builddir());
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 {
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);
213 verbose_print("cd back to the source directory: $sourcedir");
214 $self->_cd($sourcedir);
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?
228 if ($self->get_builddir()) {
229 if (-d $self->get_builddir()) {
230 doit("rm", "-rf", $self->get_builddir());
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.
245 # Emit warnings pre action.
246 if (exists $self->{warnings}) {
247 for my $msg (@{$self->{warnings}}) {
248 warning("warning: " . $msg);
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.
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.
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.
281 # destdir parameter specifies where to install files.