summaryrefslogtreecommitdiffstats
path: root/scripts/Dpkg/BuildDriver/DebianRules.pm
blob: ffc198796dd438c6a0a76360e93ced72ae454562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# Copyright © 2020-2024 Guillem Jover <guillem@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

=encoding utf8

=head1 NAME

Dpkg::BuildDriver::DebianRules - build a Debian package using debian/rules

=head1 DESCRIPTION

This class is used by dpkg-buildpackage to drive the build of a Debian
package, using F<debian/rules>.

B<Note>: This is a private module, its API can change at any time.

=cut

package Dpkg::BuildDriver::DebianRules 0.01;

use strict;
use warnings;

use Dpkg ();
use Dpkg::Gettext;
use Dpkg::ErrorHandling;
use Dpkg::Path qw(find_command);
use Dpkg::BuildTypes;
use Dpkg::BuildAPI qw(get_build_api);

=head1 METHODS

=over 4

=item $bd = Dpkg::BuildDriver::DebianRules->new(%opts)

Create a new Dpkg::BuildDriver::DebianRules object.

Supports or requires the same Dpkg::BuildDriver->new() options.

=cut

sub new {
    my ($this, %opts) = @_;
    my $class = ref($this) || $this;

    my $self = {
        ctrl => $opts{ctrl},
        root_cmd => $opts{root_cmd},
        as_root => $opts{as_root},
        debian_rules => $opts{debian_rules},
        rrr_override => $opts{rrr_override},
    };
    bless $self, $class;

    my $rrr = $self->_parse_rules_requires_root();

    $self->{rules_requires_root} = $rrr;

    return $self;
}

sub _setup_rootcommand {
    my $self = shift;

    if ($< == 0) {
        warning(g_('using a gain-root-command while being root'))
            if @{$self->{root_cmd}};
    } else {
        push @{$self->{root_cmd}}, 'fakeroot'
            unless @{$self->{root_cmd}};
    }

    if (@{$self->{root_cmd}} && ! find_command($self->{root_cmd}[0])) {
        if ($self->{root_cmd}[0] eq 'fakeroot' && $< != 0) {
            error(g_("fakeroot not found, either install the fakeroot\n" .
                     'package, specify a command with the -r option, ' .
                     'or run this as root'));
        } else {
            error(g_("gain-root-command '%s' not found"), $self->{root_cmd}[0]);
        }
    }
}

my %target_build = map { $_ => 1 } qw(
    build
    build-arch
    build-indep
);
my %target_legacy_root = map { $_ => 1 } qw(
    clean
    binary
    binary-arch
    binary-indep
);
my %target_official =  map { $_ => 1 } qw(
    clean
    build
    build-arch
    build-indep
    binary
    binary-arch
    binary-indep
);

