BuildFlags.pm 9.5 KB

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