BuildOptions.pm 4.4 KB

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