Arch.pm 9.1 KB

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