]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Buildsystems.pm
Add support for multiarch.
[debhelper.git] / Debian / Debhelper / Dh_Buildsystems.pm
1 # A module for loading and managing debhelper build system classes.
2 # This module is intended to be used by all dh_auto_* programs.
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 use constant BUILD_STEPS => qw(configure build test install clean);
18
19 # Historical order must be kept for backwards compatibility. New
20 # build systems MUST be added to the END of the list.
21 our @BUILDSYSTEMS = (
22         "autoconf",
23         (! compat(7) ? "perl_build" : ()),
24         "perl_makemaker",
25         "makefile",
26         "python_distutils",
27         (compat(7) ? "perl_build" : ()),
28         "cmake",
29         "ant",
30         "qmake",
31 );
32
33 my $opt_buildsys;
34 my $opt_sourcedir;
35 my $opt_builddir;
36 my $opt_list;
37 my $opt_parallel;
38
39 sub create_buildsystem_instance {
40         my $system=shift;
41         my %bsopts=@_;
42         my $module = "Debian::Debhelper::Buildsystem::$system";
43
44         eval "use $module";
45         if ($@) {
46                 error("unable to load build system class '$system': $@");
47         }
48
49         if (!exists $bsopts{builddir} && defined $opt_builddir) {
50                 $bsopts{builddir} = ($opt_builddir eq "") ? undef : $opt_builddir;
51         }
52         if (!exists $bsopts{sourcedir} && defined $opt_sourcedir) {
53                 $bsopts{sourcedir} = ($opt_sourcedir eq "") ? undef : $opt_sourcedir;
54         }
55         if (!exists $bsopts{parallel}) {
56                 $bsopts{parallel} = $opt_parallel;
57         }
58         return $module->new(%bsopts);
59 }
60
61 # Autoselect a build system from the list of instances
62 sub autoselect_buildsystem {
63         my $step=shift;
64         my $selected;
65         my $selected_level = 0;
66
67         foreach my $inst (@_) {
68                 # Only derived (i.e. more specific) build system can be
69                 # considered beyond the currently selected one.
70                 next if defined $selected && !$inst->isa(ref $selected);
71
72                 # If the build system says it is auto-buildable at the current
73                 # step and it can provide more specific information about its
74                 # status than its parent (if any), auto-select it.
75                 my $level = $inst->check_auto_buildable($step);
76                 if ($level > $selected_level) {
77                         $selected = $inst;
78                         $selected_level = $level;
79                 }
80         }
81         return $selected;
82 }
83
84 # Similar to create_build system_instance(), but it attempts to autoselect
85 # a build system if none was specified. In case autoselection fails, undef
86 # is returned.
87 sub load_buildsystem {
88         my $system=shift;
89         my $step=shift;
90         if (defined $system) {
91                 my $inst = create_buildsystem_instance($system, @_);
92                 return $inst;
93         }
94         else {
95                 # Try to determine build system automatically
96                 my @buildsystems;
97                 foreach $system (@BUILDSYSTEMS) {
98                         push @buildsystems, create_buildsystem_instance($system, @_);
99                 }
100                 return autoselect_buildsystem($step, @buildsystems);
101         }
102 }
103
104 sub load_all_buildsystems {
105         my $incs=shift || \@INC;
106         my (%buildsystems, @buildsystems);
107
108         foreach my $inc (@$incs) {
109                 my $path = File::Spec->catdir($inc, "Debian/Debhelper/Buildsystem");
110                 if (-d $path) {
111                         foreach my $module_path (glob "$path/*.pm") {
112                                 my $name = basename($module_path);
113                                 $name =~ s/\.pm$//;
114                                 next if exists $buildsystems{$name};
115                                 $buildsystems{$name} = create_buildsystem_instance($name, @_);
116                         }
117                 }
118         }
119
120         # Standard debhelper build systems first
121         foreach my $name (@BUILDSYSTEMS) {
122                 error("standard debhelper build system '$name' could not be found/loaded")
123                     if not exists $buildsystems{$name};
124                 push @buildsystems, $buildsystems{$name};
125                 delete $buildsystems{$name};
126         }
127
128         # The rest are 3rd party build systems
129         foreach my $name (keys %buildsystems) {
130                 my $inst = $buildsystems{$name};
131                 $inst->{thirdparty} = 1;
132                 push @buildsystems, $inst;
133         }
134
135         return @buildsystems;
136 }
137
138 sub buildsystems_init {
139         my %args=@_;
140
141         my $max_parallel=1;
142
143         # Available command line options
144         my %options = (
145             "D=s" => \$opt_sourcedir,
146             "sourcedirectory=s" => \$opt_sourcedir,
147
148             "B:s" => \$opt_builddir,
149             "builddirectory:s" => \$opt_builddir,
150
151             "S=s" => \$opt_buildsys,
152             "buildsystem=s" => \$opt_buildsys,
153
154             "l" => \$opt_list,
155             "list" => \$opt_list,
156
157             "parallel" => sub { $max_parallel = -1 },
158             "max-parallel=i" => \$max_parallel,
159         );
160         $args{options}{$_} = $options{$_} foreach keys(%options);
161         Debian::Debhelper::Dh_Lib::init(%args);
162         set_parallel($max_parallel);
163 }
164
165 sub set_parallel {
166         my $max=shift;
167
168         $opt_parallel=1;
169
170         if (exists $ENV{DEB_BUILD_OPTIONS}) {
171                 # Get number of processes from parallel=n tag limiting it
172                 # with $max if needed
173                 foreach my $opt (split(/\s+/, $ENV{DEB_BUILD_OPTIONS})) {
174                         if ($opt =~ /^parallel=(-?\d+)$/) {
175                                 $opt_parallel = $1;
176                                 if ($max > 0 && $opt_parallel > $max) {
177                                         $opt_parallel = $max;
178                                 }
179                         }
180                 }
181         }
182 }
183
184 sub buildsystems_list {
185         my $step=shift;
186
187         my @buildsystems = load_all_buildsystems();
188         my $auto = autoselect_buildsystem($step, grep { ! $_->{thirdparty} } @buildsystems);
189         my $specified;
190
191         # List build systems (including auto and specified status)
192         foreach my $inst (@buildsystems) {
193                 if (! defined $specified && defined $opt_buildsys && $opt_buildsys eq $inst->NAME()) {
194                         $specified = $inst;
195                 }
196                 printf("%-20s %s", $inst->NAME(), $inst->DESCRIPTION());
197                 print " [3rd party]" if $inst->{thirdparty};
198                 print "\n";
199         }
200         print "\n";
201         print "Auto-selected: ", $auto->NAME(), "\n" if defined $auto;
202         print "Specified: ", $specified->NAME(), "\n" if defined $specified;
203         print "No system auto-selected or specified\n"
204                 if ! defined $auto && ! defined $specified;
205 }
206
207 sub buildsystems_do {
208         my $step=shift;
209
210         if (!defined $step) {
211                 $step = basename($0);
212                 $step =~ s/^dh_auto_//;
213         }
214
215         if (grep(/^\Q$step\E$/, BUILD_STEPS) == 0) {
216                 error("unrecognized build step: " . $step);
217         }
218
219         if ($opt_list) {
220                 buildsystems_list($step);
221                 exit 0;
222         }
223
224         my $buildsystem = load_buildsystem($opt_buildsys, $step);
225         if (defined $buildsystem) {
226                 $buildsystem->pre_building_step($step);
227                 $buildsystem->$step(@_, @{$dh{U_PARAMS}});
228                 $buildsystem->post_building_step($step);
229         }
230         return 0;
231 }
232
233 1