File Coverage

File:Dpkg/Build/Types.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
17package Dpkg::Build::Types;
18
19
7
7
7
15
7
78
use strict;
20
7
7
7
7
7
239
use warnings;
21
22our $VERSION = '0.02';
23our @EXPORT = qw(
24    BUILD_DEFAULT
25    BUILD_SOURCE
26    BUILD_ARCH_DEP
27    BUILD_ARCH_INDEP
28    BUILD_BINARY
29    BUILD_FULL
30    build_has_any
31    build_has_all
32    build_has_none
33    build_is
34    set_build_type
35    set_build_type_from_options
36    set_build_type_from_targets
37    get_build_options_from_type
38);
39
40
7
7
7
8
7
79
use Exporter qw(import);
41
42
7
7
7
122
1
149
use Dpkg::Gettext;
43
7
7
7
132
0
223
use Dpkg::ErrorHandling;
44
45=encoding utf8
46
47 - 87
=head1 NAME

Dpkg::Build::Types - track build types

=head1 DESCRIPTION

The Dpkg::Build::Types 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.

=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
88
89# Simple types.
90use constant {
91
7
236
    BUILD_DEFAULT      => 1,
92    BUILD_SOURCE       => 2,
93    BUILD_ARCH_DEP     => 4,
94    BUILD_ARCH_INDEP   => 8,
95
7
7
8
7
};
96
97# Composed types.
98
7
7
7
14
6
115
use constant BUILD_BINARY => BUILD_ARCH_DEP | BUILD_ARCH_INDEP;
99
7
7
7
7
7
1691
use constant BUILD_FULL   => BUILD_BINARY | BUILD_SOURCE;
100
101my $current_type = BUILD_FULL | BUILD_DEFAULT;
102my $current_option = undef;
103
104my @build_types = qw(full source binary any all);
105my %build_types = (
106    full => BUILD_FULL,
107    source => BUILD_SOURCE,
108    binary => BUILD_BINARY,
109    any => BUILD_ARCH_DEP,
110    all => BUILD_ARCH_INDEP,
111);
112my %build_targets = (
113    'clean' => BUILD_SOURCE,
114    'build' => BUILD_BINARY,
115    'build-arch' => BUILD_ARCH_DEP,
116    'build-indep' => BUILD_ARCH_INDEP,
117    'binary' => BUILD_BINARY,
118    'binary-arch' => BUILD_ARCH_DEP,
119    'binary-indep' => BUILD_ARCH_INDEP,
120);
121
122=back
123
124 - 133
=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
134
135sub build_has_any
136{
137
7
1
5
    my ($bits) = @_;
138
139
7
9
    return $current_type & $bits;
140}
141
142 - 147
=item build_has_all($bits)

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

=cut
148
149sub build_has_all
150{
151
38
1
342
    my ($bits) = @_;
152
153
38
67
    return ($current_type & $bits) == $bits;
154}
155
156 - 161
=item build_has_none($bits)

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

=cut
162
163sub build_has_none
164{
165
7
1
6
    my ($bits) = @_;
166
167
7
12
    return !($current_type & $bits);
168}
169
170 - 175
=item build_is($bits)

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

=cut
176
177sub build_is
178{
179
11
1
8
    my ($bits) = @_;
180
181
11
15
    return $current_type == $bits;
182}
183
184 - 192
=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
193
194sub set_build_type
195{
196
30
1
103
    my ($build_type, $build_option, %opts) = @_;
197
198    usageerr(g_('cannot combine %s and %s'), $current_option, $build_option)
199        if not $opts{nocheck} and
200
30
102
           build_has_none(BUILD_DEFAULT) and $current_type != $build_type;
201
202
30
45
    $current_type = $build_type;
203
30
43
    $current_option = $build_option;
204}
205
206 - 214
=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
215
216sub set_build_type_from_options
217{
218
3
1
4
    my ($build_parts, $build_option, %opts) = @_;
219
220
3
2
    my $build_type = 0;
221
3
5
    foreach my $type (split /,/, $build_parts) {
222        usageerr(g_('unknown build type %s'), $type)
223
5
5
            unless exists $build_types{$type};
224
5
4
        $build_type |= $build_types{$type};
225    }
226
227
3
4
    set_build_type($build_type, $build_option, %opts);
228}
229
230 - 238
=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
239
240sub set_build_type_from_targets
241{
242
3
1
3
    my ($build_targets, $build_option, %opts) = @_;
243
244
3
3
    my $build_type = 0;
245
3
3
    foreach my $target (split /,/, $build_targets) {
246
5
7
        $build_type |= $build_targets{$target} // BUILD_BINARY;
247    }
248
249
3
3
    set_build_type($build_type, $build_option, %opts);
250}
251
252 - 256
=item get_build_options_from_type()

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

=cut
257
258sub get_build_options_from_type
259{
260
27
1
17
    my $local_type = $current_type;
261
262
27
26
    my @parts;
263
27
42
    foreach my $type (@build_types) {
264
135
137
        my $part_bits = $build_types{$type};
265
135
151
        if (($local_type & $part_bits) == $part_bits) {
266
28
79
            push @parts, $type;
267
28
43
            $local_type &= ~$part_bits;
268        }
269    }
270
271
27
52
    return join ',', @parts;
272}
273
274=back
275
276 - 282
=head1 CHANGES

=head2 Version 0.xx

This is a private module.

=cut
283
2841;