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