dpkg-buildflags.pl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-buildflags
  4. #
  5. # Copyright © 2010-2011 Raphaël Hertzog <hertzog@debian.org>
  6. # Copyright © 2012-2013 Guillem Jover <guillem@debian.org>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. use strict;
  21. use warnings;
  22. use Dpkg ();
  23. use Dpkg::Gettext;
  24. use Dpkg::ErrorHandling qw(:DEFAULT report);
  25. use Dpkg::BuildFlags;
  26. use Dpkg::Vendor qw(get_current_vendor);
  27. textdomain('dpkg-dev');
  28. sub version {
  29. printf _g("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  30. printf _g('
  31. This is free software; see the GNU General Public License version 2 or
  32. later for copying conditions. There is NO warranty.
  33. ');
  34. }
  35. sub usage {
  36. printf _g(
  37. 'Usage: %s [<command>]')
  38. . "\n\n" . _g(
  39. 'Commands:
  40. --get <flag> output the requested flag to stdout.
  41. --origin <flag> output the origin of the flag to stdout:
  42. value is one of vendor, system, user, env.
  43. --query-features <area>
  44. output the status of features for the given area.
  45. --list output a list of the flags supported by the current vendor.
  46. --export=(sh|make|cmdline|configure)
  47. output something convenient to import the compilation
  48. flags in a shell script, in make, or in a command line.
  49. --dump output all compilation flags with their values
  50. --status print a synopsis with all parameters affecting the
  51. behaviour of dpkg-buildflags and the resulting flags
  52. and their origin.
  53. --help show this help message.
  54. --version show the version.
  55. '), $Dpkg::PROGNAME;
  56. }
  57. my ($param, $action);
  58. while (@ARGV) {
  59. $_ = shift(@ARGV);
  60. if (m/^--(get|origin|query-features)$/) {
  61. usageerr(_g('two commands specified: --%s and --%s'), $1, $action)
  62. if defined($action);
  63. $action = $1;
  64. $param = shift(@ARGV);
  65. usageerr(_g('%s needs a parameter'), $_) unless defined $param;
  66. } elsif (m/^--export(?:=(sh|make|cmdline|configure))?$/) {
  67. usageerr(_g('two commands specified: --%s and --%s'), 'export', $action)
  68. if defined($action);
  69. my $type = $1 || 'sh';
  70. # Map legacy aliases.
  71. $type = 'cmdline' if $type eq 'configure';
  72. $action = "export-$type";
  73. } elsif (m/^--(list|status|dump)$/) {
  74. usageerr(_g('two commands specified: --%s and --%s'), $1, $action)
  75. if defined($action);
  76. $action = $1;
  77. } elsif (m/^-(\?|-help)$/) {
  78. usage();
  79. exit 0;
  80. } elsif (m/^--version$/) {
  81. version();
  82. exit 0;
  83. } else {
  84. usageerr(_g("unknown option \`%s'"), $_);
  85. }
  86. }
  87. $action //= 'dump';
  88. my $build_flags = Dpkg::BuildFlags->new();
  89. if ($action eq 'list') {
  90. foreach my $flag ($build_flags->list()) {
  91. print "$flag\n";
  92. }
  93. exit(0);
  94. }
  95. $build_flags->load_config();
  96. if ($action eq 'get') {
  97. if ($build_flags->has($param)) {
  98. print $build_flags->get($param) . "\n";
  99. exit(0);
  100. }
  101. } elsif ($action eq 'origin') {
  102. if ($build_flags->has($param)) {
  103. print $build_flags->get_origin($param) . "\n";
  104. exit(0);
  105. }
  106. } elsif ($action eq 'query-features') {
  107. if ($build_flags->has_features($param)) {
  108. my %features = $build_flags->get_features($param);
  109. my $para_shown = 0;
  110. foreach my $feature (sort keys %features) {
  111. print $para_shown++ ? "\n" : '';
  112. printf "Feature: %s\n", $feature;
  113. printf "Enabled: %s\n", $features{$feature} ? 'yes' : 'no';
  114. }
  115. exit(0);
  116. }
  117. } elsif ($action =~ m/^export-(.*)$/) {
  118. my $export_type = $1;
  119. foreach my $flag ($build_flags->list()) {
  120. next unless $flag =~ /^[A-Z]/; # Skip flags starting with lowercase
  121. my $value = $build_flags->get($flag);
  122. if ($export_type eq 'sh') {
  123. $value =~ s/"/\"/g;
  124. print "export $flag=\"$value\"\n";
  125. } elsif ($export_type eq 'make') {
  126. $value =~ s/\$/\$\$/g;
  127. print "export $flag := $value\n";
  128. } elsif ($export_type eq 'cmdline') {
  129. print "$flag=\"$value\" ";
  130. }
  131. }
  132. exit(0);
  133. } elsif ($action eq 'dump') {
  134. foreach my $flag ($build_flags->list()) {
  135. my $value = $build_flags->get($flag);
  136. print "$flag=$value\n";
  137. }
  138. exit(0);
  139. } elsif ($action eq 'status') {
  140. # Prefix everything with "dpkg-buildflags: status: " to allow easy
  141. # extraction from a build log. Thus we use report with a non-translated
  142. # type string.
  143. # First print all environment variables that might have changed the
  144. # results (only existing ones, might make sense to add an option to
  145. # also show which ones could have set to modify it).
  146. my @envvars = Dpkg::BuildEnv::list_accessed();
  147. for my $envvar (@envvars) {
  148. if (exists $ENV{$envvar}) {
  149. printf report('status', 'environment variable %s=%s',
  150. $envvar, $ENV{$envvar});
  151. }
  152. }
  153. my $vendor = Dpkg::Vendor::get_current_vendor() || 'undefined';
  154. print report('status', "vendor is $vendor");
  155. # Then the resulting features:
  156. foreach my $area (sort $build_flags->get_feature_areas()) {
  157. my $fs;
  158. my %features = $build_flags->get_features($area);
  159. foreach my $feature (sort keys %features) {
  160. $fs .= sprintf(' %s=%s', $feature, $features{$feature} ? 'yes' : 'no');
  161. }
  162. print report('status', "$area features:$fs");
  163. }
  164. # Then the resulting values (with their origin):
  165. foreach my $flag ($build_flags->list()) {
  166. my $value = $build_flags->get($flag);
  167. my $origin = $build_flags->get_origin($flag);
  168. my $maintainer = $build_flags->is_maintainer_modified($flag) ? '+maintainer' : '';
  169. print report('status', "$flag [$origin$maintainer]: $value");
  170. }
  171. exit(0);
  172. }
  173. exit(1);