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