dpkg-architecture.pl 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-architecture
  4. #
  5. # Copyright © 2004-2005 Scott James Remnant <scott@netsplit.com>,
  6. # Copyright © 1999 Marcus Brinkmann <brinkmd@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 <http://www.gnu.org/licenses/>.
  20. use strict;
  21. use warnings;
  22. use Dpkg;
  23. use Dpkg::Gettext;
  24. use Dpkg::ErrorHandling;
  25. use Dpkg::Arch qw(get_raw_build_arch get_raw_host_arch get_gcc_host_gnu_type
  26. debarch_to_cpuattrs
  27. get_valid_arches debarch_eq debarch_is debarch_to_debtriplet
  28. debarch_to_gnutriplet gnutriplet_to_debarch
  29. debarch_to_multiarch);
  30. textdomain("dpkg-dev");
  31. sub version {
  32. printf _g("Debian %s version %s.\n"), $progname, $version;
  33. printf _g("
  34. Copyright (C) 1999-2001 Marcus Brinkmann <brinkmd\@debian.org>.
  35. Copyright (C) 2004-2005 Scott James Remnant <scott\@netsplit.com>.");
  36. printf _g("
  37. This is free software; see the GNU General Public License version 2 or
  38. later for copying conditions. There is NO warranty.
  39. ");
  40. }
  41. sub usage {
  42. printf _g(
  43. "Usage: %s [<option> ...] [<action>]
  44. Options:
  45. -a<debian-arch> set current Debian architecture.
  46. -t<gnu-system> set current GNU system type.
  47. -L list valid architectures.
  48. -f force flag (override variables set in environment).
  49. Actions:
  50. -l list variables (default).
  51. -e<debian-arch> compare with current Debian architecture.
  52. -i<arch-alias> check if current Debian architecture is <arch-alias>.
  53. -q<variable> prints only the value of <variable>.
  54. -s print command to set environment variables.
  55. -u print command to unset environment variables.
  56. -c <command> set environment and run the command in it.
  57. --help show this help message.
  58. --version show the version.
  59. "), $progname;
  60. }
  61. sub list_arches()
  62. {
  63. foreach my $arch (get_valid_arches()) {
  64. print "$arch\n";
  65. }
  66. }
  67. my $req_host_arch = '';
  68. my $req_host_gnu_type = '';
  69. my $req_eq_arch = '';
  70. my $req_is_arch = '';
  71. my $req_variable_to_print;
  72. my $action = 'l';
  73. my $force = 0;
  74. while (@ARGV) {
  75. $_=shift(@ARGV);
  76. if (m/^-a/) {
  77. $req_host_arch = "$'";
  78. } elsif (m/^-t/) {
  79. $req_host_gnu_type = "$'";
  80. } elsif (m/^-e/) {
  81. $req_eq_arch = "$'";
  82. $action = 'e';
  83. } elsif (m/^-i/) {
  84. $req_is_arch = "$'";
  85. $action = 'i';
  86. } elsif (m/^-[lsu]$/) {
  87. $action = $_;
  88. $action =~ s/^-//;
  89. } elsif (m/^-f$/) {
  90. $force=1;
  91. } elsif (m/^-q/) {
  92. $req_variable_to_print = "$'";
  93. $action = 'q';
  94. } elsif (m/^-c$/) {
  95. $action = 'c';
  96. last;
  97. } elsif (m/^-L$/) {
  98. list_arches();
  99. exit unless @ARGV;
  100. } elsif (m/^-(h|-help)$/) {
  101. usage();
  102. exit 0;
  103. } elsif (m/^--version$/) {
  104. version();
  105. exit 0;
  106. } else {
  107. usageerr(_g("unknown option \`%s'"), $_);
  108. }
  109. }
  110. # Set default values:
  111. my %v;
  112. my @ordered = qw(DEB_BUILD_ARCH DEB_BUILD_ARCH_OS DEB_BUILD_ARCH_CPU
  113. DEB_BUILD_ARCH_BITS DEB_BUILD_ARCH_ENDIAN
  114. DEB_BUILD_GNU_CPU DEB_BUILD_GNU_SYSTEM DEB_BUILD_GNU_TYPE
  115. DEB_BUILD_MULTIARCH
  116. DEB_HOST_ARCH DEB_HOST_ARCH_OS DEB_HOST_ARCH_CPU
  117. DEB_HOST_ARCH_BITS DEB_HOST_ARCH_ENDIAN
  118. DEB_HOST_GNU_CPU DEB_HOST_GNU_SYSTEM DEB_HOST_GNU_TYPE
  119. DEB_HOST_MULTIARCH);
  120. $v{DEB_BUILD_ARCH} = get_raw_build_arch();
  121. $v{DEB_BUILD_GNU_TYPE} = debarch_to_gnutriplet($v{DEB_BUILD_ARCH});
  122. # Set user values:
  123. if ($req_host_arch ne '' && $req_host_gnu_type eq '') {
  124. $req_host_gnu_type = debarch_to_gnutriplet($req_host_arch);
  125. die (sprintf(_g("unknown Debian architecture %s, you must specify " .
  126. "GNU system type, too"), $req_host_arch))
  127. unless defined $req_host_gnu_type;
  128. }
  129. if ($req_host_gnu_type ne '' && $req_host_arch eq '') {
  130. $req_host_arch = gnutriplet_to_debarch($req_host_gnu_type);
  131. die (sprintf(_g("unknown GNU system type %s, you must specify " .
  132. "Debian architecture, too"), $req_host_gnu_type))
  133. unless defined $req_host_arch;
  134. }
  135. if ($req_host_gnu_type ne '' && $req_host_arch ne '') {
  136. my $dfl_host_gnu_type = debarch_to_gnutriplet($req_host_arch);
  137. die (sprintf(_g("unknown default GNU system type for Debian architecture %s"),
  138. $req_host_arch))
  139. unless defined $dfl_host_gnu_type;
  140. warning(_g("Default GNU system type %s for Debian arch %s does not " .
  141. "match specified GNU system type %s"), $dfl_host_gnu_type,
  142. $req_host_arch, $req_host_gnu_type)
  143. if $dfl_host_gnu_type ne $req_host_gnu_type;
  144. }
  145. if ($req_host_arch eq '') {
  146. $v{DEB_HOST_ARCH} = get_raw_host_arch();
  147. } else {
  148. $v{DEB_HOST_ARCH} = $req_host_arch;
  149. }
  150. if ($req_host_gnu_type eq '') {
  151. $v{DEB_HOST_GNU_TYPE} = debarch_to_gnutriplet($v{DEB_HOST_ARCH});
  152. } else {
  153. $v{DEB_HOST_GNU_TYPE} = $req_host_gnu_type;
  154. }
  155. my $gcc = get_gcc_host_gnu_type();
  156. warning(_g("Specified GNU system type %s does not match gcc system type %s."),
  157. $v{DEB_HOST_GNU_TYPE}, $gcc)
  158. if !($req_is_arch or $req_eq_arch) &&
  159. ($gcc ne '') && ($gcc ne $v{DEB_HOST_GNU_TYPE});
  160. # Split the Debian and GNU names
  161. my $abi;
  162. ($abi, $v{DEB_HOST_ARCH_OS}, $v{DEB_HOST_ARCH_CPU}) = debarch_to_debtriplet($v{DEB_HOST_ARCH});
  163. ($abi, $v{DEB_BUILD_ARCH_OS}, $v{DEB_BUILD_ARCH_CPU}) = debarch_to_debtriplet($v{DEB_BUILD_ARCH});
  164. ($v{DEB_HOST_GNU_CPU}, $v{DEB_HOST_GNU_SYSTEM}) = split(/-/, $v{DEB_HOST_GNU_TYPE}, 2);
  165. ($v{DEB_BUILD_GNU_CPU}, $v{DEB_BUILD_GNU_SYSTEM}) = split(/-/, $v{DEB_BUILD_GNU_TYPE}, 2);
  166. ($v{DEB_HOST_ARCH_BITS}, $v{DEB_HOST_ARCH_ENDIAN}) = debarch_to_cpuattrs($v{DEB_HOST_ARCH});
  167. ($v{DEB_BUILD_ARCH_BITS}, $v{DEB_BUILD_ARCH_ENDIAN}) = debarch_to_cpuattrs($v{DEB_BUILD_ARCH});
  168. $v{DEB_BUILD_MULTIARCH} = debarch_to_multiarch($v{DEB_BUILD_ARCH});
  169. $v{DEB_HOST_MULTIARCH} = debarch_to_multiarch($v{DEB_HOST_ARCH});
  170. for my $k (@ordered) {
  171. $v{$k} = $ENV{$k} if (defined ($ENV{$k}) && !$force);
  172. }
  173. if ($action eq 'l') {
  174. foreach my $k (@ordered) {
  175. print "$k=$v{$k}\n";
  176. }
  177. } elsif ($action eq 's') {
  178. foreach my $k (@ordered) {
  179. print "$k=$v{$k}; ";
  180. }
  181. print "export ".join(" ",@ordered)."\n";
  182. } elsif ($action eq 'u') {
  183. print "unset ".join(" ",@ordered)."\n";
  184. } elsif ($action eq 'e') {
  185. exit !debarch_eq($v{DEB_HOST_ARCH}, $req_eq_arch);
  186. } elsif ($action eq 'i') {
  187. exit !debarch_is($v{DEB_HOST_ARCH}, $req_is_arch);
  188. } elsif ($action eq 'c') {
  189. @ENV{keys %v} = values %v;
  190. exec @ARGV;
  191. } elsif ($action eq 'q') {
  192. if (exists $v{$req_variable_to_print}) {
  193. print "$v{$req_variable_to_print}\n";
  194. } else {
  195. die sprintf(_g("%s is not a supported variable name"), $req_variable_to_print);
  196. }
  197. }