# Check whether we are doing some kind of rootless build, and sanity check
# the fields values.
sub _parse_rules_requires_root {
    my $self = shift;

    my %rrr;
    my $rrr;
    my $rrr_default;
    my $keywords_base;
    my $keywords_impl;

    if (get_build_api($self->{ctrl}) >= 1) {
        $rrr_default = 'no';
    } else {
        $rrr_default = 'binary-targets';
    }

    my $ctrl_src = $self->{ctrl}->get_source();
    $rrr = $self->{rrr_override} // $ctrl_src->{'Rules-Requires-Root'} // $rrr_default;

    foreach my $keyword (split ' ', $rrr) {
        if ($keyword =~ m{/}) {
            if ($keyword =~ m{^dpkg/target/(.*)$}p and $target_official{$1}) {
                error(g_('disallowed target in %s field keyword %s'),
                      'Rules-Requires-Root', $keyword);
            } elsif ($keyword ne 'dpkg/target-subcommand') {
                error(g_('%s field keyword "%s" is unknown in dpkg namespace'),
                      'Rules-Requires-Root', $keyword);
            }
            $keywords_impl++;
        } else {
            if ($keyword ne lc $keyword and
                (lc $keyword eq 'no' or lc $keyword eq 'binary-targets')) {
                error(g_('%s field keyword "%s" is uppercase; use "%s" instead'),
                      'Rules-Requires-Root', $keyword, lc $keyword);
            } elsif (lc $keyword eq 'yes') {
                error(g_('%s field keyword "%s" is invalid; use "%s" instead'),
                      'Rules-Requires-Root', $keyword, 'binary-targets');
            } elsif ($keyword ne 'no' and $keyword ne 'binary-targets') {
                warning(g_('%s field keyword "%s" is unknown'),
                        'Rules-Requires-Root', $keyword);
            }
            $keywords_base++;
        }

        if ($rrr{$keyword}++) {
            error(g_('field %s contains duplicate keyword %s'),
                  'Rules-Requires-Root', $keyword);
        }
    }

    if ($self->{as_root} || ! exists $rrr{no}) {
        $self->_setup_rootcommand();
    }

    # Notify the children we do support R³.
    $ENV{DEB_RULES_REQUIRES_ROOT} = join ' ', sort keys %rrr;

    if ($keywords_base > 1 or $keywords_base and $keywords_impl) {
        error(g_('%s field contains both global and implementation specific keywords'),
              'Rules-Requires-Root');
    } elsif ($keywords_impl) {
        # Set only on <implementations-keywords>.
        $ENV{DEB_GAIN_ROOT_CMD} = join ' ', @{$self->{root_cmd}};
    } else {
        # We should not provide the variable otherwise.
        delete $ENV{DEB_GAIN_ROOT_CMD};
    }

    return \%rrr;
}

sub _rules_requires_root {
    my ($self, $target) = @_;

    return 1 if $self->{as_root};
    return 1 if $self->{rules_requires_root}{"dpkg/target/$target"};
    return 1 if $self->{rules_requires_root}{'binary-targets'} and $target_legacy_root{$target};
    return 0;
}

sub _run_cmd {
    my @cmd = @_;
    printcmd(@cmd);
    system @cmd and subprocerr("@cmd");
}

sub _run_rules_cond_root {
    my ($self, $target) = @_;

    my @cmd;
    push @cmd, @{$self->{root_cmd}} if $self->_rules_requires_root($target);
    push @cmd, @{$self->{debian_rules}}, $target;

    _run_cmd(@cmd);
}

=item $bd->pre_check()

Perform build driver specific checks, before anything else.

This checks whether the F<debian/rules> file is executable,
and if not then make it so.

=cut

sub pre_check {
    my $self = shift;

    if (@{$self->{debian_rules}} == 1 && ! -x $self->{debian_rules}[0]) {
        warning(g_('%s is not executable; fixing that'),
                $self->{debian_rules}[0]);
        # No checks of failures, non fatal.
        chmod 0755, $self->{debian_rules}[0];
    }
}

=item $bool = $bd->need_build_task($build_task, $binary_task)

Returns whether we need to use the build task.

B<Note>: This method is needed as long as we support building as root-like.
Once that is not needed this method will be deprecated.

=cut

sub need_build_task {
    my ($self, $build_task, $binary_task) = @_;

    # If we are building rootless, there is no need to call the build target
    # independently as non-root.
    return 0 if not $self->_rules_requires_root($binary_task);
    return 1;
}

=item $bd->run_build_task($build_task, $binary_task)

Executes the build task for the build.

B<Note>: This method is needed as long as we support building as root-like.
Once that is not needed this method will be deprecated.

=cut

sub run_build_task {
    my ($self, $build_task, $binary_task) = @_;

    # If we are building rootless, there is no need to call the build
    # target independently as non-root.
    if ($self->_rules_requires_root($binary_task)) {
        _run_cmd(@{$self->{debian_rules}}, $build_task)
    }

    return;
}

=item $bd->run_task($task)

Executes the given task for the build.

=cut

sub run_task {
    my ($self, $task) = @_;

    $self->_run_rules_cond_root($task);

    return;
}

=back

=head1 CHANGES

=head2 Version 0.xx

This is a private module.

=cut

1;