BuildFlags.pm 7.4 KB

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