BuildOptions.pm 4.5 KB

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