Arch.pm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package Dpkg::Arch;
  2. use strict;
  3. use warnings;
  4. use Exporter;
  5. our @ISA = qw(Exporter);
  6. our @EXPORT_OK = qw(get_host_arch get_valid_arches debarch_eq debarch_is
  7. debtriplet_to_gnutriplet gnutriplet_to_debtriplet
  8. debtriplet_to_debarch debarch_to_debtriplet);
  9. use Dpkg;
  10. use Dpkg::ErrorHandling qw(syserr subprocerr);
  11. my (@cpu, @os);
  12. my (%cputable, %ostable);
  13. my (%cputable_re, %ostable_re);
  14. my %debtriplet_to_debarch;
  15. my %debarch_to_debtriplet;
  16. {
  17. my $host_arch;
  18. sub get_host_arch()
  19. {
  20. return $host_arch if defined $host_arch;
  21. $host_arch = `dpkg-architecture -qDEB_HOST_ARCH`;
  22. $? && subprocerr("dpkg-architecture -qDEB_HOST_ARCH");
  23. chomp $host_arch;
  24. return $host_arch;
  25. }
  26. }
  27. sub get_valid_arches()
  28. {
  29. read_cputable() if (!@cpu);
  30. read_ostable() if (!@os);
  31. my @arches;
  32. foreach my $os (@os) {
  33. foreach my $cpu (@cpu) {
  34. my $arch = debtriplet_to_debarch(split(/-/, $os, 2), $cpu);
  35. push @arches, $arch if defined($arch);
  36. }
  37. }
  38. return @arches;
  39. }
  40. sub read_cputable
  41. {
  42. local $_;
  43. open CPUTABLE, "$pkgdatadir/cputable"
  44. or syserr(_g("unable to open cputable"));
  45. while (<CPUTABLE>) {
  46. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)/) {
  47. $cputable{$1} = $2;
  48. $cputable_re{$1} = $3;
  49. push @cpu, $1;
  50. }
  51. }
  52. close CPUTABLE;
  53. }
  54. sub read_ostable
  55. {
  56. local $_;
  57. open OSTABLE, "$pkgdatadir/ostable"
  58. or syserr(_g("unable to open ostable"));
  59. while (<OSTABLE>) {
  60. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)/) {
  61. $ostable{$1} = $2;
  62. $ostable_re{$1} = $3;
  63. push @os, $1;
  64. }
  65. }
  66. close OSTABLE;
  67. }
  68. sub read_triplettable()
  69. {
  70. read_cputable() if (!@cpu);
  71. local $_;
  72. open TRIPLETTABLE, "$pkgdatadir/triplettable"
  73. or syserr(_g("unable to open triplettable"));
  74. while (<TRIPLETTABLE>) {
  75. if (m/^(?!\#)(\S+)\s+(\S+)/) {
  76. my $debtriplet = $1;
  77. my $debarch = $2;
  78. if ($debtriplet =~ /<cpu>/) {
  79. foreach my $_cpu (@cpu) {
  80. (my $dt = $debtriplet) =~ s/<cpu>/$_cpu/;
  81. (my $da = $debarch) =~ s/<cpu>/$_cpu/;
  82. $debarch_to_debtriplet{$da} = $dt;
  83. $debtriplet_to_debarch{$dt} = $da;
  84. }
  85. } else {
  86. $debarch_to_debtriplet{$2} = $1;
  87. $debtriplet_to_debarch{$1} = $2;
  88. }
  89. }
  90. }
  91. close TRIPLETTABLE;
  92. }
  93. sub debtriplet_to_gnutriplet(@)
  94. {
  95. read_cputable() if (!@cpu);
  96. read_ostable() if (!@os);
  97. my ($abi, $os, $cpu) = @_;
  98. return undef unless defined($abi) && defined($os) && defined($cpu) &&
  99. exists($cputable{$cpu}) && exists($ostable{"$abi-$os"});
  100. return join("-", $cputable{$cpu}, $ostable{"$abi-$os"});
  101. }
  102. sub gnutriplet_to_debtriplet($)
  103. {
  104. my ($gnu) = @_;
  105. return undef unless defined($gnu);
  106. my ($gnu_cpu, $gnu_os) = split(/-/, $gnu, 2);
  107. return undef unless defined($gnu_cpu) && defined($gnu_os);
  108. read_cputable() if (!@cpu);
  109. read_ostable() if (!@os);
  110. my ($os, $cpu);
  111. foreach my $_cpu (@cpu) {
  112. if ($gnu_cpu =~ /^$cputable_re{$_cpu}$/) {
  113. $cpu = $_cpu;
  114. last;
  115. }
  116. }
  117. foreach my $_os (@os) {
  118. if ($gnu_os =~ /^(.*-)?$ostable_re{$_os}$/) {
  119. $os = $_os;
  120. last;
  121. }
  122. }
  123. return undef if !defined($cpu) || !defined($os);
  124. return (split(/-/, $os, 2), $cpu);
  125. }
  126. sub debtriplet_to_debarch(@)
  127. {
  128. read_triplettable() if (!%debtriplet_to_debarch);
  129. my ($abi, $os, $cpu) = @_;
  130. if (!defined($abi) || !defined($os) || !defined($cpu)) {
  131. return undef;
  132. } elsif (exists $debtriplet_to_debarch{"$abi-$os-$cpu"}) {
  133. return $debtriplet_to_debarch{"$abi-$os-$cpu"};
  134. } else {
  135. return undef;
  136. }
  137. }
  138. sub debarch_to_debtriplet($)
  139. {
  140. read_triplettable() if (!%debarch_to_debtriplet);
  141. local ($_) = @_;
  142. my $arch;
  143. if (/^linux-([^-]*)/) {
  144. # XXX: Might disappear in the future, not sure yet.
  145. $arch = $1;
  146. } else {
  147. $arch = $_;
  148. }
  149. my $triplet = $debarch_to_debtriplet{$arch};
  150. if (defined($triplet)) {
  151. return split('-', $triplet, 3);
  152. } else {
  153. return undef;
  154. }
  155. }
  156. sub debwildcard_to_debtriplet($)
  157. {
  158. local ($_) = @_;
  159. if (/any/) {
  160. if (/^([^-]*)-([^-]*)-(.*)/) {
  161. return ($1, $2, $3);
  162. } elsif (/^([^-]*)-([^-]*)$/) {
  163. return ('any', $1, $2);
  164. } else {
  165. return ($_, $_, $_);
  166. }
  167. } else {
  168. return debarch_to_debtriplet($_);
  169. }
  170. }
  171. sub debarch_eq($$)
  172. {
  173. my ($a, $b) = @_;
  174. return 1 if ($a eq $b);
  175. my @a = debarch_to_debtriplet($a);
  176. my @b = debarch_to_debtriplet($b);
  177. return 0 if grep(!defined, (@a, @b));
  178. return ($a[0] eq $b[0] && $a[1] eq $b[1] && $a[2] eq $b[2]);
  179. }
  180. sub debarch_is($$)
  181. {
  182. my ($real, $alias) = @_;
  183. return 1 if ($alias eq $real or $alias eq 'any');
  184. my @real = debarch_to_debtriplet($real);
  185. my @alias = debwildcard_to_debtriplet($alias);
  186. return 0 if grep(!defined, (@real, @alias));
  187. if (($alias[0] eq $real[0] || $alias[0] eq 'any') &&
  188. ($alias[1] eq $real[1] || $alias[1] eq 'any') &&
  189. ($alias[2] eq $real[2] || $alias[2] eq 'any')) {
  190. return 1;
  191. }
  192. return 0;
  193. }
  194. 1;