Arch.pm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package Dpkg::Arch;
  2. use strict;
  3. use warnings;
  4. use Exporter;
  5. our @ISA = qw(Exporter);
  6. our @EXPORT_OK = qw(get_build_arch get_host_arch get_gcc_host_gnu_type
  7. get_valid_arches debarch_eq debarch_is
  8. debarch_to_gnutriplet gnutriplet_to_debarch
  9. debtriplet_to_gnutriplet gnutriplet_to_debtriplet
  10. debtriplet_to_debarch debarch_to_debtriplet);
  11. use Dpkg;
  12. use Dpkg::Gettext;
  13. use Dpkg::ErrorHandling qw(syserr subprocerr warning);
  14. my (@cpu, @os);
  15. my (%cputable, %ostable);
  16. my (%cputable_re, %ostable_re);
  17. my %debtriplet_to_debarch;
  18. my %debarch_to_debtriplet;
  19. {
  20. my $build_arch;
  21. my $host_arch;
  22. my $gcc_host_gnu_type;
  23. sub get_build_arch()
  24. {
  25. return $build_arch if defined $build_arch;
  26. my $build_arch = `dpkg --print-architecture`;
  27. # FIXME: Handle bootstrapping
  28. syserr("dpkg --print-architecture failed") if $? >> 8;
  29. chomp $build_arch;
  30. return $build_arch;
  31. }
  32. sub get_gcc_host_gnu_type()
  33. {
  34. return $gcc_host_gnu_type if defined $gcc_host_gnu_type;
  35. my $gcc_host_gnu_type = `\${CC:-gcc} -dumpmachine`;
  36. if ($? >> 8) {
  37. $gcc_host_gnu_type = '';
  38. } else {
  39. chomp $gcc_host_gnu_type;
  40. }
  41. return $gcc_host_gnu_type;
  42. }
  43. sub get_host_arch()
  44. {
  45. return $host_arch if defined $host_arch;
  46. my $gcc_host_gnu_type = get_gcc_host_gnu_type();
  47. if ($gcc_host_gnu_type eq '') {
  48. warning(_g("Couldn't determine gcc system type, falling back to " .
  49. "default (native compilation)"));
  50. } else {
  51. my (@host_archtriplet) = gnutriplet_to_debtriplet($gcc_host_gnu_type);
  52. $host_arch = debtriplet_to_debarch(@host_archtriplet);
  53. if (defined $host_arch) {
  54. $gcc_host_gnu_type = debtriplet_to_gnutriplet(@host_archtriplet);
  55. } else {
  56. warning(_g("Unknown gcc system type %s, falling back to " .
  57. "default (native compilation)"), $gcc_host_gnu_type);
  58. $gcc_host_gnu_type = '';
  59. }
  60. }
  61. if (!defined($host_arch)) {
  62. # Switch to native compilation.
  63. $host_arch = get_build_arch();
  64. }
  65. return $host_arch;
  66. }
  67. }
  68. sub get_valid_arches()
  69. {
  70. read_cputable() if (!@cpu);
  71. read_ostable() if (!@os);
  72. my @arches;
  73. foreach my $os (@os) {
  74. foreach my $cpu (@cpu) {
  75. my $arch = debtriplet_to_debarch(split(/-/, $os, 2), $cpu);
  76. push @arches, $arch if defined($arch);
  77. }
  78. }
  79. return @arches;
  80. }
  81. sub read_cputable
  82. {
  83. local $_;
  84. open CPUTABLE, "$pkgdatadir/cputable"
  85. or syserr(_g("unable to open cputable"));
  86. while (<CPUTABLE>) {
  87. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)/) {
  88. $cputable{$1} = $2;
  89. $cputable_re{$1} = $3;
  90. push @cpu, $1;
  91. }
  92. }
  93. close CPUTABLE;
  94. }
  95. sub read_ostable
  96. {
  97. local $_;
  98. open OSTABLE, "$pkgdatadir/ostable"
  99. or syserr(_g("unable to open ostable"));
  100. while (<OSTABLE>) {
  101. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)/) {
  102. $ostable{$1} = $2;
  103. $ostable_re{$1} = $3;
  104. push @os, $1;
  105. }
  106. }
  107. close OSTABLE;
  108. }
  109. sub read_triplettable()
  110. {
  111. read_cputable() if (!@cpu);
  112. local $_;
  113. open TRIPLETTABLE, "$pkgdatadir/triplettable"
  114. or syserr(_g("unable to open triplettable"));
  115. while (<TRIPLETTABLE>) {
  116. if (m/^(?!\#)(\S+)\s+(\S+)/) {
  117. my $debtriplet = $1;
  118. my $debarch = $2;
  119. if ($debtriplet =~ /<cpu>/) {
  120. foreach my $_cpu (@cpu) {
  121. (my $dt = $debtriplet) =~ s/<cpu>/$_cpu/;
  122. (my $da = $debarch) =~ s/<cpu>/$_cpu/;
  123. $debarch_to_debtriplet{$da} = $dt;
  124. $debtriplet_to_debarch{$dt} = $da;
  125. }
  126. } else {
  127. $debarch_to_debtriplet{$2} = $1;
  128. $debtriplet_to_debarch{$1} = $2;
  129. }
  130. }
  131. }
  132. close TRIPLETTABLE;
  133. }
  134. sub debtriplet_to_gnutriplet(@)
  135. {
  136. read_cputable() if (!@cpu);
  137. read_ostable() if (!@os);
  138. my ($abi, $os, $cpu) = @_;
  139. return undef unless defined($abi) && defined($os) && defined($cpu) &&
  140. exists($cputable{$cpu}) && exists($ostable{"$abi-$os"});
  141. return join("-", $cputable{$cpu}, $ostable{"$abi-$os"});
  142. }
  143. sub gnutriplet_to_debtriplet($)
  144. {
  145. my ($gnu) = @_;
  146. return undef unless defined($gnu);
  147. my ($gnu_cpu, $gnu_os) = split(/-/, $gnu, 2);
  148. return undef unless defined($gnu_cpu) && defined($gnu_os);
  149. read_cputable() if (!@cpu);
  150. read_ostable() if (!@os);
  151. my ($os, $cpu);
  152. foreach my $_cpu (@cpu) {
  153. if ($gnu_cpu =~ /^$cputable_re{$_cpu}$/) {
  154. $cpu = $_cpu;
  155. last;
  156. }
  157. }
  158. foreach my $_os (@os) {
  159. if ($gnu_os =~ /^(.*-)?$ostable_re{$_os}$/) {
  160. $os = $_os;
  161. last;
  162. }
  163. }
  164. return undef if !defined($cpu) || !defined($os);
  165. return (split(/-/, $os, 2), $cpu);
  166. }
  167. sub debtriplet_to_debarch(@)
  168. {
  169. read_triplettable() if (!%debtriplet_to_debarch);
  170. my ($abi, $os, $cpu) = @_;
  171. if (!defined($abi) || !defined($os) || !defined($cpu)) {
  172. return undef;
  173. } elsif (exists $debtriplet_to_debarch{"$abi-$os-$cpu"}) {
  174. return $debtriplet_to_debarch{"$abi-$os-$cpu"};
  175. } else {
  176. return undef;
  177. }
  178. }
  179. sub debarch_to_debtriplet($)
  180. {
  181. read_triplettable() if (!%debarch_to_debtriplet);
  182. local ($_) = @_;
  183. my $arch;
  184. if (/^linux-([^-]*)/) {
  185. # XXX: Might disappear in the future, not sure yet.
  186. $arch = $1;
  187. } else {
  188. $arch = $_;
  189. }
  190. my $triplet = $debarch_to_debtriplet{$arch};
  191. if (defined($triplet)) {
  192. return split('-', $triplet, 3);
  193. } else {
  194. return undef;
  195. }
  196. }
  197. sub debarch_to_gnutriplet($)
  198. {
  199. my ($arch) = @_;
  200. return debtriplet_to_gnutriplet(debarch_to_debtriplet($arch));
  201. }
  202. sub gnutriplet_to_debarch($)
  203. {
  204. my ($gnu) = @_;
  205. return debtriplet_to_debarch(gnutriplet_to_debtriplet($gnu));
  206. }
  207. sub debwildcard_to_debtriplet($)
  208. {
  209. local ($_) = @_;
  210. if (/any/) {
  211. if (/^([^-]*)-([^-]*)-(.*)/) {
  212. return ($1, $2, $3);
  213. } elsif (/^([^-]*)-([^-]*)$/) {
  214. return ('any', $1, $2);
  215. } else {
  216. return ($_, $_, $_);
  217. }
  218. } else {
  219. return debarch_to_debtriplet($_);
  220. }
  221. }
  222. sub debarch_eq($$)
  223. {
  224. my ($a, $b) = @_;
  225. return 1 if ($a eq $b);
  226. my @a = debarch_to_debtriplet($a);
  227. my @b = debarch_to_debtriplet($b);
  228. return 0 if grep(!defined, (@a, @b));
  229. return ($a[0] eq $b[0] && $a[1] eq $b[1] && $a[2] eq $b[2]);
  230. }
  231. sub debarch_is($$)
  232. {
  233. my ($real, $alias) = @_;
  234. return 1 if ($alias eq $real or $alias eq 'any');
  235. my @real = debarch_to_debtriplet($real);
  236. my @alias = debwildcard_to_debtriplet($alias);
  237. return 0 if grep(!defined, (@real, @alias));
  238. if (($alias[0] eq $real[0] || $alias[0] eq 'any') &&
  239. ($alias[1] eq $real[1] || $alias[1] eq 'any') &&
  240. ($alias[2] eq $real[2] || $alias[2] eq 'any')) {
  241. return 1;
  242. }
  243. return 0;
  244. }
  245. 1;