BuildFlags.pm 6.0 KB

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