dpkg-buildflags.pl 5.9 KB

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