BuildOptions.pm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. return 0 unless defined $content;
  62. my $count = 0;
  63. foreach (split(/\s+/, $content)) {
  64. unless (/^([a-z][a-z0-9_-]*)(?:=(\S*))?$/) {
  65. warning(_g("invalid flag in %s: %s"), $source, $_);
  66. next;
  67. }
  68. $count += $self->set($1, $2, $source);
  69. }
  70. return $count;
  71. }
  72. =item $bo->set($option, $value, [$source])
  73. Store the given option in the objet with the given value. It's legitimate
  74. for a value to be undefined if the option is a simple boolean (its
  75. presence means true, its absence means false). The $source is optional
  76. and indicates where the option comes from.
  77. The known options have their values checked for sanity. Options without
  78. values have their value removed and options with invalid values are
  79. discarded.
  80. =cut
  81. sub set {
  82. my ($self, $key, $value, $source) = @_;
  83. # Sanity checks
  84. if ($key =~ /^(noopt|nostrip|nocheck)$/ && defined($value)) {
  85. $value = undef;
  86. } elsif ($key eq 'parallel') {
  87. $value = "" if not defined($value);
  88. return 0 if $value !~ /^\d*$/;
  89. }
  90. $self->{'options'}{$key} = $value;
  91. $self->{'source'}{$key} = $source;
  92. return 1;
  93. }
  94. =item $bo->get($option)
  95. Return the value associated to the option. It might be undef even if the
  96. option exists. You might want to check with $bo->has($option) to verify if
  97. the option is stored in the object.
  98. =cut
  99. sub get {
  100. my ($self, $key) = @_;
  101. return $self->{'options'}{$key};
  102. }
  103. =item $bo->has($option)
  104. Returns a boolean indicating whether the option is stored in the object.
  105. =cut
  106. sub has {
  107. my ($self, $key) = @_;
  108. return exists $self->{'options'}{$key};
  109. }
  110. =item $string = $bo->output($fh)
  111. Return a string representation of the build options suitable to be
  112. assigned to an environment variable. Can optionnaly output that string to
  113. the given filehandle.
  114. =cut
  115. sub output {
  116. my ($self, $fh) = @_;
  117. my $o = $self->{'options'};
  118. my $res = join(" ", map { defined($o->{$_}) ? $_ . "=" . $o->{$_} : $_ } sort keys %$o);
  119. print $fh $res if defined $fh;
  120. return $res;
  121. }
  122. =item $bo->export([$var])
  123. Export the build options to the given environment variable. If omitted,
  124. DEB_BUILD_OPTIONS is assumed. The value set to the variable is also
  125. returned.
  126. =cut
  127. sub export {
  128. my ($self, $var) = @_;
  129. $var = "DEB_BUILD_OPTIONS" unless defined $var;
  130. my $content = $self->output();
  131. $ENV{$var} = $content;
  132. return $content;
  133. }
  134. =back
  135. =head1 AUTHOR
  136. Raphaël Hertzog <hertzog@debian.org>
  137. =cut
  138. 1;