1 # Copyright © 2007 Frank Lichtenheld <djpig@debian.org>
2 # Copyright © 2008, 2012-2017 Guillem Jover <guillem@debian.org>
3 # Copyright © 2010 Raphaël Hertzog <hertzog@debian.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18 package Dpkg::BuildOptions;
23 our $VERSION = '1.02';
26 use Dpkg::ErrorHandling;
33 Dpkg::BuildOptions - parse and update build options
37 The Dpkg::BuildOptions object can be used to manipulate options stored
38 in environment variables like DEB_BUILD_OPTIONS and
39 DEB_BUILD_MAINT_OPTIONS.
45 =item $bo = Dpkg::BuildOptions->new(%opts)
47 Create a new Dpkg::BuildOptions object. It will be initialized based
48 on the value of the environment variable named $opts{envvar} (or
49 DEB_BUILD_OPTIONS if that option is not set).
54 my ($this, %opts) = @_;
55 my $class = ref($this) || $this;
60 envvar => $opts{envvar} // 'DEB_BUILD_OPTIONS',
63 $self->merge(Dpkg::Build::Env::get($self->{envvar}), $self->{envvar});
69 Reset the object to not have any option (it's empty).
75 $self->{options} = {};
79 =item $bo->merge($content, $source)
81 Merge the options set in $content and record that they come from the
82 source $source. $source is mainly used in warning messages currently
83 to indicate where invalid options have been detected.
85 $content is a space separated list of options with optional assigned
86 values like "nocheck parallel=2".
91 my ($self, $content, $source) = @_;
92 return 0 unless defined $content;
94 foreach (split(/\s+/, $content)) {
95 unless (/^([a-z][a-z0-9_-]*)(?:=(\S*))?$/) {
96 warning(g_('invalid flag in %s: %s'), $source, $_);
99 $count += $self->set($1, $2, $source);
104 =item $bo->set($option, $value, [$source])
106 Store the given option in the object with the given value. It's legitimate
107 for a value to be undefined if the option is a simple boolean (its
108 presence means true, its absence means false). The $source is optional
109 and indicates where the option comes from.
111 The known options have their values checked for sanity. Options without
112 values have their value removed and options with invalid values are
118 my ($self, $key, $value, $source) = @_;
121 if ($key =~ /^(noopt|nostrip|nocheck)$/ && defined($value)) {
123 } elsif ($key eq 'parallel') {
125 return 0 if $value !~ /^\d*$/;
128 $self->{options}{$key} = $value;
129 $self->{source}{$key} = $source;
134 =item $bo->get($option)
136 Return the value associated to the option. It might be undef even if the
137 option exists. You might want to check with $bo->has($option) to verify if
138 the option is stored in the object.
143 my ($self, $key) = @_;
144 return $self->{options}{$key};
147 =item $bo->has($option)
149 Returns a boolean indicating whether the option is stored in the object.
154 my ($self, $key) = @_;
155 return exists $self->{options}{$key};
158 =item $bo->parse_features($option, $use_feature)
160 Parse the $option values, as a set of known features to enable or disable,
161 as specified in the $use_feature hash reference.
163 Each feature is prefixed with a ‘B<+>’ or a ‘B<->’ character as a marker
164 to enable or disable it. The special feature “B<all>” can be used to act
165 on all known features.
167 Unknown of malformed features will emit warnings.
172 my ($self, $option, $use_feature) = @_;
174 foreach my $feature (split(/,/, $self->get($option) // '')) {
175 $feature = lc $feature;
176 if ($feature =~ s/^([+-])//) {
177 my $value = ($1 eq '+') ? 1 : 0;
178 if ($feature eq 'all') {
179 $use_feature->{$_} = $value foreach keys %{$use_feature};
181 if (exists $use_feature->{$feature}) {
182 $use_feature->{$feature} = $value;
184 warning(g_('unknown %s feature in %s variable: %s'),
185 $option, $self->{envvar}, $feature);
189 warning(g_('incorrect value in %s option of %s variable: %s'),
190 $option, $self->{envvar}, $feature);
195 =item $string = $bo->output($fh)
197 Return a string representation of the build options suitable to be
198 assigned to an environment variable. Can optionally output that string to
199 the given filehandle.
204 my ($self, $fh) = @_;
205 my $o = $self->{options};
206 my $res = join(' ', map { defined($o->{$_}) ? $_ . '=' . $o->{$_} : $_ } sort keys %$o);
207 print { $fh } $res if defined $fh;
211 =item $bo->export([$var])
213 Export the build options to the given environment variable. If omitted,
214 the environment variable defined at creation time is assumed. The value
215 set to the variable is also returned.
220 my ($self, $var) = @_;
221 $var //= $self->{envvar};
222 my $content = $self->output();
223 Dpkg::Build::Env::set($var, $content);
231 =head2 Version 1.02 (dpkg 1.18.19)
233 New method: $bo->parse_features().
235 =head2 Version 1.01 (dpkg 1.16.1)
237 Enable to use another environment variable instead of DEB_BUILD_OPTIONS.
238 Thus add support for the "envvar" option at creation time.
240 =head2 Version 1.00 (dpkg 1.15.6)
242 Mark the module as public.