]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Buildsystems.pm
ac09dd289d50f6c7f79fcecc782a014197b5b3d6
[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 use File::Spec;
13
14 use base 'Exporter';
15 our @EXPORT=qw(&buildsystems_init &buildsystems_do &load_buildsystem &load_all_buildsystems);
16
17 # Historical order must be kept for backwards compatibility. New
18 # buildsystems MUST be added to the END of the list.
19 our @BUILDSYSTEMS = (
20     "autoconf",
21     "perl_makemaker",
22     "makefile",
23     "python_distutils",
24     "perl_build",
25     "cmake",
26 );
27
28 my $opt_buildsys;
29 my $opt_sourcedir;
30 my $opt_builddir;
31 my $opt_list;
32
33 sub create_buildsystem_instance {
34         my $system=shift;
35         my %bsopts=@_;
36         my $module = "Debian::Debhelper::Buildsystem::$system";
37
38         eval "use $module";
39         if ($@) {
40                 error("unable to load buildsystem class '$system': $@");
41         }
42
43         if (!exists $bsopts{builddir} && defined $opt_builddir) {
44                 $bsopts{builddir} = ($opt_builddir eq "") ? undef : $opt_builddir;
45         }
46         if (!exists $bsopts{sourcedir} && defined $opt_sourcedir) {
47                 $bsopts{sourcedir} = ($opt_sourcedir eq "") ? undef : $opt_sourcedir;
48         }
49         return $module->new(%bsopts);
50 }
51
52 # Similar to create_buildsystem_instance(), but it attempts to autoselect
53 # a buildsystem if none was specified. In case autoselection fails, undef
54 # is returned.
55 sub load_buildsystem {
56         my $system=shift;
57         my $step=shift;
58         if (defined $system) {
59                 my $inst = create_buildsystem_instance($system, @_);
60                 return $inst;
61         }
62         else {
63                 # Try to determine build system automatically
64                 for $system (@BUILDSYSTEMS) {
65                         my $inst = create_buildsystem_instance($system, @_);
66                         if ($inst->check_auto_buildable($step)) {
67                                 return $inst;
68                         }
69                 }
70         }
71         return;
72 }
73
74 sub load_all_buildsystems {
75         my $incs=shift || \@INC;
76         my (%buildsystems, @buildsystems);
77
78         for my $inc (@$incs) {
79                 my $path = File::Spec->catdir($inc, "Debian/Debhelper/Buildsystem");
80                 if (-d $path) {
81                         for my $module_path (glob "$path/*.pm") {
82                                 my $name = basename($module_path);
83                                 $name =~ s/\.pm$//;
84                                 next if exists $buildsystems{$name};
85                                 $buildsystems{$name} = create_buildsystem_instance($name, @_);
86                         }
87                 }
88         }
89
90         # Push debhelper built-in buildsystems first
91         for my $name (@BUILDSYSTEMS) {
92                 error("Debhelper built-in buildsystem '$name' could not be found/loaded")
93                     if not exists $buildsystems{$name};
94                 push @buildsystems, $buildsystems{$name};
95                 delete $buildsystems{$name};
96         }
97
98         # The rest are 3rd party buildsystems
99         for my $name (keys %buildsystems) {
100                 my $inst = $buildsystems{$name};
101                 $inst->{thirdparty} = 1;
102                 push @buildsystems, $inst;
103         }
104
105         return @buildsystems;
106 }
107
108 sub buildsystems_init {
109         my %args=@_;
110
111         # TODO: Not documented in the manual pages yet.
112         # Initialize options from environment variables
113         if (exists $ENV{DH_AUTO_BUILDDIRECTORY}) {
114                 $opt_builddir = $ENV{DH_AUTO_BUILDDIRECTORY};
115         }
116         if (exists $ENV{DH_AUTO_BUILDSYSTEM}) {
117                 $opt_buildsys = $ENV{DH_AUTO_BUILDSYSTEM};
118         }
119
120         # Available command line options
121         my %options = (
122             "d=s" => \$opt_sourcedir,
123             "sourcedirectory=s" => \$opt_sourcedir,
124         
125             "b:s" => \$opt_builddir,
126             "builddirectory:s" => \$opt_builddir,
127
128             "m=s" => \$opt_buildsys,
129             "buildsystem=s" => \$opt_buildsys,
130
131             "l" => \$opt_list,
132             "--list" => \$opt_list,
133         );
134         $args{options}{$_} = $options{$_} foreach keys(%options);
135         Debian::Debhelper::Dh_Lib::init(%args);
136 }
137
138 sub buildsystems_list {
139         my $step=shift;
140
141         # List buildsystems (including auto and specified status)
142         my ($auto, $specified);
143         my @buildsystems = load_all_buildsystems();
144         for my $inst (@buildsystems) {
145                 my $is_specified = defined $opt_buildsys && $opt_buildsys eq $inst->NAME();
146                 if (! defined $specified && defined $opt_buildsys && $opt_buildsys eq $inst->NAME()) {
147                         $specified = $inst->NAME();
148                 }
149                 elsif (! defined $auto && ! $inst->{thirdparty} && $inst->check_auto_buildable($step)) {
150                         $auto = $inst->NAME();
151                 }
152                 printf("%s - %s", $inst->NAME(), $inst->DESCRIPTION());
153                 print " [3rd party]" if $inst->{thirdparty};
154                 print "\n";
155         }
156         print "\n";
157         print "Auto-selected: $auto\n" if defined $auto;
158         print "Specified: $specified\n" if defined $specified;
159         print "No system auto-selected or specified\n"
160                 if ! defined $auto && ! defined $specified;
161 }
162
163 sub buildsystems_do {
164         my $step=shift;
165
166         if (!defined $step) {
167                 $step = basename($0);
168                 $step =~ s/^dh_auto_//;
169         }
170
171         if (grep(/^\Q$step\E$/, qw{configure build test install clean}) == 0) {
172                 error("unrecognized build step: " . $step);
173         }
174
175         if ($opt_list) {
176                 buildsystems_list($step);
177                 exit 0;
178         }
179
180         my $buildsystem = load_buildsystem($opt_buildsys, $step);
181         if (defined $buildsystem) {
182                 $buildsystem->pre_building_step($step);
183                 $buildsystem->$step(@_, @{$dh{U_PARAMS}});
184                 $buildsystem->post_building_step($step);
185         }
186         return 0;
187 }
188
189 1;