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