dpkg-buildflags.pl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 REPORT_STATUS);
  25. use Dpkg::Build::Env;
  26. use Dpkg::BuildFlags;
  27. use Dpkg::Vendor qw(get_current_vendor);
  28. textdomain('dpkg-dev');
  29. sub version {
  30. printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  31. printf g_('
  32. This is free software; see the GNU General Public License version 2 or
  33. later for copying conditions. There is NO warranty.
  34. ');
  35. }
  36. sub usage {
  37. printf g_(
  38. 'Usage: %s [<command>]')
  39. . "\n\n" . g_(
  40. 'Commands:
  41. --get <flag> output the requested flag to stdout.
  42. --origin <flag> output the origin of the flag to stdout:
  43. value is one of vendor, system, user, env.
  44. --query-features <area>
  45. output the status of features for the given area.
  46. --list output a list of the flags supported by the current vendor.
  47. --export=(sh|make|cmdline|configure)
  48. output something convenient to import the compilation
  49. flags in a shell script, in make, or in a command line.
  50. --dump output all compilation flags with their values
  51. --status print a synopsis with all parameters affecting the
  52. behaviour of dpkg-buildflags and the resulting flags
  53. and their origin.
  54. --help show this help message.
  55. --version show the version.
  56. '), $Dpkg::PROGNAME;
  57. }
  58. my ($param, $action);
  59. my $load_config = 1;
  60. while (@ARGV) {
  61. $_ = shift(@ARGV);
  62. if (m/^--(get|origin|query-features)$/) {
  63. usageerr(g_('two commands specified: --%s and --%s'), $1, $action)
  64. if defined($action);
  65. $action = $1;
  66. $param = shift(@ARGV);
  67. usageerr(g_('%s needs a parameter'), $_) unless defined $param;
  68. } elsif (m/^--export(?:=(sh|make|cmdline|configure))?$/) {
  69. usageerr(g_('two commands specified: --%s and --%s'), 'export', $action)
  70. if defined($action);
  71. my $type = $1 || 'sh';
  72. # Map legacy aliases.
  73. $type = 'cmdline' if $type eq 'configure';
  74. $action = "export-$type";
  75. } elsif (m/^--(list|status|dump)$/) {
  76. usageerr(g_('two commands specified: --%s and --%s'), $1, $action)
  77. if defined($action);
  78. $action = $1;
  79. $load_config = 0 if $action eq 'list';
  80. } elsif (m/^-(?:\?|-help)$/) {
  81. usage();
  82. exit 0;
  83. } elsif (m/^--version$/) {
  84. version();
  85. exit 0;
  86. } else {
  87. usageerr(g_("unknown option '%s'"), $_);
  88. }
  89. }
  90. $action //= 'dump';
  91. my $build_flags = Dpkg::BuildFlags->new();
  92. $build_flags->load_config() if $load_config;
  93. if ($action eq 'list') {
  94. foreach my $flag ($build_flags->list()) {
  95. print "$flag\n";
  96. }
  97. } elsif ($action eq 'get') {
  98. exit 1 unless $build_flags->has($param);
  99. print $build_flags->get($param) . "\n";
  100. } elsif ($action eq 'origin') {
  101. exit 1 unless $build_flags->has($param);
  102. print $build_flags->get_origin($param) . "\n";
  103. } elsif ($action eq 'query-features') {
  104. exit 1 unless $build_flags->has_features($param);
  105. my %features = $build_flags->get_features($param);
  106. my $para_shown = 0;
  107. foreach my $feature (sort keys %features) {
  108. print $para_shown++ ? "\n" : '';
  109. printf "Feature: %s\n", $feature;
  110. printf "Enabled: %s\n", $features{$feature} ? 'yes' : 'no';
  111. }
  112. } elsif ($action =~ m/^export-(.*)$/) {
  113. my $export_type = $1;
  114. foreach my $flag ($build_flags->list()) {
  115. next unless $flag =~ /^[A-Z]/; # Skip flags starting with lowercase
  116. my $value = $build_flags->get($flag);
  117. if ($export_type eq 'sh') {
  118. $value =~ s/"/\"/g;
  119. print "export $flag=\"$value\"\n";
  120. } elsif ($export_type eq 'make') {
  121. $value =~ s/\$/\$\$/g;
  122. print "export $flag := $value\n";
  123. } elsif ($export_type eq 'cmdline') {
  124. print "$flag=\"$value\" ";
  125. }
  126. }
  127. } elsif ($action eq 'dump') {
  128. foreach my $flag ($build_flags->list()) {
  129. my $value = $build_flags->get($flag);
  130. print "$flag=$value\n";
  131. }
  132. } elsif ($action eq 'status') {
  133. # Prefix everything with "dpkg-buildflags: status: " to allow easy
  134. # extraction from a build log. Thus we use report with a non-translated
  135. # type string.
  136. # First print all environment variables that might have changed the
  137. # results (only existing ones, might make sense to add an option to
  138. # also show which ones could have set to modify it).
  139. my @envvars = Dpkg::Build::Env::list_accessed();
  140. for my $envvar (@envvars) {
  141. if (exists $ENV{$envvar}) {
  142. printf report(REPORT_STATUS, 'environment variable %s=%s',
  143. $envvar, $ENV{$envvar});
  144. }
  145. }
  146. my $vendor = Dpkg::Vendor::get_current_vendor() || 'undefined';
  147. print report(REPORT_STATUS, "vendor is $vendor");
  148. # Then the resulting features:
  149. foreach my $area (sort $build_flags->get_feature_areas()) {
  150. my $fs;
  151. my %features = $build_flags->get_features($area);
  152. foreach my $feature (sort keys %features) {
  153. $fs .= sprintf(' %s=%s', $feature, $features{$feature} ? 'yes' : 'no');
  154. }
  155. print report(REPORT_STATUS, "$area features:$fs");
  156. }
  157. # Then the resulting values (with their origin):
  158. foreach my $flag ($build_flags->list()) {
  159. my $value = $build_flags->get($flag);
  160. my $origin = $build_flags->get_origin($flag);
  161. my $maintainer = $build_flags->is_maintainer_modified($flag) ? '+maintainer' : '';
  162. print report(REPORT_STATUS, "$flag [$origin$maintainer]: $value");
  163. }
  164. }