Arch.pm 8.2 KB

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