Arch.pm 9.5 KB

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