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