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