dpkg (1.18.25) stretch; urgency=medium
[dpkg] / scripts / Dpkg / Build / Types.pm
CommitLineData
1479465f
GJ
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
19use strict;
20use 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 get_build_options_from_type
37);
38
39use Exporter qw(import);
40
41use Dpkg::Gettext;
42use Dpkg::ErrorHandling;
43
44=encoding utf8
45
46=head1 NAME
47
48Dpkg::Build::Types - track build types
49
50=head1 DESCRIPTION
51
52The Dpkg::Build::Types module is used by various tools to track and decide
53what artifacts need to be built.
54
55The build types are bit constants that are exported by default. Multiple
56types can be ORed.
57
58=head1 CONSTANTS
59
60=over 4
61
62=item BUILD_DEFAULT
63
64This build is the default.
65
66=item BUILD_SOURCE
67
68This build includes source artifacts.
69
70=item BUILD_ARCH_DEP
71
72This build includes architecture dependent binary artifacts.
73
74=item BUILD_ARCH_INDEP
75
76This build includes architecture independent binary artifacts.
77
78=item BUILD_BINARY
79
80This build includes binary artifacts.
81
82=item BUILD_FULL
83
84This build includes source and binary artifacts.
85
86=cut
87
88# Simple types.
89use constant {
90 BUILD_DEFAULT => 1,
91 BUILD_SOURCE => 2,
92 BUILD_ARCH_DEP => 4,
93 BUILD_ARCH_INDEP => 8,
94};
95
96# Composed types.
97use constant BUILD_BINARY => BUILD_ARCH_DEP | BUILD_ARCH_INDEP;
98use constant BUILD_FULL => BUILD_BINARY | BUILD_SOURCE;
99
100my $current_type = BUILD_FULL | BUILD_DEFAULT;
101my $current_option = undef;
102
103my @build_types = qw(full source binary any all);
104my %build_types = (
105 full => BUILD_FULL,
106 source => BUILD_SOURCE,
107 binary => BUILD_BINARY,
108 any => BUILD_ARCH_DEP,
109 all => BUILD_ARCH_INDEP,
110);
111
112=back
113
114=head1 FUNCTIONS
115
116=over 4
117
118=item build_has_any($bits)
119
120Return a boolean indicating whether the current build type has any of the
121specified $bits.
122
123=cut
124
125sub build_has_any
126{
127 my ($bits) = @_;
128
129 return $current_type & $bits;
130}
131
132=item build_has_all($bits)
133
134Return a boolean indicating whether the current build type has all the
135specified $bits.
136
137=cut
138
139sub build_has_all
140{
141 my ($bits) = @_;
142
143 return ($current_type & $bits) == $bits;
144}
145
146=item build_has_none($bits)
147
148Return a boolean indicating whether the current build type has none of the
149specified $bits.
150
151=cut
152
153sub build_has_none
154{
155 my ($bits) = @_;
156
157 return !($current_type & $bits);
158}
159
160=item build_is($bits)
161
162Return a boolean indicating whether the current build type is the specified
163set of $bits.
164
165=cut
166
167sub build_is
168{
169 my ($bits) = @_;
170
171 return $current_type == $bits;
172}
173
174=item set_build_type($build_type, $build_option, %opts)
175
176Set the current build type to $build_type, which was specified via the
177$build_option command-line option.
178
179The function will check and abort on incompatible build type assignments,
180this behavior can be disabled by using the boolean option "nocheck".
181
182=cut
183
184sub set_build_type
185{
186 my ($build_type, $build_option, %opts) = @_;
187
188 usageerr(g_('cannot combine %s and %s'), $current_option, $build_option)
189 if not $opts{nocheck} and
190 build_has_none(BUILD_DEFAULT) and $current_type != $build_type;
191
192 $current_type = $build_type;
193 $current_option = $build_option;
194}
195
196=item set_build_type_from_options($build_type, $build_option, %opts)
197
198Set the current build type from a list of build type components.
199
200The function will check and abort on incompatible build type assignments,
201this behavior can be disabled by using the boolean option "nocheck".
202
203=cut
204
205sub set_build_type_from_options
206{
207 my ($build_parts, $build_option, %opts) = @_;
208
209 my $build_type = 0;
210 foreach my $type (split /,/, $build_parts) {
211 usageerr(g_('unknown build type %s'), $type)
212 unless exists $build_types{$type};
213 $build_type |= $build_types{$type};
214 }
215
216 set_build_type($build_type, $build_option, %opts);
217}
218
219=item get_build_options_from_type()
220
221Get the current build type as a set of comma-separated string options.
222
223=cut
224
225sub get_build_options_from_type
226{
227 my $local_type = $current_type;
228
229 my @parts;
230 foreach my $type (@build_types) {
231 my $part_bits = $build_types{$type};
232 if (($local_type & $part_bits) == $part_bits) {
233 push @parts, $type;
234 $local_type &= ~$part_bits;
235 }
236 }
237
238 return join ',', @parts;
239}
240
241=back
242
243=head1 CHANGES
244
245=head2 Version 0.xx
246
247This is a private module.
248
249=cut
250
2511;