]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Getopt.pm
Allow individual debhelper programs to define their own special options by passing...
[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 # Add an item to the with list.
68 sub AddWith { my($option,$value)=@_;
69         push @{$dh{WITH}},$value;
70 }
71
72 # This collects non-options values.
73 sub NonOption {
74         push @{$dh{ARGV}}, @_;
75 }
76
77 # Parse options and set %dh values.
78 sub parseopts {
79         my %options=%{shift()} if ref $_[0];
80
81         my $ret=GetOptions(
82                 "v" => \$dh{VERBOSE},
83                 "verbose" => \$dh{VERBOSE},
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                 "dbg-package=s" => \&AddDebugPackage,
95                 
96                 "s" => \&AddPackage,
97                 "same-arch" => \&AddPackage,
98         
99                 "N=s" => \&ExcludePackage,
100                 "no-package=s" => \&ExcludePackage,
101         
102                 "n" => \$dh{NOSCRIPTS},
103                 "noscripts" => \$dh{NOSCRIPTS},
104                 "o" => \$dh{ONLYSCRIPTS},
105                 "onlyscripts" => \$dh{ONLYSCRIPTS},
106
107                 "x" => \$dh{INCLUDE_CONFFILES}, # is -x for some unknown historical reason..
108                 "include-conffiles" => \$dh{INCLUDE_CONFFILES},
109         
110                 "X=s" => \&AddExclude,
111                 "exclude=s" => \&AddExclude,
112                 
113                 "ignore=s" => \&AddIgnore,
114         
115                 "d" => \$dh{D_FLAG},
116                 "remove-d" => \$dh{D_FLAG},
117                 "dirs-only" => \$dh{D_FLAG},
118         
119                 "r" => \$dh{R_FLAG},
120                 "no-restart-on-upgrade" => \$dh{R_FLAG},
121                 "no-start" => \$dh{NO_START},
122                 "R|restart-after-upgrade" => \$dh{RESTART_AFTER_UPGRADE},
123         
124                 "k" => \$dh{K_FLAG},
125                 "keep" => \$dh{K_FLAG},
126                 "keep-debug" => \$dh{K_FLAG},
127
128                 "P=s" => \$dh{TMPDIR},
129                 "tmpdir=s" => \$dh{TMPDIR},
130
131                 "u=s", => \$dh{U_PARAMS},
132                 "update-rcd-params=s", => \$dh{U_PARAMS},
133                 "dpkg-shlibdeps-params=s", => \$dh{U_PARAMS},
134                 "dpkg-gencontrol-params=s", => \$dh{U_PARAMS},
135
136                 "l=s", => \$dh{L_PARAMS},
137
138                 "m=s", => \$dh{M_PARAMS},
139                 "major=s" => \$dh{M_PARAMS},
140
141                 "V:s", => \$dh{V_FLAG},
142                 "version-info:s" => \$dh{V_FLAG},
143
144                 "A" => \$dh{PARAMS_ALL},
145                 "all" => \$dh{PARAMS_ALL},
146
147                 "no-act" => \$dh{NO_ACT},
148         
149                 "init-script=s" => \$dh{INIT_SCRIPT},
150                 
151                 "sourcedir=s" => \$dh{SOURCEDIR},
152                 
153                 "destdir=s" => \$dh{DESTDIR},
154
155                 "filename=s" => \$dh{FILENAME},
156                 
157                 "priority=s" => \$dh{PRIORITY},
158                 
159                 "flavor=s" => \$dh{FLAVOR},
160
161                 "autodest" => \$dh{AUTODEST},
162
163                 "h|help" => \&showhelp,
164
165                 "mainpackage=s" => \$dh{MAINPACKAGE},
166
167                 "list-missing" => \$dh{LIST_MISSING},
168
169                 "fail-missing" => \$dh{FAIL_MISSING},
170                 
171                 "L|libpackage=s" => \$dh{LIBPACKAGE},
172                 
173                 "name=s" => \$dh{NAME},
174                 
175                 "error-handler=s" => \$dh{ERROR_HANDLER},
176                 
177                 "add-udeb=s" => \$dh{SHLIBS_UDEB},
178                 
179                 "language=s" => \$dh{LANGUAGE},
180
181                 "until=s" => \$dh{UNTIL},
182                 "after=s" => \$dh{AFTER},
183                 "before=s" => \$dh{BEFORE},
184                 "remaining" => \$dh{REMAINING},
185                 "with=s" => \&AddWith,
186
187                 %options,
188
189                 "<>" => \&NonOption,
190         );
191
192         if (!$ret) {
193                 error("unknown option; aborting");
194         }
195         
196         # Check to see if -V was specified. If so, but no parameters were
197         # passed, the variable will be defined but empty.
198         if (defined($dh{V_FLAG})) {
199                 $dh{V_FLAG_SET}=1;
200         }
201         
202         # If we have not been given any packages to act on, assume they
203         # want us to act on them all. Note we have to do this before excluding
204         # packages out, below.
205         if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
206                 if ($dh{DOINDEP} || $dh{DOARCH} || $dh{DOSAME}) {
207                         # User specified that all arch (in)dep package be
208                         # built, and there are none of that type.
209                         warning("I have no package to build");
210                         exit(0);
211                 }
212                 push @{$dh{DOPACKAGES}},getpackages();
213         }
214
215         # Remove excluded packages from the list of packages to act on.
216         # Also unique the list, in case some options were specified that
217         # added a package to it twice.
218         my @package_list;
219         my $package;
220         my %packages_seen;
221         foreach $package (@{$dh{DOPACKAGES}}) {
222                 if (! $exclude_package{$package}) {
223                         if (! exists $packages_seen{$package}) {
224                                 $packages_seen{$package}=1;
225                                 push @package_list, $package;   
226                         }
227                 }
228         }
229         @{$dh{DOPACKAGES}}=@package_list;
230
231         # If there are no packages to act on now, it's an error.
232         if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
233                 error("I have no package to build");
234         }
235
236         if (defined $dh{U_PARAMS}) {
237                 # Split the U_PARAMS up into an array.
238                 my $u=$dh{U_PARAMS};
239                 undef $dh{U_PARAMS};
240                 push @{$dh{U_PARAMS}}, split(/\s+/,$u);
241         }
242
243         # Anything left in @ARGV is options that appeared after a --
244         # These options are added to the U_PARAMS array, while the
245         # non-option values we collected replace them in @ARGV;
246         push @{$dh{U_PARAMS}}, @ARGV;
247         @ARGV=@{$dh{ARGV}} if exists $dh{ARGV};
248 }
249
250 sub import {
251         # Enable bundling of short command line options.
252         Getopt::Long::config("bundling");
253 }               
254
255 1