Arch.pm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. # This program is free software; you can redistribute it and/or modify
  2. # it under the terms of the GNU General Public License as published by
  3. # the Free Software Foundation; either version 2 of the License, or
  4. # (at your option) any later version.
  5. #
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. #
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. package Dpkg::Arch;
  14. use strict;
  15. use warnings;
  16. our $VERSION = "0.01";
  17. use base qw(Exporter);
  18. our @EXPORT_OK = qw(get_raw_build_arch get_raw_host_arch
  19. get_build_arch get_host_arch get_gcc_host_gnu_type
  20. get_valid_arches debarch_eq debarch_is
  21. debarch_to_cpuattrs
  22. debarch_to_gnutriplet gnutriplet_to_debarch
  23. debtriplet_to_gnutriplet gnutriplet_to_debtriplet
  24. debtriplet_to_debarch debarch_to_debtriplet);
  25. use Dpkg;
  26. use Dpkg::Gettext;
  27. use Dpkg::ErrorHandling;
  28. my (@cpu, @os);
  29. my (%cputable, %ostable);
  30. my (%cputable_re, %ostable_re);
  31. my (%cpubits, %cpuendian);
  32. my %debtriplet_to_debarch;
  33. my %debarch_to_debtriplet;
  34. {
  35. my $build_arch;
  36. my $host_arch;
  37. my $gcc_host_gnu_type;
  38. sub get_raw_build_arch()
  39. {
  40. return $build_arch if defined $build_arch;
  41. my $build_arch = `dpkg --print-architecture`;
  42. # FIXME: Handle bootstrapping
  43. syserr("dpkg --print-architecture failed") if $? >> 8;
  44. chomp $build_arch;
  45. return $build_arch;
  46. }
  47. sub get_build_arch()
  48. {
  49. return $ENV{DEB_BUILD_ARCH} || get_raw_build_arch();
  50. }
  51. sub get_gcc_host_gnu_type()
  52. {
  53. return $gcc_host_gnu_type if defined $gcc_host_gnu_type;
  54. my $gcc_host_gnu_type = `\${CC:-gcc} -dumpmachine`;
  55. if ($? >> 8) {
  56. $gcc_host_gnu_type = '';
  57. } else {
  58. chomp $gcc_host_gnu_type;
  59. }
  60. return $gcc_host_gnu_type;
  61. }
  62. sub get_raw_host_arch()
  63. {
  64. return $host_arch if defined $host_arch;
  65. $gcc_host_gnu_type = get_gcc_host_gnu_type();
  66. if ($gcc_host_gnu_type eq '') {
  67. warning(_g("Couldn't determine gcc system type, falling back to " .
  68. "default (native compilation)"));
  69. } else {
  70. my (@host_archtriplet) = gnutriplet_to_debtriplet($gcc_host_gnu_type);
  71. $host_arch = debtriplet_to_debarch(@host_archtriplet);
  72. if (defined $host_arch) {
  73. $gcc_host_gnu_type = debtriplet_to_gnutriplet(@host_archtriplet);
  74. } else {
  75. warning(_g("Unknown gcc system type %s, falling back to " .
  76. "default (native compilation)"), $gcc_host_gnu_type);
  77. $gcc_host_gnu_type = '';
  78. }
  79. }
  80. if (!defined($host_arch)) {
  81. # Switch to native compilation.
  82. $host_arch = get_raw_build_arch();
  83. }
  84. return $host_arch;
  85. }
  86. sub get_host_arch()
  87. {
  88. return $ENV{DEB_HOST_ARCH} || get_raw_host_arch();
  89. }
  90. }
  91. sub get_valid_arches()
  92. {
  93. read_cputable() if (!@cpu);
  94. read_ostable() if (!@os);
  95. my @arches;
  96. foreach my $os (@os) {
  97. foreach my $cpu (@cpu) {
  98. my $arch = debtriplet_to_debarch(split(/-/, $os, 2), $cpu);
  99. push @arches, $arch if defined($arch);
  100. }
  101. }
  102. return @arches;
  103. }
  104. sub read_cputable
  105. {
  106. local $_;
  107. local $/ = "\n";
  108. open CPUTABLE, "$pkgdatadir/cputable"
  109. or syserr(_g("cannot open %s"), "cputable");
  110. while (<CPUTABLE>) {
  111. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) {
  112. $cputable{$1} = $2;
  113. $cputable_re{$1} = $3;
  114. $cpubits{$1} = $4;
  115. $cpuendian{$1} = $5;
  116. push @cpu, $1;
  117. }
  118. }
  119. close CPUTABLE;
  120. }
  121. sub read_ostable
  122. {
  123. local $_;
  124. local $/ = "\n";
  125. open OSTABLE, "$pkgdatadir/ostable"
  126. or syserr(_g("cannot open %s"), "ostable");
  127. while (<OSTABLE>) {
  128. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)/) {
  129. $ostable{$1} = $2;
  130. $ostable_re{$1} = $3;
  131. push @os, $1;
  132. }
  133. }
  134. close OSTABLE;
  135. }
  136. sub read_triplettable()
  137. {
  138. read_cputable() if (!@cpu);
  139. local $_;
  140. local $/ = "\n";
  141. open TRIPLETTABLE, "$pkgdatadir/triplettable"
  142. or syserr(_g("cannot open %s"), "triplettable");
  143. while (<TRIPLETTABLE>) {
  144. if (m/^(?!\#)(\S+)\s+(\S+)/) {
  145. my $debtriplet = $1;
  146. my $debarch = $2;
  147. if ($debtriplet =~ /<cpu>/) {
  148. foreach my $_cpu (@cpu) {
  149. (my $dt = $debtriplet) =~ s/<cpu>/$_cpu/;
  150. (my $da = $debarch) =~ s/<cpu>/$_cpu/;
  151. $debarch_to_debtriplet{$da} = $dt;
  152. $debtriplet_to_debarch{$dt} = $da;
  153. }
  154. } else {
  155. $debarch_to_debtriplet{$2} = $1;
  156. $debtriplet_to_debarch{$1} = $2;
  157. }
  158. }
  159. }
  160. close TRIPLETTABLE;
  161. }
  162. sub debtriplet_to_gnutriplet(@)
  163. {
  164. read_cputable() if (!@cpu);
  165. read_ostable() if (!@os);
  166. my ($abi, $os, $cpu) = @_;
  167. return undef unless defined($abi) && defined($os) && defined($cpu) &&
  168. exists($cputable{$cpu}) && exists($ostable{"$abi-$os"});
  169. return join("-", $cputable{$cpu}, $ostable{"$abi-$os"});
  170. }
  171. sub gnutriplet_to_debtriplet($)
  172. {
  173. my ($gnu) = @_;
  174. return undef unless defined($gnu);
  175. my ($gnu_cpu, $gnu_os) = split(/-/, $gnu, 2);
  176. return undef unless defined($gnu_cpu) && defined($gnu_os);
  177. read_cputable() if (!@cpu);
  178. read_ostable() if (!@os);
  179. my ($os, $cpu);
  180. foreach my $_cpu (@cpu) {
  181. if ($gnu_cpu =~ /^$cputable_re{$_cpu}$/) {
  182. $cpu = $_cpu;
  183. last;
  184. }
  185. }
  186. foreach my $_os (@os) {
  187. if ($gnu_os =~ /^(.*-)?$ostable_re{$_os}$/) {
  188. $os = $_os;
  189. last;
  190. }
  191. }
  192. return undef if !defined($cpu) || !defined($os);
  193. return (split(/-/, $os, 2), $cpu);
  194. }
  195. sub debtriplet_to_debarch(@)
  196. {
  197. read_triplettable() if (!%debtriplet_to_debarch);
  198. my ($abi, $os, $cpu) = @_;
  199. if (!defined($abi) || !defined($os) || !defined($cpu)) {
  200. return undef;
  201. } elsif (exists $debtriplet_to_debarch{"$abi-$os-$cpu"}) {
  202. return $debtriplet_to_debarch{"$abi-$os-$cpu"};
  203. } else {
  204. return undef;
  205. }
  206. }
  207. sub debarch_to_debtriplet($)
  208. {
  209. read_triplettable() if (!%debarch_to_debtriplet);
  210. local ($_) = @_;
  211. my $arch;
  212. if (/^linux-([^-]*)/) {
  213. # XXX: Might disappear in the future, not sure yet.
  214. $arch = $1;
  215. } else {
  216. $arch = $_;
  217. }
  218. my $triplet = $debarch_to_debtriplet{$arch};
  219. if (defined($triplet)) {
  220. return split('-', $triplet, 3);
  221. } else {
  222. return undef;
  223. }
  224. }
  225. sub debarch_to_gnutriplet($)
  226. {
  227. my ($arch) = @_;
  228. return debtriplet_to_gnutriplet(debarch_to_debtriplet($arch));
  229. }
  230. sub gnutriplet_to_debarch($)
  231. {
  232. my ($gnu) = @_;
  233. return debtriplet_to_debarch(gnutriplet_to_debtriplet($gnu));
  234. }
  235. sub debwildcard_to_debtriplet($)
  236. {
  237. local ($_) = @_;
  238. if (/any/) {
  239. if (/^([^-]*)-([^-]*)-(.*)/) {
  240. return ($1, $2, $3);
  241. } elsif (/^([^-]*)-([^-]*)$/) {
  242. return ('any', $1, $2);
  243. } else {
  244. return ($_, $_, $_);
  245. }
  246. } else {
  247. return debarch_to_debtriplet($_);
  248. }
  249. }
  250. sub debarch_to_cpuattrs($)
  251. {
  252. my ($arch) = @_;
  253. my ($abi, $os, $cpu) = debarch_to_debtriplet($arch);
  254. if (defined($cpu)) {
  255. return ($cpubits{$cpu}, $cpuendian{$cpu});
  256. } else {
  257. return undef;
  258. }
  259. }
  260. sub debarch_eq($$)
  261. {
  262. my ($a, $b) = @_;
  263. return 1 if ($a eq $b);
  264. my @a = debarch_to_debtriplet($a);
  265. my @b = debarch_to_debtriplet($b);
  266. return 0 if grep(!defined, (@a, @b));
  267. return ($a[0] eq $b[0] && $a[1] eq $b[1] && $a[2] eq $b[2]);
  268. }
  269. sub debarch_is($$)
  270. {
  271. my ($real, $alias) = @_;
  272. return 1 if ($alias eq $real or $alias eq 'any');
  273. my @real = debarch_to_debtriplet($real);
  274. my @alias = debwildcard_to_debtriplet($alias);
  275. return 0 if grep(!defined, (@real, @alias));
  276. if (($alias[0] eq $real[0] || $alias[0] eq 'any') &&
  277. ($alias[1] eq $real[1] || $alias[1] eq 'any') &&
  278. ($alias[2] eq $real[2] || $alias[2] eq 'any')) {
  279. return 1;
  280. }
  281. return 0;
  282. }
  283. 1;