home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / perl5 / Dpkg / BuildOptions.pm < prev    next >
Encoding:
Perl POD Document  |  2012-09-17  |  4.5 KB  |  194 lines

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