dpkg-buildflags.pl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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;
  24. use Dpkg::BuildFlags;
  25. textdomain("dpkg-dev");
  26. sub version {
  27. printf _g("Debian %s version %s.\n"), $progname, $version;
  28. printf _g("
  29. This is free software; see the GNU General Public License version 2 or
  30. later for copying conditions. There is NO warranty.
  31. ");
  32. }
  33. sub usage {
  34. printf _g(
  35. "Usage: %s [<action>]
  36. Actions:
  37. --get <flag> output the requested flag to stdout.
  38. --origin <flag> output the origin of the flag to stdout:
  39. value is one of vendor, system, user, env.
  40. --query-features <area>
  41. output the status of features for the given area.
  42. --list output a list of the flags supported by the current vendor.
  43. --export=(sh|make|configure)
  44. output something convenient to import the
  45. compilation flags in a shell script, in make,
  46. or on a ./configure command line.
  47. --dump output all compilation flags with their values
  48. --help show this help message.
  49. --version show the version.
  50. "), $progname;
  51. }
  52. my ($param, $action);
  53. while (@ARGV) {
  54. $_ = shift(@ARGV);
  55. if (m/^--(get|origin|query-features)$/) {
  56. usageerr(_g("two commands specified: --%s and --%s"), $1, $action)
  57. if defined($action);
  58. $action = $1;
  59. $param = shift(@ARGV);
  60. usageerr(_g("%s needs a parameter"), $_) unless defined $param;
  61. } elsif (m/^--export(?:=(sh|make|configure))?$/) {
  62. usageerr(_g("two commands specified: --%s and --%s"), "export", $action)
  63. if defined($action);
  64. my $type = $1 || "sh";
  65. $action = "export-$type";
  66. } elsif (m/^--dump$/) {
  67. usageerr(_g("two commands specified: --%s and --%s"), "dump", $action)
  68. if defined($action);
  69. $action = "dump";
  70. } elsif (m/^--list$/) {
  71. usageerr(_g("two commands specified: --%s and --%s"), "list", $action)
  72. if defined($action);
  73. $action = "list";
  74. } elsif (m/^-(h|-help)$/) {
  75. usage();
  76. exit 0;
  77. } elsif (m/^--version$/) {
  78. version();
  79. exit 0;
  80. } else {
  81. usageerr(_g("unknown option \`%s'"), $_);
  82. }
  83. }
  84. $action = "dump" unless defined($action);
  85. my $build_flags = Dpkg::BuildFlags->new();
  86. if ($action eq "list") {
  87. foreach my $flag ($build_flags->list()) {
  88. print "$flag\n";
  89. }
  90. exit(0);
  91. }
  92. $build_flags->load_config();
  93. if ($action eq "get") {
  94. if ($build_flags->has($param)) {
  95. print $build_flags->get($param) . "\n";
  96. exit(0);
  97. }
  98. } elsif ($action eq "origin") {
  99. if ($build_flags->has($param)) {
  100. print $build_flags->get_origin($param) . "\n";
  101. exit(0);
  102. }
  103. } elsif ($action eq "query-features") {
  104. if ($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. exit(0);
  113. }
  114. } elsif ($action =~ m/^export-(.*)$/) {
  115. my $export_type = $1;
  116. foreach my $flag ($build_flags->list()) {
  117. next unless $flag =~ /^[A-Z]/; # Skip flags starting with lowercase
  118. my $value = $build_flags->get($flag);
  119. if ($export_type eq "sh") {
  120. $value =~ s/"/\"/g;
  121. print "export $flag=\"$value\"\n";
  122. } elsif ($export_type eq "make") {
  123. $value =~ s/\$/\$\$/g;
  124. print "export $flag := $value\n";
  125. } elsif ($export_type eq "configure") {
  126. print "$flag=\"$value\" ";
  127. }
  128. }
  129. exit(0);
  130. } elsif ($action eq "dump") {
  131. foreach my $flag ($build_flags->list()) {
  132. my $value = $build_flags->get($flag);
  133. print "$flag=$value\n";
  134. }
  135. exit(0);
  136. }
  137. exit(1);