]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Getopt.pm
Fix warning about unknown options passed to commands in override targets.
[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         my %options=%{$params{options}} if ref $params{options};
73         
74         my $oldwarn;
75         if ($params{ignore_unknown_options}) {
76                 $oldwarn=$SIG{__WARN__};
77                 $SIG{__WARN__}=sub {};
78         }
79
80         my $ret=Getopt::Long::GetOptionsFromArray($array,
81                 "v" => \$dh{VERBOSE},
82                 "verbose" => \$dh{VERBOSE},
83
84                 "no-act" => \$dh{NO_ACT},
85         
86                 "i" => \&AddPackage,
87                 "indep" => \&AddPackage,
88         
89                 "a" => \&AddPackage,
90                 "arch" => \&AddPackage,
91         
92                 "p=s" => \&AddPackage,
93                 "package=s" => \&AddPackage,
94                 
95                 "N=s" => \&ExcludePackage,
96                 "no-package=s" => \&ExcludePackage,
97         
98                 "remaining-packages" => \$dh{EXCLUDE_LOGGED},
99         
100                 "dbg-package=s" => \&AddDebugPackage,
101                 
102                 "s" => \&AddPackage,
103                 "same-arch" => \&AddPackage,
104         
105                 "n" => \$dh{NOSCRIPTS},
106                 "noscripts" => \$dh{NOSCRIPTS},
107                 "o" => \$dh{ONLYSCRIPTS},
108                 "onlyscripts" => \$dh{ONLYSCRIPTS},
109
110                 "X=s" => \&AddExclude,
111                 "exclude=s" => \&AddExclude,
112                 
113                 "d" => \$dh{D_FLAG},
114         
115                 "k" => \$dh{K_FLAG},
116                 "keep" => \$dh{K_FLAG},
117
118                 "P=s" => \$dh{TMPDIR},
119                 "tmpdir=s" => \$dh{TMPDIR},
120
121                 "u=s", => \$dh{U_PARAMS},
122
123                 "V:s", => \$dh{V_FLAG},
124
125                 "A" => \$dh{PARAMS_ALL},
126                 "all" => \$dh{PARAMS_ALL},
127         
128                 "priority=s" => \$dh{PRIORITY},
129                 
130                 "h|help" => \&showhelp,
131
132                 "mainpackage=s" => \$dh{MAINPACKAGE},
133
134                 "name=s" => \$dh{NAME},
135
136                 "error-handler=s" => \$dh{ERROR_HANDLER},
137                 
138                 "ignore=s" => \&AddIgnore,
139
140                 %options,
141
142                 "<>" => \&NonOption,
143         );
144
145         if ($params{ignore_unknown_options}) {
146                 $SIG{__WARN__}=$oldwarn;
147                 return 1;
148         }
149         else {
150                 return $ret;
151         }
152 }
153
154 sub split_options_string {
155         my $str=shift;
156         $str=~s/^\s+//;
157         return split(/\s+/,$str);
158 }
159
160 # Parse options and set %dh values.
161 sub parseopts {
162         my %params=@_;
163         
164         my @ARGV_extra;
165
166         # DH_INTERNAL_OPTIONS is used to pass additional options from
167         # dh through an override target to a command.
168         if (defined $ENV{DH_INTERNAL_OPTIONS}) {
169                 @ARGV_extra=split(/\x1e/, $ENV{DH_INTERNAL_OPTIONS});
170                 getoptions(\@ARGV_extra, %params, ignore_unknown_options => 1);
171
172                 # Avoid forcing acting on packages specified in
173                 # DH_INTERNAL_OPTIONS. This way, -p can be specified
174                 # at the command line to act on a specific package, and if
175                 # nothing is specified, the excludes will cause the set of
176                 # packages DH_INTERNAL_OPTIONS specifies to be acted on.
177                 if (defined $dh{DOPACKAGES}) {
178                         foreach my $package (getpackages()) {
179                                 if (! grep { $_ eq $package } @{$dh{DOPACKAGES}}) {
180                                         $exclude_package{$package}=1;
181                                 }
182                         }
183                 }
184                 delete $dh{DOPACKAGES};
185                 delete $dh{DOINDEP};
186                 delete $dh{DOARCH};
187         }
188         
189         # DH_OPTIONS can contain additional options to be parsed like @ARGV
190         if (defined $ENV{DH_OPTIONS}) {
191                 @ARGV_extra=split_options_string($ENV{DH_OPTIONS});
192                 my $ret=getoptions(\@ARGV_extra, %params);
193                 if (!$ret) {
194                         warning("warning: ignored unknown options in DH_OPTIONS");
195                 }
196         }
197
198         my $ret=getoptions(\@ARGV, %params);
199         if (!$ret) {
200                 warning("warning: unknown options will be a fatal error in a future debhelper release");
201                 #error("unknown option; aborting");
202         }
203
204         # Check to see if -V was specified. If so, but no parameters were
205         # passed, the variable will be defined but empty.
206         if (defined($dh{V_FLAG})) {
207                 $dh{V_FLAG_SET}=1;
208         }
209         
210         # If we have not been given any packages to act on, assume they
211         # want us to act on them all. Note we have to do this before excluding
212         # packages out, below.
213         if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
214                 if ($dh{DOINDEP} || $dh{DOARCH}) {
215                         # User specified that all arch (in)dep package be
216                         # built, and there are none of that type.
217                         warning("You asked that all arch in(dep) packages be built, but there are none of that type.");
218                         exit(0);
219                 }
220                 push @{$dh{DOPACKAGES}},getpackages();
221         }
222
223         # Remove excluded packages from the list of packages to act on.
224         # Also unique the list, in case some options were specified that
225         # added a package to it twice.
226         my @package_list;
227         my $package;
228         my %packages_seen;
229         foreach $package (@{$dh{DOPACKAGES}}) {
230                 if (defined($dh{EXCLUDE_LOGGED}) &&
231                     grep { $_ eq basename($0) } load_log($package)) {
232                         $exclude_package{$package}=1;
233                 }
234                 if (! $exclude_package{$package}) {
235                         if (! exists $packages_seen{$package}) {
236                                 $packages_seen{$package}=1;
237                                 push @package_list, $package;   
238                         }
239                 }
240         }
241         @{$dh{DOPACKAGES}}=@package_list;
242
243         if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
244                 warning("No packages to build.");
245                 exit(0);
246         }
247
248         if (defined $dh{U_PARAMS}) {
249                 # Split the U_PARAMS up into an array.
250                 my $u=$dh{U_PARAMS};
251                 undef $dh{U_PARAMS};
252                 push @{$dh{U_PARAMS}}, split(/\s+/,$u);
253         }
254
255         # Anything left in @ARGV is options that appeared after a --
256         # These options are added to the U_PARAMS array, while the
257         # non-option values we collected replace them in @ARGV;
258         push @{$dh{U_PARAMS}}, @ARGV, @ARGV_extra;
259         @ARGV=@{$dh{ARGV}} if exists $dh{ARGV};
260 }
261
262 sub import {
263         # Enable bundling of short command line options.
264         Getopt::Long::config("bundling");
265 }               
266
267 1