Types.pm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. package Dpkg::Build::Types;
  17. use strict;
  18. use warnings;
  19. our $VERSION = '0.02';
  20. our @EXPORT = qw(
  21. BUILD_DEFAULT
  22. BUILD_SOURCE
  23. BUILD_ARCH_DEP
  24. BUILD_ARCH_INDEP
  25. BUILD_BINARY
  26. BUILD_FULL
  27. build_has_any
  28. build_has_all
  29. build_has_none
  30. build_is
  31. set_build_type
  32. set_build_type_from_options
  33. get_build_options_from_type
  34. );
  35. use Exporter qw(import);
  36. use Dpkg::Gettext;
  37. use Dpkg::ErrorHandling;
  38. =encoding utf8
  39. =head1 NAME
  40. Dpkg::Build::Types - track build types
  41. =head1 DESCRIPTION
  42. The Dpkg::Build::Types module is used by various tools to track and decide
  43. what artifacts need to be built.
  44. The build types are bit constants that are exported by default. Multiple
  45. types can be ORed.
  46. =head1 CONSTANTS
  47. =over 4
  48. =item BUILD_DEFAULT
  49. This build is the default.
  50. =item BUILD_SOURCE
  51. This build includes source artifacts.
  52. =item BUILD_ARCH_DEP
  53. This build includes architecture dependent binary artifacts.
  54. =item BUILD_ARCH_INDEP
  55. This build includes architecture independent binary artifacts.
  56. =item BUILD_BINARY
  57. This build includes binary artifacts.
  58. =item BUILD_FULL
  59. This build includes source and binary artifacts.
  60. =cut
  61. # Simple types.
  62. use constant {
  63. BUILD_DEFAULT => 1,
  64. BUILD_SOURCE => 2,
  65. BUILD_ARCH_DEP => 4,
  66. BUILD_ARCH_INDEP => 8,
  67. };
  68. # Composed types.
  69. use constant BUILD_BINARY => BUILD_ARCH_DEP | BUILD_ARCH_INDEP;
  70. use constant BUILD_FULL => BUILD_BINARY | BUILD_SOURCE;
  71. my $current_type = BUILD_FULL | BUILD_DEFAULT;
  72. my $current_option = undef;
  73. my @build_types = qw(full source binary any all);
  74. my %build_types = (
  75. full => BUILD_FULL,
  76. source => BUILD_SOURCE,
  77. binary => BUILD_BINARY,
  78. any => BUILD_ARCH_DEP,
  79. all => BUILD_ARCH_INDEP,
  80. );
  81. =back
  82. =head1 FUNCTIONS
  83. =over 4
  84. =item build_has_any($bits)
  85. Return a boolean indicating whether the current build type has any of the
  86. specified $bits.
  87. =cut
  88. sub build_has_any
  89. {
  90. my ($bits) = @_;
  91. return $current_type & $bits;
  92. }
  93. =item build_has_all($bits)
  94. Return a boolean indicating whether the current build type has all the
  95. specified $bits.
  96. =cut
  97. sub build_has_all
  98. {
  99. my ($bits) = @_;
  100. return ($current_type & $bits) == $bits;
  101. }
  102. =item build_has_none($bits)
  103. Return a boolean indicating whether the current build type has none of the
  104. specified $bits.
  105. =cut
  106. sub build_has_none
  107. {
  108. my ($bits) = @_;
  109. return !($current_type & $bits);
  110. }
  111. =item build_is($bits)
  112. Return a boolean indicating whether the current build type is the specified
  113. set of $bits.
  114. =cut
  115. sub build_is
  116. {
  117. my ($bits) = @_;
  118. return $current_type == $bits;
  119. }
  120. =item set_build_type($build_type, $build_option, %opts)
  121. Set the current build type to $build_type, which was specified via the
  122. $build_option command-line option.
  123. The function will check and abort on incompatible build type assignments,
  124. this behavior can be disabled by using the boolean option "nocheck".
  125. =cut
  126. sub set_build_type
  127. {
  128. my ($build_type, $build_option, %opts) = @_;
  129. usageerr(g_('cannot combine %s and %s'), $current_option, $build_option)
  130. if not $opts{nocheck} and
  131. build_has_none(BUILD_DEFAULT) and $current_type != $build_type;
  132. $current_type = $build_type;
  133. $current_option = $build_option;
  134. }
  135. =item set_build_type_from_options($build_type, $build_option, %opts)
  136. Set the current build type from a list of build type components.
  137. The function will check and abort on incompatible build type assignments,
  138. this behavior can be disabled by using the boolean option "nocheck".
  139. =cut
  140. sub set_build_type_from_options
  141. {
  142. my ($build_parts, $build_option, %opts) = @_;
  143. my $build_type = 0;
  144. foreach my $type (split /,/, $build_parts) {
  145. usageerr(g_('unknown build type %s'), $type)
  146. unless exists $build_types{$type};
  147. $build_type |= $build_types{$type};
  148. }
  149. set_build_type($build_type, $build_option, %opts);
  150. }
  151. =item get_build_options_from_type()
  152. Get the current build type as a set of comma-separated string options.
  153. =cut
  154. sub get_build_options_from_type
  155. {
  156. my $local_type = $current_type;
  157. my @parts;
  158. foreach my $type (@build_types) {
  159. my $part_bits = $build_types{$type};
  160. if (($local_type & $part_bits) == $part_bits) {
  161. push @parts, $type;
  162. $local_type &= ~$part_bits;
  163. }
  164. }
  165. return join ',', @parts;
  166. }
  167. =back
  168. =head1 CHANGES
  169. =head2 Version 0.xx
  170. This is a private module.
  171. =cut
  172. 1;