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