dpkg-architecture.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-architecture
  4. #
  5. # Copyright © 1999-2001 Marcus Brinkmann <brinkmd@debian.org>
  6. # Copyright © 2004-2005 Scott James Remnant <scott@netsplit.com>,
  7. # Copyright © 2006-2014 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 <https://www.gnu.org/licenses/>.
  21. use strict;
  22. use warnings;
  23. use Dpkg ();
  24. use Dpkg::Gettext;
  25. use Dpkg::Getopt;
  26. use Dpkg::ErrorHandling;
  27. use Dpkg::Arch qw(:getters :mappers debarch_eq debarch_is);
  28. textdomain('dpkg-dev');
  29. sub version {
  30. printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  31. printf g_('
  32. This is free software; see the GNU General Public License version 2 or
  33. later for copying conditions. There is NO warranty.
  34. ');
  35. }
  36. sub usage {
  37. printf g_(
  38. 'Usage: %s [<option>...] [<command>]')
  39. . "\n\n" . g_(
  40. 'Commands:
  41. -l, --list list variables (default).
  42. -L, --list-known list valid architectures (matching some criteria).
  43. -e, --equal <arch> compare with host Debian architecture.
  44. -i, --is <arch-wildcard> match against host Debian architecture.
  45. -q, --query <variable> prints only the value of <variable>.
  46. -s, --print-set print command to set environment variables.
  47. -u, --print-unset print command to unset environment variables.
  48. -c, --command <command> set environment and run the command in it.
  49. -?, --help show this help message.
  50. --version show the version.')
  51. . "\n\n" . g_(
  52. 'Options:
  53. -a, --host-arch <arch> set host Debian architecture.
  54. -t, --host-type <type> set host GNU system type.
  55. -A, --target-arch <arch> set target Debian architecture.
  56. -T, --target-type <type> set target GNU system type.
  57. -W, --match-wildcard <arch-wildcard>
  58. restrict architecture list matching <arch-wildcard>.
  59. -B, --match-bits <arch-bits>
  60. restrict architecture list matching <arch-bits>.
  61. -E, --match-endian <arch-endian>
  62. restrict architecture list matching <arch-endian>.
  63. -f, --force force flag (override variables set in environment).')
  64. . "\n", $Dpkg::PROGNAME;
  65. }
  66. sub check_arch_coherency
  67. {
  68. my ($arch, $gnu_type) = @_;
  69. if ($arch ne '' && $gnu_type eq '') {
  70. $gnu_type = debarch_to_gnutriplet($arch);
  71. error(g_('unknown Debian architecture %s, you must specify ' .
  72. 'GNU system type, too'), $arch)
  73. unless defined $gnu_type;
  74. }
  75. if ($gnu_type ne '' && $arch eq '') {
  76. $arch = gnutriplet_to_debarch($gnu_type);
  77. error(g_('unknown GNU system type %s, you must specify ' .
  78. 'Debian architecture, too'), $gnu_type)
  79. unless defined $arch;
  80. }
  81. if ($gnu_type ne '' && $arch ne '') {
  82. my $dfl_gnu_type = debarch_to_gnutriplet($arch);
  83. error(g_('unknown default GNU system type for Debian architecture %s'),
  84. $arch)
  85. unless defined $dfl_gnu_type;
  86. warning(g_('default GNU system type %s for Debian arch %s does not ' .
  87. 'match specified GNU system type %s'), $dfl_gnu_type,
  88. $arch, $gnu_type)
  89. if $dfl_gnu_type ne $gnu_type;
  90. }
  91. return ($arch, $gnu_type);
  92. }
  93. use constant {
  94. DEB_NONE => 0,
  95. DEB_BUILD => 1,
  96. DEB_HOST => 2,
  97. DEB_TARGET => 64,
  98. DEB_ARCH_INFO => 4,
  99. DEB_ARCH_ATTR => 8,
  100. DEB_MULTIARCH => 16,
  101. DEB_GNU_INFO => 32,
  102. };
  103. use constant DEB_ALL => DEB_BUILD | DEB_HOST | DEB_TARGET |
  104. DEB_ARCH_INFO | DEB_ARCH_ATTR |
  105. DEB_MULTIARCH | DEB_GNU_INFO;
  106. my %arch_vars = (
  107. DEB_BUILD_ARCH => DEB_BUILD,
  108. DEB_BUILD_ARCH_ABI => DEB_BUILD | DEB_ARCH_INFO,
  109. DEB_BUILD_ARCH_LIBC => DEB_BUILD | DEB_ARCH_INFO,
  110. DEB_BUILD_ARCH_OS => DEB_BUILD | DEB_ARCH_INFO,
  111. DEB_BUILD_ARCH_CPU => DEB_BUILD | DEB_ARCH_INFO,
  112. DEB_BUILD_ARCH_BITS => DEB_BUILD | DEB_ARCH_ATTR,
  113. DEB_BUILD_ARCH_ENDIAN => DEB_BUILD | DEB_ARCH_ATTR,
  114. DEB_BUILD_MULTIARCH => DEB_BUILD | DEB_MULTIARCH,
  115. DEB_BUILD_GNU_CPU => DEB_BUILD | DEB_GNU_INFO,
  116. DEB_BUILD_GNU_SYSTEM => DEB_BUILD | DEB_GNU_INFO,
  117. DEB_BUILD_GNU_TYPE => DEB_BUILD | DEB_GNU_INFO,
  118. DEB_HOST_ARCH => DEB_HOST,
  119. DEB_HOST_ARCH_ABI => DEB_HOST | DEB_ARCH_INFO,
  120. DEB_HOST_ARCH_LIBC => DEB_HOST | DEB_ARCH_INFO,
  121. DEB_HOST_ARCH_OS => DEB_HOST | DEB_ARCH_INFO,
  122. DEB_HOST_ARCH_CPU => DEB_HOST | DEB_ARCH_INFO,
  123. DEB_HOST_ARCH_BITS => DEB_HOST | DEB_ARCH_ATTR,
  124. DEB_HOST_ARCH_ENDIAN => DEB_HOST | DEB_ARCH_ATTR,
  125. DEB_HOST_MULTIARCH => DEB_HOST | DEB_MULTIARCH,
  126. DEB_HOST_GNU_CPU => DEB_HOST | DEB_GNU_INFO,
  127. DEB_HOST_GNU_SYSTEM => DEB_HOST | DEB_GNU_INFO,
  128. DEB_HOST_GNU_TYPE => DEB_HOST | DEB_GNU_INFO,
  129. DEB_TARGET_ARCH => DEB_TARGET,
  130. DEB_TARGET_ARCH_ABI => DEB_TARGET | DEB_ARCH_INFO,
  131. DEB_TARGET_ARCH_LIBC => DEB_TARGET | DEB_ARCH_INFO,
  132. DEB_TARGET_ARCH_OS => DEB_TARGET | DEB_ARCH_INFO,
  133. DEB_TARGET_ARCH_CPU => DEB_TARGET | DEB_ARCH_INFO,
  134. DEB_TARGET_ARCH_BITS => DEB_TARGET | DEB_ARCH_ATTR,
  135. DEB_TARGET_ARCH_ENDIAN => DEB_TARGET | DEB_ARCH_ATTR,
  136. DEB_TARGET_MULTIARCH => DEB_TARGET | DEB_MULTIARCH,
  137. DEB_TARGET_GNU_CPU => DEB_TARGET | DEB_GNU_INFO,
  138. DEB_TARGET_GNU_SYSTEM => DEB_TARGET | DEB_GNU_INFO,
  139. DEB_TARGET_GNU_TYPE => DEB_TARGET | DEB_GNU_INFO,
  140. );
  141. my $req_vars = DEB_ALL;
  142. my $req_host_arch = '';
  143. my $req_host_gnu_type = '';
  144. my $req_target_arch = '';
  145. my $req_target_gnu_type = '';
  146. my $req_eq_arch = '';
  147. my $req_is_arch = '';
  148. my $req_match_wildcard = '';
  149. my $req_match_bits = '';
  150. my $req_match_endian = '';
  151. my $req_variable_to_print;
  152. my $action = 'list';
  153. my $force = 0;
  154. sub action_needs($) {
  155. my $bits = shift;
  156. return (($req_vars & $bits) == $bits);
  157. }
  158. @ARGV = normalize_options(@ARGV);
  159. while (@ARGV) {
  160. my $arg = shift;
  161. if ($arg eq '-a' or $arg eq '--host-arch') {
  162. $req_host_arch = shift;
  163. } elsif ($arg eq '-t' or $arg eq '--host-type') {
  164. $req_host_gnu_type = shift;
  165. } elsif ($arg eq '-A' or $arg eq '--target-arch') {
  166. $req_target_arch = shift;
  167. } elsif ($arg eq '-T' or $arg eq '--target-type') {
  168. $req_target_gnu_type = shift;
  169. } elsif ($arg eq '-W' or $arg eq '--match-wildcard') {
  170. $req_match_wildcard = shift;
  171. } elsif ($arg eq '-B' or $arg eq '--match-bits') {
  172. $req_match_bits = shift;
  173. } elsif ($arg eq '-E' or $arg eq '--match-endian') {
  174. $req_match_endian = shift;
  175. } elsif ($arg eq '-e' or $arg eq '--equal') {
  176. $req_eq_arch = shift;
  177. $req_vars = $arch_vars{DEB_HOST_ARCH};
  178. $action = 'equal';
  179. } elsif ($arg eq '-i' or $arg eq '--is') {
  180. $req_is_arch = shift;
  181. $req_vars = $arch_vars{DEB_HOST_ARCH};
  182. $action = 'is';
  183. } elsif ($arg eq '-u' or $arg eq '--print-unset') {
  184. $req_vars = DEB_NONE;
  185. $action = 'print-unset';
  186. } elsif ($arg eq '-l' or $arg eq '--list') {
  187. $action = 'list';
  188. } elsif ($arg eq '-s' or $arg eq '--print-set') {
  189. $req_vars = DEB_ALL;
  190. $action = 'print-set';
  191. } elsif ($arg eq '-f' or $arg eq '--force') {
  192. $force=1;
  193. } elsif ($arg eq '-q' or $arg eq '--query') {
  194. my $varname = shift;
  195. error(g_('%s is not a supported variable name'), $varname)
  196. unless (exists $arch_vars{$varname});
  197. $req_variable_to_print = "$varname";
  198. $req_vars = $arch_vars{$varname};
  199. $action = 'query';
  200. } elsif ($arg eq '-c' or $arg eq '--command') {
  201. $action = 'command';
  202. last;
  203. } elsif ($arg eq '-L' or $arg eq '--list-known') {
  204. $req_vars = 0;
  205. $action = 'list-known';
  206. } elsif ($arg eq '-?' or $arg eq '--help') {
  207. usage();
  208. exit 0;
  209. } elsif ($arg eq '--version') {
  210. version();
  211. exit 0;
  212. } else {
  213. usageerr(g_("unknown option '%s'"), $arg);
  214. }
  215. }
  216. my %v;
  217. #
  218. # Set build variables
  219. #
  220. $v{DEB_BUILD_ARCH} = get_raw_build_arch()
  221. if (action_needs(DEB_BUILD));
  222. ($v{DEB_BUILD_ARCH_ABI}, $v{DEB_BUILD_ARCH_LIBC},
  223. $v{DEB_BUILD_ARCH_OS}, $v{DEB_BUILD_ARCH_CPU}) = debarch_to_debtuple($v{DEB_BUILD_ARCH})
  224. if (action_needs(DEB_BUILD | DEB_ARCH_INFO));
  225. ($v{DEB_BUILD_ARCH_BITS}, $v{DEB_BUILD_ARCH_ENDIAN}) = debarch_to_cpuattrs($v{DEB_BUILD_ARCH})
  226. if (action_needs(DEB_BUILD | DEB_ARCH_ATTR));
  227. $v{DEB_BUILD_MULTIARCH} = debarch_to_multiarch($v{DEB_BUILD_ARCH})
  228. if (action_needs(DEB_BUILD | DEB_MULTIARCH));
  229. if (action_needs(DEB_BUILD | DEB_GNU_INFO)) {
  230. $v{DEB_BUILD_GNU_TYPE} = debarch_to_gnutriplet($v{DEB_BUILD_ARCH});
  231. ($v{DEB_BUILD_GNU_CPU}, $v{DEB_BUILD_GNU_SYSTEM}) = split(/-/, $v{DEB_BUILD_GNU_TYPE}, 2);
  232. }
  233. #
  234. # Set host variables
  235. #
  236. # First perform some sanity checks on the host arguments passed.
  237. ($req_host_arch, $req_host_gnu_type) = check_arch_coherency($req_host_arch, $req_host_gnu_type);
  238. # Proceed to compute the host variables if needed.
  239. $v{DEB_HOST_ARCH} = $req_host_arch || get_raw_host_arch()
  240. if (action_needs(DEB_HOST));
  241. ($v{DEB_HOST_ARCH_ABI}, $v{DEB_HOST_ARCH_LIBC},
  242. $v{DEB_HOST_ARCH_OS}, $v{DEB_HOST_ARCH_CPU}) = debarch_to_debtuple($v{DEB_HOST_ARCH})
  243. if (action_needs(DEB_HOST | DEB_ARCH_INFO));
  244. ($v{DEB_HOST_ARCH_BITS}, $v{DEB_HOST_ARCH_ENDIAN}) = debarch_to_cpuattrs($v{DEB_HOST_ARCH})
  245. if (action_needs(DEB_HOST | DEB_ARCH_ATTR));
  246. $v{DEB_HOST_MULTIARCH} = debarch_to_multiarch($v{DEB_HOST_ARCH})
  247. if (action_needs(DEB_HOST | DEB_MULTIARCH));
  248. if (action_needs(DEB_HOST | DEB_GNU_INFO)) {
  249. if ($req_host_gnu_type eq '') {
  250. $v{DEB_HOST_GNU_TYPE} = debarch_to_gnutriplet($v{DEB_HOST_ARCH});
  251. } else {
  252. $v{DEB_HOST_GNU_TYPE} = $req_host_gnu_type;
  253. }
  254. ($v{DEB_HOST_GNU_CPU}, $v{DEB_HOST_GNU_SYSTEM}) = split(/-/, $v{DEB_HOST_GNU_TYPE}, 2);
  255. my $host_gnu_type = get_host_gnu_type();
  256. warning(g_('specified GNU system type %s does not match CC system ' .
  257. 'type %s, try setting a correct CC environment variable'),
  258. $v{DEB_HOST_GNU_TYPE}, $host_gnu_type)
  259. if ($host_gnu_type ne '') && ($host_gnu_type ne $v{DEB_HOST_GNU_TYPE});
  260. }
  261. #
  262. # Set target variables
  263. #
  264. # First perform some sanity checks on the target arguments passed.
  265. ($req_target_arch, $req_target_gnu_type) = check_arch_coherency($req_target_arch, $req_target_gnu_type);
  266. # Proceed to compute the target variables if needed.
  267. $v{DEB_TARGET_ARCH} = $req_target_arch || $req_host_arch || get_raw_host_arch()
  268. if (action_needs(DEB_TARGET));
  269. ($v{DEB_TARGET_ARCH_ABI}, $v{DEB_TARGET_ARCH_LIBC},
  270. $v{DEB_TARGET_ARCH_OS}, $v{DEB_TARGET_ARCH_CPU}) = debarch_to_debtuple($v{DEB_TARGET_ARCH})
  271. if (action_needs(DEB_TARGET | DEB_ARCH_INFO));
  272. ($v{DEB_TARGET_ARCH_BITS}, $v{DEB_TARGET_ARCH_ENDIAN}) = debarch_to_cpuattrs($v{DEB_TARGET_ARCH})
  273. if (action_needs(DEB_TARGET | DEB_ARCH_ATTR));
  274. $v{DEB_TARGET_MULTIARCH} = debarch_to_multiarch($v{DEB_TARGET_ARCH})
  275. if (action_needs(DEB_TARGET | DEB_MULTIARCH));
  276. if (action_needs(DEB_TARGET | DEB_GNU_INFO)) {
  277. if ($req_target_gnu_type eq '') {
  278. $v{DEB_TARGET_GNU_TYPE} = debarch_to_gnutriplet($v{DEB_TARGET_ARCH});
  279. } else {
  280. $v{DEB_TARGET_GNU_TYPE} = $req_target_gnu_type;
  281. }
  282. ($v{DEB_TARGET_GNU_CPU}, $v{DEB_TARGET_GNU_SYSTEM}) = split(/-/, $v{DEB_TARGET_GNU_TYPE}, 2);
  283. }
  284. for my $k (keys %arch_vars) {
  285. $v{$k} = $ENV{$k} if (length $ENV{$k} && !$force);
  286. }
  287. if ($action eq 'list') {
  288. foreach my $k (sort keys %arch_vars) {
  289. print "$k=$v{$k}\n";
  290. }
  291. } elsif ($action eq 'print-set') {
  292. foreach my $k (sort keys %arch_vars) {
  293. print "$k=$v{$k}; ";
  294. }
  295. print 'export ' . join(' ', sort keys %arch_vars) . "\n";
  296. } elsif ($action eq 'print-unset') {
  297. print 'unset ' . join(' ', sort keys %arch_vars) . "\n";
  298. } elsif ($action eq 'equal') {
  299. exit !debarch_eq($v{DEB_HOST_ARCH}, $req_eq_arch);
  300. } elsif ($action eq 'is') {
  301. exit !debarch_is($v{DEB_HOST_ARCH}, $req_is_arch);
  302. } elsif ($action eq 'command') {
  303. @ENV{keys %v} = values %v;
  304. exec @ARGV;
  305. } elsif ($action eq 'query') {
  306. print "$v{$req_variable_to_print}\n";
  307. } elsif ($action eq 'list-known') {
  308. foreach my $arch (get_valid_arches()) {
  309. my ($bits, $endian) = debarch_to_cpuattrs($arch);
  310. next if $req_match_endian and $endian ne $req_match_endian;
  311. next if $req_match_bits and $bits ne $req_match_bits;
  312. next if $req_match_wildcard and not debarch_is($arch, $req_match_wildcard);
  313. print "$arch\n";
  314. }
  315. }