]> git.donarmstrong.com Git - debhelper.git/blob - Dh_Getopt.pm
r306: * Corrected slash substitution problem in dh_installwm.
[debhelper.git] / Dh_Getopt.pm
1 #!/usr/bin/perl -w
2 #
3 # Debhelper option processing library.
4 #
5 # Joey Hess GPL copyright 1998.
6
7 package Dh_Getopt;
8 use strict;
9
10 use 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 # Parse options and return a hash of the values.
54 sub parseopts {
55         undef %options;
56         
57         my $ret=GetOptions(
58                 "v" => \$options{VERBOSE},
59                 "verbose" => \$options{VERBOSE},
60         
61                 "i" => \&AddPackage,
62                 "indep" => \&AddPackage,
63         
64                 "a" => \&AddPackage,
65                 "arch" => \&AddPackage,
66         
67                 "p=s" => \&AddPackage,
68                 "package=s" => \&AddPackage,
69         
70                 "s" => \&AddPackage,
71                 "same-arch" => \&AddPackage,
72         
73                 "N=s" => \&ExcludePackage,
74                 "no-package=s" => \&ExcludePackage,
75         
76                 "n" => \$options{NOSCRIPTS},
77                 "noscripts" => \$options{NOSCRIPTS},
78
79                 "x" => \$options{INCLUDE_CONFFILES}, # is -x for some unknown historical reason..
80                 "include-conffiles" => \$options{INCLUDE_CONFFILES},
81         
82                 "X=s" => \&AddExclude,
83                 "exclude=s" => \&AddExclude,
84         
85                 "d" => \$options{D_FLAG},
86                 "remove-d" => \$options{D_FLAG},
87                 "dirs-only" => \$options{D_FLAG},
88         
89                 "r" => \$options{R_FLAG},
90                 "no-restart-on-upgrade" => \$options{R_FLAG},
91         
92                 "k" => \$options{K_FLAG},
93                 "keep" => \$options{K_FLAG},
94
95                 "P=s" => \$options{TMPDIR},
96                 "tmpdir=s" => \$options{TMPDIR},
97
98                 "u=s", => \$options{U_PARAMS},
99                 "update-rcd-params=s", => \$options{U_PARAMS},
100                 "dpkg-shlibdeps-params=s", => \$options{U_PARAMS},
101                 "dpkg-gencontrol-params=s", => \$options{U_PARAMS},
102
103                 "l=s", => \$options{L_PARAMS},
104
105                 "m=s", => \$options{M_PARAMS},
106                 "major=s" => \$options{M_PARAMS},
107
108                 "V:s", => \$options{V_FLAG},
109                 "version-info:s" => \$options{V_FLAG},
110
111                 "A" => \$options{PARAMS_ALL},
112                 "all" => \$options{PARAMS_ALL},
113
114                 "no-act" => \$options{NO_ACT},
115         
116                 "init-script=s" => \$options{INIT_SCRIPT},
117                 
118                 "sourcedir=s" => \$options{SOURCEDIR},
119                 
120                 "destdir=s" => \$options{DESTDIR},
121                 
122                 "number=s" => \$options{number},
123                 
124                 "flavor=s" => \$options{flavor},
125         );
126
127         if (!$ret) {
128                 error("unknown option; aborting");
129         }
130         
131         # Check to see if -V was specified. If so, but no parameters were
132         # passed, the variable will be defined but empty.
133         if (defined($options{V_FLAG})) {
134                 $options{V_FLAG_SET}=1;
135         }
136         
137         # If we have not been given any packages to act on, assume they
138         # want us to act on them all. Note we have to do this before excluding
139         # packages out, below.
140         if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
141                 if ($options{DOINDEP} || $options{DOARCH} || $options{DOSAME}) {
142                                 # User specified that all arch (in)dep package be
143                                 # built, and there are none of that type.
144                                 error("I have no package to build");
145                 }
146                 push @{$options{DOPACKAGES}},GetPackages();
147         }
148         
149         # Remove excluded packages from the list of packages to act on.
150         my @package_list;
151         my $package;
152         foreach $package (@{$options{DOPACKAGES}}) {
153                 if (! $exclude_package{$package}) {
154                         push @package_list, $package;   
155                 }
156         }
157         @{$options{DOPACKAGES}}=@package_list;
158
159         # Generate EXCLUDE_FIND.
160         $options{EXCLUDE_FIND}='';
161         foreach (@{$options{EXCLUDE}}) {
162                 $options{EXCLUDE_FIND}.="-regex .*".quotemeta($_).".* -or ";
163         }
164         $options{EXCLUDE_FIND}=~s/ -or $//;
165
166         # If there are no packages to act on now, it's an error.
167         if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
168                 error("I have no package to build");
169         }
170
171         return %options;
172 }       
173
174 sub import {
175         # Enable bundling of short command line options.
176         Getopt::Long::config("bundling");
177 }               
178
179 1