BuildFlags.pm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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::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. $envvar = "DEB_" . $flag . "_PREPEND";
  103. if (exists $ENV{$envvar}) {
  104. $self->prepend($flag, $ENV{$envvar}, "env");
  105. }
  106. }
  107. }
  108. =item $bf->load_config()
  109. Call successively load_system_config(), load_user_config() and
  110. load_environment_config() to update the default build flags
  111. defined by the vendor.
  112. =cut
  113. sub load_config {
  114. my ($self) = @_;
  115. $self->load_system_config();
  116. $self->load_user_config();
  117. $self->load_environment_config();
  118. }
  119. =item $bf->set($flag, $value, $source)
  120. Update the build flag $flag with value $value and record its origin as $source.
  121. =cut
  122. sub set {
  123. my ($self, $flag, $value, $src) = @_;
  124. $self->{flags}->{$flag} = $value;
  125. $self->{origin}->{$flag} = $src;
  126. }
  127. =item $bf->append($flag, $value, $source)
  128. Append the options listed in $value to the current value of the flag $flag.
  129. Record its origin as $source.
  130. =cut
  131. sub append {
  132. my ($self, $flag, $value, $src) = @_;
  133. if (length($self->{flags}->{$flag})) {
  134. $self->{flags}->{$flag} .= " $value";
  135. } else {
  136. $self->{flags}->{$flag} = $value;
  137. }
  138. $self->{origin}->{$flag} = $src;
  139. }
  140. =item $bf->prepend($flag, $value, $source)
  141. Prepend the options listed in $value to the current value of the flag $flag.
  142. Record its origin as $source (if defined).
  143. =cut
  144. sub prepend {
  145. my ($self, $flag, $value, $src) = @_;
  146. if (length($self->{flags}->{$flag})) {
  147. $self->{flags}->{$flag} = "$value " . $self->{flags}->{$flag};
  148. } else {
  149. $self->{flags}->{$flag} = $value;
  150. }
  151. $self->{origin}->{$flag} = $src if defined $src;
  152. }
  153. =item $bf->update_from_conffile($file, $source)
  154. Update the current build flags based on the configuration directives
  155. contained in $file. See dpkg-buildflags(1) for the format of the directives.
  156. $source is the origin recorded for any build flag set or modified.
  157. =cut
  158. sub update_from_conffile {
  159. my ($self, $file, $src) = @_;
  160. return unless -e $file;
  161. open(CNF, "<", $file) or syserr(_g("cannot read %s"), $file);
  162. while(<CNF>) {
  163. chomp;
  164. next if /^\s*#/; # Skip comments
  165. next if /^\s*$/; # Skip empty lines
  166. if (/^(append|prepend|set)\s+(\S+)\s+(\S.*\S)\s*$/i) {
  167. my ($op, $flag, $value) = ($1, $2, $3);
  168. unless (exists $self->{flags}->{$flag}) {
  169. warning(_g("line %d of %s mentions unknown flag %s"), $., $file, $flag);
  170. $self->{flags}->{$flag} = "";
  171. }
  172. if (lc($op) eq "set") {
  173. $self->set($flag, $value, $src);
  174. } elsif (lc($op) eq "append") {
  175. $self->append($flag, $value, $src);
  176. } elsif (lc($op) eq "prepend") {
  177. $self->prepend($flag, $value, $src);
  178. }
  179. } else {
  180. warning(_g("line %d of %s is invalid, it has been ignored."), $., $file);
  181. }
  182. }
  183. close(CNF);
  184. }
  185. =item $bf->get($flag)
  186. Return the value associated to the flag. It might be undef if the
  187. flag doesn't exist.
  188. =cut
  189. sub get {
  190. my ($self, $key) = @_;
  191. return $self->{'flags'}{$key};
  192. }
  193. =item $bf->get_origin($flag)
  194. Return the origin associated to the flag. It might be undef if the
  195. flag doesn't exist.
  196. =cut
  197. sub get_origin {
  198. my ($self, $key) = @_;
  199. return $self->{'origin'}{$key};
  200. }
  201. =item $bf->has($option)
  202. Returns a boolean indicating whether the flags exists in the object.
  203. =cut
  204. sub has {
  205. my ($self, $key) = @_;
  206. return exists $self->{'flags'}{$key};
  207. }
  208. =item my @flags = $bf->list()
  209. Returns the list of flags stored in the object.
  210. =cut
  211. sub list {
  212. my ($self) = @_;
  213. return sort keys %{$self->{'flags'}};
  214. }
  215. =back
  216. =head1 CHANGES
  217. =head2 Version 1.01
  218. New method: $bf->prepend() very similar to append(). Implement support of
  219. the prepend operation everywhere.
  220. =head1 AUTHOR
  221. Raphaël Hertzog <hertzog@debian.org>
  222. =cut
  223. 1;