BuildFlags.pm 8.6 KB

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