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