BuildFlags.pm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. package Dpkg::BuildFlags;
  16. use strict;
  17. use warnings;
  18. our $VERSION = "1.00";
  19. use Dpkg::Gettext;
  20. use Dpkg::BuildOptions;
  21. use Dpkg::ErrorHandling;
  22. use Dpkg::Vendor qw(run_vendor_hook);
  23. =encoding utf8
  24. =head1 NAME
  25. Dpkg::BuildFlags - query build flags
  26. =head1 DESCRIPTION
  27. The Dpkg::BuildFlags object is used by dpkg-buildflags and can be used
  28. to query the same information.
  29. =head1 FUNCTIONS
  30. =over 4
  31. =item my $bf = Dpkg::BuildFlags->new()
  32. Create a new Dpkg::BuildFlags object. It will be initialized based
  33. on the value of several configuration files and environment variables.
  34. =cut
  35. sub new {
  36. my ($this, %opts) = @_;
  37. my $class = ref($this) || $this;
  38. my $self = {
  39. };
  40. bless $self, $class;
  41. $self->load_vendor_defaults();
  42. return $self;
  43. }
  44. =item $bf->load_vendor_defaults()
  45. Reset the flags stored to the default set provided by the vendor.
  46. =cut
  47. sub load_vendor_defaults {
  48. my ($self) = @_;
  49. $self->{'options'} = {};
  50. $self->{'source'} = {};
  51. my $build_opts = Dpkg::BuildOptions->new();
  52. my $default_flags = $build_opts->has("noopt") ? "-g -O0" : "-g -O2";
  53. $self->{flags} = {
  54. CPPFLAGS => '',
  55. CFLAGS => $default_flags,
  56. CXXFLAGS => $default_flags,
  57. FFLAGS => $default_flags,
  58. LDFLAGS => '',
  59. };
  60. $self->{origin} = {
  61. CPPFLAGS => 'vendor',
  62. CFLAGS => 'vendor',
  63. CXXFLAGS => 'vendor',
  64. FFLAGS => 'vendor',
  65. LDFLAGS => 'vendor',
  66. };
  67. run_vendor_hook("update-buildflags", $self->{flags}, $self->{origin});
  68. }
  69. =item $bf->load_system_config()
  70. Update flags from the system configuration.
  71. =cut
  72. sub load_system_config {
  73. my ($self) = @_;
  74. $self->update_from_conffile("/etc/dpkg/buildflags.conf", "system");
  75. }
  76. =item $bf->load_user_config()
  77. Update flags from the user configuration.
  78. =cut
  79. sub load_user_config {
  80. my ($self) = @_;
  81. $self->update_from_conffile($ENV{"HOME"} . "/.config/dpkg-buildflags.conf", "user");
  82. }
  83. =item $bf->load_environment_config()
  84. Update flags based on directives stored in the environment. See
  85. dpkg-buildflags(1) for details.
  86. =cut
  87. sub load_environment_config {
  88. my ($self) = @_;
  89. foreach my $flag (keys %{$self->{flags}}) {
  90. my $envvar = "DEB_" . $flag . "_SET";
  91. if (exists $ENV{$envvar}) {
  92. $self->set($flag, $ENV{$envvar}, "env");
  93. }
  94. $envvar = "DEB_" . $flag . "_APPEND";
  95. if (exists $ENV{$envvar}) {
  96. $self->append($flag, $ENV{$envvar}, "env");
  97. }
  98. }
  99. }
  100. =item $bf->load_config()
  101. Call successively load_system_config(), load_user_config() and
  102. load_environment_config() to update the default build flags
  103. defined by the vendor.
  104. =cut
  105. sub load_config {
  106. my ($self) = @_;
  107. $self->load_system_config();
  108. $self->load_user_config();
  109. $self->load_environment_config();
  110. }
  111. =item $bf->set($flag, $value, $source)
  112. Update the build flag $flag with value $value and record its origin as $source.
  113. =cut
  114. sub set {
  115. my ($self, $flag, $value, $src) = @_;
  116. $self->{flags}->{$flag} = $value;
  117. $self->{origin}->{$flag} = $src;
  118. }
  119. =item $bf->append($flag, $value, $source)
  120. Append the options listed in $value to the current value of the flag $flag.
  121. Record its origin as $source.
  122. =cut
  123. sub append {
  124. my ($self, $flag, $value, $src) = @_;
  125. if (length($self->{flags}->{$flag})) {
  126. $self->{flags}->{$flag} .= " $value";
  127. } else {
  128. $self->{flags}->{$flag} = $value;
  129. }
  130. $self->{origin}->{$flag} = $src;
  131. }
  132. =item $bf->update_from_conffile($file, $source)
  133. Update the current build flags based on the configuration directives
  134. contained in $file. See dpkg-buildflags(1) for the format of the directives.
  135. $source is the origin recorded for any build flag set or modified.
  136. =cut
  137. sub update_from_conffile {
  138. my ($self, $file, $src) = @_;
  139. return unless -e $file;
  140. open(CNF, "<", $file) or syserr(_g("cannot read %s"), $file);
  141. while(<CNF>) {
  142. chomp;
  143. next if /^\s*#/; # Skip comments
  144. next if /^\s*$/; # Skip empty lines
  145. if (/^(append|set)\s+(\S+)\s+(\S.*\S)\s*$/i) {
  146. my ($op, $flag, $value) = ($1, $2, $3);
  147. unless (exists $self->{flags}->{$flag}) {
  148. warning(_g("line %d of %s mentions unknown flag %s"), $., $file, $flag);
  149. $self->{flags}->{$flag} = "";
  150. }
  151. if (lc($op) eq "set") {
  152. $self->set($flag, $value, $src);
  153. } elsif (lc($op) eq "append") {
  154. $self->append($flag, $value, $src);
  155. }
  156. } else {
  157. warning(_g("line %d of %s is invalid, it has been ignored."), $., $file);
  158. }
  159. }
  160. close(CNF);
  161. }
  162. =item $bf->get($flag)
  163. Return the value associated to the flag. It might be undef if the
  164. flag doesn't exist.
  165. =cut
  166. sub get {
  167. my ($self, $key) = @_;
  168. return $self->{'flags'}{$key};
  169. }
  170. =item $bf->get_origin($flag)
  171. Return the origin associated to the flag. It might be undef if the
  172. flag doesn't exist.
  173. =cut
  174. sub get_origin {
  175. my ($self, $key) = @_;
  176. return $self->{'origin'}{$key};
  177. }
  178. =item $bf->has($option)
  179. Returns a boolean indicating whether the flags exists in the object.
  180. =cut
  181. sub has {
  182. my ($self, $key) = @_;
  183. return exists $self->{'flags'}{$key};
  184. }
  185. =item my @flags = $bf->list()
  186. Returns the list of flags stored in the object.
  187. =cut
  188. sub list {
  189. my ($self) = @_;
  190. return sort keys %{$self->{'flags'}};
  191. }
  192. =back
  193. =head1 AUTHOR
  194. Raphaël Hertzog <hertzog@debian.org>
  195. =cut
  196. 1;