]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Buildsystems.pm
3908145850627160805fd72023844cfafef6b60a
[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 # Historical order must be kept for backwards compatibility. New
18 # build systems 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         "ant",
27 );
28
29 my $opt_buildsys;
30 my $opt_sourcedir;
31 my $opt_builddir;
32 my $opt_list;
33 my $opt_parallel;
34
35 sub create_buildsystem_instance {
36         my $system=shift;
37         my %bsopts=@_;
38         my $module = "Debian::Debhelper::Buildsystem::$system";
39
40         eval "use $module";
41         if ($@) {
42                 error("unable to load build system class '$system': $@");
43         }
44
45         if (!exists $bsopts{builddir} && defined $opt_builddir) {
46                 $bsopts{builddir} = ($opt_builddir eq "") ? undef : $opt_builddir;
47         }
48         if (!exists $bsopts{sourcedir} && defined $opt_sourcedir) {
49                 $bsopts{sourcedir} = ($opt_sourcedir eq "") ? undef : $opt_sourcedir;
50         }
51         if (!exists $bsopts{parallel}) {
52                 $bsopts{parallel} = $opt_parallel;
53         }
54         return $module->new(%bsopts);
55 }
56
57 # Similar to create_build system_instance(), but it attempts to autoselect
58 # a build system if none was specified. In case autoselection fails, undef
59 # is returned.
60 sub load_buildsystem {
61         my $system=shift;
62         my $step=shift;
63         if (defined $system) {
64                 my $inst = create_buildsystem_instance($system, @_);
65                 return $inst;
66         }
67         else {
68                 # Try to determine build system automatically
69                 for $system (@BUILDSYSTEMS) {
70                         my $inst = create_buildsystem_instance($system, @_);
71                         if ($inst->check_auto_buildable($step)) {
72                                 return $inst;
73                         }
74                 }
75         }
76         return;
77 }
78
79 sub load_all_buildsystems {
80         my $incs=shift || \@INC;
81         my (%buildsystems, @buildsystems);
82
83         for my $inc (@$incs) {
84                 my $path = File::Spec->catdir($inc, "Debian/Debhelper/Buildsystem");
85                 if (-d $path) {
86                         for my $module_path (glob "$path/*.pm") {
87                                 my $name = basename($module_path);
88                                 $name =~ s/\.pm$//;
89                                 next if exists $buildsystems{$name};
90                                 $buildsystems{$name} = create_buildsystem_instance($name, @_);
91                         }
92                 }
93         }
94
95         # Standard debhelper build systems first
96         for my $name (@BUILDSYSTEMS) {
97                 error("standard debhelper build system '$name' could not be found/loaded")
98                     if not exists $buildsystems{$name};
99                 push @buildsystems, $buildsystems{$name};
100                 delete $buildsystems{$name};
101         }
102
103         # The rest are 3rd party build systems
104         for my $name (keys %buildsystems) {
105                 my $inst = $buildsystems{$name};
106                 $inst->{thirdparty} = 1;
107                 push @buildsystems, $inst;
108         }
109
110         return @buildsystems;
111 }
112
113 sub buildsystems_init {
114         my %args=@_;
115
116         # Available command line options
117         my %options = (
118             "D=s" => \$opt_sourcedir,
119             "sourcedirectory=s" => \$opt_sourcedir,
120         
121             "B:s" => \$opt_builddir,
122             "builddirectory:s" => \$opt_builddir,
123
124             "S=s" => \$opt_buildsys,
125             "buildsystem=s" => \$opt_buildsys,
126
127             "l" => \$opt_list,
128             "list" => \$opt_list,
129
130             "j:i" => \$opt_parallel,
131             "parallel:i" => \$opt_parallel,
132         );
133         $args{options}{$_} = $options{$_} foreach keys(%options);
134         Debian::Debhelper::Dh_Lib::init(%args);
135
136         # Post-process parallel building option. Initially $opt_parallel may have
137         # such values:
138         # * undef - no --parallel option was specified. This tells buildsystem class
139         #   not to mess with MAKEFLAGS (with the exception of cleaning MAKEFLAGS
140         #   from pointless unavailable jobserver options to avoid warnings) nor
141         #   enable parallel.
142         # * 1 - --parallel=1 option was specified, hence the package should never be
143         #   built in parallel mode. Cleans MAKEFLAGS if needed.
144         # * 0 - --parallel was specified without interger argument meaning package
145         #   does not want to enforce limit on maximum number of parallel processes.
146         # * N > 1 - --parallel=N was specified where N is the maximum number parallel
147         #   processes the package wants to enforce.
148         # Taken DEB_BUILD_OPTIONS and all this into account, set $opt_parallel to the
149         # number of parallel processes to be used for *this* build.
150         if (defined $opt_parallel) {
151                 if ($opt_parallel >= 0 && exists $ENV{DEB_BUILD_OPTIONS}) {
152                         # Parse parallel=n tag
153                         my $n;
154                         foreach my $opt (split(/\s+/, $ENV{DEB_BUILD_OPTIONS})) {
155                                 $n = $1 if $opt =~ /^parallel=(\d+)$/;
156                         }
157                         if (defined $n && $n > 0) {
158                                 $opt_parallel = $n if $opt_parallel == 0 || $n < $opt_parallel;
159                         }
160                         else {
161                                 # Invalid value in the parallel tag. Disable.
162                                 $opt_parallel = 1;
163                         }
164                 }
165                 else {
166                         # In case invalid number was passed
167                         $opt_parallel = 1;
168                 }
169         }
170 }
171
172 sub buildsystems_list {
173         my $step=shift;
174
175         # List build systems (including auto and specified status)
176         my ($auto, $specified);
177         for my $inst (load_all_buildsystems()) {
178                 my $is_specified = defined $opt_buildsys && $opt_buildsys eq $inst->NAME();
179                 if (! defined $specified && defined $opt_buildsys && $opt_buildsys eq $inst->NAME()) {
180                         $specified = $inst->NAME();
181                 }
182                 elsif (! defined $auto && ! $inst->{thirdparty} && $inst->check_auto_buildable($step)) {
183                         $auto = $inst->NAME();
184                 }
185                 printf("%-20s %s", $inst->NAME(), $inst->DESCRIPTION());
186                 print " [3rd party]" if $inst->{thirdparty};
187                 print "\n";
188         }
189         print "\n";
190         print "Auto-selected: $auto\n" if defined $auto;
191         print "Specified: $specified\n" if defined $specified;
192         print "No system auto-selected or specified\n"
193                 if ! defined $auto && ! defined $specified;
194 }
195
196 sub buildsystems_do {
197         my $step=shift;
198
199         if (!defined $step) {
200                 $step = basename($0);
201                 $step =~ s/^dh_auto_//;
202         }
203
204         if (grep(/^\Q$step\E$/, qw{configure build test install clean}) == 0) {
205                 error("unrecognized build step: " . $step);
206         }
207
208         if ($opt_list) {
209                 buildsystems_list($step);
210                 exit 0;
211         }
212
213         my $buildsystem = load_buildsystem($opt_buildsys, $step);
214         if (defined $buildsystem) {
215                 $buildsystem->pre_building_step($step);
216                 $buildsystem->$step(@_, @{$dh{U_PARAMS}});
217                 $buildsystem->post_building_step($step);
218         }
219         return 0;
220 }
221
222 1