BuildOptions.pm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. # Copyright © 2007 Frank Lichtenheld <djpig@debian.org>
  2. # Copyright © 2008, 2012-2017 Guillem Jover <guillem@debian.org>
  3. # Copyright © 2010 Raphaël Hertzog <hertzog@debian.org>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. package Dpkg::BuildOptions;
  18. use strict;
  19. use warnings;
  20. our $VERSION = '1.02';
  21. use Dpkg::Gettext;
  22. use Dpkg::ErrorHandling;
  23. use Dpkg::Build::Env;
  24. =encoding utf8
  25. =head1 NAME
  26. Dpkg::BuildOptions - parse and update build options
  27. =head1 DESCRIPTION
  28. The Dpkg::BuildOptions object can be used to manipulate options stored
  29. in environment variables like DEB_BUILD_OPTIONS and
  30. DEB_BUILD_MAINT_OPTIONS.
  31. =head1 METHODS
  32. =over 4
  33. =item $bo = Dpkg::BuildOptions->new(%opts)
  34. Create a new Dpkg::BuildOptions object. It will be initialized based
  35. on the value of the environment variable named $opts{envvar} (or
  36. DEB_BUILD_OPTIONS if that option is not set).
  37. =cut
  38. sub new {
  39. my ($this, %opts) = @_;
  40. my $class = ref($this) || $this;
  41. my $self = {
  42. options => {},
  43. source => {},
  44. envvar => $opts{envvar} // 'DEB_BUILD_OPTIONS',
  45. };
  46. bless $self, $class;
  47. $self->merge(Dpkg::Build::Env::get($self->{envvar}), $self->{envvar});
  48. return $self;
  49. }
  50. =item $bo->reset()
  51. Reset the object to not have any option (it's empty).
  52. =cut
  53. sub reset {
  54. my $self = shift;
  55. $self->{options} = {};
  56. $self->{source} = {};
  57. }
  58. =item $bo->merge($content, $source)
  59. Merge the options set in $content and record that they come from the
  60. source $source. $source is mainly used in warning messages currently
  61. to indicate where invalid options have been detected.
  62. $content is a space separated list of options with optional assigned
  63. values like "nocheck parallel=2".
  64. =cut
  65. sub merge {
  66. my ($self, $content, $source) = @_;
  67. return 0 unless defined $content;
  68. my $count = 0;
  69. foreach (split(/\s+/, $content)) {
  70. unless (/^([a-z][a-z0-9_-]*)(?:=(\S*))?$/) {
  71. warning(g_('invalid flag in %s: %s'), $source, $_);
  72. next;
  73. }
  74. $count += $self->set($1, $2, $source);
  75. }
  76. return $count;
  77. }
  78. =item $bo->set($option, $value, [$source])
  79. Store the given option in the object with the given value. It's legitimate
  80. for a value to be undefined if the option is a simple boolean (its
  81. presence means true, its absence means false). The $source is optional
  82. and indicates where the option comes from.
  83. The known options have their values checked for sanity. Options without
  84. values have their value removed and options with invalid values are
  85. discarded.
  86. =cut
  87. sub set {
  88. my ($self, $key, $value, $source) = @_;
  89. # Sanity checks
  90. if ($key =~ /^(noopt|nostrip|nocheck)$/ && defined($value)) {
  91. $value = undef;
  92. } elsif ($key eq 'parallel') {
  93. $value //= '';
  94. return 0 if $value !~ /^\d*$/;
  95. }
  96. $self->{options}{$key} = $value;
  97. $self->{source}{$key} = $source;
  98. return 1;
  99. }
  100. =item $bo->get($option)
  101. Return the value associated to the option. It might be undef even if the
  102. option exists. You might want to check with $bo->has($option) to verify if
  103. the option is stored in the object.
  104. =cut
  105. sub get {
  106. my ($self, $key) = @_;
  107. return $self->{options}{$key};
  108. }
  109. =item $bo->has($option)
  110. Returns a boolean indicating whether the option is stored in the object.
  111. =cut
  112. sub has {
  113. my ($self, $key) = @_;
  114. return exists $self->{options}{$key};
  115. }
  116. =item $bo->parse_features($option, $use_feature)
  117. Parse the $option values, as a set of known features to enable or disable,
  118. as specified in the $use_feature hash reference.
  119. Each feature is prefixed with a ‘B<+>’ or a ‘B<->’ character as a marker
  120. to enable or disable it. The special feature “B<all>” can be used to act
  121. on all known features.
  122. Unknown of malformed features will emit warnings.
  123. =cut
  124. sub parse_features {
  125. my ($self, $option, $use_feature) = @_;
  126. foreach my $feature (split(/,/, $self->get($option) // '')) {
  127. $feature = lc $feature;
  128. if ($feature =~ s/^([+-])//) {
  129. my $value = ($1 eq '+') ? 1 : 0;
  130. if ($feature eq 'all') {
  131. $use_feature->{$_} = $value foreach keys %{$use_feature};
  132. } else {
  133. if (exists $use_feature->{$feature}) {
  134. $use_feature->{$feature} = $value;
  135. } else {
  136. warning(g_('unknown %s feature in %s variable: %s'),
  137. $option, $self->{envvar}, $feature);
  138. }
  139. }
  140. } else {
  141. warning(g_('incorrect value in %s option of %s variable: %s'),
  142. $option, $self->{envvar}, $feature);
  143. }
  144. }
  145. }
  146. =item $string = $bo->output($fh)
  147. Return a string representation of the build options suitable to be
  148. assigned to an environment variable. Can optionally output that string to
  149. the given filehandle.
  150. =cut
  151. sub output {
  152. my ($self, $fh) = @_;
  153. my $o = $self->{options};
  154. my $res = join(' ', map { defined($o->{$_}) ? $_ . '=' . $o->{$_} : $_ } sort keys %$o);
  155. print { $fh } $res if defined $fh;
  156. return $res;
  157. }
  158. =item $bo->export([$var])
  159. Export the build options to the given environment variable. If omitted,
  160. the environment variable defined at creation time is assumed. The value
  161. set to the variable is also returned.
  162. =cut
  163. sub export {
  164. my ($self, $var) = @_;
  165. $var //= $self->{envvar};
  166. my $content = $self->output();
  167. Dpkg::Build::Env::set($var, $content);
  168. return $content;
  169. }
  170. =back
  171. =head1 CHANGES
  172. =head2 Version 1.02 (dpkg 1.18.19)
  173. New method: $bo->parse_features().
  174. =head2 Version 1.01 (dpkg 1.16.1)
  175. Enable to use another environment variable instead of DEB_BUILD_OPTIONS.
  176. Thus add support for the "envvar" option at creation time.
  177. =head2 Version 1.00 (dpkg 1.15.6)
  178. Mark the module as public.
  179. =cut
  180. 1;