Dpkg/BuildDriver/DebianRules.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 46 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 280 CC 29 SCAR 41.6
CC
29
Cov
0%
_parse_rules_requires_root
182.0
_setup_rootcommand
42.0
pre_check
42.0
CRAP 64.0
1 # Copyright © 2020-2024 Guillem Jover <guillem@debian.org>
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <https://www.gnu.org/licenses/>.
15  
16 =encoding utf8
17  
18 =head1 NAME
19  
20 Dpkg::BuildDriver::DebianRules - build a Debian package using debian/rules
21  
22 =head1 DESCRIPTION
23  
24 This class is used by dpkg-buildpackage to drive the build of a Debian
25 package, using F<debian/rules>.
26  
27 B<Note>: This is a private module, its API can change at any time.
28  
29 =cut
30  
31 package Dpkg::BuildDriver::DebianRules 0.01;
32  
33 use v5.36;
34  
35 use Errno qw(ENOENT);
36 use Fcntl qw(:mode);
37 use File::stat ();
38  
39 use Dpkg ();
40 use Dpkg::Gettext;
41 use Dpkg::ErrorHandling;
42 use Dpkg::Path qw(find_command);
43 use Dpkg::BuildTypes;
44  
45 =head1 METHODS
46  
47 =over 4
48  
49 =item $bd = Dpkg::BuildDriver::DebianRules->new(%opts)
50  
51 Create a new Dpkg::BuildDriver::DebianRules object.
52  
53 Supports or requires the same Dpkg::BuildDriver->new() options.
54  
55 =cut
56  
57 sub new {
58 my ($this, %opts) = @_;
59 my $class = ref($this) || $this;
60  
61 my $self = {
62 ctrl => $opts{ctrl},
63 root_cmd => $opts{root_cmd} // [],
64 as_root => $opts{as_root},
65 debian_rules => $opts{debian_rules} // [ 'debian/rules' ],
66 rrr_override => $opts{rrr_override},
67 };
68 bless $self, $class;
69  
70 my $rrr = $self->_parse_rules_requires_root();
71  
72 $self->{rules_requires_root} = $rrr;
73  
74 return $self;
75 }
76  
77 sub _setup_rootcommand {
78 my $self = shift;
79  
80 if ($< == 0) {
81 warning(g_('using a gain-root-command while being root'))
82 if @{$self->{root_cmd}};
83 } else {
84 push @{$self->{root_cmd}}, 'fakeroot'
85 unless @{$self->{root_cmd}};
86 }
87  
88 if (@{$self->{root_cmd}} && ! find_command($self->{root_cmd}[0])) {
89 if ($self->{root_cmd}[0] eq 'fakeroot' && $< != 0) {
90 error(g_("fakeroot not found, either install the fakeroot\n" .
91 'package, specify a command with the -r option, ' .
92 'or run this as root'));
93 } else {
94 error(g_("gain-root-command '%s' not found"), $self->{root_cmd}[0]);
95 }
96 }
97 }
98  
99 my %target_build = map { $_ => 1 } qw(
100 build
101 build-arch
102 build-indep
103 );
104 my %target_legacy_root = map { $_ => 1 } qw(
105 clean
106 binary
107 binary-arch
108 binary-indep
109 );
110 my %target_official = map { $_ => 1 } qw(
111 clean
112 build
113 build-arch
114 build-indep
115 binary
116 binary-arch
117 binary-indep
118 );
119  
120 # Check whether we are doing some kind of rootless build, and sanity check
121 # the fields values.
122 sub _parse_rules_requires_root {
123 my $self = shift;
124  
125 my %rrr;
126 my $rrr;
127 my $rrr_default;
128 my $keywords_base = 0;
129 my $keywords_impl = 0;
130  
131 $rrr_default = 'no';
132  
133 my $ctrl_src = $self->{ctrl}->get_source();
134 $rrr = $self->{rrr_override} // $ctrl_src->{'Rules-Requires-Root'} // $rrr_default;
135  
136 foreach my $keyword (split ' ', $rrr) {
137 if ($keyword =~ m{/}) {
138 if ($keyword =~ m{^dpkg/target/(.*)$}p) {
139 error(g_('disallowed target in %s field keyword %s'),
140 'Rules-Requires-Root', $keyword)
141 if $target_official{$1};
142 } elsif ($keyword =~ m{^dpkg/(.*)$}) {
143 error(g_('%s field keyword "%s" is unknown in dpkg namespace'),
144 'Rules-Requires-Root', $keyword)
145 if $1 ne 'target-subcommand';
146 }
147 $keywords_impl++;
148 } else {
149 if ($keyword ne lc $keyword and
150 (lc $keyword eq 'no' or lc $keyword eq 'binary-targets')) {
151 error(g_('%s field keyword "%s" is uppercase; use "%s" instead'),
152 'Rules-Requires-Root', $keyword, lc $keyword);
153 } elsif (lc $keyword eq 'yes') {
154 error(g_('%s field keyword "%s" is invalid; use "%s" instead'),
155 'Rules-Requires-Root', $keyword, 'binary-targets');
156 } elsif ($keyword ne 'no' and $keyword ne 'binary-targets') {
157 warning(g_('%s field keyword "%s" is unknown'),
158 'Rules-Requires-Root', $keyword);
159 }
160 $keywords_base++;
161 }
162  
163 if ($rrr{$keyword}++) {
164 error(g_('field %s contains duplicate keyword %s'),
165 'Rules-Requires-Root', $keyword);
166 }
167 }
168  
169 if ($self->{as_root} || ! exists $rrr{no}) {
170 $self->_setup_rootcommand();
171 }
172  
173 # Notify the children we do support R³.
174 $ENV{DEB_RULES_REQUIRES_ROOT} = join ' ', sort keys %rrr;
175  
176 if ($keywords_base > 1 or $keywords_base and $keywords_impl) {
177 error(g_('%s field contains both global and implementation specific keywords'),
178 'Rules-Requires-Root');
179 } elsif ($keywords_impl) {
180 # Set only on <implementations-keywords>.
181 $ENV{DEB_GAIN_ROOT_CMD} = join ' ', @{$self->{root_cmd}};
182 } else {
183 # We should not provide the variable otherwise.
184 delete $ENV{DEB_GAIN_ROOT_CMD};
185 }
186  
187 return \%rrr;
188 }
189  
190 sub _rules_requires_root {
191 my ($self, $target) = @_;
192  
193 return 1 if $self->{as_root};
194 return 1 if $self->{rules_requires_root}{"dpkg/target/$target"};
195 return 1 if $self->{rules_requires_root}{'binary-targets'} and $target_legacy_root{$target};
196 return 0;
197 }
198  
199 sub _run_cmd {
200 my @cmd = @_;
201 printcmd(@cmd);
202 system @cmd and subprocerr("@cmd");
203 }
204  
205 sub _run_rules_cond_root {
206 my ($self, $target) = @_;
207  
208 my @cmd;
209 push @cmd, @{$self->{root_cmd}} if $self->_rules_requires_root($target);
210 push @cmd, @{$self->{debian_rules}}, $target;
211  
212 _run_cmd(@cmd);
213 }
214  
215 =item $bd->pre_check()
216  
217 Perform build driver specific checks, before anything else.
218  
219 This checks whether the F<debian/rules> file is executable,
220 and if not then make it so.
221  
222 =cut
223  
224 sub pre_check {
225 my $self = shift;
226  
227 if (@{$self->{debian_rules}} == 1) {
228 my $rules = $self->{debian_rules}[0];
229  
230 my $st = File::stat::lstat($rules);
231 if (! defined $st) {
232 syserr(g_('cannot stat %s'), $rules) if $! != ENOENT;
233 warning(g_('%s does not exist'), $rules);
234 } elsif (-f $st) {
235 if (! -x $st) {
236 warning(g_('%s is not executable; fixing that'), $rules);
237  
238 chmod S_IMODE($st->mode) | 0o111, $rules
239 or syserr(g_('cannot make %s executable'), $rules);
240 }
241 } else {
242 warning(g_('%s is not a plain file'), $rules);
243 }
244 }
245 }
246  
247 =item $bool = $bd->need_build_task($build_task, $binary_task)
248  
249 Returns whether we need to use the build task.
250  
251 B<Note>: This method is needed as long as we support building as root-like.
252 Once that is not needed this method will be deprecated.
253  
254 =cut
255  
256 sub need_build_task {
257 my ($self, $build_task, $binary_task) = @_;
258  
259 # If we are building rootless, there is no need to call the build target
260 # independently as non-root.
261 return 0 if not $self->_rules_requires_root($binary_task);
262 return 1;
263 }
264  
265 =item $bd->run_build_task($build_task, $binary_task)
266  
267 Executes the build task for the build.
268  
269 B<Note>: This method is needed as long as we support building as root-like.
270 Once that is not needed this method will be deprecated.
271  
272 =cut
273  
274 sub run_build_task {
275 my ($self, $build_task, $binary_task) = @_;
276  
277 # If we are building rootless, there is no need to call the build
278 # target independently as non-root.
279 if ($self->_rules_requires_root($binary_task)) {
280 _run_cmd(@{$self->{debian_rules}}, $build_task)
281 }
282  
283 return;
284 }
285  
286 =item $bd->run_task($task)
287  
288 Executes the given task for the build.
289  
290 =cut
291  
292 sub run_task {
293 my ($self, $task) = @_;
294  
295 $self->_run_rules_cond_root($task);
296  
297 return;
298 }
299  
300 =back
301  
302 =head1 CHANGES
303  
304 =head2 Version 0.xx
305  
306 This is a private module.
307  
308 =cut
309  
310 1;