Dpkg/BuildOptions.pm

filter: Statementstmt 95.6% 66 / 69 Branchbran 81.8% 18 / 22 Conditioncond 80.0% 12 / 15 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 15 / 15 Podpod 100% 9 / 9 totaltotal 89.9% 125 / 139 CC 21 SCAR 14.6
CC
21
Cov
91%
parse_features
8.4
set
6.0
merge
4.0
CRAP 4.3
« Dpkg/Vendor.pm ↑ Summary Dpkg/Gettext.pm »
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>
4 #
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.
9 #
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.
14 #
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/>.
17  
18 =encoding utf8
19  
20 =head1 NAME
21  
22 Dpkg::BuildOptions - parse and update build options
23  
24 =head1 DESCRIPTION
25  
26 This class can be used to manipulate options stored
27 in environment variables like DEB_BUILD_OPTIONS and
28 DEB_BUILD_MAINT_OPTIONS.
29  
30 =cut
31  
32 package Dpkg::BuildOptions 1.02;
33  
34 3 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 3 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 3 ▶use Dpkg::Gettext;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use Dpkg::ErrorHandling;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use Dpkg::BuildEnv;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 =head1 METHODS
43  
44 =over 4
45  
46 =item $bo = Dpkg::BuildOptions->new(%opts)
47  
48 Create a new Dpkg::BuildOptions object. It will be initialized based
49 on the value of the environment variable named B<DEB_BUILD_OPTIONS>.
50  
51 Options:
52  
53 =over
54  
55 =item B<envvar>
56  
57 Set the environment variable name to use instead of B<DEB_BUILD_OPTIONS>
58 to initialize the build flags.
59  
60 =back
61  
62 =cut
63  
64 56 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
65 56 my ($this, %opts) = @_;
66 56 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0560
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
67  
68 my $self = {
69 options => {},
70 source => {},
71 56 ▶ envvar => $opts{envvar} // 'DEB_BUILD_OPTIONS',
Condition100%2/2
$opts{"envvar"} // "DEB_BUILD_OPTIONS"
l!l
533
MC/DC100%1/1
$opts{"envvar"} // "DEB_BUILD_OPTIONS"
$opts{"envvar"}
Truth table100%2/2
A
A= $opts{"envvar"}
Aresult
11
00
72 };
73 56 bless $self, $class;
74 56 $self->merge(Dpkg::BuildEnv::get($self->{envvar}), $self->{envvar});
75 56 return $self;
76 }
77  
78 =item $bo->reset()
79  
80 Reset the object to not have any option (it's empty).
81  
82 =cut
83  
84 2 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
85 2 my $self = shift;
86 2 $self->{options} = {};
87 2 $self->{source} = {};
88 }
89  
90 =item $count = $bo->merge($content, $source)
91  
92 Merge the options set in $content and record that they come from the
93 source $source. $source is mainly used in warning messages currently
94 to indicate where invalid options have been detected.
95  
96 $content is a space separated list of options with optional assigned
97 values like "nocheck parallel=2".
98  
99 =cut
100  
101 57 ▶sub merge {
Subroutine merge: called CC=4 SCAR=13.9
Pod documented
102 57 my ($self, $content, $source) = @_;
103 57 ▶ return 0 unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 35 22 57
104 22 my $count = 0;
105 22 foreach (split(/\s+/, $content)) {
106 34 ▶ unless (/^([a-z][a-z0-9_-]*)(?:=(\S*))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^([a-z][a-z0-9_-]*)(?:=(\S*))?$/u) 3 31 34
107 3 warning(g_('invalid flag in %s: %s'), $source, $_);
108 3 next;
109 }
110 ## no critic (RegularExpressions::ProhibitCaptureWithoutTest)
111 31 $count += $self->set($1, $2, $source);
112 }
113 22 return $count;
114 }
115  
116 =item $bool = $bo->set($option, $value, [$source])
117  
118 Store the given option in the object with the given value. It's legitimate
119 for a value to be undefined if the option is a simple boolean (its
120 presence means true, its absence means false). The $source is optional
121 and indicates where the option comes from.
122  
123 The known options have their values checked for sanity. Options without
124 values have their value removed and options with invalid values are
125 discarded.
126  
127 =cut
128  
129 38 ▶sub set {
Subroutine set: called CC=6 SCAR=17.9
Pod documented
130 38 my ($self, $key, $value, $source) = @_;
131  
132 # Sanity checks.
133 38 ▶ if (any { $_ eq $key } qw(terse noopt nostrip nocheck) and defined $value) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
if (&any(sub { $_ eq $key; } , 'terse', 'noopt', 'nostrip', 'nocheck') and defined $value) { } 1 37 38
elsif ($key eq 'parallel') { } 4 33 37
Condition100%3/3
&any(sub { $_ eq $key; } , "terse", "noopt", "nostrip", "nocheck") and defined $value
!ll&&!rl&&r
3341
MC/DC100%2/2
&any(sub { $_ eq $key; } , "terse", "noopt", "nostrip", "nocheck") and defined $value
&any(sub { $_ eq $key; } , "terse", "noopt", "nostrip", "nocheck") defined $value
Truth table100%3/3
A and B
A= &any(sub { $_ eq $key; } , "terse", "noopt", "nostrip", "nocheck")B= defined $value
ABresult
0X0
100
111
134 1 $value = undef;
135 } elsif ($key eq 'parallel') {
136 4 ▶ $value //= '';
Condition100%2/2
$value //= ""
l!l
31
MC/DC100%1/1
$value //= ""
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
137 4 ▶ return 0 if $value !~ /^\d*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^\d*$/u 0 4 4
138 }
139  
140 38 $self->{options}{$key} = $value;
141 38 $self->{source}{$key} = $source;
142  
143 38 return 1;
144 }
145  
146 =item $value = $bo->get($option)
147  
148 Return the value associated to the option. It might be undef even if the
149 option exists. You might want to check with $bo->has($option) to verify if
150 the option is stored in the object.
151  
152 =cut
153  
154 377 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
155 377 my ($self, $key) = @_;
156 377 return $self->{options}{$key};
157 }
158  
159 =item $bool = $bo->has($option)
160  
161 Returns a boolean indicating whether the option is stored in the object.
162  
163 =cut
164  
165 36 ▶sub has {
Subroutine has: called CC=1 SCAR=0.0
Pod documented
166 36 my ($self, $key) = @_;
167 36 return exists $self->{options}{$key};
168 }
169  
170 =item $bo->parse_features($option, $use_feature)
171  
172 Parse the $option values, as a set of known features to enable or disable,
173 as specified in the $use_feature hash reference.
174  
175 Each feature is prefixed with a 'B<+>' or a 'B<->' character as a marker
176 to enable or disable it. The special feature "B<all>" can be used to act
177 on all known features.
178  
179 Unknown or malformed features will emit warnings.
180  
181 =cut
182  
183 368 ▶sub parse_features {
Subroutine parse_features: called CC=8 SCAR=21.3
Pod documented
184 368 my ($self, $option, $use_feature) = @_;
185  
186 368 ▶ foreach my $feature (split(/,/, $self->get($option) // '')) {
Condition100%2/2
$self->get($option) // ""
l!l
23345
MC/DC100%1/1
$self->get($option) // ""
$self->get($option)
Truth table100%2/2
A
A= $self->get($option)
Aresult
11
00
187 27 $feature = lc $feature;
188 27 ▶ if ($feature =~ s/^([+-])//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($feature =~ s/^([+-])//u) { } 27 0 27
189 27 ▶ my $value = ($1 eq '+') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '+' ? : 15 12 27
190 27 ▶ if ($feature eq 'all') {
Branch: 2 branches
BranchTrueFalseTotal
if ($feature eq 'all') { } 3 24 27
elsif (exists $use_feature->{$feature}) { } 24 0 24
191 3 $use_feature->{$_} = $value foreach keys %{$use_feature};
192 } elsif (exists $use_feature->{$feature}) {
193 24 $use_feature->{$feature} = $value;
194 } else {
195 warning(g_('unknown %s feature in %s variable: %s'),
196 0 $option, $self->{envvar}, $feature);
197 }
198 } else {
199 warning(g_('incorrect value in %s option of %s variable: %s'),
200 0 $option, $self->{envvar}, $feature);
201 }
202 }
203 }
204  
205 =item $string = $bo->output($fh)
206  
207 Return a string representation of the build options suitable to be
208 assigned to an environment variable. Can optionally output that string to
209 the given filehandle.
210  
211 =cut
212  
213 3 ▶sub output {
Subroutine output: called CC=3 SCAR=11.2
Pod documented
214 3 my ($self, $fh) = @_;
215 3 my $o = $self->{options};
216 3 ▶ my $res = join(' ', map { defined($o->{$_}) ? $_ . '=' . $o->{$_} : $_ } sort keys %$o);
Branch: 1 branch
BranchTrueFalseTotal
defined $o->{$_} ? : 2 4 6
217 0 ▶ print { $fh } $res if defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fh 0 3 3
218 3 return $res;
219 }
220  
221 =item $string = $bo->export([$var])
222  
223 Export the build options to the given environment variable. If omitted,
224 the environment variable defined at creation time is assumed. The value
225 set to the variable is also returned.
226  
227 =cut
228  
229 2 ▶sub export {
Subroutine export: called CC=2 SCAR=7.0
Pod documented
230 2 my ($self, $var) = @_;
231 2 ▶ $var //= $self->{envvar};
Condition66%2/3
$var //= $self->{"envvar"}
l!l&&r!l&&!r
110
MC/DC0%0/2
$var //= $self->{"envvar"}
$var $self->{"envvar"}
Truth table66%2/3
A //= B
A= $varB= $self->{"envvar"}
ABresult
1X1
011
000
232 2 my $content = $self->output();
233 2 Dpkg::BuildEnv::set($var, $content);
234 2 return $content;
235 }
236  
237 =back
238  
239 =head1 CHANGES
240  
241 =head2 Version 1.02 (dpkg 1.18.19)
242  
243 New method: $bo->parse_features().
244  
245 =head2 Version 1.01 (dpkg 1.16.1)
246  
247 Enable to use another environment variable instead of DEB_BUILD_OPTIONS.
248 Thus add support for the "envvar" option at creation time.
249  
250 =head2 Version 1.00 (dpkg 1.15.6)
251  
252 Mark the module as public.
253  
254 =cut
255  
256 3 1;
« Dpkg/Vendor.pm ↑ Summary Dpkg/Gettext.pm »