dpkg-architecture.pl 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. # Copyright © 2006-2011 Guillem Jover <guillem@debian.org>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. use strict;
  22. use warnings;
  23. use Dpkg;
  24. use Dpkg::Gettext;
  25. use Dpkg::ErrorHandling;
  26. use Dpkg::Arch qw(get_raw_build_arch get_raw_host_arch get_gcc_host_gnu_type
  27. debarch_to_cpuattrs
  28. get_valid_arches debarch_eq debarch_is debarch_to_debtriplet
  29. debarch_to_gnutriplet gnutriplet_to_debarch
  30. debarch_to_multiarch);
  31. textdomain("dpkg-dev");
  32. sub version {
  33. printf _g("Debian %s version %s.\n"), $progname, $version;
  34. printf _g("
  35. Copyright (C) 1999-2001 Marcus Brinkmann <brinkmd\@debian.org>.
  36. Copyright (C) 2004-2005 Scott James Remnant <scott\@netsplit.com>.");
  37. printf _g("
  38. This is free software; see the GNU General Public License version 2 or
  39. later for copying conditions. There is NO warranty.
  40. ");
  41. }
  42. sub usage {
  43. printf _g(
  44. "Usage: %s [<option> ...] [<action>]
  45. Options:
  46. -a<debian-arch> set current Debian architecture.
  47. -t<gnu-system> set current GNU system type.
  48. -L list valid architectures.
  49. -f force flag (override variables set in environment).
  50. Actions:
  51. -l list variables (default).
  52. -e<debian-arch> compare with current Debian architecture.
  53. -i<arch-alias> check if current Debian architecture is <arch-alias>.
  54. -q<variable> prints only the value of <variable>.
  55. -s print command to set environment variables.
  56. -u print command to unset environment variables.
  57. -c <command> set environment and run the command in it.
  58. --help show this help message.
  59. --version show the version.
  60. "), $progname;
  61. }
  62. sub list_arches()
  63. {
  64. foreach my $arch (get_valid_arches()) {
  65. print "$arch\n";
  66. }
  67. }
  68. use constant {
  69. DEB_NONE => 0,
  70. DEB_BUILD => 1,
  71. DEB_HOST => 2,
  72. DEB_ARCH_INFO => 4,
  73. DEB_ARCH_ATTR => 8,
  74. DEB_MULTIARCH => 16,
  75. DEB_GNU_INFO => 32,
  76. };
  77. use constant DEB_ALL => DEB_BUILD | DEB_HOST | DEB_ARCH_INFO | DEB_ARCH_ATTR |
  78. DEB_MULTIARCH | DEB_GNU_INFO;
  79. my %arch_vars = (
  80. "DEB_BUILD_ARCH" => DEB_BUILD,
  81. "DEB_BUILD_ARCH_OS" => DEB_BUILD | DEB_ARCH_INFO,
  82. "DEB_BUILD_ARCH_CPU" => DEB_BUILD | DEB_ARCH_INFO,
  83. "DEB_BUILD_ARCH_BITS" => DEB_BUILD | DEB_ARCH_ATTR,
  84. "DEB_BUILD_ARCH_ENDIAN" => DEB_BUILD | DEB_ARCH_ATTR,
  85. "DEB_BUILD_MULTIARCH" => DEB_BUILD | DEB_MULTIARCH,
  86. "DEB_BUILD_GNU_CPU" => DEB_BUILD | DEB_GNU_INFO,
  87. "DEB_BUILD_GNU_SYSTEM" => DEB_BUILD | DEB_GNU_INFO,
  88. "DEB_BUILD_GNU_TYPE" => DEB_BUILD | DEB_GNU_INFO,
  89. "DEB_HOST_ARCH" => DEB_HOST,
  90. "DEB_HOST_ARCH_OS" => DEB_HOST | DEB_ARCH_INFO,
  91. "DEB_HOST_ARCH_CPU" => DEB_HOST | DEB_ARCH_INFO,
  92. "DEB_HOST_ARCH_BITS" => DEB_HOST | DEB_ARCH_ATTR,
  93. "DEB_HOST_ARCH_ENDIAN" => DEB_HOST | DEB_ARCH_ATTR,
  94. "DEB_HOST_MULTIARCH" => DEB_HOST | DEB_MULTIARCH,
  95. "DEB_HOST_GNU_CPU" => DEB_HOST | DEB_GNU_INFO,
  96. "DEB_HOST_GNU_SYSTEM" => DEB_HOST | DEB_GNU_INFO,
  97. "DEB_HOST_GNU_TYPE" => DEB_HOST | DEB_GNU_INFO,
  98. );
  99. my $req_vars = DEB_ALL;
  100. my $req_host_arch = '';
  101. my $req_host_gnu_type = '';
  102. my $req_eq_arch = '';
  103. my $req_is_arch = '';
  104. my $req_variable_to_print;
  105. my $action = 'l';
  106. my $force = 0;
  107. sub action_needs($) {
  108. my ($bits) = @_;
  109. return (($req_vars & $bits) == $bits);
  110. }
  111. while (@ARGV) {
  112. $_=shift(@ARGV);
  113. if (m/^-a/) {
  114. $req_host_arch = "$'";
  115. } elsif (m/^-t/) {
  116. $req_host_gnu_type = "$'";
  117. } elsif (m/^-e/) {
  118. $req_eq_arch = "$'";
  119. $req_vars = $arch_vars{DEB_HOST_ARCH};
  120. $action = 'e';
  121. } elsif (m/^-i/) {
  122. $req_is_arch = "$'";
  123. $req_vars = $arch_vars{DEB_HOST_ARCH};
  124. $action = 'i';
  125. } elsif (m/^-u$/) {
  126. $req_vars = DEB_NONE;
  127. $action = 'u';
  128. } elsif (m/^-[ls]$/) {
  129. $action = $_;
  130. $action =~ s/^-//;
  131. } elsif (m/^-f$/) {
  132. $force=1;
  133. } elsif (m/^-q/) {
  134. my $varname = "$'";
  135. error(_g("%s is not a supported variable name"), $varname)
  136. unless (exists $arch_vars{$varname});
  137. $req_variable_to_print = "$varname";
  138. $req_vars = $arch_vars{$varname};
  139. $action = 'q';
  140. } elsif (m/^-c$/) {
  141. $action = 'c';
  142. last;
  143. } elsif (m/^-L$/) {
  144. list_arches();
  145. exit unless @ARGV;
  146. } elsif (m/^-(h|-help)$/) {
  147. usage();
  148. exit 0;
  149. } elsif (m/^--version$/) {
  150. version();
  151. exit 0;
  152. } else {
  153. usageerr(_g("unknown option \`%s'"), $_);
  154. }
  155. }
  156. my %v;
  157. my $abi;
  158. #
  159. # Set build variables
  160. #
  161. $v{DEB_BUILD_ARCH} = get_raw_build_arch()
  162. if (action_needs(DEB_BUILD));
  163. ($abi, $v{DEB_BUILD_ARCH_OS}, $v{DEB_BUILD_ARCH_CPU}) = debarch_to_debtriplet($v{DEB_BUILD_ARCH})
  164. if (action_needs(DEB_BUILD | DEB_ARCH_INFO));
  165. ($v{DEB_BUILD_ARCH_BITS}, $v{DEB_BUILD_ARCH_ENDIAN}) = debarch_to_cpuattrs($v{DEB_BUILD_ARCH})
  166. if (action_needs(DEB_BUILD | DEB_ARCH_ATTR));
  167. $v{DEB_BUILD_MULTIARCH} = debarch_to_multiarch($v{DEB_BUILD_ARCH})
  168. if (action_needs(DEB_BUILD | DEB_MULTIARCH));
  169. if (action_needs(DEB_BUILD | DEB_GNU_INFO)) {
  170. $v{DEB_BUILD_GNU_TYPE} = debarch_to_gnutriplet($v{DEB_BUILD_ARCH});
  171. ($v{DEB_BUILD_GNU_CPU}, $v{DEB_BUILD_GNU_SYSTEM}) = split(/-/, $v{DEB_BUILD_GNU_TYPE}, 2);
  172. }
  173. #
  174. # Set host variables
  175. #
  176. # First perform some sanity checks on the host arguments passed.
  177. if ($req_host_arch ne '' && $req_host_gnu_type eq '') {
  178. $req_host_gnu_type = debarch_to_gnutriplet($req_host_arch);
  179. error(_g("unknown Debian architecture %s, you must specify " .
  180. "GNU system type, too"), $req_host_arch)
  181. unless defined $req_host_gnu_type;
  182. }
  183. if ($req_host_gnu_type ne '' && $req_host_arch eq '') {
  184. $req_host_arch = gnutriplet_to_debarch($req_host_gnu_type);
  185. error(_g("unknown GNU system type %s, you must specify " .
  186. "Debian architecture, too"), $req_host_gnu_type)
  187. unless defined $req_host_arch;
  188. }
  189. if ($req_host_gnu_type ne '' && $req_host_arch ne '') {
  190. my $dfl_host_gnu_type = debarch_to_gnutriplet($req_host_arch);
  191. error(_g("unknown default GNU system type for Debian architecture %s"),
  192. $req_host_arch)
  193. unless defined $dfl_host_gnu_type;
  194. warning(_g("Default GNU system type %s for Debian arch %s does not " .
  195. "match specified GNU system type %s"), $dfl_host_gnu_type,
  196. $req_host_arch, $req_host_gnu_type)
  197. if $dfl_host_gnu_type ne $req_host_gnu_type;
  198. }
  199. # Proceed to compute the host variables if needed.
  200. if (action_needs(DEB_HOST)) {
  201. if ($req_host_arch eq '') {
  202. $v{DEB_HOST_ARCH} = get_raw_host_arch();
  203. } else {
  204. $v{DEB_HOST_ARCH} = $req_host_arch;
  205. }
  206. }
  207. ($abi, $v{DEB_HOST_ARCH_OS}, $v{DEB_HOST_ARCH_CPU}) = debarch_to_debtriplet($v{DEB_HOST_ARCH})
  208. if (action_needs(DEB_HOST | DEB_ARCH_INFO));
  209. ($v{DEB_HOST_ARCH_BITS}, $v{DEB_HOST_ARCH_ENDIAN}) = debarch_to_cpuattrs($v{DEB_HOST_ARCH})
  210. if (action_needs(DEB_HOST | DEB_ARCH_ATTR));
  211. $v{DEB_HOST_MULTIARCH} = debarch_to_multiarch($v{DEB_HOST_ARCH})
  212. if (action_needs(DEB_HOST | DEB_MULTIARCH));
  213. if (action_needs(DEB_HOST | DEB_GNU_INFO)) {
  214. if ($req_host_gnu_type eq '') {
  215. $v{DEB_HOST_GNU_TYPE} = debarch_to_gnutriplet($v{DEB_HOST_ARCH});
  216. } else {
  217. $v{DEB_HOST_GNU_TYPE} = $req_host_gnu_type;
  218. }
  219. ($v{DEB_HOST_GNU_CPU}, $v{DEB_HOST_GNU_SYSTEM}) = split(/-/, $v{DEB_HOST_GNU_TYPE}, 2);
  220. my $gcc = get_gcc_host_gnu_type();
  221. warning(_g("specified GNU system type %s does not match gcc system " .
  222. "type %s, try setting a correct CC environment variable"),
  223. $v{DEB_HOST_GNU_TYPE}, $gcc)
  224. if ($gcc ne '') && ($gcc ne $v{DEB_HOST_GNU_TYPE});
  225. }
  226. for my $k (keys %arch_vars) {
  227. $v{$k} = $ENV{$k} if (defined ($ENV{$k}) && !$force);
  228. }
  229. if ($action eq 'l') {
  230. foreach my $k (sort keys %arch_vars) {
  231. print "$k=$v{$k}\n";
  232. }
  233. } elsif ($action eq 's') {
  234. foreach my $k (sort keys %arch_vars) {
  235. print "$k=$v{$k}; ";
  236. }
  237. print "export " . join(" ", sort keys %arch_vars) . "\n";
  238. } elsif ($action eq 'u') {
  239. print "unset " . join(" ", sort keys %arch_vars) . "\n";
  240. } elsif ($action eq 'e') {
  241. exit !debarch_eq($v{DEB_HOST_ARCH}, $req_eq_arch);
  242. } elsif ($action eq 'i') {
  243. exit !debarch_is($v{DEB_HOST_ARCH}, $req_is_arch);
  244. } elsif ($action eq 'c') {
  245. @ENV{keys %v} = values %v;
  246. exec @ARGV;
  247. } elsif ($action eq 'q') {
  248. print "$v{$req_variable_to_print}\n";
  249. }