File Coverage

File:Dpkg/BuildAPI.pm
Coverage:91.8%

linestmtbrancondsubpodtimecode
1# Copyright © 2020-2022 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 - 29
=head1 NAME

Dpkg::BuildAPI - handle build API versions

=head1 DESCRIPTION

The Dpkg::BuildAPI module provides functions to fetch the current dpkg
build API level.

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

=cut
30
31package Dpkg::BuildAPI 0.01;
32
33
9
9
9
34
7
218
use strict;
34
9
9
9
26
19
380
use warnings;
35
36our @EXPORT_OK = qw(
37    get_build_api
38    reset_build_api
39);
40
41
9
9
9
20
9
156
use Exporter qw(import);
42
43
9
9
9
20
53
322
use Dpkg::Gettext;
44
9
9
9
17
48
352
use Dpkg::ErrorHandling;
45
9
9
9
73
56
115
use Dpkg::BuildEnv;
46
9
9
9
5838
17
630
use Dpkg::Version;
47
9
9
9
7472
11
430
use Dpkg::Deps;
48
49use constant {
50
9
2218
    DEFAULT_BUILD_API => '0',
51    MAX_BUILD_API => '1',
52
9
9
33
8
};
53
54my $build_api;
55
56 - 67
=head1 FUNCTIONS

=over 4

=item $level = get_build_api([$ctrl])

Get the build API level, from the environment variable B<DPKG_BUILD_API>,
or if not defined and a $ctrl L<Dpkg::Control::Info> object passed as an
argument, from its build dependency fields. If no $ctrl object gets passed
the previous value obtained is returned.

=cut
68
69sub get_build_api {
70
33
1
41
    my $ctrl = shift;
71
72
33
87
    return $build_api if defined $build_api && ! defined $ctrl;
73
74
33
75
    if (Dpkg::BuildEnv::has('DPKG_BUILD_API')) {
75
6
11
        $build_api = Dpkg::BuildEnv::get('DPKG_BUILD_API');
76    } elsif (defined $ctrl) {
77
21
40
        my $src = $ctrl->get_source();
78        my @dep_list = deps_concat(map {
79
21
63
22
56
            $src->{$_ }
80        } qw(Build-Depends Build-Depends-Indep Build-Depends-Arch));
81
82
21
37
        my $deps = deps_parse(@dep_list,
83            build_dep => 1,
84            reduce_restrictions => 1,
85        );
86
87
21
24
        if (not defined $deps) {
88
0
0
            $build_api = DEFAULT_BUILD_API;
89
0
0
            return $build_api;
90        }
91
92        deps_iterate($deps, sub {
93
21
17
            my $dep = shift;
94
95
21
27
            return 1 if $dep->{package} ne 'dpkg-build-api';
96
97
21
67
            if (! defined $dep->{relation} || $dep->{relation} ne REL_EQ) {
98
6
10
                error(g_('dpkg build API level needs an exact version'));
99            }
100
101
15
87
            if (defined $build_api and $build_api ne $dep->{version}) {
102                error(g_('dpkg build API level with conflicting versions: %s vs %s'),
103
3
92
                      $build_api, $dep->{version});
104            }
105
106
12
13
            $build_api = $dep->{version};
107
108
12
23
            return 1;
109
21
113
        });
110    }
111
112
24
103
    $build_api //= DEFAULT_BUILD_API;
113
114
24
69
    if ($build_api !~ m/^[0-9]+$/) {
115
6
11
        error(g_("invalid dpkg build API level '%s'"), $build_api);
116    }
117
118
18
40
    if ($build_api > MAX_BUILD_API) {
119
6
12
        error(g_("dpkg build API level '%s' greater than max '%s'"),
120              $build_api, MAX_BUILD_API);
121    }
122
123
12
30
    return $build_api;
124}
125
126 - 130
=item reset_build_api()

Reset the cached build API level.

=cut
131
132sub reset_build_api {
133
27
1
46
    $build_api = undef;
134}
135
136=back
137
138 - 144
=head1 CHANGES

=head2 Version 0.xx

This is a private module.

=cut
145
1461;