Arch.pm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. gnutriplet_to_multiarch debarch_to_multiarch);
  26. use Dpkg;
  27. use Dpkg::Gettext;
  28. use Dpkg::ErrorHandling;
  29. my (@cpu, @os);
  30. my (%cputable, %ostable);
  31. my (%cputable_re, %ostable_re);
  32. my (%cpubits, %cpuendian);
  33. my %debtriplet_to_debarch;
  34. my %debarch_to_debtriplet;
  35. {
  36. my $build_arch;
  37. my $host_arch;
  38. my $gcc_host_gnu_type;
  39. sub get_raw_build_arch()
  40. {
  41. return $build_arch if defined $build_arch;
  42. my $build_arch = `dpkg --print-architecture`;
  43. # FIXME: Handle bootstrapping
  44. syserr("dpkg --print-architecture failed") if $? >> 8;
  45. chomp $build_arch;
  46. return $build_arch;
  47. }
  48. sub get_build_arch()
  49. {
  50. return $ENV{DEB_BUILD_ARCH} || get_raw_build_arch();
  51. }
  52. sub get_gcc_host_gnu_type()
  53. {
  54. return $gcc_host_gnu_type if defined $gcc_host_gnu_type;
  55. my $gcc_host_gnu_type = `\${CC:-gcc} -dumpmachine`;
  56. if ($? >> 8) {
  57. $gcc_host_gnu_type = '';
  58. } else {
  59. chomp $gcc_host_gnu_type;
  60. }
  61. return $gcc_host_gnu_type;
  62. }
  63. sub get_raw_host_arch()
  64. {
  65. return $host_arch if defined $host_arch;
  66. $gcc_host_gnu_type = get_gcc_host_gnu_type();
  67. if ($gcc_host_gnu_type eq '') {
  68. warning(_g("Couldn't determine gcc system type, falling back to " .
  69. "default (native compilation)"));
  70. } else {
  71. my (@host_archtriplet) = gnutriplet_to_debtriplet($gcc_host_gnu_type);
  72. $host_arch = debtriplet_to_debarch(@host_archtriplet);
  73. if (defined $host_arch) {
  74. $gcc_host_gnu_type = debtriplet_to_gnutriplet(@host_archtriplet);
  75. } else {
  76. warning(_g("Unknown gcc system type %s, falling back to " .
  77. "default (native compilation)"), $gcc_host_gnu_type);
  78. $gcc_host_gnu_type = '';
  79. }
  80. }
  81. if (!defined($host_arch)) {
  82. # Switch to native compilation.
  83. $host_arch = get_raw_build_arch();
  84. }
  85. return $host_arch;
  86. }
  87. sub get_host_arch()
  88. {
  89. return $ENV{DEB_HOST_ARCH} || get_raw_host_arch();
  90. }
  91. }
  92. sub get_valid_arches()
  93. {
  94. read_cputable() if (!@cpu);
  95. read_ostable() if (!@os);
  96. my @arches;
  97. foreach my $os (@os) {
  98. foreach my $cpu (@cpu) {
  99. my $arch = debtriplet_to_debarch(split(/-/, $os, 2), $cpu);
  100. push @arches, $arch if defined($arch);
  101. }
  102. }
  103. return @arches;
  104. }
  105. sub read_cputable
  106. {
  107. local $_;
  108. local $/ = "\n";
  109. open CPUTABLE, "$pkgdatadir/cputable"
  110. or syserr(_g("cannot open %s"), "cputable");
  111. while (<CPUTABLE>) {
  112. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) {
  113. $cputable{$1} = $2;
  114. $cputable_re{$1} = $3;
  115. $cpubits{$1} = $4;
  116. $cpuendian{$1} = $5;
  117. push @cpu, $1;
  118. }
  119. }
  120. close CPUTABLE;
  121. }
  122. sub read_ostable
  123. {
  124. local $_;
  125. local $/ = "\n";
  126. open OSTABLE, "$pkgdatadir/ostable"
  127. or syserr(_g("cannot open %s"), "ostable");
  128. while (<OSTABLE>) {
  129. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)/) {
  130. $ostable{$1} = $2;
  131. $ostable_re{$1} = $3;
  132. push @os, $1;
  133. }
  134. }
  135. close OSTABLE;
  136. }
  137. sub read_triplettable()
  138. {
  139. read_cputable() if (!@cpu);
  140. local $_;
  141. local $/ = "\n";
  142. open TRIPLETTABLE, "$pkgdatadir/triplettable"
  143. or syserr(_g("cannot open %s"), "triplettable");
  144. while (<TRIPLETTABLE>) {
  145. if (m/^(?!\#)(\S+)\s+(\S+)/) {
  146. my $debtriplet = $1;
  147. my $debarch = $2;
  148. if ($debtriplet =~ /<cpu>/) {
  149. foreach my $_cpu (@cpu) {
  150. (my $dt = $debtriplet) =~ s/<cpu>/$_cpu/;
  151. (my $da = $debarch) =~ s/<cpu>/$_cpu/;
  152. $debarch_to_debtriplet{$da} = $dt;
  153. $debtriplet_to_debarch{$dt} = $da;
  154. }
  155. } else {
  156. $debarch_to_debtriplet{$2} = $1;
  157. $debtriplet_to_debarch{$1} = $2;
  158. }
  159. }
  160. }
  161. close TRIPLETTABLE;
  162. }
  163. sub debtriplet_to_gnutriplet(@)
  164. {
  165. read_cputable() if (!@cpu);
  166. read_ostable() if (!@os);
  167. my ($abi, $os, $cpu) = @_;
  168. return undef unless defined($abi) && defined($os) && defined($cpu) &&
  169. exists($cputable{$cpu}) && exists($ostable{"$abi-$os"});
  170. return join("-", $cputable{$cpu}, $ostable{"$abi-$os"});
  171. }
  172. sub gnutriplet_to_debtriplet($)
  173. {
  174. my ($gnu) = @_;
  175. return undef unless defined($gnu);
  176. my ($gnu_cpu, $gnu_os) = split(/-/, $gnu, 2);
  177. return undef unless defined($gnu_cpu) && defined($gnu_os);
  178. read_cputable() if (!@cpu);
  179. read_ostable() if (!@os);
  180. my ($os, $cpu);
  181. foreach my $_cpu (@cpu) {
  182. if ($gnu_cpu =~ /^$cputable_re{$_cpu}$/) {
  183. $cpu = $_cpu;
  184. last;
  185. }
  186. }
  187. foreach my $_os (@os) {
  188. if ($gnu_os =~ /^(.*-)?$ostable_re{$_os}$/) {
  189. $os = $_os;
  190. last;
  191. }
  192. }
  193. return undef if !defined($cpu) || !defined($os);
  194. return (split(/-/, $os, 2), $cpu);
  195. }
  196. sub gnutriplet_to_multiarch($)
  197. {
  198. my ($gnu) = @_;
  199. my ($cpu, $cdr) = split('-', $gnu, 2);
  200. if ($cpu =~ /^i[456]86$/) {
  201. return "i386-$cdr";
  202. } else {
  203. return $gnu;
  204. }
  205. }
  206. sub debarch_to_multiarch($)
  207. {
  208. my ($arch) = @_;
  209. return gnutriplet_to_multiarch(debarch_to_gnutriplet($arch));
  210. }
  211. sub debtriplet_to_debarch(@)
  212. {
  213. read_triplettable() if (!%debtriplet_to_debarch);
  214. my ($abi, $os, $cpu) = @_;
  215. if (!defined($abi) || !defined($os) || !defined($cpu)) {
  216. return undef;
  217. } elsif (exists $debtriplet_to_debarch{"$abi-$os-$cpu"}) {
  218. return $debtriplet_to_debarch{"$abi-$os-$cpu"};
  219. } else {
  220. return undef;
  221. }
  222. }
  223. sub debarch_to_debtriplet($)
  224. {
  225. read_triplettable() if (!%debarch_to_debtriplet);
  226. local ($_) = @_;
  227. my $arch;
  228. if (/^linux-([^-]*)/) {
  229. # XXX: Might disappear in the future, not sure yet.
  230. $arch = $1;
  231. } else {
  232. $arch = $_;
  233. }
  234. my $triplet = $debarch_to_debtriplet{$arch};
  235. if (defined($triplet)) {
  236. return split('-', $triplet, 3);
  237. } else {
  238. return undef;
  239. }
  240. }
  241. sub debarch_to_gnutriplet($)
  242. {
  243. my ($arch) = @_;
  244. return debtriplet_to_gnutriplet(debarch_to_debtriplet($arch));
  245. }
  246. sub gnutriplet_to_debarch($)
  247. {
  248. my ($gnu) = @_;
  249. return debtriplet_to_debarch(gnutriplet_to_debtriplet($gnu));
  250. }
  251. sub debwildcard_to_debtriplet($)
  252. {
  253. local ($_) = @_;
  254. if (/any/) {
  255. if (/^([^-]*)-([^-]*)-(.*)/) {
  256. return ($1, $2, $3);
  257. } elsif (/^([^-]*)-([^-]*)$/) {
  258. return ('any', $1, $2);
  259. } else {
  260. return ($_, $_, $_);
  261. }
  262. } else {
  263. return debarch_to_debtriplet($_);
  264. }
  265. }
  266. sub debarch_to_cpuattrs($)
  267. {
  268. my ($arch) = @_;
  269. my ($abi, $os, $cpu) = debarch_to_debtriplet($arch);
  270. if (defined($cpu)) {
  271. return ($cpubits{$cpu}, $cpuendian{$cpu});
  272. } else {
  273. return undef;
  274. }
  275. }
  276. sub debarch_eq($$)
  277. {
  278. my ($a, $b) = @_;
  279. return 1 if ($a eq $b);
  280. my @a = debarch_to_debtriplet($a);
  281. my @b = debarch_to_debtriplet($b);
  282. return 0 if grep(!defined, (@a, @b));
  283. return ($a[0] eq $b[0] && $a[1] eq $b[1] && $a[2] eq $b[2]);
  284. }
  285. sub debarch_is($$)
  286. {
  287. my ($real, $alias) = @_;
  288. return 1 if ($alias eq $real or $alias eq 'any');
  289. my @real = debarch_to_debtriplet($real);
  290. my @alias = debwildcard_to_debtriplet($alias);
  291. return 0 if grep(!defined, (@real, @alias));
  292. if (($alias[0] eq $real[0] || $alias[0] eq 'any') &&
  293. ($alias[1] eq $real[1] || $alias[1] eq 'any') &&
  294. ($alias[2] eq $real[2] || $alias[2] eq 'any')) {
  295. return 1;
  296. }
  297. return 0;
  298. }
  299. 1;