dpkg-architecture.pl 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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-2012 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. 'Options:
  44. -a<debian-arch> set current Debian architecture.
  45. -t<gnu-system> set current GNU system type.
  46. -L list valid architectures.
  47. -f force flag (override variables set in environment).')
  48. . "\n\n" . _g(
  49. 'Commands:
  50. -l list variables (default).
  51. -e<debian-arch> compare with current Debian architecture.
  52. -i<arch-alias> check if current Debian architecture is <arch-alias>.
  53. -q<variable> prints only the value of <variable>.
  54. -s print command to set environment variables.
  55. -u print command to unset environment variables.
  56. -c <command> set environment and run the command in it.
  57. -?, --help show this help message.
  58. --version show the version.
  59. '), $Dpkg::PROGNAME;
  60. }
  61. sub list_arches()
  62. {
  63. foreach my $arch (get_valid_arches()) {
  64. print "$arch\n";
  65. }
  66. }
  67. use constant {
  68. DEB_NONE => 0,
  69. DEB_BUILD => 1,
  70. DEB_HOST => 2,
  71. DEB_ARCH_INFO => 4,
  72. DEB_ARCH_ATTR => 8,
  73. DEB_MULTIARCH => 16,
  74. DEB_GNU_INFO => 32,
  75. };
  76. use constant DEB_ALL => DEB_BUILD | DEB_HOST | DEB_ARCH_INFO | DEB_ARCH_ATTR |
  77. DEB_MULTIARCH | DEB_GNU_INFO;
  78. my %arch_vars = (
  79. DEB_BUILD_ARCH => DEB_BUILD,
  80. DEB_BUILD_ARCH_OS => DEB_BUILD | DEB_ARCH_INFO,
  81. DEB_BUILD_ARCH_CPU => DEB_BUILD | DEB_ARCH_INFO,
  82. DEB_BUILD_ARCH_BITS => DEB_BUILD | DEB_ARCH_ATTR,
  83. DEB_BUILD_ARCH_ENDIAN => DEB_BUILD | DEB_ARCH_ATTR,
  84. DEB_BUILD_MULTIARCH => DEB_BUILD | DEB_MULTIARCH,
  85. DEB_BUILD_GNU_CPU => DEB_BUILD | DEB_GNU_INFO,
  86. DEB_BUILD_GNU_SYSTEM => DEB_BUILD | DEB_GNU_INFO,
  87. DEB_BUILD_GNU_TYPE => DEB_BUILD | DEB_GNU_INFO,
  88. DEB_HOST_ARCH => DEB_HOST,
  89. DEB_HOST_ARCH_OS => DEB_HOST | DEB_ARCH_INFO,
  90. DEB_HOST_ARCH_CPU => DEB_HOST | DEB_ARCH_INFO,
  91. DEB_HOST_ARCH_BITS => DEB_HOST | DEB_ARCH_ATTR,
  92. DEB_HOST_ARCH_ENDIAN => DEB_HOST | DEB_ARCH_ATTR,
  93. DEB_HOST_MULTIARCH => DEB_HOST | DEB_MULTIARCH,
  94. DEB_HOST_GNU_CPU => DEB_HOST | DEB_GNU_INFO,
  95. DEB_HOST_GNU_SYSTEM => DEB_HOST | DEB_GNU_INFO,
  96. DEB_HOST_GNU_TYPE => DEB_HOST | DEB_GNU_INFO,
  97. );
  98. my $req_vars = DEB_ALL;
  99. my $req_host_arch = '';
  100. my $req_host_gnu_type = '';
  101. my $req_eq_arch = '';
  102. my $req_is_arch = '';
  103. my $req_variable_to_print;
  104. my $action = 'l';
  105. my $force = 0;
  106. sub action_needs($) {
  107. my ($bits) = @_;
  108. return (($req_vars & $bits) == $bits);
  109. }
  110. while (@ARGV) {
  111. $_=shift(@ARGV);
  112. if (m/^-a/) {
  113. $req_host_arch = "$'";
  114. } elsif (m/^-t/) {
  115. $req_host_gnu_type = "$'";
  116. } elsif (m/^-e/) {
  117. $req_eq_arch = "$'";
  118. $req_vars = $arch_vars{DEB_HOST_ARCH};
  119. $action = 'e';
  120. } elsif (m/^-i/) {
  121. $req_is_arch = "$'";
  122. $req_vars = $arch_vars{DEB_HOST_ARCH};
  123. $action = 'i';
  124. } elsif (m/^-u$/) {
  125. $req_vars = DEB_NONE;
  126. $action = 'u';
  127. } elsif (m/^-[ls]$/) {
  128. $action = $_;
  129. $action =~ s/^-//;
  130. } elsif (m/^-f$/) {
  131. $force=1;
  132. } elsif (m/^-q/) {
  133. my $varname = "$'";
  134. error(_g('%s is not a supported variable name'), $varname)
  135. unless (exists $arch_vars{$varname});
  136. $req_variable_to_print = "$varname";
  137. $req_vars = $arch_vars{$varname};
  138. $action = 'q';
  139. } elsif (m/^-c$/) {
  140. $action = 'c';
  141. last;
  142. } elsif (m/^-L$/) {
  143. list_arches();
  144. exit unless @ARGV;
  145. } elsif (m/^-(\?|-help)$/) {
  146. usage();
  147. exit 0;
  148. } elsif (m/^--version$/) {
  149. version();
  150. exit 0;
  151. } else {
  152. usageerr(_g("unknown option \`%s'"), $_);
  153. }
  154. }
  155. my %v;
  156. my $abi;
  157. #
  158. # Set build variables
  159. #
  160. $v{DEB_BUILD_ARCH} = get_raw_build_arch()
  161. if (action_needs(DEB_BUILD));
  162. ($abi, $v{DEB_BUILD_ARCH_OS}, $v{DEB_BUILD_ARCH_CPU}) = debarch_to_debtriplet($v{DEB_BUILD_ARCH})
  163. if (action_needs(DEB_BUILD | DEB_ARCH_INFO));
  164. ($v{DEB_BUILD_ARCH_BITS}, $v{DEB_BUILD_ARCH_ENDIAN}) = debarch_to_cpuattrs($v{DEB_BUILD_ARCH})
  165. if (action_needs(DEB_BUILD | DEB_ARCH_ATTR));
  166. $v{DEB_BUILD_MULTIARCH} = debarch_to_multiarch($v{DEB_BUILD_ARCH})
  167. if (action_needs(DEB_BUILD | DEB_MULTIARCH));
  168. if (action_needs(DEB_BUILD | DEB_GNU_INFO)) {
  169. $v{DEB_BUILD_GNU_TYPE} = debarch_to_gnutriplet($v{DEB_BUILD_ARCH});
  170. ($v{DEB_BUILD_GNU_CPU}, $v{DEB_BUILD_GNU_SYSTEM}) = split(/-/, $v{DEB_BUILD_GNU_TYPE}, 2);
  171. }
  172. #
  173. # Set host variables
  174. #
  175. # First perform some sanity checks on the host arguments passed.
  176. if ($req_host_arch ne '' && $req_host_gnu_type eq '') {
  177. $req_host_gnu_type = debarch_to_gnutriplet($req_host_arch);
  178. error(_g('unknown Debian architecture %s, you must specify ' .
  179. 'GNU system type, too'), $req_host_arch)
  180. unless defined $req_host_gnu_type;
  181. }
  182. if ($req_host_gnu_type ne '' && $req_host_arch eq '') {
  183. $req_host_arch = gnutriplet_to_debarch($req_host_gnu_type);
  184. error(_g('unknown GNU system type %s, you must specify ' .
  185. 'Debian architecture, too'), $req_host_gnu_type)
  186. unless defined $req_host_arch;
  187. }
  188. if ($req_host_gnu_type ne '' && $req_host_arch ne '') {
  189. my $dfl_host_gnu_type = debarch_to_gnutriplet($req_host_arch);
  190. error(_g('unknown default GNU system type for Debian architecture %s'),
  191. $req_host_arch)
  192. unless defined $dfl_host_gnu_type;
  193. warning(_g('default GNU system type %s for Debian arch %s does not ' .
  194. 'match specified GNU system type %s'), $dfl_host_gnu_type,
  195. $req_host_arch, $req_host_gnu_type)
  196. if $dfl_host_gnu_type ne $req_host_gnu_type;
  197. }
  198. # Proceed to compute the host variables if needed.
  199. if (action_needs(DEB_HOST)) {
  200. if ($req_host_arch eq '') {
  201. $v{DEB_HOST_ARCH} = get_raw_host_arch();
  202. } else {
  203. $v{DEB_HOST_ARCH} = $req_host_arch;
  204. }
  205. }
  206. ($abi, $v{DEB_HOST_ARCH_OS}, $v{DEB_HOST_ARCH_CPU}) = debarch_to_debtriplet($v{DEB_HOST_ARCH})
  207. if (action_needs(DEB_HOST | DEB_ARCH_INFO));
  208. ($v{DEB_HOST_ARCH_BITS}, $v{DEB_HOST_ARCH_ENDIAN}) = debarch_to_cpuattrs($v{DEB_HOST_ARCH})
  209. if (action_needs(DEB_HOST | DEB_ARCH_ATTR));
  210. $v{DEB_HOST_MULTIARCH} = debarch_to_multiarch($v{DEB_HOST_ARCH})
  211. if (action_needs(DEB_HOST | DEB_MULTIARCH));
  212. if (action_needs(DEB_HOST | DEB_GNU_INFO)) {
  213. if ($req_host_gnu_type eq '') {
  214. $v{DEB_HOST_GNU_TYPE} = debarch_to_gnutriplet($v{DEB_HOST_ARCH});
  215. } else {
  216. $v{DEB_HOST_GNU_TYPE} = $req_host_gnu_type;
  217. }
  218. ($v{DEB_HOST_GNU_CPU}, $v{DEB_HOST_GNU_SYSTEM}) = split(/-/, $v{DEB_HOST_GNU_TYPE}, 2);
  219. my $gcc = get_gcc_host_gnu_type();
  220. warning(_g('specified GNU system type %s does not match gcc system ' .
  221. 'type %s, try setting a correct CC environment variable'),
  222. $v{DEB_HOST_GNU_TYPE}, $gcc)
  223. if ($gcc ne '') && ($gcc ne $v{DEB_HOST_GNU_TYPE});
  224. }
  225. for my $k (keys %arch_vars) {
  226. $v{$k} = $ENV{$k} if (defined ($ENV{$k}) && !$force);
  227. }
  228. if ($action eq 'l') {
  229. foreach my $k (sort keys %arch_vars) {
  230. print "$k=$v{$k}\n";
  231. }
  232. } elsif ($action eq 's') {
  233. foreach my $k (sort keys %arch_vars) {
  234. print "$k=$v{$k}; ";
  235. }
  236. print 'export ' . join(' ', sort keys %arch_vars) . "\n";
  237. } elsif ($action eq 'u') {
  238. print 'unset ' . join(' ', sort keys %arch_vars) . "\n";
  239. } elsif ($action eq 'e') {
  240. exit !debarch_eq($v{DEB_HOST_ARCH}, $req_eq_arch);
  241. } elsif ($action eq 'i') {
  242. exit !debarch_is($v{DEB_HOST_ARCH}, $req_is_arch);
  243. } elsif ($action eq 'c') {
  244. @ENV{keys %v} = values %v;
  245. exec @ARGV;
  246. } elsif ($action eq 'q') {
  247. print "$v{$req_variable_to_print}\n";
  248. }