]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Getopt.pm
6f95dd35bf844c4f77757059d92d7440fac7409a
[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                 "keep-debug" => \$options{K_FLAG},
125
126                 "P=s" => \$options{TMPDIR},
127                 "tmpdir=s" => \$options{TMPDIR},
128
129                 "u=s", => \$options{U_PARAMS},
130                 "update-rcd-params=s", => \$options{U_PARAMS},
131                 "dpkg-shlibdeps-params=s", => \$options{U_PARAMS},
132                 "dpkg-gencontrol-params=s", => \$options{U_PARAMS},
133
134                 "l=s", => \$options{L_PARAMS},
135
136                 "m=s", => \$options{M_PARAMS},
137                 "major=s" => \$options{M_PARAMS},
138
139                 "V:s", => \$options{V_FLAG},
140                 "version-info:s" => \$options{V_FLAG},
141
142                 "A" => \$options{PARAMS_ALL},
143                 "all" => \$options{PARAMS_ALL},
144
145                 "no-act" => \$options{NO_ACT},
146         
147                 "init-script=s" => \$options{INIT_SCRIPT},
148                 
149                 "sourcedir=s" => \$options{SOURCEDIR},
150                 
151                 "destdir=s" => \$options{DESTDIR},
152
153                 "filename=s" => \$options{FILENAME},
154                 
155                 "priority=s" => \$options{PRIORITY},
156                 
157                 "flavor=s" => \$options{FLAVOR},
158
159                 "autodest" => \$options{AUTODEST},
160
161                 "h|help" => \&showhelp,
162
163                 "mainpackage=s" => \$options{MAINPACKAGE},
164
165                 "list-missing" => \$options{LIST_MISSING},
166
167                 "fail-missing" => \$options{FAIL_MISSING},
168                 
169                 "L|libpackage=s" => \$options{LIBPACKAGE},
170                 
171                 "name=s" => \$options{NAME},
172                 
173                 "error-handler=s" => \$options{ERROR_HANDLER},
174                 
175                 "add-udeb=s" => \$options{SHLIBS_UDEB},
176                 
177                 "language=s" => \$options{LANGUAGE},
178
179                 "<>" => \&NonOption,
180         );
181
182         if (!$ret) {
183                 error("unknown option; aborting");
184         }
185         
186         # Check to see if -V was specified. If so, but no parameters were
187         # passed, the variable will be defined but empty.
188         if (defined($options{V_FLAG})) {
189                 $options{V_FLAG_SET}=1;
190         }
191         
192         # If we have not been given any packages to act on, assume they
193         # want us to act on them all. Note we have to do this before excluding
194         # packages out, below.
195         if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
196                 if ($options{DOINDEP} || $options{DOARCH} || $options{DOSAME}) {
197                         # User specified that all arch (in)dep package be
198                         # built, and there are none of that type.
199                         warning("I have no package to build");
200                         exit(0);
201                 }
202                 push @{$options{DOPACKAGES}},getpackages();
203         }
204
205         # Remove excluded packages from the list of packages to act on.
206         # Also unique the list, in case some options were specified that
207         # added a package to it twice.
208         my @package_list;
209         my $package;
210         my %packages_seen;
211         foreach $package (@{$options{DOPACKAGES}}) {
212                 if (! $exclude_package{$package}) {
213                         if (! exists $packages_seen{$package}) {
214                                 $packages_seen{$package}=1;
215                                 push @package_list, $package;   
216                         }
217                 }
218         }
219         @{$options{DOPACKAGES}}=@package_list;
220
221         # If there are no packages to act on now, it's an error.
222         if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
223                 error("I have no package to build");
224         }
225
226         if (defined $options{U_PARAMS}) {
227                 # Split the U_PARAMS up into an array.
228                 my $u=$options{U_PARAMS};
229                 undef $options{U_PARAMS};
230                 push @{$options{U_PARAMS}}, split(/\s+/,$u);
231         }
232
233         # Anything left in @ARGV is options that appeared after a --
234         # These options are added to the U_PARAMS array, while the
235         # non-option values we collected replace them in @ARGV;
236         push @{$options{U_PARAMS}}, @ARGV;
237         @ARGV=@{$options{ARGV}} if exists $options{ARGV};
238
239         return %options;
240 }
241
242 sub import {
243         # Enable bundling of short command line options.
244         Getopt::Long::config("bundling");
245 }               
246
247 1