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