BuildFlags.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 <https://www.gnu.org/licenses/>.
  15. package Dpkg::BuildFlags;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '1.03';
  19. use Dpkg ();
  20. use Dpkg::Gettext;
  21. use Dpkg::Build::Env;
  22. use Dpkg::BuildOptions;
  23. use Dpkg::ErrorHandling;
  24. use Dpkg::Vendor qw(run_vendor_hook);
  25. =encoding utf8
  26. =head1 NAME
  27. Dpkg::BuildFlags - query build flags
  28. =head1 DESCRIPTION
  29. The Dpkg::BuildFlags object is used by dpkg-buildflags and can be used
  30. to query the same information.
  31. =head1 METHODS
  32. =over 4
  33. =item $bf = Dpkg::BuildFlags->new()
  34. Create a new Dpkg::BuildFlags object. It will be initialized based
  35. on the value of several configuration files and environment variables.
  36. =cut
  37. sub new {
  38. my ($this, %opts) = @_;
  39. my $class = ref($this) || $this;
  40. my $self = {
  41. };
  42. bless $self, $class;
  43. $self->load_vendor_defaults();
  44. return $self;
  45. }
  46. =item $bf->load_vendor_defaults()
  47. Reset the flags stored to the default set provided by the vendor.
  48. =cut
  49. sub load_vendor_defaults {
  50. my $self = shift;
  51. $self->{options} = {};
  52. $self->{source} = {};
  53. $self->{features} = {};
  54. my $build_opts = Dpkg::BuildOptions->new();
  55. $self->{build_options} = $build_opts;
  56. my $default_flags = $build_opts->has('noopt') ? '-g -O0' : '-g -O2';
  57. $self->{flags} = {
  58. CPPFLAGS => '',
  59. CFLAGS => $default_flags,
  60. CXXFLAGS => $default_flags,
  61. OBJCFLAGS => $default_flags,
  62. OBJCXXFLAGS => $default_flags,
  63. GCJFLAGS => $default_flags,
  64. FFLAGS => $default_flags,
  65. FCFLAGS => $default_flags,
  66. LDFLAGS => '',
  67. };
  68. $self->{origin} = {
  69. CPPFLAGS => 'vendor',
  70. CFLAGS => 'vendor',
  71. CXXFLAGS => 'vendor',
  72. OBJCFLAGS => 'vendor',
  73. OBJCXXFLAGS => 'vendor',
  74. GCJFLAGS => 'vendor',
  75. FFLAGS => 'vendor',
  76. FCFLAGS => 'vendor',
  77. LDFLAGS => 'vendor',
  78. };
  79. $self->{maintainer} = {
  80. CPPFLAGS => 0,
  81. CFLAGS => 0,
  82. CXXFLAGS => 0,
  83. OBJCFLAGS => 0,
  84. OBJCXXFLAGS => 0,
  85. GCJFLAGS => 0,
  86. FFLAGS => 0,
  87. FCFLAGS => 0,
  88. LDFLAGS => 0,
  89. };
  90. # The vendor hook will add the feature areas build flags.
  91. run_vendor_hook('update-buildflags', $self);
  92. }
  93. =item $bf->load_system_config()
  94. Update flags from the system configuration.
  95. =cut
  96. sub load_system_config {
  97. my $self = shift;
  98. $self->update_from_conffile("$Dpkg::CONFDIR/buildflags.conf", 'system');
  99. }
  100. =item $bf->load_user_config()
  101. Update flags from the user configuration.
  102. =cut
  103. sub load_user_config {
  104. my $self = shift;
  105. my $confdir = $ENV{XDG_CONFIG_HOME};
  106. $confdir ||= $ENV{HOME} . '/.config' if length $ENV{HOME};
  107. if (length $confdir) {
  108. $self->update_from_conffile("$confdir/dpkg/buildflags.conf", 'user');
  109. }
  110. }
  111. =item $bf->load_environment_config()
  112. Update flags based on user directives stored in the environment. See
  113. dpkg-buildflags(1) for details.
  114. =cut
  115. sub load_environment_config {
  116. my $self = shift;
  117. foreach my $flag (keys %{$self->{flags}}) {
  118. my $envvar = 'DEB_' . $flag . '_SET';
  119. if (Dpkg::Build::Env::has($envvar)) {
  120. $self->set($flag, Dpkg::Build::Env::get($envvar), 'env');
  121. }
  122. $envvar = 'DEB_' . $flag . '_STRIP';
  123. if (Dpkg::Build::Env::has($envvar)) {
  124. $self->strip($flag, Dpkg::Build::Env::get($envvar), 'env');
  125. }
  126. $envvar = 'DEB_' . $flag . '_APPEND';
  127. if (Dpkg::Build::Env::has($envvar)) {
  128. $self->append($flag, Dpkg::Build::Env::get($envvar), 'env');
  129. }
  130. $envvar = 'DEB_' . $flag . '_PREPEND';
  131. if (Dpkg::Build::Env::has($envvar)) {
  132. $self->prepend($flag, Dpkg::Build::Env::get($envvar), 'env');
  133. }
  134. }
  135. }
  136. =item $bf->load_maintainer_config()
  137. Update flags based on maintainer directives stored in the environment. See
  138. dpkg-buildflags(1) for details.
  139. =cut
  140. sub load_maintainer_config {
  141. my $self = shift;
  142. foreach my $flag (keys %{$self->{flags}}) {
  143. my $envvar = 'DEB_' . $flag . '_MAINT_SET';
  144. if (Dpkg::Build::Env::has($envvar)) {
  145. $self->set($flag, Dpkg::Build::Env::get($envvar), undef, 1);
  146. }
  147. $envvar = 'DEB_' . $flag . '_MAINT_STRIP';
  148. if (Dpkg::Build::Env::has($envvar)) {
  149. $self->strip($flag, Dpkg::Build::Env::get($envvar), undef, 1);
  150. }
  151. $envvar = 'DEB_' . $flag . '_MAINT_APPEND';
  152. if (Dpkg::Build::Env::has($envvar)) {
  153. $self->append($flag, Dpkg::Build::Env::get($envvar), undef, 1);
  154. }
  155. $envvar = 'DEB_' . $flag . '_MAINT_PREPEND';
  156. if (Dpkg::Build::Env::has($envvar)) {
  157. $self->prepend($flag, Dpkg::Build::Env::get($envvar), undef, 1);
  158. }
  159. }
  160. }
  161. =item $bf->load_config()
  162. Call successively load_system_config(), load_user_config(),
  163. load_environment_config() and load_maintainer_config() to update the
  164. default build flags defined by the vendor.
  165. =cut
  166. sub load_config {
  167. my $self = shift;
  168. $self->load_system_config();
  169. $self->load_user_config();
  170. $self->load_environment_config();
  171. $self->load_maintainer_config();
  172. }
  173. =item $bf->set($flag, $value, $source, $maint)
  174. Update the build flag $flag with value $value and record its origin as
  175. $source (if defined). Record it as maintainer modified if $maint is
  176. defined and true.
  177. =cut
  178. sub set {
  179. my ($self, $flag, $value, $src, $maint) = @_;
  180. $self->{flags}->{$flag} = $value;
  181. $self->{origin}->{$flag} = $src if defined $src;
  182. $self->{maintainer}->{$flag} = $maint if $maint;
  183. }
  184. =item $bf->set_feature($area, $feature, $enabled)
  185. Update the boolean state of whether a specific feature within a known
  186. feature area has been enabled. The only currently known feature areas
  187. are "qa", "sanitize", "hardening" and "reproducible".
  188. =cut
  189. sub set_feature {
  190. my ($self, $area, $feature, $enabled) = @_;
  191. $self->{features}{$area}{$feature} = $enabled;
  192. }
  193. =item $bf->strip($flag, $value, $source, $maint)
  194. Update the build flag $flag by stripping the flags listed in $value and
  195. record its origin as $source (if defined). Record it as maintainer modified
  196. if $maint is defined and true.
  197. =cut
  198. sub strip {
  199. my ($self, $flag, $value, $src, $maint) = @_;
  200. foreach my $tostrip (split(/\s+/, $value)) {
  201. next unless length $tostrip;
  202. $self->{flags}->{$flag} =~ s/(^|\s+)\Q$tostrip\E(\s+|$)/ /g;
  203. }
  204. $self->{flags}->{$flag} =~ s/^\s+//g;
  205. $self->{flags}->{$flag} =~ s/\s+$//g;
  206. $self->{origin}->{$flag} = $src if defined $src;
  207. $self->{maintainer}->{$flag} = $maint if $maint;
  208. }
  209. =item $bf->append($flag, $value, $source, $maint)
  210. Append the options listed in $value to the current value of the flag $flag.
  211. Record its origin as $source (if defined). Record it as maintainer modified
  212. if $maint is defined and true.
  213. =cut
  214. sub append {
  215. my ($self, $flag, $value, $src, $maint) = @_;
  216. if (length($self->{flags}->{$flag})) {
  217. $self->{flags}->{$flag} .= " $value";
  218. } else {
  219. $self->{flags}->{$flag} = $value;
  220. }
  221. $self->{origin}->{$flag} = $src if defined $src;
  222. $self->{maintainer}->{$flag} = $maint if $maint;
  223. }
  224. =item $bf->prepend($flag, $value, $source, $maint)
  225. Prepend the options listed in $value to the current value of the flag $flag.
  226. Record its origin as $source (if defined). Record it as maintainer modified
  227. if $maint is defined and true.
  228. =cut
  229. sub prepend {
  230. my ($self, $flag, $value, $src, $maint) = @_;
  231. if (length($self->{flags}->{$flag})) {
  232. $self->{flags}->{$flag} = "$value " . $self->{flags}->{$flag};
  233. } else {
  234. $self->{flags}->{$flag} = $value;
  235. }
  236. $self->{origin}->{$flag} = $src if defined $src;
  237. $self->{maintainer}->{$flag} = $maint if $maint;
  238. }
  239. =item $bf->update_from_conffile($file, $source)
  240. Update the current build flags based on the configuration directives
  241. contained in $file. See dpkg-buildflags(1) for the format of the directives.
  242. $source is the origin recorded for any build flag set or modified.
  243. =cut
  244. sub update_from_conffile {
  245. my ($self, $file, $src) = @_;
  246. local $_;
  247. return unless -e $file;
  248. open(my $conf_fh, '<', $file) or syserr(g_('cannot read %s'), $file);
  249. while (<$conf_fh>) {
  250. chomp;
  251. next if /^\s*#/; # Skip comments
  252. next if /^\s*$/; # Skip empty lines
  253. if (/^(append|prepend|set|strip)\s+(\S+)\s+(\S.*\S)\s*$/i) {
  254. my ($op, $flag, $value) = ($1, $2, $3);
  255. unless (exists $self->{flags}->{$flag}) {
  256. warning(g_('line %d of %s mentions unknown flag %s'), $., $file, $flag);
  257. $self->{flags}->{$flag} = '';
  258. }
  259. if (lc($op) eq 'set') {
  260. $self->set($flag, $value, $src);
  261. } elsif (lc($op) eq 'strip') {
  262. $self->strip($flag, $value, $src);
  263. } elsif (lc($op) eq 'append') {
  264. $self->append($flag, $value, $src);
  265. } elsif (lc($op) eq 'prepend') {
  266. $self->prepend($flag, $value, $src);
  267. }
  268. } else {
  269. warning(g_('line %d of %s is invalid, it has been ignored'), $., $file);
  270. }
  271. }
  272. close($conf_fh);
  273. }
  274. =item $bf->get($flag)
  275. Return the value associated to the flag. It might be undef if the
  276. flag doesn't exist.
  277. =cut
  278. sub get {
  279. my ($self, $key) = @_;
  280. return $self->{flags}{$key};
  281. }
  282. =item $bf->get_feature_areas()
  283. Return the feature areas (i.e. the area values has_features will return
  284. true for).
  285. =cut
  286. sub get_feature_areas {
  287. my $self = shift;
  288. return keys %{$self->{features}};
  289. }
  290. =item $bf->get_features($area)
  291. Return, for the given area, a hash with keys as feature names, and values
  292. as booleans indicating whether the feature is enabled or not.
  293. =cut
  294. sub get_features {
  295. my ($self, $area) = @_;
  296. return %{$self->{features}{$area}};
  297. }
  298. =item $bf->get_origin($flag)
  299. Return the origin associated to the flag. It might be undef if the
  300. flag doesn't exist.
  301. =cut
  302. sub get_origin {
  303. my ($self, $key) = @_;
  304. return $self->{origin}{$key};
  305. }
  306. =item $bf->is_maintainer_modified($flag)
  307. Return true if the flag is modified by the maintainer.
  308. =cut
  309. sub is_maintainer_modified {
  310. my ($self, $key) = @_;
  311. return $self->{maintainer}{$key};
  312. }
  313. =item $bf->has_features($area)
  314. Returns true if the given area of features is known, and false otherwise.
  315. The only currently recognized feature areas are "qa", "sanitize", "hardening"
  316. and "reproducible".
  317. =cut
  318. sub has_features {
  319. my ($self, $area) = @_;
  320. return exists $self->{features}{$area};
  321. }
  322. =item $bf->has($option)
  323. Returns a boolean indicating whether the flags exists in the object.
  324. =cut
  325. sub has {
  326. my ($self, $key) = @_;
  327. return exists $self->{flags}{$key};
  328. }
  329. =item @flags = $bf->list()
  330. Returns the list of flags stored in the object.
  331. =cut
  332. sub list {
  333. my $self = shift;
  334. my @list = sort keys %{$self->{flags}};
  335. return @list;
  336. }
  337. =back
  338. =head1 CHANGES
  339. =head2 Version 1.03 (dpkg 1.16.5)
  340. New method: $bf->get_feature_areas() to list possible values for
  341. $bf->get_features.
  342. New method $bf->is_maintainer_modified() and new optional parameter to
  343. $bf->set(), $bf->append(), $bf->prepend(), $bf->strip().
  344. =head2 Version 1.02 (dpkg 1.16.2)
  345. New methods: $bf->get_features(), $bf->has_features(), $bf->set_feature().
  346. =head2 Version 1.01 (dpkg 1.16.1)
  347. New method: $bf->prepend() very similar to append(). Implement support of
  348. the prepend operation everywhere.
  349. New method: $bf->load_maintainer_config() that update the build flags
  350. based on the package maintainer directives.
  351. =head2 Version 1.00 (dpkg 1.15.7)
  352. Mark the module as public.
  353. =cut
  354. 1;