]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Getopt.pm
1e52109e119b064ab938eeada9747972ab953d63
[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.
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 # Passed an option name and an option value, adds packages to the list
20 # of packages. We need this so the list will be built up in the right
21 # order.
22 sub AddPackage { my($option,$value)=@_;
23         if ($option eq 'i' or $option eq 'indep') {
24                 push @{$options{DOPACKAGES}}, GetPackages('indep');
25                 $options{DOINDEP}=1;
26         }
27         elsif ($option eq 'a' or $option eq 'arch') {
28                 push @{$options{DOPACKAGES}}, GetPackages('arch');
29                 $options{DOARCH}=1;
30         }
31         elsif ($option eq 'p' or $option eq 'package') {
32                 push @{$options{DOPACKAGES}}, $value;
33         }
34         elsif ($option eq 's' or $option eq 'same-arch') {
35                 push @{$options{DOPACKAGES}}, GetPackages('same');
36                 $options{DOSAME}=1;
37         }
38         else {
39                 error("bad option $option - should never happen!\n");
40         }
41 }
42
43 # Add a package to a list of packages that should not be acted on.
44 sub ExcludePackage { my($option,$value)=@_;
45         $exclude_package{$value}=1;
46 }
47
48 # Add another item to the exclude list.
49 sub AddExclude { my($option,$value)=@_;
50         push @{$options{EXCLUDE}},$value;
51 }
52
53 # This collects non-options values.
54 sub NonOption {
55         push @{$options{ARGV}}, @_;
56 }
57
58 # Parse options and return a hash of the values.
59 sub parseopts {
60         undef %options;
61         
62         my $ret=GetOptions(
63                 "v" => \$options{VERBOSE},
64                 "verbose" => \$options{VERBOSE},
65         
66                 "i" => \&AddPackage,
67                 "indep" => \&AddPackage,
68         
69                 "a" => \&AddPackage,
70                 "arch" => \&AddPackage,
71         
72                 "p=s" => \&AddPackage,
73                 "package=s" => \&AddPackage,
74         
75                 "s" => \&AddPackage,
76                 "same-arch" => \&AddPackage,
77         
78                 "N=s" => \&ExcludePackage,
79                 "no-package=s" => \&ExcludePackage,
80         
81                 "n" => \$options{NOSCRIPTS},
82                 "noscripts" => \$options{NOSCRIPTS},
83
84                 "x" => \$options{INCLUDE_CONFFILES}, # is -x for some unknown historical reason..
85                 "include-conffiles" => \$options{INCLUDE_CONFFILES},
86         
87                 "X=s" => \&AddExclude,
88                 "exclude=s" => \&AddExclude,
89         
90                 "d" => \$options{D_FLAG},
91                 "remove-d" => \$options{D_FLAG},
92                 "dirs-only" => \$options{D_FLAG},
93         
94                 "r" => \$options{R_FLAG},
95                 "no-restart-on-upgrade" => \$options{R_FLAG},
96         
97                 "k" => \$options{K_FLAG},
98                 "keep" => \$options{K_FLAG},
99
100                 "P=s" => \$options{TMPDIR},
101                 "tmpdir=s" => \$options{TMPDIR},
102
103                 "u=s", => \$options{U_PARAMS},
104                 "update-rcd-params=s", => \$options{U_PARAMS},
105                 "dpkg-shlibdeps-params=s", => \$options{U_PARAMS},
106                 "dpkg-gencontrol-params=s", => \$options{U_PARAMS},
107
108                 "l=s", => \$options{L_PARAMS},
109
110                 "m=s", => \$options{M_PARAMS},
111                 "major=s" => \$options{M_PARAMS},
112
113                 "V:s", => \$options{V_FLAG},
114                 "version-info:s" => \$options{V_FLAG},
115
116                 "A" => \$options{PARAMS_ALL},
117                 "all" => \$options{PARAMS_ALL},
118
119                 "no-act" => \$options{NO_ACT},
120         
121                 "init-script=s" => \$options{INIT_SCRIPT},
122                 
123                 "sourcedir=s" => \$options{SOURCEDIR},
124                 
125                 "destdir=s" => \$options{DESTDIR},
126                 
127                 "number=i" => \$options{PRIORITY},      # deprecated
128                 "priority=i" => \$options{PRIORITY},
129                 
130                 "flavor=s" => \$options{FLAVOR},
131                 
132                 "<>" => \&NonOption,
133         );
134
135         if (!$ret) {
136                 error("unknown option; aborting");
137         }
138         
139         # Check to see if -V was specified. If so, but no parameters were
140         # passed, the variable will be defined but empty.
141         if (defined($options{V_FLAG})) {
142                 $options{V_FLAG_SET}=1;
143         }
144         
145         # If we have not been given any packages to act on, assume they
146         # want us to act on them all. Note we have to do this before excluding
147         # packages out, below.
148         if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
149                 if ($options{DOINDEP} || $options{DOARCH} || $options{DOSAME}) {
150                                 # User specified that all arch (in)dep package be
151                                 # built, and there are none of that type.
152                                 error("I have no package to build");
153                 }
154                 push @{$options{DOPACKAGES}},GetPackages();
155         }
156         
157         # Remove excluded packages from the list of packages to act on.
158         my @package_list;
159         my $package;
160         foreach $package (@{$options{DOPACKAGES}}) {
161                 if (! $exclude_package{$package}) {
162                         push @package_list, $package;   
163                 }
164         }
165         @{$options{DOPACKAGES}}=@package_list;
166
167         # Generate EXCLUDE_FIND.
168         $options{EXCLUDE_FIND}='';
169         foreach (@{$options{EXCLUDE}}) {
170                 $options{EXCLUDE_FIND}.="-regex .*".quotemeta($_).".* -or ";
171         }
172         $options{EXCLUDE_FIND}=~s/ -or $//;
173
174         # If there are no packages to act on now, it's an error.
175         if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
176                 error("I have no package to build");
177         }
178
179         # Anything left in @ARGV is options that appeared after a --
180         # These options are added to U_PARAMS, while the non-option
181         # values we collected replace them in @ARGV;
182         $options{U_PARAMS}.=join(' ', @ARGV);
183         @ARGV=@{$options{ARGV}} if exists $options{ARGV};
184
185         return %options;
186 }       
187
188 sub import {
189         # Enable bundling of short command line options.
190         Getopt::Long::config("bundling");
191 }               
192
193 1