BuildFlags.pm 10 KB

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