BuildOptions.pm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # Copyright © 2007 Frank Lichtenheld <djpig@debian.org>
  2. # Copyright © 2008, 2012-2015 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.01';
  21. use Dpkg::Gettext;
  22. use Dpkg::ErrorHandling;
  23. use Dpkg::BuildEnv;
  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 FUNCTIONS
  32. =over 4
  33. =item my $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::BuildEnv::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 $string = $bo->output($fh)
  117. Return a string representation of the build options suitable to be
  118. assigned to an environment variable. Can optionnaly output that string to
  119. the given filehandle.
  120. =cut
  121. sub output {
  122. my ($self, $fh) = @_;
  123. my $o = $self->{options};
  124. my $res = join(' ', map { defined($o->{$_}) ? $_ . '=' . $o->{$_} : $_ } sort keys %$o);
  125. print { $fh } $res if defined $fh;
  126. return $res;
  127. }
  128. =item $bo->export([$var])
  129. Export the build options to the given environment variable. If omitted,
  130. the environment variable defined at creation time is assumed. The value
  131. set to the variable is also returned.
  132. =cut
  133. sub export {
  134. my ($self, $var) = @_;
  135. $var //= $self->{envvar};
  136. my $content = $self->output();
  137. Dpkg::BuildEnv::set($var, $content);
  138. return $content;
  139. }
  140. =back
  141. =head1 CHANGES
  142. =head2 Version 1.01
  143. Enable to use another environment variable instead of DEB_BUILD_OPTIONS.
  144. Thus add support for the "envvar" option at creation time.
  145. =head2 Version 1.00
  146. Mark the module as public.
  147. =head1 AUTHOR
  148. Raphaël Hertzog <hertzog@debian.org>
  149. =cut
  150. 1;