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