File Coverage

File:Dpkg/BuildTypes.pm
Coverage:93.5%

linestmtbrancondsubpodtimecode
1# Copyright © 2007 Frank Lichtenheld <djpig@debian.org>
2# Copyright © 2010, 2013-2016 Guillem Jover <guillem@debian.org>
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17=encoding utf8
18
19 - 33
=head1 NAME

Dpkg::BuildTypes - track build types

=head1 DESCRIPTION

The Dpkg::BuildTypes module is used by various tools to track and decide
what artifacts need to be built.

The build types are bit constants that are exported by default. Multiple
types can be ORed.

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

=cut
34
35package Dpkg::BuildTypes 0.02;
36
37
21
21
21
52
16
296
use strict;
38
21
21
21
28
15
775
use warnings;
39
40our @EXPORT = qw(
41    BUILD_DEFAULT
42    BUILD_SOURCE
43    BUILD_ARCH_DEP
44    BUILD_ARCH_INDEP
45    BUILD_BINARY
46    BUILD_FULL
47    build_has_any
48    build_has_all
49    build_has_none
50    build_is
51    set_build_type
52    set_build_type_from_options
53    set_build_type_from_targets
54    get_build_options_from_type
55);
56
57
21
21
21
44
8
255
use Exporter qw(import);
58
59
21
21
21
463
4
570
use Dpkg::Gettext;
60
21
21
21
552
34
905
use Dpkg::ErrorHandling;
61
62 - 90
=head1 CONSTANTS

=over 4

=item BUILD_DEFAULT

This build is the default.

=item BUILD_SOURCE

This build includes source artifacts.

=item BUILD_ARCH_DEP

This build includes architecture dependent binary artifacts.

=item BUILD_ARCH_INDEP

This build includes architecture independent binary artifacts.

=item BUILD_BINARY

This build includes binary artifacts.

=item BUILD_FULL

This build includes source and binary artifacts.

=cut
91
92# Simple types.
93use constant {
94
21
952
    BUILD_DEFAULT      => 1,
95    BUILD_SOURCE       => 2,
96    BUILD_ARCH_DEP     => 4,
97    BUILD_ARCH_INDEP   => 8,
98
21
21
45
15
};
99
100# Composed types.
101
21
21
21
43
19
482
use constant BUILD_BINARY => BUILD_ARCH_DEP | BUILD_ARCH_INDEP;
102
21
21
21
35
15
6051
use constant BUILD_FULL   => BUILD_BINARY | BUILD_SOURCE;
103
104my $current_type = BUILD_FULL | BUILD_DEFAULT;
105my $current_option = undef;
106
107my @build_types = qw(full source binary any all);
108my %build_types = (
109    full => BUILD_FULL,
110    source => BUILD_SOURCE,
111    binary => BUILD_BINARY,
112    any => BUILD_ARCH_DEP,
113    all => BUILD_ARCH_INDEP,
114);
115my %build_targets = (
116    'clean' => BUILD_SOURCE,
117    'build' => BUILD_BINARY,
118    'build-arch' => BUILD_ARCH_DEP,
119    'build-indep' => BUILD_ARCH_INDEP,
120    'binary' => BUILD_BINARY,
121    'binary-arch' => BUILD_ARCH_DEP,
122    'binary-indep' => BUILD_ARCH_INDEP,
123);
124
125=back
126
127 - 136
=head1 FUNCTIONS

=over 4

=item build_has_any($bits)

Return a boolean indicating whether the current build type has any of the
specified $bits.

=cut
137
138sub build_has_any
139{
140
21
1
28
    my ($bits) = @_;
141
142
21
47
    return $current_type & $bits;
143}
144
145 - 150
=item build_has_all($bits)

Return a boolean indicating whether the current build type has all the
specified $bits.

=cut
151
152sub build_has_all
153{
154
114
1
159
    my ($bits) = @_;
155
156
114
323
    return ($current_type & $bits) == $bits;
157}
158
159 - 164
=item build_has_none($bits)

Return a boolean indicating whether the current build type has none of the
specified $bits.

=cut
165
166sub build_has_none
167{
168
21
1
26
    my ($bits) = @_;
169
170
21
68
    return !($current_type & $bits);
171}
172
173 - 178
=item build_is($bits)

Return a boolean indicating whether the current build type is the specified
set of $bits.

=cut
179
180sub build_is
181{
182
33
1
41
    my ($bits) = @_;
183
184
33
71
    return $current_type == $bits;
185}
186
187 - 195
=item set_build_type($build_type, $build_option, %opts)

Set the current build type to $build_type, which was specified via the
$build_option command-line option.

The function will check and abort on incompatible build type assignments,
this behavior can be disabled by using the boolean option "nocheck".

=cut
196
197sub set_build_type
198{
199
90
1
196
    my ($build_type, $build_option, %opts) = @_;
200
201    usageerr(g_('cannot combine %s and %s'), $current_option, $build_option)
202        if not $opts{nocheck} and
203
90
232
           build_has_none(BUILD_DEFAULT) and $current_type != $build_type;
204
205
90
97
    $current_type = $build_type;
206
90
139
    $current_option = $build_option;
207}
208
209 - 217
=item set_build_type_from_options($build_types, $build_option, %opts)

Set the current build type from a list of comma-separated build type
components.

The function will check and abort on incompatible build type assignments,
this behavior can be disabled by using the boolean option "nocheck".

=cut
218
219sub set_build_type_from_options
220{
221
9
1
20
    my ($build_parts, $build_option, %opts) = @_;
222
223
9
8
    my $build_type = 0;
224
9
18
    foreach my $type (split /,/, $build_parts) {
225        usageerr(g_('unknown build type %s'), $type)
226
15
32
            unless exists $build_types{$type};
227
15
18
        $build_type |= $build_types{$type};
228    }
229
230
9
17
    set_build_type($build_type, $build_option, %opts);
231}
232
233 - 241
=item set_build_type_from_targets($build_targets, $build_option, %opts)

Set the current build type from a list of comma-separated build target
components.

The function will check and abort on incompatible build type assignments,
this behavior can be disabled by using the boolean option "nocheck".

=cut
242
243sub set_build_type_from_targets
244{
245
9
1
14
    my ($build_targets, $build_option, %opts) = @_;
246
247
9
5
    my $build_type = 0;
248
9
14
    foreach my $target (split /,/, $build_targets) {
249
15
25
        $build_type |= $build_targets{$target} // BUILD_BINARY;
250    }
251
252
9
12
    set_build_type($build_type, $build_option, %opts);
253}
254
255 - 259
=item get_build_options_from_type()

Get the current build type as a set of comma-separated string options.

=cut
260
261sub get_build_options_from_type
262{
263
81
1
83
    my $local_type = $current_type;
264
265
81
66
    my @parts;
266
81
178
    foreach my $type (@build_types) {
267
405
355
        my $part_bits = $build_types{$type};
268
405
408
        if (($local_type & $part_bits) == $part_bits) {
269
84
110
            push @parts, $type;
270
84
102
            $local_type &= ~$part_bits;
271        }
272    }
273
274
81
216
    return join ',', @parts;
275}
276
277=back
278
279 - 285
=head1 CHANGES

=head2 Version 0.xx

This is a private module.

=cut
286
2871;