Arch.pm 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. # Copyright © 2006-2013 Guillem Jover <guillem@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package Dpkg::Arch;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '0.01';
  19. use Exporter qw(import);
  20. our @EXPORT_OK = qw(get_raw_build_arch get_raw_host_arch
  21. get_build_arch get_host_arch get_gcc_host_gnu_type
  22. get_valid_arches debarch_eq debarch_is debarch_is_wildcard
  23. debarch_to_cpuattrs
  24. debarch_to_gnutriplet gnutriplet_to_debarch
  25. debtriplet_to_gnutriplet gnutriplet_to_debtriplet
  26. debtriplet_to_debarch debarch_to_debtriplet
  27. gnutriplet_to_multiarch debarch_to_multiarch);
  28. use POSIX qw(:errno_h);
  29. use Dpkg ();
  30. use Dpkg::Gettext;
  31. use Dpkg::ErrorHandling;
  32. use Dpkg::Util qw(:list);
  33. use Dpkg::BuildEnv;
  34. my (@cpu, @os);
  35. my (%cputable, %ostable);
  36. my (%cputable_re, %ostable_re);
  37. my (%cpubits, %cpuendian);
  38. my %abibits;
  39. my %debtriplet_to_debarch;
  40. my %debarch_to_debtriplet;
  41. {
  42. my $build_arch;
  43. my $host_arch;
  44. my $gcc_host_gnu_type;
  45. sub get_raw_build_arch()
  46. {
  47. return $build_arch if defined $build_arch;
  48. # Note: We *always* require an installed dpkg when inferring the
  49. # build architecture. The bootstrapping case is handled by
  50. # dpkg-architecture itself, by avoiding computing the DEB_BUILD_
  51. # variables when they are not requested.
  52. $build_arch = `dpkg --print-architecture`;
  53. syserr('dpkg --print-architecture failed') if $? >> 8;
  54. chomp $build_arch;
  55. return $build_arch;
  56. }
  57. sub get_build_arch()
  58. {
  59. return Dpkg::BuildEnv::get('DEB_BUILD_ARCH') || get_raw_build_arch();
  60. }
  61. sub get_gcc_host_gnu_type()
  62. {
  63. return $gcc_host_gnu_type if defined $gcc_host_gnu_type;
  64. $gcc_host_gnu_type = `\${CC:-gcc} -dumpmachine`;
  65. if ($? >> 8) {
  66. $gcc_host_gnu_type = '';
  67. } else {
  68. chomp $gcc_host_gnu_type;
  69. }
  70. return $gcc_host_gnu_type;
  71. }
  72. sub get_raw_host_arch()
  73. {
  74. return $host_arch if defined $host_arch;
  75. $gcc_host_gnu_type = get_gcc_host_gnu_type();
  76. if ($gcc_host_gnu_type eq '') {
  77. warning(_g("couldn't determine gcc system type, falling back to " .
  78. 'default (native compilation)'));
  79. } else {
  80. my (@host_archtriplet) = gnutriplet_to_debtriplet($gcc_host_gnu_type);
  81. $host_arch = debtriplet_to_debarch(@host_archtriplet);
  82. if (defined $host_arch) {
  83. $gcc_host_gnu_type = debtriplet_to_gnutriplet(@host_archtriplet);
  84. } else {
  85. warning(_g('unknown gcc system type %s, falling back to ' .
  86. 'default (native compilation)'), $gcc_host_gnu_type);
  87. $gcc_host_gnu_type = '';
  88. }
  89. }
  90. if (!defined($host_arch)) {
  91. # Switch to native compilation.
  92. $host_arch = get_raw_build_arch();
  93. }
  94. return $host_arch;
  95. }
  96. sub get_host_arch()
  97. {
  98. return Dpkg::BuildEnv::get('DEB_HOST_ARCH') || get_raw_host_arch();
  99. }
  100. }
  101. sub get_valid_arches()
  102. {
  103. read_cputable();
  104. read_ostable();
  105. my @arches;
  106. foreach my $os (@os) {
  107. foreach my $cpu (@cpu) {
  108. my $arch = debtriplet_to_debarch(split(/-/, $os, 2), $cpu);
  109. push @arches, $arch if defined($arch);
  110. }
  111. }
  112. return @arches;
  113. }
  114. my $cputable_loaded = 0;
  115. sub read_cputable
  116. {
  117. return if ($cputable_loaded);
  118. local $_;
  119. local $/ = "\n";
  120. open my $cputable_fh, '<', "$Dpkg::DATADIR/cputable"
  121. or syserr(_g('cannot open %s'), 'cputable');
  122. while (<$cputable_fh>) {
  123. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) {
  124. $cputable{$1} = $2;
  125. $cputable_re{$1} = $3;
  126. $cpubits{$1} = $4;
  127. $cpuendian{$1} = $5;
  128. push @cpu, $1;
  129. }
  130. }
  131. close $cputable_fh;
  132. $cputable_loaded = 1;
  133. }
  134. my $ostable_loaded = 0;
  135. sub read_ostable
  136. {
  137. return if ($ostable_loaded);
  138. local $_;
  139. local $/ = "\n";
  140. open my $ostable_fh, '<', "$Dpkg::DATADIR/ostable"
  141. or syserr(_g('cannot open %s'), 'ostable');
  142. while (<$ostable_fh>) {
  143. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)/) {
  144. $ostable{$1} = $2;
  145. $ostable_re{$1} = $3;
  146. push @os, $1;
  147. }
  148. }
  149. close $ostable_fh;
  150. $ostable_loaded = 1;
  151. }
  152. my $abitable_loaded = 0;
  153. sub abitable_load()
  154. {
  155. return if ($abitable_loaded);
  156. local $_;
  157. local $/ = "\n";
  158. # Because the abitable is only for override information, do not fail if
  159. # it does not exist, as that will only mean the other tables do not have
  160. # an entry needing to be overridden. This way we do not require a newer
  161. # dpkg by libdpkg-perl.
  162. if (open my $abitable_fh, '<', "$Dpkg::DATADIR/abitable") {
  163. while (<$abitable_fh>) {
  164. if (m/^(?!\#)(\S+)\s+(\S+)/) {
  165. $abibits{$1} = $2;
  166. }
  167. }
  168. close $abitable_fh;
  169. } elsif ($! != ENOENT) {
  170. syserr(_g('cannot open %s'), 'abitable');
  171. }
  172. $abitable_loaded = 1;
  173. }
  174. my $triplettable_loaded = 0;
  175. sub read_triplettable()
  176. {
  177. return if ($triplettable_loaded);
  178. read_cputable();
  179. local $_;
  180. local $/ = "\n";
  181. open my $triplettable_fh, '<', "$Dpkg::DATADIR/triplettable"
  182. or syserr(_g('cannot open %s'), 'triplettable');
  183. while (<$triplettable_fh>) {
  184. if (m/^(?!\#)(\S+)\s+(\S+)/) {
  185. my $debtriplet = $1;
  186. my $debarch = $2;
  187. if ($debtriplet =~ /<cpu>/) {
  188. foreach my $_cpu (@cpu) {
  189. (my $dt = $debtriplet) =~ s/<cpu>/$_cpu/;
  190. (my $da = $debarch) =~ s/<cpu>/$_cpu/;
  191. next if exists $debarch_to_debtriplet{$da}
  192. or exists $debtriplet_to_debarch{$dt};
  193. $debarch_to_debtriplet{$da} = $dt;
  194. $debtriplet_to_debarch{$dt} = $da;
  195. }
  196. } else {
  197. $debarch_to_debtriplet{$2} = $1;
  198. $debtriplet_to_debarch{$1} = $2;
  199. }
  200. }
  201. }
  202. close $triplettable_fh;
  203. $triplettable_loaded = 1;
  204. }
  205. sub debtriplet_to_gnutriplet(@)
  206. {
  207. read_cputable();
  208. read_ostable();
  209. my ($abi, $os, $cpu) = @_;
  210. return unless defined($abi) && defined($os) && defined($cpu) &&
  211. exists($cputable{$cpu}) && exists($ostable{"$abi-$os"});
  212. return join('-', $cputable{$cpu}, $ostable{"$abi-$os"});
  213. }
  214. sub gnutriplet_to_debtriplet($)
  215. {
  216. my ($gnu) = @_;
  217. return unless defined($gnu);
  218. my ($gnu_cpu, $gnu_os) = split(/-/, $gnu, 2);
  219. return unless defined($gnu_cpu) && defined($gnu_os);
  220. read_cputable();
  221. read_ostable();
  222. my ($os, $cpu);
  223. foreach my $_cpu (@cpu) {
  224. if ($gnu_cpu =~ /^$cputable_re{$_cpu}$/) {
  225. $cpu = $_cpu;
  226. last;
  227. }
  228. }
  229. foreach my $_os (@os) {
  230. if ($gnu_os =~ /^(.*-)?$ostable_re{$_os}$/) {
  231. $os = $_os;
  232. last;
  233. }
  234. }
  235. return if !defined($cpu) || !defined($os);
  236. return (split(/-/, $os, 2), $cpu);
  237. }
  238. sub gnutriplet_to_multiarch($)
  239. {
  240. my ($gnu) = @_;
  241. my ($cpu, $cdr) = split(/-/, $gnu, 2);
  242. if ($cpu =~ /^i[456]86$/) {
  243. return "i386-$cdr";
  244. } else {
  245. return $gnu;
  246. }
  247. }
  248. sub debarch_to_multiarch($)
  249. {
  250. my ($arch) = @_;
  251. return gnutriplet_to_multiarch(debarch_to_gnutriplet($arch));
  252. }
  253. sub debtriplet_to_debarch(@)
  254. {
  255. read_triplettable();
  256. my ($abi, $os, $cpu) = @_;
  257. if (!defined($abi) || !defined($os) || !defined($cpu)) {
  258. return;
  259. } elsif (exists $debtriplet_to_debarch{"$abi-$os-$cpu"}) {
  260. return $debtriplet_to_debarch{"$abi-$os-$cpu"};
  261. } else {
  262. return;
  263. }
  264. }
  265. sub debarch_to_debtriplet($)
  266. {
  267. read_triplettable();
  268. local ($_) = @_;
  269. my $arch;
  270. if (/^linux-([^-]*)/) {
  271. # XXX: Might disappear in the future, not sure yet.
  272. $arch = $1;
  273. } else {
  274. $arch = $_;
  275. }
  276. my $triplet = $debarch_to_debtriplet{$arch};
  277. if (defined($triplet)) {
  278. return split(/-/, $triplet, 3);
  279. } else {
  280. return;
  281. }
  282. }
  283. sub debarch_to_gnutriplet($)
  284. {
  285. my ($arch) = @_;
  286. return debtriplet_to_gnutriplet(debarch_to_debtriplet($arch));
  287. }
  288. sub gnutriplet_to_debarch($)
  289. {
  290. my ($gnu) = @_;
  291. return debtriplet_to_debarch(gnutriplet_to_debtriplet($gnu));
  292. }
  293. sub debwildcard_to_debtriplet($)
  294. {
  295. my ($arch) = @_;
  296. my @tuple = split /-/, $arch, 3;
  297. if (any { $_ eq 'any' } @tuple) {
  298. if (scalar @tuple == 3) {
  299. return @tuple;
  300. } elsif (scalar @tuple == 2) {
  301. return ('any', @tuple);
  302. } else {
  303. return ('any', 'any', 'any');
  304. }
  305. } else {
  306. return debarch_to_debtriplet($arch);
  307. }
  308. }
  309. sub debarch_to_cpuattrs($)
  310. {
  311. my ($arch) = @_;
  312. my ($abi, $os, $cpu) = debarch_to_debtriplet($arch);
  313. if (defined($cpu)) {
  314. abitable_load();
  315. return ($abibits{$abi} || $cpubits{$cpu}, $cpuendian{$cpu});
  316. } else {
  317. return;
  318. }
  319. }
  320. sub debarch_eq($$)
  321. {
  322. my ($a, $b) = @_;
  323. return 1 if ($a eq $b);
  324. my @a = debarch_to_debtriplet($a);
  325. my @b = debarch_to_debtriplet($b);
  326. return 0 if scalar @a != 3 or scalar @b != 3;
  327. return ($a[0] eq $b[0] && $a[1] eq $b[1] && $a[2] eq $b[2]);
  328. }
  329. sub debarch_is($$)
  330. {
  331. my ($real, $alias) = @_;
  332. return 1 if ($alias eq $real or $alias eq 'any');
  333. my @real = debarch_to_debtriplet($real);
  334. my @alias = debwildcard_to_debtriplet($alias);
  335. return 0 if scalar @real != 3 or scalar @alias != 3;
  336. if (($alias[0] eq $real[0] || $alias[0] eq 'any') &&
  337. ($alias[1] eq $real[1] || $alias[1] eq 'any') &&
  338. ($alias[2] eq $real[2] || $alias[2] eq 'any')) {
  339. return 1;
  340. }
  341. return 0;
  342. }
  343. sub debarch_is_wildcard($)
  344. {
  345. my ($arch) = @_;
  346. return 0 if $arch eq 'all';
  347. my @triplet = debwildcard_to_debtriplet($arch);
  348. return 0 if scalar @triplet != 3;
  349. return 1 if any { $_ eq 'any' } @triplet;
  350. return 0;
  351. }
  352. 1;