]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Getopt.pm
4df3eed27ae26944d40afe27820e86c516ab36ca
[debhelper.git] / Debian / Debhelper / Dh_Getopt.pm
1 #!/usr/bin/perl -w
2 #
3 # Debhelper option processing library.
4 #
5 # Joey Hess GPL copyright 1998-2002
6
7 package Debian::Debhelper::Dh_Getopt;
8 use strict;
9
10 use Debian::Debhelper::Dh_Lib;
11 use Getopt::Long;
12
13 my %exclude_package;
14
15 sub showhelp {
16         my $prog=basename($0);
17         print "Usage: $prog [options]\n\n";
18         print "  $prog is a part of debhelper. See debhelper(7)\n";
19         print "  and $prog(1) for complete usage instructions.\n"; 
20         exit(1);
21 }
22
23 # Passed an option name and an option value, adds packages to the list
24 # of packages. We need this so the list will be built up in the right
25 # order.
26 sub AddPackage { my($option,$value)=@_;
27         if ($option eq 'i' or $option eq 'indep') {
28                 push @{$dh{DOPACKAGES}}, getpackages('indep');
29                 $dh{DOINDEP}=1;
30         }
31         elsif ($option eq 'a' or $option eq 'arch' or
32                $option eq 's' or $option eq 'same-arch') {
33                 push @{$dh{DOPACKAGES}}, getpackages('arch');
34                 $dh{DOARCH}=1;
35         }
36         elsif ($option eq 'p' or $option eq 'package') {
37                 push @{$dh{DOPACKAGES}}, $value;
38         }
39         else {
40                 error("bad option $option - should never happen!\n");
41         }
42 }
43
44 # Adds packages to the list of debug packages.
45 sub AddDebugPackage { my($option,$value)=@_;
46         push @{$dh{DEBUGPACKAGES}}, $value;
47 }
48
49 # Add a package to a list of packages that should not be acted on.
50 sub ExcludePackage { my($option,$value)=@_;
51         $exclude_package{$value}=1;
52 }
53
54 # Add another item to the exclude list.
55 sub AddExclude { my($option,$value)=@_;
56         push @{$dh{EXCLUDE}},$value;
57 }
58
59 # Add a file to the ignore list.
60 sub AddIgnore { my($option,$file)=@_;
61         $dh{IGNORE}->{$file}=1;
62 }
63
64 # This collects non-options values.
65 sub NonOption {
66         push @{$dh{ARGV}}, @_;
67 }
68
69 sub getoptions {
70         my $array=shift;
71         my %params=@_;
72
73         my %options=(   
74                 "v" => \$dh{VERBOSE},
75                 "verbose" => \$dh{VERBOSE},
76
77                 "no-act" => \$dh{NO_ACT},
78         
79                 "i" => \&AddPackage,
80                 "indep" => \&AddPackage,
81         
82                 "a" => \&AddPackage,
83                 "arch" => \&AddPackage,
84         
85                 "p=s" => \&AddPackage,
86                 "package=s" => \&AddPackage,
87                 
88                 "N=s" => \&ExcludePackage,
89                 "no-package=s" => \&ExcludePackage,
90         
91                 "remaining-packages" => \$dh{EXCLUDE_LOGGED},
92         
93                 "dbg-package=s" => \&AddDebugPackage,
94                 
95                 "s" => \&AddPackage,
96                 "same-arch" => \&AddPackage,
97         
98                 "n" => \$dh{NOSCRIPTS},
99                 "noscripts" => \$dh{NOSCRIPTS},
100                 "o" => \$dh{ONLYSCRIPTS},
101                 "onlyscripts" => \$dh{ONLYSCRIPTS},
102
103                 "X=s" => \&AddExclude,
104                 "exclude=s" => \&AddExclude,
105                 
106                 "d" => \$dh{D_FLAG},
107         
108                 "k" => \$dh{K_FLAG},
109                 "keep" => \$dh{K_FLAG},
110
111                 "P=s" => \$dh{TMPDIR},
112                 "tmpdir=s" => \$dh{TMPDIR},
113
114                 "u=s", => \$dh{U_PARAMS},
115
116                 "V:s", => \$dh{V_FLAG},
117
118                 "A" => \$dh{PARAMS_ALL},
119                 "all" => \$dh{PARAMS_ALL},
120         
121                 "priority=s" => \$dh{PRIORITY},
122                 
123                 "h|help" => \&showhelp,
124
125                 "mainpackage=s" => \$dh{MAINPACKAGE},
126
127                 "name=s" => \$dh{NAME},
128
129                 "error-handler=s" => \$dh{ERROR_HANDLER},
130                 
131                 "ignore=s" => \&AddIgnore,
132
133                 "O=s" => sub { my($option,$value)=@_;
134                         # Try to parse an option, but ignore it
135                         # if it is not known.
136                         if (getoptions([$value], %params, test => 1)) {
137                                 getoptions([$value], %params);
138                         }
139                 },
140
141                 (ref $params{options} ? %{$params{options}} : ()) ,
142
143                 "<>" => \&NonOption,
144         );
145
146         if ($params{test}) {
147                 foreach my $key (keys %options) {
148                         $options{$key}=sub {};
149                 }
150         }
151
152         my $oldwarn;
153         if ($params{test} || $params{ignore_unknown_options}) {
154                 $oldwarn=$SIG{__WARN__};
155                 $SIG{__WARN__}=sub {};
156         }
157         my $ret=Getopt::Long::GetOptionsFromArray($array, %options);
158         if ($oldwarn) {
159                 $SIG{__WARN__}=$oldwarn;
160         }
161
162         return 1 if $params{ignore_unknown_options};
163         return $ret;
164 }
165
166 sub split_options_string {
167         my $str=shift;
168         $str=~s/^\s+//;
169         return split(/\s+/,$str);
170 }
171
172 # Parse options and set %dh values.
173 sub parseopts {
174         my %params=@_;
175         
176         my @ARGV_extra;
177
178         # DH_INTERNAL_OPTIONS is used to pass additional options from
179         # dh through an override target to a command.
180         if (defined $ENV{DH_INTERNAL_OPTIONS}) {
181                 @ARGV_extra=split(/\x1e/, $ENV{DH_INTERNAL_OPTIONS});
182                 getoptions(\@ARGV_extra, %params, ignore_unknown_options => 1);
183
184                 # Avoid forcing acting on packages specified in
185                 # DH_INTERNAL_OPTIONS. This way, -p can be specified
186                 # at the command line to act on a specific package, but when
187                 # nothing is specified, the excludes will cause the set of
188                 # packages DH_INTERNAL_OPTIONS specifies to be acted on.
189                 if (defined $dh{DOPACKAGES}) {
190                         foreach my $package (getpackages()) {
191                                 if (! grep { $_ eq $package } @{$dh{DOPACKAGES}}) {
192                                         $exclude_package{$package}=1;
193                                 }
194                         }
195                 }
196                 delete $dh{DOPACKAGES};
197                 delete $dh{DOINDEP};
198                 delete $dh{DOARCH};
199         }
200         
201         # DH_OPTIONS can contain additional options to be parsed like @ARGV
202         if (defined $ENV{DH_OPTIONS}) {
203                 @ARGV_extra=split_options_string($ENV{DH_OPTIONS});
204                 my $ret=getoptions(\@ARGV_extra, %params);
205                 if (!$ret) {
206                         warning("warning: ignored unknown options in DH_OPTIONS");
207                 }
208         }
209
210         my $ret=getoptions(\@ARGV, %params);
211         if (!$ret) {
212                 warning("warning: unknown options will be a fatal error in a future debhelper release");
213                 #error("unknown option; aborting");
214         }
215
216         # Check to see if -V was specified. If so, but no parameters were
217         # passed, the variable will be defined but empty.
218         if (defined($dh{V_FLAG})) {
219                 $dh{V_FLAG_SET}=1;
220         }
221         
222         # If we have not been given any packages to act on, assume they
223         # want us to act on them all. Note we have to do this before excluding
224         # packages out, below.
225         if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
226                 if ($dh{DOINDEP} || $dh{DOARCH}) {
227                         # User specified that all arch (in)dep package be
228                         # built, and there are none of that type.
229                         warning("You asked that all arch in(dep) packages be built, but there are none of that type.");
230                         exit(0);
231                 }
232                 push @{$dh{DOPACKAGES}},getpackages();
233         }
234
235         # Remove excluded packages from the list of packages to act on.
236         # Also unique the list, in case some options were specified that
237         # added a package to it twice.
238         my @package_list;
239         my $package;
240         my %packages_seen;
241         foreach $package (@{$dh{DOPACKAGES}}) {
242                 if (defined($dh{EXCLUDE_LOGGED}) &&
243                     grep { $_ eq basename($0) } load_log($package)) {
244                         $exclude_package{$package}=1;
245                 }
246                 if (! $exclude_package{$package}) {
247                         if (! exists $packages_seen{$package}) {
248                                 $packages_seen{$package}=1;
249                                 push @package_list, $package;   
250                         }
251                 }
252         }
253         @{$dh{DOPACKAGES}}=@package_list;
254
255         if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
256                 warning("No packages to build.");
257                 exit(0);
258         }
259
260         if (defined $dh{U_PARAMS}) {
261                 # Split the U_PARAMS up into an array.
262                 my $u=$dh{U_PARAMS};
263                 undef $dh{U_PARAMS};
264                 push @{$dh{U_PARAMS}}, split(/\s+/,$u);
265         }
266
267         # Anything left in @ARGV is options that appeared after a --
268         # These options are added to the U_PARAMS array, while the
269         # non-option values we collected replace them in @ARGV;
270         push @{$dh{U_PARAMS}}, @ARGV, @ARGV_extra;
271         @ARGV=@{$dh{ARGV}} if exists $dh{ARGV};
272 }
273
274 sub import {
275         # Enable bundling of short command line options.
276         Getopt::Long::config("bundling");
277 }               
278
279 1