OLD | NEW |
| (Empty) |
1 # Copyright © 2007 Frank Lichtenheld <djpig@debian.org> | |
2 # Copyright © 2010 Raphaël Hertzog <hertzog@debian.org> | |
3 # | |
4 # This program is free software; you can redistribute it and/or modify | |
5 # it under the terms of the GNU General Public License as published by | |
6 # the Free Software Foundation; either version 2 of the License, or | |
7 # (at your option) any later version. | |
8 # | |
9 # This program is distributed in the hope that it will be useful, | |
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 # GNU General Public License for more details. | |
13 # | |
14 # You should have received a copy of the GNU General Public License | |
15 # along with this program. If not, see <https://www.gnu.org/licenses/>. | |
16 | |
17 package Dpkg::BuildOptions; | |
18 | |
19 use strict; | |
20 use warnings; | |
21 | |
22 our $VERSION = '1.01'; | |
23 | |
24 use Dpkg::Gettext; | |
25 use Dpkg::ErrorHandling; | |
26 use Dpkg::BuildEnv; | |
27 | |
28 =encoding utf8 | |
29 | |
30 =head1 NAME | |
31 | |
32 Dpkg::BuildOptions - parse and update build options | |
33 | |
34 =head1 DESCRIPTION | |
35 | |
36 The Dpkg::BuildOptions object can be used to manipulate options stored | |
37 in environment variables like DEB_BUILD_OPTIONS and | |
38 DEB_BUILD_MAINT_OPTIONS. | |
39 | |
40 =head1 FUNCTIONS | |
41 | |
42 =over 4 | |
43 | |
44 =item my $bo = Dpkg::BuildOptions->new(%opts) | |
45 | |
46 Create a new Dpkg::BuildOptions object. It will be initialized based | |
47 on the value of the environment variable named $opts{envvar} (or | |
48 DEB_BUILD_OPTIONS if that option is not set). | |
49 | |
50 =cut | |
51 | |
52 sub new { | |
53 my ($this, %opts) = @_; | |
54 my $class = ref($this) || $this; | |
55 | |
56 my $self = { | |
57 options => {}, | |
58 source => {}, | |
59 envvar => $opts{envvar} // 'DEB_BUILD_OPTIONS', | |
60 }; | |
61 bless $self, $class; | |
62 $self->merge(Dpkg::BuildEnv::get($self->{envvar}), $self->{envvar}); | |
63 return $self; | |
64 } | |
65 | |
66 =item $bo->reset() | |
67 | |
68 Reset the object to not have any option (it's empty). | |
69 | |
70 =cut | |
71 | |
72 sub reset { | |
73 my ($self) = @_; | |
74 $self->{options} = {}; | |
75 $self->{source} = {}; | |
76 } | |
77 | |
78 =item $bo->merge($content, $source) | |
79 | |
80 Merge the options set in $content and record that they come from the | |
81 source $source. $source is mainly used in warning messages currently | |
82 to indicate where invalid options have been detected. | |
83 | |
84 $content is a space separated list of options with optional assigned | |
85 values like "nocheck parallel=2". | |
86 | |
87 =cut | |
88 | |
89 sub merge { | |
90 my ($self, $content, $source) = @_; | |
91 return 0 unless defined $content; | |
92 my $count = 0; | |
93 foreach (split(/\s+/, $content)) { | |
94 unless (/^([a-z][a-z0-9_-]*)(?:=(\S*))?$/) { | |
95 warning(_g('invalid flag in %s: %s'), $source, $_); | |
96 next; | |
97 } | |
98 $count += $self->set($1, $2, $source); | |
99 } | |
100 return $count; | |
101 } | |
102 | |
103 =item $bo->set($option, $value, [$source]) | |
104 | |
105 Store the given option in the objet with the given value. It's legitimate | |
106 for a value to be undefined if the option is a simple boolean (its | |
107 presence means true, its absence means false). The $source is optional | |
108 and indicates where the option comes from. | |
109 | |
110 The known options have their values checked for sanity. Options without | |
111 values have their value removed and options with invalid values are | |
112 discarded. | |
113 | |
114 =cut | |
115 | |
116 sub set { | |
117 my ($self, $key, $value, $source) = @_; | |
118 | |
119 # Sanity checks | |
120 if ($key =~ /^(noopt|nostrip|nocheck)$/ && defined($value)) { | |
121 $value = undef; | |
122 } elsif ($key eq 'parallel') { | |
123 $value //= ''; | |
124 return 0 if $value !~ /^\d*$/; | |
125 } | |
126 | |
127 $self->{options}{$key} = $value; | |
128 $self->{source}{$key} = $source; | |
129 | |
130 return 1; | |
131 } | |
132 | |
133 =item $bo->get($option) | |
134 | |
135 Return the value associated to the option. It might be undef even if the | |
136 option exists. You might want to check with $bo->has($option) to verify if | |
137 the option is stored in the object. | |
138 | |
139 =cut | |
140 | |
141 sub get { | |
142 my ($self, $key) = @_; | |
143 return $self->{options}{$key}; | |
144 } | |
145 | |
146 =item $bo->has($option) | |
147 | |
148 Returns a boolean indicating whether the option is stored in the object. | |
149 | |
150 =cut | |
151 | |
152 sub has { | |
153 my ($self, $key) = @_; | |
154 return exists $self->{options}{$key}; | |
155 } | |
156 | |
157 =item $string = $bo->output($fh) | |
158 | |
159 Return a string representation of the build options suitable to be | |
160 assigned to an environment variable. Can optionnaly output that string to | |
161 the given filehandle. | |
162 | |
163 =cut | |
164 | |
165 sub output { | |
166 my ($self, $fh) = @_; | |
167 my $o = $self->{options}; | |
168 my $res = join(' ', map { defined($o->{$_}) ? $_ . '=' . $o->{$_} : $_ } sor
t keys %$o); | |
169 print { $fh } $res if defined $fh; | |
170 return $res; | |
171 } | |
172 | |
173 =item $bo->export([$var]) | |
174 | |
175 Export the build options to the given environment variable. If omitted, | |
176 the environment variable defined at creation time is assumed. The value | |
177 set to the variable is also returned. | |
178 | |
179 =cut | |
180 | |
181 sub export { | |
182 my ($self, $var) = @_; | |
183 $var = $self->{envvar} unless defined $var; | |
184 my $content = $self->output(); | |
185 Dpkg::BuildEnv::set($var, $content); | |
186 return $content; | |
187 } | |
188 | |
189 =back | |
190 | |
191 =head1 CHANGES | |
192 | |
193 =head2 Version 1.01 | |
194 | |
195 Enable to use another environment variable instead of DEB_BUILD_OPTIONS. | |
196 Thus add support for the "envvar" option at creation time. | |
197 | |
198 =head1 AUTHOR | |
199 | |
200 Raphaël Hertzog <hertzog@debian.org> | |
201 | |
202 =cut | |
203 | |
204 1; | |
OLD | NEW |