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 / BuildFlags.pm < prev    next >
Encoding:
Perl POD Document  |  2012-09-17  |  5.9 KB  |  268 lines

  1. # Copyright ┬⌐ 2010 Rapha├½l Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15.  
  16. package Dpkg::BuildFlags;
  17.  
  18. use strict;
  19. use warnings;
  20.  
  21. our $VERSION = "1.00";
  22.  
  23. use Dpkg::Gettext;
  24. use Dpkg::BuildOptions;
  25. use Dpkg::ErrorHandling;
  26. use Dpkg::Vendor qw(run_vendor_hook);
  27.  
  28. =encoding utf8
  29.  
  30. =head1 NAME
  31.  
  32. Dpkg::BuildFlags - query build flags
  33.  
  34. =head1 DESCRIPTION
  35.  
  36. The Dpkg::BuildFlags object is used by dpkg-buildflags and can be used
  37. to query the same information.
  38.  
  39. =head1 FUNCTIONS
  40.  
  41. =over 4
  42.  
  43. =item my $bf = Dpkg::BuildFlags->new()
  44.  
  45. Create a new Dpkg::BuildFlags object. It will be initialized based
  46. on the value of several configuration files and environment variables.
  47.  
  48. =cut
  49.  
  50. sub new {
  51.     my ($this, %opts) = @_;
  52.     my $class = ref($this) || $this;
  53.  
  54.     my $self = {
  55.     };
  56.     bless $self, $class;
  57.     $self->load_vendor_defaults();
  58.     return $self;
  59. }
  60.  
  61. =item $bf->load_vendor_defaults()
  62.  
  63. Reset the flags stored to the default set provided by the vendor.
  64.  
  65. =cut
  66.  
  67. sub load_vendor_defaults {
  68.     my ($self) = @_;
  69.     $self->{'options'} = {};
  70.     $self->{'source'} = {};
  71.     my $build_opts = Dpkg::BuildOptions->new();
  72.     my $default_flags = $build_opts->has("noopt") ? "-g -O0" : "-g -O2";
  73.     $self->{flags} = {
  74.     CPPFLAGS => '',
  75.     CFLAGS   => $default_flags,
  76.     CXXFLAGS => $default_flags,
  77.     FFLAGS   => $default_flags,
  78.     LDFLAGS  => '',
  79.     };
  80.     $self->{origin} = {
  81.     CPPFLAGS => 'vendor',
  82.     CFLAGS   => 'vendor',
  83.     CXXFLAGS => 'vendor',
  84.     FFLAGS   => 'vendor',
  85.     LDFLAGS  => 'vendor',
  86.     };
  87.     run_vendor_hook("update-buildflags", $self);
  88. }
  89.  
  90. =item $bf->load_system_config()
  91.  
  92. Update flags from the system configuration.
  93.  
  94. =cut
  95.  
  96. sub load_system_config {
  97.     my ($self) = @_;
  98.     $self->update_from_conffile("/etc/dpkg/buildflags.conf", "system");
  99. }
  100.  
  101. =item $bf->load_user_config()
  102.  
  103. Update flags from the user configuration.
  104.  
  105. =cut
  106.  
  107. sub load_user_config {
  108.     my ($self) = @_;
  109.     my $confdir = $ENV{'XDG_CONFIG_HOME'} || $ENV{"HOME"} . "/.config";
  110.     $self->update_from_conffile("$confdir/dpkg/buildflags.conf", "user");
  111. }
  112.  
  113. =item $bf->load_environment_config()
  114.  
  115. Update flags based on directives stored in the environment. See
  116. dpkg-buildflags(1) for details.
  117.  
  118. =cut
  119.  
  120. sub load_environment_config {
  121.     my ($self) = @_;
  122.     foreach my $flag (keys %{$self->{flags}}) {
  123.     my $envvar = "DEB_" . $flag . "_SET";
  124.     if (exists $ENV{$envvar}) {
  125.         $self->set($flag, $ENV{$envvar}, "env");
  126.     }
  127.     $envvar = "DEB_" . $flag . "_APPEND";
  128.     if (exists $ENV{$envvar}) {
  129.         $self->append($flag, $ENV{$envvar}, "env");
  130.     }
  131.     }
  132. }
  133.  
  134. =item $bf->load_config()
  135.  
  136. Call successively load_system_config(), load_user_config() and
  137. load_environment_config() to update the default build flags
  138. defined by the vendor.
  139.  
  140. =cut
  141.  
  142. sub load_config {
  143.     my ($self) = @_;
  144.     $self->load_system_config();
  145.     $self->load_user_config();
  146.     $self->load_environment_config();
  147. }
  148.  
  149. =item $bf->set($flag, $value, $source)
  150.  
  151. Update the build flag $flag with value $value and record its origin as $source.
  152.  
  153. =cut
  154.  
  155. sub set {
  156.     my ($self, $flag, $value, $src) = @_;
  157.     $self->{flags}->{$flag} = $value;
  158.     $self->{origin}->{$flag} = $src;
  159. }
  160.  
  161. =item $bf->append($flag, $value, $source)
  162.  
  163. Append the options listed in $value to the current value of the flag $flag.
  164. Record its origin as $source.
  165.  
  166. =cut
  167.  
  168. sub append {
  169.     my ($self, $flag, $value, $src) = @_;
  170.     if (length($self->{flags}->{$flag})) {
  171.         $self->{flags}->{$flag} .= " $value";
  172.     } else {
  173.         $self->{flags}->{$flag} = $value;
  174.     }
  175.     $self->{origin}->{$flag} = $src;
  176. }
  177.  
  178. =item $bf->update_from_conffile($file, $source)
  179.  
  180. Update the current build flags based on the configuration directives
  181. contained in $file. See dpkg-buildflags(1) for the format of the directives.
  182.  
  183. $source is the origin recorded for any build flag set or modified.
  184.  
  185. =cut
  186.  
  187. sub update_from_conffile {
  188.     my ($self, $file, $src) = @_;
  189.     return unless -e $file;
  190.     open(CNF, "<", $file) or syserr(_g("cannot read %s"), $file);
  191.     while(<CNF>) {
  192.         chomp;
  193.         next if /^\s*#/; # Skip comments
  194.         next if /^\s*$/; # Skip empty lines
  195.         if (/^(append|set)\s+(\S+)\s+(\S.*\S)\s*$/i) {
  196.             my ($op, $flag, $value) = ($1, $2, $3);
  197.             unless (exists $self->{flags}->{$flag}) {
  198.                 warning(_g("line %d of %s mentions unknown flag %s"), $., $file, $flag);
  199.                 $self->{flags}->{$flag} = "";
  200.             }
  201.             if (lc($op) eq "set") {
  202.                 $self->set($flag, $value, $src);
  203.             } elsif (lc($op) eq "append") {
  204.                 $self->append($flag, $value, $src);
  205.             }
  206.         } else {
  207.             warning(_g("line %d of %s is invalid, it has been ignored."), $., $file);
  208.         }
  209.     }
  210.     close(CNF);
  211. }
  212.  
  213. =item $bf->get($flag)
  214.  
  215. Return the value associated to the flag. It might be undef if the
  216. flag doesn't exist.
  217.  
  218. =cut
  219.  
  220. sub get {
  221.     my ($self, $key) = @_;
  222.     return $self->{'flags'}{$key};
  223. }
  224.  
  225. =item $bf->get_origin($flag)
  226.  
  227. Return the origin associated to the flag. It might be undef if the
  228. flag doesn't exist.
  229.  
  230. =cut
  231.  
  232. sub get_origin {
  233.     my ($self, $key) = @_;
  234.     return $self->{'origin'}{$key};
  235. }
  236.  
  237. =item $bf->has($option)
  238.  
  239. Returns a boolean indicating whether the flags exists in the object.
  240.  
  241. =cut
  242.  
  243. sub has {
  244.     my ($self, $key) = @_;
  245.     return exists $self->{'flags'}{$key};
  246. }
  247.  
  248. =item my @flags = $bf->list()
  249.  
  250. Returns the list of flags stored in the object.
  251.  
  252. =cut
  253.  
  254. sub list {
  255.     my ($self) = @_;
  256.     return sort keys %{$self->{'flags'}};
  257. }
  258.  
  259. =back
  260.  
  261. =head1 AUTHOR
  262.  
  263. Rapha├½l Hertzog <hertzog@debian.org>
  264.  
  265. =cut
  266.  
  267. 1;
  268.