]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Buildsystems.pm
3b150bd11062bbf8b325ef3bf362c24a8c691815
[debhelper.git] / Debian / Debhelper / Dh_Buildsystems.pm
1 # A module for loading and managing debhelper build system class.
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 );
27
28 my $opt_buildsys;
29 my $opt_sourcedir;
30 my $opt_builddir;
31 my $opt_list;
32 my $opt_help_buildsys;
33
34 sub create_buildsystem_instance {
35         my $system=shift;
36         my %bsopts=@_;
37         my $module = "Debian::Debhelper::Buildsystem::$system";
38
39         eval "use $module";
40         if ($@) {
41                 error("unable to load build system class '$system': $@");
42         }
43
44         if (!exists $bsopts{builddir} && defined $opt_builddir) {
45                 $bsopts{builddir} = ($opt_builddir eq "") ? undef : $opt_builddir;
46         }
47         if (!exists $bsopts{sourcedir} && defined $opt_sourcedir) {
48                 $bsopts{sourcedir} = ($opt_sourcedir eq "") ? undef : $opt_sourcedir;
49         }
50         return $module->new(%bsopts);
51 }
52
53 # Similar to create_build system_instance(), but it attempts to autoselect
54 # a build system if none was specified. In case autoselection fails, undef
55 # is returned.
56 sub load_buildsystem {
57         my $system=shift;
58         my $step=shift;
59         if (defined $system) {
60                 my $inst = create_buildsystem_instance($system, @_);
61                 return $inst;
62         }
63         else {
64                 # Try to determine build system automatically
65                 for $system (@BUILDSYSTEMS) {
66                         my $inst = create_buildsystem_instance($system, @_);
67                         if ($inst->check_auto_buildable($step)) {
68                                 return $inst;
69                         }
70                 }
71         }
72         return;
73 }
74
75 sub load_all_buildsystems {
76         my $incs=shift || \@INC;
77         my (%buildsystems, @buildsystems);
78
79         for my $inc (@$incs) {
80                 my $path = File::Spec->catdir($inc, "Debian/Debhelper/Buildsystem");
81                 if (-d $path) {
82                         for my $module_path (glob "$path/*.pm") {
83                                 my $name = basename($module_path);
84                                 $name =~ s/\.pm$//;
85                                 next if exists $buildsystems{$name};
86                                 $buildsystems{$name} = create_buildsystem_instance($name, @_);
87                         }
88                 }
89         }
90
91         # Push standard debhelper build systems first
92         for my $name (@BUILDSYSTEMS) {
93                 error("standard debhelper build system '$name' could not be found/loaded")
94                     if not exists $buildsystems{$name};
95                 push @buildsystems, $buildsystems{$name};
96                 delete $buildsystems{$name};
97         }
98
99         # The rest are 3rd party build systems
100         for my $name (keys %buildsystems) {
101                 my $inst = $buildsystems{$name};
102                 $inst->{thirdparty} = 1;
103                 push @buildsystems, $inst;
104         }
105
106         return @buildsystems;
107 }
108
109 sub buildsystems_init {
110         my %args=@_;
111
112         # Available command line options
113         my %options = (
114             "D=s" => \$opt_sourcedir,
115             "sourcedirectory=s" => \$opt_sourcedir,
116         
117             "B:s" => \$opt_builddir,
118             "builddirectory:s" => \$opt_builddir,
119
120             "S=s" => \$opt_buildsys,
121             "buildsystem=s" => \$opt_buildsys,
122
123             "l" => \$opt_list,
124             "list" => \$opt_list,
125
126             "help-buildsystem" => \$opt_help_buildsys,
127         );
128         $args{options}{$_} = $options{$_} foreach keys(%options);
129         Debian::Debhelper::Dh_Lib::init(%args);
130 }
131
132 sub buildsystems_list {
133         my $step=shift;
134
135         # List build systems (including auto and specified status)
136         my ($auto, $specified);
137         my @buildsystems = load_all_buildsystems();
138         for my $inst (@buildsystems) {
139                 my $is_specified = defined $opt_buildsys && $opt_buildsys eq $inst->NAME();
140                 if (! defined $specified && defined $opt_buildsys && $opt_buildsys eq $inst->NAME()) {
141                         $specified = $inst->NAME();
142                 }
143                 elsif (! defined $auto && ! $inst->{thirdparty} && $inst->check_auto_buildable($step)) {
144                         $auto = $inst->NAME();
145                 }
146                 printf("%s - %s", $inst->NAME(), $inst->DESCRIPTION());
147                 print " [3rd party]" if $inst->{thirdparty};
148                 print "\n";
149         }
150         print "\n";
151         print "Auto-selected: $auto\n" if defined $auto;
152         print "Specified: $specified\n" if defined $specified;
153         print "No system auto-selected or specified\n"
154                 if ! defined $auto && ! defined $specified;
155 }
156
157 sub help_buildsystem {
158         my $step=shift;
159
160         # Print build system help page to standard output
161
162         my $inst = load_buildsystem($opt_buildsys, $step);
163         if ($inst) {
164                 my $pmfile = ref $inst;
165                 $pmfile =~ s#::#/#g;
166                 $pmfile = $INC{"$pmfile.pm"};
167
168                 # Display help with perldoc if it is installed and output is
169                 # a tty
170                 my $perldoc;
171                 if (-t STDOUT) {
172                         eval "use Pod::Perldoc";
173                         $perldoc = "Pod::Perldoc" if (!$@);
174                 }
175                 if ($perldoc) {
176                         $perldoc = new Pod::Perldoc();
177                         $perldoc->{args} = [ '-oman',
178                                              '-w', 'section=7" "--name=dh_auto_'.lc($inst->NAME()),
179                                              '-w', 'center=Dh_auto build system documentation',
180                                              '-w', 'release=',
181                                              '-F', $pmfile ];
182                         $perldoc->process();
183                 }
184                 else {
185                         # No perldoc on the system. Use Pod::Usage to emit simple text
186                         eval "use Pod::Usage";
187                         pod2usage( -message => "Help page for the ".$inst->NAME()." build system\n" .
188                                                '<' . '-'x74 . '>',
189                                    -input => $pmfile, -exitval => 'NOEXIT',
190                                    -verbose => 2, -noperldoc => 1 );
191                         print '<', '-'x74, '>', "\n";
192                 }
193                 return 0;
194         }
195         else {
196                 print STDERR "No system auto-selected or specified. Try using --buildsystem option\n";
197                 return 1;
198         }
199 }
200
201 sub buildsystems_do {
202         my $step=shift;
203
204         if (!defined $step) {
205                 $step = basename($0);
206                 $step =~ s/^dh_auto_//;
207         }
208
209         if (grep(/^\Q$step\E$/, qw{configure build test install clean}) == 0) {
210                 error("unrecognized build step: " . $step);
211         }
212
213         if ($opt_list) {
214                 buildsystems_list($step);
215                 exit 0;
216         }
217
218         if ($opt_help_buildsys) {
219                 exit help_buildsystem($step);
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;