BuildFlags.pm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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);
  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. my $confdir = $ENV{'XDG_CONFIG_HOME'} || $ENV{"HOME"} . "/.config";
  82. $self->update_from_conffile("$confdir/dpkg/buildflags.conf", "user");
  83. }
  84. =item $bf->load_environment_config()
  85. Update flags based on directives stored in the environment. See
  86. dpkg-buildflags(1) for details.
  87. =cut
  88. sub load_environment_config {
  89. my ($self) = @_;
  90. foreach my $flag (keys %{$self->{flags}}) {
  91. my $envvar = "DEB_" . $flag . "_SET";
  92. if (exists $ENV{$envvar}) {
  93. $self->set($flag, $ENV{$envvar}, "env");
  94. }
  95. $envvar = "DEB_" . $flag . "_APPEND";
  96. if (exists $ENV{$envvar}) {
  97. $self->append($flag, $ENV{$envvar}, "env");
  98. }
  99. }
  100. }
  101. =item $bf->load_config()
  102. Call successively load_system_config(), load_user_config() and
  103. load_environment_config() to update the default build flags
  104. defined by the vendor.
  105. =cut
  106. sub load_config {
  107. my ($self) = @_;
  108. $self->load_system_config();
  109. $self->load_user_config();
  110. $self->load_environment_config();
  111. }
  112. =item $bf->set($flag, $value, $source)
  113. Update the build flag $flag with value $value and record its origin as $source.
  114. =cut
  115. sub set {
  116. my ($self, $flag, $value, $src) = @_;
  117. $self->{flags}->{$flag} = $value;
  118. $self->{origin}->{$flag} = $src;
  119. }
  120. =item $bf->append($flag, $value, $source)
  121. Append the options listed in $value to the current value of the flag $flag.
  122. Record its origin as $source.
  123. =cut
  124. sub append {
  125. my ($self, $flag, $value, $src) = @_;
  126. if (length($self->{flags}->{$flag})) {
  127. $self->{flags}->{$flag} .= " $value";
  128. } else {
  129. $self->{flags}->{$flag} = $value;
  130. }
  131. $self->{origin}->{$flag} = $src;
  132. }
  133. =item $bf->update_from_conffile($file, $source)
  134. Update the current build flags based on the configuration directives
  135. contained in $file. See dpkg-buildflags(1) for the format of the directives.
  136. $source is the origin recorded for any build flag set or modified.
  137. =cut
  138. sub update_from_conffile {
  139. my ($self, $file, $src) = @_;
  140. return unless -e $file;
  141. open(CNF, "<", $file) or syserr(_g("cannot read %s"), $file);
  142. while(<CNF>) {
  143. chomp;
  144. next if /^\s*#/; # Skip comments
  145. next if /^\s*$/; # Skip empty lines
  146. if (/^(append|set)\s+(\S+)\s+(\S.*\S)\s*$/i) {
  147. my ($op, $flag, $value) = ($1, $2, $3);
  148. unless (exists $self->{flags}->{$flag}) {
  149. warning(_g("line %d of %s mentions unknown flag %s"), $., $file, $flag);
  150. $self->{flags}->{$flag} = "";
  151. }
  152. if (lc($op) eq "set") {
  153. $self->set($flag, $value, $src);
  154. } elsif (lc($op) eq "append") {
  155. $self->append($flag, $value, $src);
  156. }
  157. } else {
  158. warning(_g("line %d of %s is invalid, it has been ignored."), $., $file);
  159. }
  160. }
  161. close(CNF);
  162. }
  163. =item $bf->get($flag)
  164. Return the value associated to the flag. It might be undef if the
  165. flag doesn't exist.
  166. =cut
  167. sub get {
  168. my ($self, $key) = @_;
  169. return $self->{'flags'}{$key};
  170. }
  171. =item $bf->get_origin($flag)
  172. Return the origin associated to the flag. It might be undef if the
  173. flag doesn't exist.
  174. =cut
  175. sub get_origin {
  176. my ($self, $key) = @_;
  177. return $self->{'origin'}{$key};
  178. }
  179. =item $bf->has($option)
  180. Returns a boolean indicating whether the flags exists in the object.
  181. =cut
  182. sub has {
  183. my ($self, $key) = @_;
  184. return exists $self->{'flags'}{$key};
  185. }
  186. =item my @flags = $bf->list()
  187. Returns the list of flags stored in the object.
  188. =cut
  189. sub list {
  190. my ($self) = @_;
  191. return sort keys %{$self->{'flags'}};
  192. }
  193. =back
  194. =head1 AUTHOR
  195. Raphaël Hertzog <hertzog@debian.org>
  196. =cut
  197. 1;