]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Getopt.pm
r2033: * Add --ignore option. This is intended to ease dealing with upstream
[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 use Exporter;
13 #use vars qw{@ISA @EXPORT};
14 #@ISA=qw(Exporter);
15 #@EXPORT=qw(&aparseopts); # FIXME: for some reason, this doesn't work.
16
17 my (%options, %exclude_package);
18
19 sub showhelp {
20         my $prog=basename($0);
21         print "Usage: $prog [options]\n\n";
22         print "  $prog is a part of debhelper. See debhelper(7)\n";
23         print "  and $prog(1) for complete usage instructions.\n"; 
24         exit(1);
25 }
26
27 # Passed an option name and an option value, adds packages to the list
28 # of packages. We need this so the list will be built up in the right
29 # order.
30 sub AddPackage { my($option,$value)=@_;
31         if ($option eq 'i' or $option eq 'indep') {
32                 push @{$options{DOPACKAGES}}, getpackages('indep');
33                 $options{DOINDEP}=1;
34         }
35         elsif ($option eq 'a' or $option eq 'arch') {
36                 push @{$options{DOPACKAGES}}, getpackages('arch');
37                 $options{DOARCH}=1;
38         }
39         elsif ($option eq 'p' or $option eq 'package') {
40                 push @{$options{DOPACKAGES}}, $value;
41         }
42         elsif ($option eq 's' or $option eq 'same-arch') {
43                 push @{$options{DOPACKAGES}}, getpackages('same');
44                 $options{DOSAME}=1;
45         }
46         else {
47                 error("bad option $option - should never happen!\n");
48         }
49 }
50
51 # Adds packages to the list of debug packages.
52 sub AddDebugPackage { my($option,$value)=@_;
53         push @{$options{DEBUGPACKAGES}}, $value;
54 }
55
56 # Add a package to a list of packages that should not be acted on.
57 sub ExcludePackage { my($option,$value)=@_;
58         $exclude_package{$value}=1;
59 }
60
61 # Add another item to the exclude list.
62 sub AddExclude { my($option,$value)=@_;
63         push @{$options{EXCLUDE}},$value;
64 }
65
66 # Add a file to the ignore list.
67 sub AddIgnore { my($option,$file)=@_;
68         $options{IGNORE}->{$file}=1;
69 }
70
71 # This collects non-options values.
72 sub NonOption {
73         push @{$options{ARGV}}, @_;
74 }
75
76 # Parse options and return a hash of the values.
77 sub parseopts {
78         undef %options;
79         
80         my $ret=GetOptions(
81                 "v" => \$options{VERBOSE},
82                 "verbose" => \$options{VERBOSE},
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                 "dbg-package=s" => \&AddDebugPackage,
94                 
95                 "s" => \&AddPackage,
96                 "same-arch" => \&AddPackage,
97         
98                 "N=s" => \&ExcludePackage,
99                 "no-package=s" => \&ExcludePackage,
100         
101                 "n" => \$options{NOSCRIPTS},
102                 "noscripts" => \$options{NOSCRIPTS},
103                 "o" => \$options{ONLYSCRIPTS},
104                 "onlyscripts" => \$options{ONLYSCRIPTS},
105
106                 "x" => \$options{INCLUDE_CONFFILES}, # is -x for some unknown historical reason..
107                 "include-conffiles" => \$options{INCLUDE_CONFFILES},
108         
109                 "X=s" => \&AddExclude,
110                 "exclude=s" => \&AddExclude,
111                 
112                 "ignore=s" => \&AddIgnore,
113         
114                 "d" => \$options{D_FLAG},
115                 "remove-d" => \$options{D_FLAG},
116                 "dirs-only" => \$options{D_FLAG},
117         
118                 "r" => \$options{R_FLAG},
119                 "no-restart-on-upgrade" => \$options{R_FLAG},
120                 "no-start" => \$options{NO_START},
121         
122                 "k" => \$options{K_FLAG},
123                 "keep" => \$options{K_FLAG},
124
125                 "P=s" => \$options{TMPDIR},
126                 "tmpdir=s" => \$options{TMPDIR},
127
128                 "u=s", => \$options{U_PARAMS},
129                 "update-rcd-params=s", => \$options{U_PARAMS},
130                 "dpkg-shlibdeps-params=s", => \$options{U_PARAMS},
131                 "dpkg-gencontrol-params=s", => \$options{U_PARAMS},
132
133                 "l=s", => \$options{L_PARAMS},
134
135                 "m=s", => \$options{M_PARAMS},
136                 "major=s" => \$options{M_PARAMS},
137
138                 "V:s", => \$options{V_FLAG},
139                 "version-info:s" => \$options{V_FLAG},
140
141                 "A" => \$options{PARAMS_ALL},
142                 "all" => \$options{PARAMS_ALL},
143
144                 "no-act" => \$options{NO_ACT},
145         
146                 "init-script=s" => \$options{INIT_SCRIPT},
147                 
148                 "sourcedir=s" => \$options{SOURCEDIR},
149                 
150                 "destdir=s" => \$options{DESTDIR},
151
152                 "filename=s" => \$options{FILENAME},
153                 
154                 "priority=s" => \$options{PRIORITY},
155                 
156                 "flavor=s" => \$options{FLAVOR},
157
158                 "autodest" => \$options{AUTODEST},
159
160                 "h|help" => \&showhelp,
161
162                 "mainpackage=s" => \$options{MAINPACKAGE},
163
164                 "list-missing" => \$options{LIST_MISSING},
165
166                 "fail-missing" => \$options{FAIL_MISSING},
167                 
168                 "L|libpackage=s" => \$options{LIBPACKAGE},
169                 
170                 "name=s" => \$options{NAME},
171                 
172                 "keep-debug" => \$options{KEEP_DEBUG},
173                 
174                 "error-handler=s" => \$options{ERROR_HANDLER},
175                 
176                 "add-udeb=s" => \$options{SHLIBS_UDEB},
177                 
178                 "language=s" => \$options{LANGUAGE},
179
180                 "<>" => \&NonOption,
181         );
182
183         if (!$ret) {
184                 error("unknown option; aborting");
185         }
186         
187         # Check to see if -V was specified. If so, but no parameters were
188         # passed, the variable will be defined but empty.
189         if (defined($options{V_FLAG})) {
190                 $options{V_FLAG_SET}=1;
191         }
192         
193         # If we have not been given any packages to act on, assume they
194         # want us to act on them all. Note we have to do this before excluding
195         # packages out, below.
196         if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
197                 if ($options{DOINDEP} || $options{DOARCH} || $options{DOSAME}) {
198                         # User specified that all arch (in)dep package be
199                         # built, and there are none of that type.
200                         warning("I have no package to build");
201                         exit(0);
202                 }
203                 push @{$options{DOPACKAGES}},getpackages();
204         }
205
206         # Remove excluded packages from the list of packages to act on.
207         # Also unique the list, in case some options were specified that
208         # added a package to it twice.
209         my @package_list;
210         my $package;
211         my %packages_seen;
212         foreach $package (@{$options{DOPACKAGES}}) {
213                 if (! $exclude_package{$package}) {
214                         if (! exists $packages_seen{$package}) {
215                                 $packages_seen{$package}=1;
216                                 push @package_list, $package;   
217                         }
218                 }
219         }
220         @{$options{DOPACKAGES}}=@package_list;
221
222         # If there are no packages to act on now, it's an error.
223         if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
224                 error("I have no package to build");
225         }
226
227         if (defined $options{U_PARAMS}) {
228                 # Split the U_PARAMS up into an array.
229                 my $u=$options{U_PARAMS};
230                 undef $options{U_PARAMS};
231                 push @{$options{U_PARAMS}}, split(/\s+/,$u);
232         }
233
234         # Anything left in @ARGV is options that appeared after a --
235         # These options are added to the U_PARAMS array, while the
236         # non-option values we collected replace them in @ARGV;
237         push @{$options{U_PARAMS}}, @ARGV;
238         @ARGV=@{$options{ARGV}} if exists $options{ARGV};
239
240         return %options;
241 }
242
243 sub import {
244         # Enable bundling of short command line options.
245         Getopt::Long::config("bundling");
246 }               
247
248 1