dpkg-architecture.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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.
  46. -e<debian-arch> compare with host Debian architecture.
  47. -i<arch-alias> check if host Debian architecture is <arch-alias>.
  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. -f force flag (override variables set in environment).')
  61. . "\n", $Dpkg::PROGNAME;
  62. }
  63. sub list_arches()
  64. {
  65. foreach my $arch (get_valid_arches()) {
  66. print "$arch\n";
  67. }
  68. }
  69. sub check_arch_coherency
  70. {
  71. my ($arch, $gnu_type) = @_;
  72. if ($arch ne '' && $gnu_type eq '') {
  73. $gnu_type = debarch_to_gnutriplet($arch);
  74. error(_g('unknown Debian architecture %s, you must specify ' .
  75. 'GNU system type, too'), $arch)
  76. unless defined $gnu_type;
  77. }
  78. if ($gnu_type ne '' && $arch eq '') {
  79. $arch = gnutriplet_to_debarch($gnu_type);
  80. error(_g('unknown GNU system type %s, you must specify ' .
  81. 'Debian architecture, too'), $gnu_type)
  82. unless defined $arch;
  83. }
  84. if ($gnu_type ne '' && $arch ne '') {
  85. my $dfl_gnu_type = debarch_to_gnutriplet($arch);
  86. error(_g('unknown default GNU system type for Debian architecture %s'),
  87. $arch)
  88. unless defined $dfl_gnu_type;
  89. warning(_g('default GNU system type %s for Debian arch %s does not ' .
  90. 'match specified GNU system type %s'), $dfl_gnu_type,
  91. $arch, $gnu_type)
  92. if $dfl_gnu_type ne $gnu_type;
  93. }
  94. return ($arch, $gnu_type);
  95. }
  96. use constant {
  97. DEB_NONE => 0,
  98. DEB_BUILD => 1,
  99. DEB_HOST => 2,
  100. DEB_TARGET => 64,
  101. DEB_ARCH_INFO => 4,
  102. DEB_ARCH_ATTR => 8,
  103. DEB_MULTIARCH => 16,
  104. DEB_GNU_INFO => 32,
  105. };
  106. use constant DEB_ALL => DEB_BUILD | DEB_HOST | DEB_TARGET |
  107. DEB_ARCH_INFO | DEB_ARCH_ATTR |
  108. DEB_MULTIARCH | DEB_GNU_INFO;
  109. my %arch_vars = (
  110. DEB_BUILD_ARCH => DEB_BUILD,
  111. DEB_BUILD_ARCH_OS => DEB_BUILD | DEB_ARCH_INFO,
  112. DEB_BUILD_ARCH_CPU => DEB_BUILD | DEB_ARCH_INFO,
  113. DEB_BUILD_ARCH_BITS => DEB_BUILD | DEB_ARCH_ATTR,
  114. DEB_BUILD_ARCH_ENDIAN => DEB_BUILD | DEB_ARCH_ATTR,
  115. DEB_BUILD_MULTIARCH => DEB_BUILD | DEB_MULTIARCH,
  116. DEB_BUILD_GNU_CPU => DEB_BUILD | DEB_GNU_INFO,
  117. DEB_BUILD_GNU_SYSTEM => DEB_BUILD | DEB_GNU_INFO,
  118. DEB_BUILD_GNU_TYPE => DEB_BUILD | DEB_GNU_INFO,
  119. DEB_HOST_ARCH => DEB_HOST,
  120. DEB_HOST_ARCH_OS => DEB_HOST | DEB_ARCH_INFO,
  121. DEB_HOST_ARCH_CPU => DEB_HOST | DEB_ARCH_INFO,
  122. DEB_HOST_ARCH_BITS => DEB_HOST | DEB_ARCH_ATTR,
  123. DEB_HOST_ARCH_ENDIAN => DEB_HOST | DEB_ARCH_ATTR,
  124. DEB_HOST_MULTIARCH => DEB_HOST | DEB_MULTIARCH,
  125. DEB_HOST_GNU_CPU => DEB_HOST | DEB_GNU_INFO,
  126. DEB_HOST_GNU_SYSTEM => DEB_HOST | DEB_GNU_INFO,
  127. DEB_HOST_GNU_TYPE => DEB_HOST | DEB_GNU_INFO,
  128. DEB_TARGET_ARCH => DEB_TARGET,
  129. DEB_TARGET_ARCH_OS => DEB_TARGET | DEB_ARCH_INFO,
  130. DEB_TARGET_ARCH_CPU => DEB_TARGET | DEB_ARCH_INFO,
  131. DEB_TARGET_ARCH_BITS => DEB_TARGET | DEB_ARCH_ATTR,
  132. DEB_TARGET_ARCH_ENDIAN => DEB_TARGET | DEB_ARCH_ATTR,
  133. DEB_TARGET_MULTIARCH => DEB_TARGET | DEB_MULTIARCH,
  134. DEB_TARGET_GNU_CPU => DEB_TARGET | DEB_GNU_INFO,
  135. DEB_TARGET_GNU_SYSTEM => DEB_TARGET | DEB_GNU_INFO,
  136. DEB_TARGET_GNU_TYPE => DEB_TARGET | DEB_GNU_INFO,
  137. );
  138. my $req_vars = DEB_ALL;
  139. my $req_host_arch = '';
  140. my $req_host_gnu_type = '';
  141. my $req_target_arch = '';
  142. my $req_target_gnu_type = '';
  143. my $req_eq_arch = '';
  144. my $req_is_arch = '';
  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/^-e/p) {
  163. $req_eq_arch = ${^POSTMATCH};
  164. $req_vars = $arch_vars{DEB_HOST_ARCH};
  165. $action = 'e';
  166. } elsif (m/^-i/p) {
  167. $req_is_arch = ${^POSTMATCH};
  168. $req_vars = $arch_vars{DEB_HOST_ARCH};
  169. $action = 'i';
  170. } elsif (m/^-u$/) {
  171. $req_vars = DEB_NONE;
  172. $action = 'u';
  173. } elsif (m/^-[ls]$/) {
  174. $action = $_;
  175. $action =~ s/^-//;
  176. } elsif (m/^-f$/) {
  177. $force=1;
  178. } elsif (m/^-q/p) {
  179. my $varname = ${^POSTMATCH};
  180. error(_g('%s is not a supported variable name'), $varname)
  181. unless (exists $arch_vars{$varname});
  182. $req_variable_to_print = "$varname";
  183. $req_vars = $arch_vars{$varname};
  184. $action = 'q';
  185. } elsif (m/^-c$/) {
  186. $action = 'c';
  187. last;
  188. } elsif (m/^-L$/) {
  189. list_arches();
  190. exit unless @ARGV;
  191. } elsif (m/^-(?:\?|-help)$/) {
  192. usage();
  193. exit 0;
  194. } elsif (m/^--version$/) {
  195. version();
  196. exit 0;
  197. } else {
  198. usageerr(_g("unknown option \`%s'"), $_);
  199. }
  200. }
  201. my %v;
  202. my $abi;
  203. #
  204. # Set build variables
  205. #
  206. $v{DEB_BUILD_ARCH} = get_raw_build_arch()
  207. if (action_needs(DEB_BUILD));
  208. ($abi, $v{DEB_BUILD_ARCH_OS}, $v{DEB_BUILD_ARCH_CPU}) = debarch_to_debtriplet($v{DEB_BUILD_ARCH})
  209. if (action_needs(DEB_BUILD | DEB_ARCH_INFO));
  210. ($v{DEB_BUILD_ARCH_BITS}, $v{DEB_BUILD_ARCH_ENDIAN}) = debarch_to_cpuattrs($v{DEB_BUILD_ARCH})
  211. if (action_needs(DEB_BUILD | DEB_ARCH_ATTR));
  212. $v{DEB_BUILD_MULTIARCH} = debarch_to_multiarch($v{DEB_BUILD_ARCH})
  213. if (action_needs(DEB_BUILD | DEB_MULTIARCH));
  214. if (action_needs(DEB_BUILD | DEB_GNU_INFO)) {
  215. $v{DEB_BUILD_GNU_TYPE} = debarch_to_gnutriplet($v{DEB_BUILD_ARCH});
  216. ($v{DEB_BUILD_GNU_CPU}, $v{DEB_BUILD_GNU_SYSTEM}) = split(/-/, $v{DEB_BUILD_GNU_TYPE}, 2);
  217. }
  218. #
  219. # Set host variables
  220. #
  221. # First perform some sanity checks on the host arguments passed.
  222. ($req_host_arch, $req_host_gnu_type) = check_arch_coherency($req_host_arch, $req_host_gnu_type);
  223. # Proceed to compute the host variables if needed.
  224. if (action_needs(DEB_HOST)) {
  225. if ($req_host_arch eq '') {
  226. $v{DEB_HOST_ARCH} = get_raw_host_arch();
  227. } else {
  228. $v{DEB_HOST_ARCH} = $req_host_arch;
  229. }
  230. }
  231. ($abi, $v{DEB_HOST_ARCH_OS}, $v{DEB_HOST_ARCH_CPU}) = debarch_to_debtriplet($v{DEB_HOST_ARCH})
  232. if (action_needs(DEB_HOST | DEB_ARCH_INFO));
  233. ($v{DEB_HOST_ARCH_BITS}, $v{DEB_HOST_ARCH_ENDIAN}) = debarch_to_cpuattrs($v{DEB_HOST_ARCH})
  234. if (action_needs(DEB_HOST | DEB_ARCH_ATTR));
  235. $v{DEB_HOST_MULTIARCH} = debarch_to_multiarch($v{DEB_HOST_ARCH})
  236. if (action_needs(DEB_HOST | DEB_MULTIARCH));
  237. if (action_needs(DEB_HOST | DEB_GNU_INFO)) {
  238. if ($req_host_gnu_type eq '') {
  239. $v{DEB_HOST_GNU_TYPE} = debarch_to_gnutriplet($v{DEB_HOST_ARCH});
  240. } else {
  241. $v{DEB_HOST_GNU_TYPE} = $req_host_gnu_type;
  242. }
  243. ($v{DEB_HOST_GNU_CPU}, $v{DEB_HOST_GNU_SYSTEM}) = split(/-/, $v{DEB_HOST_GNU_TYPE}, 2);
  244. my $gcc = get_gcc_host_gnu_type();
  245. warning(_g('specified GNU system type %s does not match gcc system ' .
  246. 'type %s, try setting a correct CC environment variable'),
  247. $v{DEB_HOST_GNU_TYPE}, $gcc)
  248. if ($gcc ne '') && ($gcc ne $v{DEB_HOST_GNU_TYPE});
  249. }
  250. #
  251. # Set target variables
  252. #
  253. # First perform some sanity checks on the target arguments passed.
  254. ($req_target_arch, $req_target_gnu_type) = check_arch_coherency($req_target_arch, $req_target_gnu_type);
  255. # Proceed to compute the target variables if needed.
  256. if (action_needs(DEB_TARGET)) {
  257. if ($req_target_arch eq '') {
  258. $v{DEB_TARGET_ARCH} = $v{DEB_HOST_ARCH};
  259. } else {
  260. $v{DEB_TARGET_ARCH} = $req_target_arch;
  261. }
  262. }
  263. ($abi, $v{DEB_TARGET_ARCH_OS}, $v{DEB_TARGET_ARCH_CPU}) = debarch_to_debtriplet($v{DEB_TARGET_ARCH})
  264. if (action_needs(DEB_TARGET | DEB_ARCH_INFO));
  265. ($v{DEB_TARGET_ARCH_BITS}, $v{DEB_TARGET_ARCH_ENDIAN}) = debarch_to_cpuattrs($v{DEB_TARGET_ARCH})
  266. if (action_needs(DEB_TARGET | DEB_ARCH_ATTR));
  267. $v{DEB_TARGET_MULTIARCH} = debarch_to_multiarch($v{DEB_TARGET_ARCH})
  268. if (action_needs(DEB_TARGET | DEB_MULTIARCH));
  269. if (action_needs(DEB_TARGET | DEB_GNU_INFO)) {
  270. if ($req_target_gnu_type eq '') {
  271. $v{DEB_TARGET_GNU_TYPE} = debarch_to_gnutriplet($v{DEB_TARGET_ARCH});
  272. } else {
  273. $v{DEB_TARGET_GNU_TYPE} = $req_target_gnu_type;
  274. }
  275. ($v{DEB_TARGET_GNU_CPU}, $v{DEB_TARGET_GNU_SYSTEM}) = split(/-/, $v{DEB_TARGET_GNU_TYPE}, 2);
  276. }
  277. for my $k (keys %arch_vars) {
  278. $v{$k} = $ENV{$k} if (length $ENV{$k} && !$force);
  279. }
  280. if ($action eq 'l') {
  281. foreach my $k (sort keys %arch_vars) {
  282. print "$k=$v{$k}\n";
  283. }
  284. } elsif ($action eq 's') {
  285. foreach my $k (sort keys %arch_vars) {
  286. print "$k=$v{$k}; ";
  287. }
  288. print 'export ' . join(' ', sort keys %arch_vars) . "\n";
  289. } elsif ($action eq 'u') {
  290. print 'unset ' . join(' ', sort keys %arch_vars) . "\n";
  291. } elsif ($action eq 'e') {
  292. exit !debarch_eq($v{DEB_HOST_ARCH}, $req_eq_arch);
  293. } elsif ($action eq 'i') {
  294. exit !debarch_is($v{DEB_HOST_ARCH}, $req_is_arch);
  295. } elsif ($action eq 'c') {
  296. @ENV{keys %v} = values %v;
  297. exec @ARGV;
  298. } elsif ($action eq 'q') {
  299. print "$v{$req_variable_to_print}\n";
  300. }