dpkg-architecture.pl 12 KB

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