]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Buildsystems.pm
more comments
[debhelper.git] / Debian / Debhelper / Dh_Buildsystems.pm
1 # A module for loading and managing debhelper buildsystem plugins.
2 # This module is intended to be used by all dh_auto_* helper commands.
3 #
4 # Copyright: © 2009 Modestas Vainius
5 # License: GPL-2+
6
7 package Debian::Debhelper::Dh_Buildsystems;
8
9 use strict;
10 use warnings;
11 use Debian::Debhelper::Dh_Lib;
12
13 use base 'Exporter';
14 our @EXPORT=qw(&buildsystems_init &buildsystems_do &load_buildsystem);
15
16 # XXX JEH as noted, this has to match historical order for back-compat
17 # XXX MDX Current dh_auto_* look like:
18 # configure: autotools, perl_makemaker, perl_build
19 # build:     makefile, python_distutils, perl_build
20 # test:      makefile, perl_build
21 # install:   makefile (with perl_makermaker) hack, python_distutils, perl_build
22 # clean:     makefile, python_distutils, perl_build
23 # So historical @BUILDSYSTEMS order (as per autodetection, see
24 # is_auto_buildable() of the respective classes):
25 #   autotools (+configure; the rest - next class)
26 #   python_distutils (+build +install +clean; the rest - next class)
27 #   perl_makemaker (+configure +install (special hack); the rest - next class)
28 #   makefile (+build +test +install +clean; configure - next class)
29 #   perl_build (handles everything)
30 # XXX JEH I think that makes sense..
31
32 # Historical order must be kept for backwards compatibility. New
33 # buildsystems MUST be added to the END of the list.
34 our @BUILDSYSTEMS = (
35     "autotools",
36     "python_distutils",
37     "perl_makemaker",
38     "makefile",
39     "perl_build",
40     "cmake",
41 );
42
43 sub create_buildsystem_instance {
44         my $system=shift;
45         my %bsopts=@_;
46         my $module = "Debian::Debhelper::Buildsystem::$system";
47
48         eval "use $module";
49         if ($@) {
50                 error("unable to load buildsystem class '$system': $@");
51         }
52
53         if (!exists $bsopts{builddir} && exists $dh{BUILDDIR}) {
54                 $bsopts{builddir} = $dh{BUILDDIR};
55         }
56         return $module->new(%bsopts);
57 }
58
59 sub load_buildsystem {
60         my ($action, $system)=@_;
61         if (defined $system) {
62                 my $inst = create_buildsystem_instance($system);
63                 verbose_print("Selected buildsystem (specified): ".$inst->NAME());
64                 return $inst;
65         }
66         else {
67                 # Try to determine build system automatically
68                 for $system (@BUILDSYSTEMS) {
69                         my $inst = create_buildsystem_instance($system, is_auto=>1);
70                         if ($inst->is_auto_buildable($action)) {
71                                 verbose_print("Selected buildsystem (auto): ". $inst->NAME());
72                                 return $inst;
73                         }
74                 }
75         }
76         return;
77 }
78
79 sub list_buildsystems {
80         for my $system (@BUILDSYSTEMS) {
81                 my $inst = create_buildsystem_instance($system);
82                 printf("%s - %s.\n", $inst->NAME(), $inst->DESCRIPTION());
83         }
84 }
85
86 sub buildsystems_init {
87         my %args=@_;
88
89         # TODO: Not documented in the manual pages yet.
90         # Initialize options from environment variables
91         # XXX JEH I think these should be my variables, they are only used
92         # inside this one file so putting them in the global %dh hash seems
93         # unnecessary.
94         if (exists $ENV{DH_AUTO_BUILDDIRECTORY}) {
95                 $dh{BUILDDIR} = $ENV{DH_AUTO_BUILDDIRECTORY};
96         }
97         if (exists $ENV{DH_AUTO_BUILDSYSTEM}) {
98                 $dh{BUILDSYS} = $ENV{DH_AUTO_BUILDSYSTEM};
99         }
100
101         # Available command line options
102         my $list_bs = sub { list_buildsystems(); exit 0 };
103         my $set_builddir = sub { $dh{BUILDDIR} = $_[1] };
104         my %options = (
105             "b:s" => $set_builddir,
106             "build-directory:s" => $set_builddir,
107             "builddirectory:s" => $set_builddir,
108
109             "m=s" => \$dh{BUILDSYS},
110             # XXX JEH Let's only keep one spelling of this.
111             "build-system=s" => \$dh{BUILDSYS},
112             "buildsystem=s" => \$dh{BUILDSYS},
113
114             "l" => $list_bs,
115             "--list" => $list_bs,
116         );
117         map { $args{options}{$_} = $options{$_} } keys(%options);
118         Debian::Debhelper::Dh_Lib::init(%args);
119 }
120
121 sub buildsystems_do {
122         my $action=shift;
123
124         if (!defined $action) {
125                 $action = basename($0);
126                 $action =~ s/^dh_auto_//;
127         }
128
129         if (grep(/^\Q$action\E$/, qw{configure build test install clean}) == 0) {
130                 error("unrecognized auto action: ".basename($0));
131         }
132
133         my $buildsystem = load_buildsystem($action, $dh{BUILDSYS});
134         if (defined $buildsystem) {
135                 $buildsystem->pre_action($action);
136                 $buildsystem->$action(@_, @{$dh{U_PARAMS}});
137                 $buildsystem->post_action($action);
138         }
139         return 0;
140 }
141
142 1;