test: Move test_data_file() to test.h
[dpkg.git] / scripts / Dpkg / BuildTypes.pm
blob7614ee6a5c168ad608623e26f761161c419259d7
1 # Copyright © 2007 Frank Lichtenheld <djpig@debian.org>
2 # Copyright © 2010, 2013-2016 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
17 =encoding utf8
19 =head1 NAME
21 Dpkg::BuildTypes - track build types
23 =head1 DESCRIPTION
25 The Dpkg::BuildTypes module is used by various tools to track and decide
26 what artifacts need to be built.
28 The build types are bit constants that are exported by default. Multiple
29 types can be ORed.
31 B<Note>: This is a private module, its API can change at any time.
33 =cut
35 package Dpkg::BuildTypes 0.02;
37 use strict;
38 use warnings;
40 our @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
57 use Exporter qw(import);
59 use Dpkg::Gettext;
60 use Dpkg::ErrorHandling;
62 =head1 CONSTANTS
64 =over 4
66 =item BUILD_DEFAULT
68 This build is the default.
70 =item BUILD_SOURCE
72 This build includes source artifacts.
74 =item BUILD_ARCH_DEP
76 This build includes architecture dependent binary artifacts.
78 =item BUILD_ARCH_INDEP
80 This build includes architecture independent binary artifacts.
82 =item BUILD_BINARY
84 This build includes binary artifacts.
86 =item BUILD_FULL
88 This build includes source and binary artifacts.
90 =cut
92 # Simple types.
93 use constant {
94 BUILD_DEFAULT => 1,
95 BUILD_SOURCE => 2,
96 BUILD_ARCH_DEP => 4,
97 BUILD_ARCH_INDEP => 8,
100 # Composed types.
101 use constant BUILD_BINARY => BUILD_ARCH_DEP | BUILD_ARCH_INDEP;
102 use constant BUILD_FULL => BUILD_BINARY | BUILD_SOURCE;
104 my $current_type = BUILD_FULL | BUILD_DEFAULT;
105 my $current_option = undef;
107 my @build_types = qw(full source binary any all);
108 my %build_types = (
109 full => BUILD_FULL,
110 source => BUILD_SOURCE,
111 binary => BUILD_BINARY,
112 any => BUILD_ARCH_DEP,
113 all => BUILD_ARCH_INDEP,
115 my %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,
125 =back
127 =head1 FUNCTIONS
129 =over 4
131 =item build_has_any($bits)
133 Return a boolean indicating whether the current build type has any of the
134 specified $bits.
136 =cut
138 sub build_has_any
140 my ($bits) = @_;
142 return $current_type & $bits;
145 =item build_has_all($bits)
147 Return a boolean indicating whether the current build type has all the
148 specified $bits.
150 =cut
152 sub build_has_all
154 my ($bits) = @_;
156 return ($current_type & $bits) == $bits;
159 =item build_has_none($bits)
161 Return a boolean indicating whether the current build type has none of the
162 specified $bits.
164 =cut
166 sub build_has_none
168 my ($bits) = @_;
170 return !($current_type & $bits);
173 =item build_is($bits)
175 Return a boolean indicating whether the current build type is the specified
176 set of $bits.
178 =cut
180 sub build_is
182 my ($bits) = @_;
184 return $current_type == $bits;
187 =item set_build_type($build_type, $build_option, %opts)
189 Set the current build type to $build_type, which was specified via the
190 $build_option command-line option.
192 The function will check and abort on incompatible build type assignments,
193 this behavior can be disabled by using the boolean option "nocheck".
195 =cut
197 sub set_build_type
199 my ($build_type, $build_option, %opts) = @_;
201 usageerr(g_('cannot combine %s and %s'), $current_option, $build_option)
202 if not $opts{nocheck} and
203 build_has_none(BUILD_DEFAULT) and $current_type != $build_type;
205 $current_type = $build_type;
206 $current_option = $build_option;
209 =item set_build_type_from_options($build_types, $build_option, %opts)
211 Set the current build type from a list of comma-separated build type
212 components.
214 The function will check and abort on incompatible build type assignments,
215 this behavior can be disabled by using the boolean option "nocheck".
217 =cut
219 sub set_build_type_from_options
221 my ($build_parts, $build_option, %opts) = @_;
223 my $build_type = 0;
224 foreach my $type (split /,/, $build_parts) {
225 usageerr(g_('unknown build type %s'), $type)
226 unless exists $build_types{$type};
227 $build_type |= $build_types{$type};
230 set_build_type($build_type, $build_option, %opts);
233 =item set_build_type_from_targets($build_targets, $build_option, %opts)
235 Set the current build type from a list of comma-separated build target
236 components.
238 The function will check and abort on incompatible build type assignments,
239 this behavior can be disabled by using the boolean option "nocheck".
241 =cut
243 sub set_build_type_from_targets
245 my ($build_targets, $build_option, %opts) = @_;
247 my $build_type = 0;
248 foreach my $target (split /,/, $build_targets) {
249 $build_type |= $build_targets{$target} // BUILD_BINARY;
252 set_build_type($build_type, $build_option, %opts);
255 =item get_build_options_from_type()
257 Get the current build type as a set of comma-separated string options.
259 =cut
261 sub get_build_options_from_type
263 my $local_type = $current_type;
265 my @parts;
266 foreach my $type (@build_types) {
267 my $part_bits = $build_types{$type};
268 if (($local_type & $part_bits) == $part_bits) {
269 push @parts, $type;
270 $local_type &= ~$part_bits;
274 return join ',', @parts;
277 =back
279 =head1 CHANGES
281 =head2 Version 0.xx
283 This is a private module.
285 =cut