]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Getopt.pm
Merge branch 'master' into buildsystems
[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') {
32                 push @{$dh{DOPACKAGES}}, getpackages('arch');
33                 $dh{DOARCH}=1;
34         }
35         elsif ($option eq 'p' or $option eq 'package') {
36                 push @{$dh{DOPACKAGES}}, $value;
37         }
38         elsif ($option eq 's' or $option eq 'same-arch') {
39                 push @{$dh{DOPACKAGES}}, getpackages('same');
40                 $dh{DOSAME}=1;
41         }
42         else {
43                 error("bad option $option - should never happen!\n");
44         }
45 }
46
47 # Adds packages to the list of debug packages.
48 sub AddDebugPackage { my($option,$value)=@_;
49         push @{$dh{DEBUGPACKAGES}}, $value;
50 }
51
52 # Add a package to a list of packages that should not be acted on.
53 sub ExcludePackage { my($option,$value)=@_;
54         $exclude_package{$value}=1;
55 }
56
57 # Add another item to the exclude list.
58 sub AddExclude { my($option,$value)=@_;
59         push @{$dh{EXCLUDE}},$value;
60 }
61
62 # Add a file to the ignore list.
63 sub AddIgnore { my($option,$file)=@_;
64         $dh{IGNORE}->{$file}=1;
65 }
66
67 # This collects non-options values.
68 sub NonOption {
69         push @{$dh{ARGV}}, @_;
70 }
71
72 sub getoptions {
73         my $array=shift;
74         my $extraoptions=shift;
75
76         my %options=(
77                 "v" => \$dh{VERBOSE},
78                 "verbose" => \$dh{VERBOSE},
79
80                 "no-act" => \$dh{NO_ACT},
81         
82                 "i" => \&AddPackage,
83                 "indep" => \&AddPackage,
84         
85                 "a" => \&AddPackage,
86                 "arch" => \&AddPackage,
87         
88                 "p=s" => \&AddPackage,
89                 "package=s" => \&AddPackage,
90                 
91                 "N=s" => \&ExcludePackage,
92                 "no-package=s" => \&ExcludePackage,
93         
94                 "remaining-packages" => \$dh{EXCLUDE_LOGGED},
95         
96                 "dbg-package=s" => \&AddDebugPackage,
97                 
98                 "s" => \&AddPackage,
99                 "same-arch" => \&AddPackage,
100         
101                 "n" => \$dh{NOSCRIPTS},
102                 "noscripts" => \$dh{NOSCRIPTS},
103                 "o" => \$dh{ONLYSCRIPTS},
104                 "onlyscripts" => \$dh{ONLYSCRIPTS},
105
106                 "X=s" => \&AddExclude,
107                 "exclude=s" => \&AddExclude,
108                 
109                 "d" => \$dh{D_FLAG},
110         
111                 "k" => \$dh{K_FLAG},
112                 "keep" => \$dh{K_FLAG},
113
114                 "P=s" => \$dh{TMPDIR},
115                 "tmpdir=s" => \$dh{TMPDIR},
116
117                 "u=s", => \$dh{U_PARAMS},
118
119                 "V:s", => \$dh{V_FLAG},
120
121                 "A" => \$dh{PARAMS_ALL},
122                 "all" => \$dh{PARAMS_ALL},
123         
124                 "sourcedir=s" => \$dh{SOURCEDIR},
125                 
126                 "destdir=s" => \$dh{DESTDIR},
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                 "<>" => \&NonOption,
141         );
142         
143         # Merge extra options and cancel default ones as needed (undef)
144         if (defined $extraoptions) {
145                 for my $opt (keys %$extraoptions) {
146                         if (defined $extraoptions->{$opt}) {
147                                 $options{$opt}=$extraoptions->{$opt};
148                         }
149                         else {
150                                 delete $options{$opt};
151                         }
152                 }
153         }
154
155         Getopt::Long::GetOptionsFromArray($array, %options);
156 }
157
158 sub split_options_string {
159         my $str=shift;
160
161         $str=~s/^\s+//;
162         return map { $_=~s/\\(\s)/$1/g; $_=~s/\s+$//g; $_ } split(/(?<!\\)\s+/,$str);
163 }
164
165 # Parse options and set %dh values.
166 sub parseopts {
167         my $options=shift;
168         my $extra_args=shift;
169         
170         my @ARGV_extra;
171
172         # DH_INTERNAL_OPTIONS is used to pass additional options from
173         # dh through an override target to a command.
174         if (defined $ENV{DH_INTERNAL_OPTIONS}) {
175                 @ARGV_extra=split_options_string($ENV{DH_INTERNAL_OPTIONS});
176                 # Unknown options will be silently ignored.
177                 my $oldwarn=$SIG{__WARN__};
178                 $SIG{__WARN__}=sub {};
179                 getoptions(\@ARGV_extra, $options);
180                 $SIG{__WARN__}=$oldwarn;
181
182                 # Avoid forcing acting on packages specified in
183                 # DH_INTERNAL_OPTIONS. This way, -p can be specified
184                 # at the command line to act on a specific package, and if
185                 # nothing is specified, the excludes will cause the set of
186                 # packages DH_INTERNAL_OPTIONS specifies to be acted on.
187                 if (defined $dh{DOPACKAGES}) {
188                         foreach my $package (getpackages()) {
189                                 if (! grep { $_ eq $package } @{$dh{DOPACKAGES}}) {
190                                         $exclude_package{$package}=1;
191                                 }
192                         }
193                 }
194                 delete $dh{DOPACKAGES};
195                 delete $dh{DOINDEP};
196                 delete $dh{DOARCH};
197                 delete $dh{DOSAME};
198         }
199         
200         # DH_OPTIONS can contain additional options
201         # to be parsed like @ARGV, but with unknown options
202         # skipped.
203         if (defined $ENV{DH_OPTIONS}) {
204                 @ARGV_extra=split_options_string($ENV{DH_OPTIONS});
205                 my $ret=getoptions(\@ARGV_extra, $options);
206                 if (!$ret) {
207                         warning("warning: ignored unknown options in DH_OPTIONS");
208                 }
209         }
210
211         if (defined $extra_args) {
212                 my @extra_opts=split_options_string($extra_args);
213                 my $ret=getoptions(\@extra_opts, $options);
214                 if (!$ret) {
215                         warning("warning: ignored unknown options");
216                 }
217                 push @ARGV_extra, @extra_opts;
218         }
219
220         my $ret=getoptions(\@ARGV, $options);
221         if (!$ret) {
222                 warning("warning: unknown options will be a fatal error in a future debhelper release");
223                 #error("unknown option; aborting");
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} || $dh{DOSAME}) {
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();
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 sub import {
285         # Enable bundling of short command line options.
286         Getopt::Long::config("bundling");
287 }               
288
289 1