dpkg-buildflags.pl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. Copyright (C) 2010-2011 Raphael Hertzog <hertzog\@debian.org>.");
  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 [<action>]
  38. Actions:
  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. --list output a list of the flags supported by the current vendor.
  43. --export=(sh|make) output commands to be executed in shell or make that export
  44. all the compilation flags as environment variables.
  45. --dump output all compilation flags with their values
  46. --help show this help message.
  47. --version show the version.
  48. "), $progname;
  49. }
  50. my ($param, $action);
  51. while (@ARGV) {
  52. $_ = shift(@ARGV);
  53. if (m/^--(get|origin)$/) {
  54. usageerr(_g("two commands specified: --%s and --%s"), $1, $action)
  55. if defined($action);
  56. $action = $1;
  57. $param = shift(@ARGV);
  58. usageerr(_g("%s needs a parameter"), $_) unless defined $param;
  59. } elsif (m/^--export(?:=(sh|make))?$/) {
  60. usageerr(_g("two commands specified: --%s and --%s"), "export", $action)
  61. if defined($action);
  62. my $type = $1 || "sh";
  63. $action = "export-$type";
  64. } elsif (m/^--dump$/) {
  65. usageerr(_g("two commands specified: --%s and --%s"), "dump", $action)
  66. if defined($action);
  67. $action = "dump";
  68. } elsif (m/^--list$/) {
  69. usageerr(_g("two commands specified: --%s and --%s"), "list", $action)
  70. if defined($action);
  71. $action = "list";
  72. } elsif (m/^-(h|-help)$/) {
  73. usage();
  74. exit 0;
  75. } elsif (m/^--version$/) {
  76. version();
  77. exit 0;
  78. } else {
  79. usageerr(_g("unknown option \`%s'"), $_);
  80. }
  81. }
  82. $action = "dump" unless defined($action);
  83. my $build_flags = Dpkg::BuildFlags->new();
  84. if ($action eq "list") {
  85. foreach my $flag ($build_flags->list()) {
  86. print "$flag\n";
  87. }
  88. exit(0);
  89. }
  90. $build_flags->load_config();
  91. if ($action eq "get") {
  92. if ($build_flags->has($param)) {
  93. print $build_flags->get($param) . "\n";
  94. exit(0);
  95. }
  96. } elsif ($action eq "origin") {
  97. if ($build_flags->has($param)) {
  98. print $build_flags->get_origin($param) . "\n";
  99. exit(0);
  100. }
  101. } elsif ($action =~ m/^export-(.*)$/) {
  102. my $export_type = $1;
  103. foreach my $flag ($build_flags->list()) {
  104. next unless $flag =~ /^[A-Z]/; # Skip flags starting with lowercase
  105. my $value = $build_flags->get($flag);
  106. if ($export_type eq "sh") {
  107. $value =~ s/"/\"/g;
  108. print "export $flag=\"$value\"\n";
  109. } elsif ($export_type eq "make") {
  110. $value =~ s/\$/\$\$/g;
  111. print "export $flag := $value\n";
  112. }
  113. }
  114. exit(0);
  115. } elsif ($action eq "dump") {
  116. foreach my $flag ($build_flags->list()) {
  117. my $value = $build_flags->get($flag);
  118. print "$flag=$value\n";
  119. }
  120. }
  121. exit(1);