BuildFlags.pm 6.6 KB

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