]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Buildsystems.pm
remove discussion
[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     "autotools",
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                 verbose_print("Selected buildsystem (specified): ".$inst->NAME());
52                 return $inst;
53         }
54         else {
55                 # Try to determine build system automatically
56                 for $system (@BUILDSYSTEMS) {
57                         my $inst = create_buildsystem_instance($system, build_action=>$action);
58                         if ($inst->is_buildable()) {
59                                 verbose_print("Selected buildsystem (auto): ". $inst->NAME());
60                                 return $inst;
61                         }
62                 }
63         }
64         return;
65 }
66
67 sub buildsystems_init {
68         my %args=@_;
69
70         # TODO: Not documented in the manual pages yet.
71         # Initialize options from environment variables
72         if (exists $ENV{DH_AUTO_BUILDDIRECTORY}) {
73                 $opt_builddir = $ENV{DH_AUTO_BUILDDIRECTORY};
74         }
75         if (exists $ENV{DH_AUTO_BUILDSYSTEM}) {
76                 $opt_buildsys = $ENV{DH_AUTO_BUILDSYSTEM};
77         }
78
79         # Available command line options
80         my %options = (
81             "b:s" => \$opt_builddir,
82             "builddirectory:s" => \$opt_builddir,
83
84             "m=s" => \$opt_buildsys,
85             "buildsystem=s" => \$opt_buildsys,
86
87             "l" => \$opt_list,
88             "--list" => \$opt_list,
89         );
90         map { $args{options}{$_} = $options{$_} } keys(%options);
91         Debian::Debhelper::Dh_Lib::init(%args);
92 }
93
94 sub buildsystems_do {
95         my $action=shift;
96
97         if (!defined $action) {
98                 $action = basename($0);
99                 $action =~ s/^dh_auto_//;
100         }
101
102         if (grep(/^\Q$action\E$/, qw{configure build test install clean}) == 0) {
103                 error("unrecognized build action: " . $action);
104         }
105
106         if ($opt_list) {
107                 # List buildsystems (including auto and specified status)
108                 my $auto_found;
109                 my $specified_found;
110                 print "STATUS (* auto, + specified) NAME - DESCRIPTION", "\n";
111                 for my $system (@BUILDSYSTEMS) {
112                         my $inst = create_buildsystem_instance($system, build_action => undef);
113                         my $is_specified = defined $opt_buildsys && $opt_buildsys eq $inst->NAME();
114                         my $status;
115                         if ($is_specified) {
116                                 $status = "+";
117                                 $specified_found = 1;
118                         }
119                         elsif (!$auto_found && $inst->check_auto_buildable($action)) {
120                                 $status = "*";
121                                 $auto_found = 1;
122                         }
123                         else {
124                                 $status = " ";
125                         }
126                         printf("%s %s - %s.\n", $status, $inst->NAME(), $inst->DESCRIPTION());
127                 }
128                 # List a 3rd party buildsystem too.
129                 if (!$specified_found && defined $opt_buildsys) {
130                         my $inst = create_buildsystem_instance($opt_buildsys, build_action => undef);
131                         printf("+ %s - %s.\n", $inst->NAME(), $inst->DESCRIPTION());
132                 }
133                 exit 0;
134         }
135
136         my $buildsystem = load_buildsystem($action, $opt_buildsys);
137         if (defined $buildsystem) {
138                 $buildsystem->pre_action($action);
139                 $buildsystem->$action(@_, @{$dh{U_PARAMS}});
140                 $buildsystem->post_action($action);
141         }
142         return 0;
143 }
144
145 1;