]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Buildsystems.pm
afb03dd7bf0139b9ec1d3f9d3cb23cac416a7bb5
[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 # Historical order must be kept for backwards compatibility. New
17 # buildsystems MUST be added to the END of the list.
18 our @BUILDSYSTEMS = (
19     "autoconf",
20     "perl_makemaker",
21     "makefile",
22     "python_distutils",
23     "perl_build",
24     "cmake",
25 );
26
27 my $opt_buildsys;
28 my $opt_builddir;
29 my $opt_list;
30
31 sub create_buildsystem_instance {
32         my $system=shift;
33         my %bsopts=@_;
34         my $module = "Debian::Debhelper::Buildsystem::$system";
35
36         eval "use $module";
37         if ($@) {
38                 error("unable to load buildsystem class '$system': $@");
39         }
40
41         if (!exists $bsopts{builddir} && defined $opt_builddir) {
42                 $bsopts{builddir} = ($opt_builddir eq "") ? undef : $opt_builddir;
43         }
44         return $module->new(%bsopts);
45 }
46
47 sub load_buildsystem {
48         my ($action, $system)=@_;
49         if (defined $system) {
50                 my $inst = create_buildsystem_instance($system);
51                 return $inst;
52         }
53         else {
54                 # Try to determine build system automatically
55                 for $system (@BUILDSYSTEMS) {
56                         my $inst = create_buildsystem_instance($system, build_action=>$action);
57                         if ($inst->is_buildable()) {
58                                 return $inst;
59                         }
60                 }
61         }
62         return;
63 }
64
65 sub buildsystems_init {
66         my %args=@_;
67
68         # TODO: Not documented in the manual pages yet.
69         # Initialize options from environment variables
70         if (exists $ENV{DH_AUTO_BUILDDIRECTORY}) {
71                 $opt_builddir = $ENV{DH_AUTO_BUILDDIRECTORY};
72         }
73         if (exists $ENV{DH_AUTO_BUILDSYSTEM}) {
74                 $opt_buildsys = $ENV{DH_AUTO_BUILDSYSTEM};
75         }
76
77         # Available command line options
78         my %options = (
79             "b:s" => \$opt_builddir,
80             "builddirectory:s" => \$opt_builddir,
81
82             "m=s" => \$opt_buildsys,
83             "buildsystem=s" => \$opt_buildsys,
84
85             "l" => \$opt_list,
86             "--list" => \$opt_list,
87         );
88         map { $args{options}{$_} = $options{$_} } keys(%options);
89         Debian::Debhelper::Dh_Lib::init(%args);
90 }
91
92 sub buildsystems_list {
93         my $action=shift;
94
95         # List buildsystems (including auto and specified status)
96         my ($auto, $specified);
97         for my $system (@BUILDSYSTEMS) {
98                 my $inst = create_buildsystem_instance($system, build_action => undef);
99                 my $is_specified = defined $opt_buildsys && $opt_buildsys eq $inst->NAME();
100                 if (! defined $specified && defined $opt_buildsys && $opt_buildsys eq $inst->NAME()) {
101                         $specified = $inst->NAME();
102                 }
103                 elsif (! defined $auto && $inst->check_auto_buildable($action)) {
104                         $auto = $inst->NAME();
105                 }
106                 printf("%s - %s\n", $inst->NAME(), $inst->DESCRIPTION());
107         }
108         # List a specified 3rd party buildsystem too.
109         if (! defined $specified && defined $opt_buildsys) {
110                 my $inst = create_buildsystem_instance($opt_buildsys, build_action => undef);
111                 printf("%s - %s.\n", $inst->NAME(), $inst->DESCRIPTION());
112                 $specified = $inst->NAME();
113         }
114         print "\n";
115         print "Auto-selected: $auto\n" if defined $auto;
116         print "Specified: $specified\n" if defined $specified;
117         print "No system auto-selected or specified\n"
118                 if ! defined $auto && ! defined $specified;
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 build action: " . $action);
131         }
132
133         if ($opt_list) {
134                 buildsystems_list($action);
135                 exit 0;
136         }
137
138         my $buildsystem = load_buildsystem($action, $opt_buildsys);
139         if (defined $buildsystem) {
140                 $buildsystem->pre_action($action);
141                 $buildsystem->$action(@_, @{$dh{U_PARAMS}});
142                 $buildsystem->post_action($action);
143         }
144         return 0;
145 }
146
147 1;