Arch.pm 4.8 KB

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