BuildFlags.pm 7.7 KB

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