Types.pm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.01';
  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. );
  33. use Exporter qw(import);
  34. use Dpkg::Gettext;
  35. use Dpkg::ErrorHandling;
  36. =encoding utf8
  37. =head1 NAME
  38. Dpkg::Build::Types - track build types
  39. =head1 DESCRIPTION
  40. The Dpkg::Build::Types module is used by various tools to track and decide
  41. what artifacts need to be built.
  42. The build types are bit constants that are exported by default. Multiple
  43. types can be ORed.
  44. =head1 CONSTANTS
  45. =over 4
  46. =item BUILD_DEFAULT
  47. This build is the default.
  48. =item BUILD_SOURCE
  49. This build includes source artifacts.
  50. =item BUILD_ARCH_DEP
  51. This build includes architecture dependent binary artifacts.
  52. =item BUILD_ARCH_INDEP
  53. This build includes architecture independent binary artifacts.
  54. =item BUILD_BINARY
  55. This build includes binary artifacts.
  56. =item BUILD_FULL
  57. This build includes source and binary artifacts.
  58. =cut
  59. # Simple types.
  60. use constant {
  61. BUILD_DEFAULT => 1,
  62. BUILD_SOURCE => 2,
  63. BUILD_ARCH_DEP => 4,
  64. BUILD_ARCH_INDEP => 8,
  65. };
  66. # Composed types.
  67. use constant BUILD_BINARY => BUILD_ARCH_DEP | BUILD_ARCH_INDEP;
  68. use constant BUILD_FULL => BUILD_BINARY | BUILD_SOURCE;
  69. my $current_type = BUILD_FULL | BUILD_DEFAULT;
  70. my $current_option = undef;
  71. =back
  72. =head1 FUNCTIONS
  73. =over 4
  74. =item build_has_any($bits)
  75. Return a boolean indicating whether the current build type has any of the
  76. specified $bits.
  77. =cut
  78. sub build_has_any
  79. {
  80. my ($bits) = @_;
  81. return $current_type & $bits;
  82. }
  83. =item build_has_all($bits)
  84. Return a boolean indicating whether the current build type has all the
  85. specified $bits.
  86. =cut
  87. sub build_has_all
  88. {
  89. my ($bits) = @_;
  90. return ($current_type & $bits) == $bits;
  91. }
  92. =item build_has_none($bits)
  93. Return a boolean indicating whether the current build type has none of the
  94. specified $bits.
  95. =cut
  96. sub build_has_none
  97. {
  98. my ($bits) = @_;
  99. return !($current_type & $bits);
  100. }
  101. =item build_is($bits)
  102. Return a boolean indicating whether the current build type is the specified
  103. set of $bits.
  104. =cut
  105. sub build_is
  106. {
  107. my ($bits) = @_;
  108. return $current_type == $bits;
  109. }
  110. =item set_build_type($build_type, $build_option, %opts)
  111. Set the current build type to $build_type, which was specified via the
  112. $build_option command-line option.
  113. The function will check and abort on incompatible build type assignments,
  114. this behavior can be disabled by using the boolean option "nocheck".
  115. =cut
  116. sub set_build_type
  117. {
  118. my ($build_type, $build_option, %opts) = @_;
  119. usageerr(g_('cannot combine %s and %s'), $current_option, $build_option)
  120. if not $opts{nocheck} and
  121. build_has_none(BUILD_DEFAULT) and $current_type != $build_type;
  122. $current_type = $build_type;
  123. $current_option = $build_option;
  124. }
  125. =back
  126. =head1 CHANGES
  127. =head2 Version 0.xx
  128. This is a private module.
  129. =cut
  130. 1;