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