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