BuildOptions.pm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # Copyright © 2007 Frank Lichtenheld <djpig@debian.org>
  2. # Copyright © 2010 Raphaël Hertzog <hertzog@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 <http://www.gnu.org/licenses/>.
  16. package Dpkg::BuildOptions;
  17. use strict;
  18. use warnings;
  19. our $VERSION = "1.01";
  20. use Dpkg::Gettext;
  21. use Dpkg::ErrorHandling;
  22. =encoding utf8
  23. =head1 NAME
  24. Dpkg::BuildOptions - parse and update build options
  25. =head1 DESCRIPTION
  26. The Dpkg::BuildOptions object can be used to manipulate options stored
  27. in environment variables like DEB_BUILD_OPTIONS and
  28. DEB_BUILD_MAINT_OPTIONS.
  29. =head1 FUNCTIONS
  30. =over 4
  31. =item my $bo = Dpkg::BuildOptions->new(%opts)
  32. Create a new Dpkg::BuildOptions object. It will be initialized based
  33. on the value of the environment variable named $opts{'envvar'} (or
  34. DEB_BUILD_OPTIONS if that option is not set).
  35. =cut
  36. sub new {
  37. my ($this, %opts) = @_;
  38. my $class = ref($this) || $this;
  39. my $self = {
  40. options => {},
  41. source => {},
  42. envvar => $opts{'envvar'} // "DEB_BUILD_OPTIONS",
  43. };
  44. bless $self, $class;
  45. $self->merge($ENV{$self->{'envvar'}}, $self->{'envvar'});
  46. return $self;
  47. }
  48. =item $bo->reset()
  49. Reset the object to not have any option (it's empty).
  50. =cut
  51. sub reset {
  52. my ($self) = @_;
  53. $self->{'options'} = {};
  54. $self->{'source'} = {};
  55. }
  56. =item $bo->merge($content, $source)
  57. Merge the options set in $content and record that they come from the
  58. source $source. $source is mainly used in warning messages currently
  59. to indicate where invalid options have been detected.
  60. $content is a space separated list of options with optional assigned
  61. values like "nocheck parallel=2".
  62. =cut
  63. sub merge {
  64. my ($self, $content, $source) = @_;
  65. return 0 unless defined $content;
  66. my $count = 0;
  67. foreach (split(/\s+/, $content)) {
  68. unless (/^([a-z][a-z0-9_-]*)(?:=(\S*))?$/) {
  69. warning(_g("invalid flag in %s: %s"), $source, $_);
  70. next;
  71. }
  72. $count += $self->set($1, $2, $source);
  73. }
  74. return $count;
  75. }
  76. =item $bo->set($option, $value, [$source])
  77. Store the given option in the objet with the given value. It's legitimate
  78. for a value to be undefined if the option is a simple boolean (its
  79. presence means true, its absence means false). The $source is optional
  80. and indicates where the option comes from.
  81. The known options have their values checked for sanity. Options without
  82. values have their value removed and options with invalid values are
  83. discarded.
  84. =cut
  85. sub set {
  86. my ($self, $key, $value, $source) = @_;
  87. # Sanity checks
  88. if ($key =~ /^(noopt|nostrip|nocheck)$/ && defined($value)) {
  89. $value = undef;
  90. } elsif ($key eq 'parallel') {
  91. $value = "" if not defined($value);
  92. return 0 if $value !~ /^\d*$/;
  93. }
  94. $self->{'options'}{$key} = $value;
  95. $self->{'source'}{$key} = $source;
  96. return 1;
  97. }
  98. =item $bo->get($option)
  99. Return the value associated to the option. It might be undef even if the
  100. option exists. You might want to check with $bo->has($option) to verify if
  101. the option is stored in the object.
  102. =cut
  103. sub get {
  104. my ($self, $key) = @_;
  105. return $self->{'options'}{$key};
  106. }
  107. =item $bo->has($option)
  108. Returns a boolean indicating whether the option is stored in the object.
  109. =cut
  110. sub has {
  111. my ($self, $key) = @_;
  112. return exists $self->{'options'}{$key};
  113. }
  114. =item $string = $bo->output($fh)
  115. Return a string representation of the build options suitable to be
  116. assigned to an environment variable. Can optionnaly output that string to
  117. the given filehandle.
  118. =cut
  119. sub output {
  120. my ($self, $fh) = @_;
  121. my $o = $self->{'options'};
  122. my $res = join(" ", map { defined($o->{$_}) ? $_ . "=" . $o->{$_} : $_ } sort keys %$o);
  123. print $fh $res if defined $fh;
  124. return $res;
  125. }
  126. =item $bo->export([$var])
  127. Export the build options to the given environment variable. If omitted,
  128. the environment variable defined at creation time is assumed. The value
  129. set to the variable is also returned.
  130. =cut
  131. sub export {
  132. my ($self, $var) = @_;
  133. $var = $self->{'envvar'} unless defined $var;
  134. my $content = $self->output();
  135. $ENV{$var} = $content;
  136. return $content;
  137. }
  138. =back
  139. =head1 CHANGES
  140. =head2 Version 1.01
  141. Enable to use another environment variable instead of DEB_BUILD_OPTIONS.
  142. Thus add support for the "envvar" option at creation time.
  143. =head1 AUTHOR
  144. Raphaël Hertzog <hertzog@debian.org>
  145. =cut
  146. 1;