dpkg-architecture.pl 8.9 KB

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