dpkg-buildflags.pl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 <https://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. my $load_config = 1;
  59. while (@ARGV) {
  60. $_ = shift(@ARGV);
  61. if (m/^--(get|origin|query-features)$/) {
  62. usageerr(g_('two commands specified: --%s and --%s'), $1, $action)
  63. if defined($action);
  64. $action = $1;
  65. $param = shift(@ARGV);
  66. usageerr(g_('%s needs a parameter'), $_) unless defined $param;
  67. } elsif (m/^--export(?:=(sh|make|cmdline|configure))?$/) {
  68. usageerr(g_('two commands specified: --%s and --%s'), 'export', $action)
  69. if defined($action);
  70. my $type = $1 || 'sh';
  71. # Map legacy aliases.
  72. $type = 'cmdline' if $type eq 'configure';
  73. $action = "export-$type";
  74. } elsif (m/^--(list|status|dump)$/) {
  75. usageerr(g_('two commands specified: --%s and --%s'), $1, $action)
  76. if defined($action);
  77. $action = $1;
  78. $load_config = 0 if $action eq 'list';
  79. } elsif (m/^-(?:\?|-help)$/) {
  80. usage();
  81. exit 0;
  82. } elsif (m/^--version$/) {
  83. version();
  84. exit 0;
  85. } else {
  86. usageerr(g_("unknown option '%s'"), $_);
  87. }
  88. }
  89. $action //= 'dump';
  90. my $build_flags = Dpkg::BuildFlags->new();
  91. $build_flags->load_config() if $load_config;
  92. if ($action eq 'list') {
  93. foreach my $flag ($build_flags->list()) {
  94. print "$flag\n";
  95. }
  96. } elsif ($action eq 'get') {
  97. exit 1 unless $build_flags->has($param);
  98. print $build_flags->get($param) . "\n";
  99. } elsif ($action eq 'origin') {
  100. exit 1 unless $build_flags->has($param);
  101. print $build_flags->get_origin($param) . "\n";
  102. } elsif ($action eq 'query-features') {
  103. exit 1 unless $build_flags->has_features($param);
  104. my %features = $build_flags->get_features($param);
  105. my $para_shown = 0;
  106. foreach my $feature (sort keys %features) {
  107. print $para_shown++ ? "\n" : '';
  108. printf "Feature: %s\n", $feature;
  109. printf "Enabled: %s\n", $features{$feature} ? 'yes' : 'no';
  110. }
  111. } elsif ($action =~ m/^export-(.*)$/) {
  112. my $export_type = $1;
  113. foreach my $flag ($build_flags->list()) {
  114. next unless $flag =~ /^[A-Z]/; # Skip flags starting with lowercase
  115. my $value = $build_flags->get($flag);
  116. if ($export_type eq 'sh') {
  117. $value =~ s/"/\"/g;
  118. print "export $flag=\"$value\"\n";
  119. } elsif ($export_type eq 'make') {
  120. $value =~ s/\$/\$\$/g;
  121. print "export $flag := $value\n";
  122. } elsif ($export_type eq 'cmdline') {
  123. print "$flag=\"$value\" ";
  124. }
  125. }
  126. } elsif ($action eq 'dump') {
  127. foreach my $flag ($build_flags->list()) {
  128. my $value = $build_flags->get($flag);
  129. print "$flag=$value\n";
  130. }
  131. } elsif ($action eq 'status') {
  132. # Prefix everything with "dpkg-buildflags: status: " to allow easy
  133. # extraction from a build log. Thus we use report with a non-translated
  134. # type string.
  135. # First print all environment variables that might have changed the
  136. # results (only existing ones, might make sense to add an option to
  137. # also show which ones could have set to modify it).
  138. my @envvars = Dpkg::BuildEnv::list_accessed();
  139. for my $envvar (@envvars) {
  140. if (exists $ENV{$envvar}) {
  141. printf report('status', 'environment variable %s=%s',
  142. $envvar, $ENV{$envvar});
  143. }
  144. }
  145. my $vendor = Dpkg::Vendor::get_current_vendor() || 'undefined';
  146. print report('status', "vendor is $vendor");
  147. # Then the resulting features:
  148. foreach my $area (sort $build_flags->get_feature_areas()) {
  149. my $fs;
  150. my %features = $build_flags->get_features($area);
  151. foreach my $feature (sort keys %features) {
  152. $fs .= sprintf(' %s=%s', $feature, $features{$feature} ? 'yes' : 'no');
  153. }
  154. print report('status', "$area features:$fs");
  155. }
  156. # Then the resulting values (with their origin):
  157. foreach my $flag ($build_flags->list()) {
  158. my $value = $build_flags->get($flag);
  159. my $origin = $build_flags->get_origin($flag);
  160. my $maintainer = $build_flags->is_maintainer_modified($flag) ? '+maintainer' : '';
  161. print report('status', "$flag [$origin$maintainer]: $value");
  162. }
  163. }