Arch.pm 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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() if (!@cpu);
  100. read_ostable() if (!@os);
  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. sub read_cputable
  111. {
  112. local $_;
  113. local $/ = "\n";
  114. open CPUTABLE, "$pkgdatadir/cputable"
  115. or syserr(_g("cannot open %s"), "cputable");
  116. while (<CPUTABLE>) {
  117. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) {
  118. $cputable{$1} = $2;
  119. $cputable_re{$1} = $3;
  120. $cpubits{$1} = $4;
  121. $cpuendian{$1} = $5;
  122. push @cpu, $1;
  123. }
  124. }
  125. close CPUTABLE;
  126. }
  127. sub read_ostable
  128. {
  129. local $_;
  130. local $/ = "\n";
  131. open OSTABLE, "$pkgdatadir/ostable"
  132. or syserr(_g("cannot open %s"), "ostable");
  133. while (<OSTABLE>) {
  134. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)/) {
  135. $ostable{$1} = $2;
  136. $ostable_re{$1} = $3;
  137. push @os, $1;
  138. }
  139. }
  140. close OSTABLE;
  141. }
  142. my $abitable_loaded = 0;
  143. sub abitable_load()
  144. {
  145. return if ($abitable_loaded);
  146. local $_;
  147. local $/ = "\n";
  148. # Because the abitable is only for override information, do not fail if
  149. # it does not exist, as that will only mean the other tables do not have
  150. # an entry needing to be overridden. This way we do not require a newer
  151. # dpkg by libdpkg-perl.
  152. if (open ABITABLE, "$pkgdatadir/abitable") {
  153. while (<ABITABLE>) {
  154. if (m/^(?!\#)(\S+)\s+(\S+)/) {
  155. $abibits{$1} = $2;
  156. }
  157. }
  158. close ABITABLE;
  159. } elsif ($! != ENOENT) {
  160. syserr(_g("cannot open %s"), "abitable");
  161. }
  162. $abitable_loaded = 1;
  163. }
  164. sub read_triplettable()
  165. {
  166. read_cputable() if (!@cpu);
  167. local $_;
  168. local $/ = "\n";
  169. open TRIPLETTABLE, "$pkgdatadir/triplettable"
  170. or syserr(_g("cannot open %s"), "triplettable");
  171. while (<TRIPLETTABLE>) {
  172. if (m/^(?!\#)(\S+)\s+(\S+)/) {
  173. my $debtriplet = $1;
  174. my $debarch = $2;
  175. if ($debtriplet =~ /<cpu>/) {
  176. foreach my $_cpu (@cpu) {
  177. (my $dt = $debtriplet) =~ s/<cpu>/$_cpu/;
  178. (my $da = $debarch) =~ s/<cpu>/$_cpu/;
  179. $debarch_to_debtriplet{$da} = $dt;
  180. $debtriplet_to_debarch{$dt} = $da;
  181. }
  182. } else {
  183. $debarch_to_debtriplet{$2} = $1;
  184. $debtriplet_to_debarch{$1} = $2;
  185. }
  186. }
  187. }
  188. close TRIPLETTABLE;
  189. }
  190. sub debtriplet_to_gnutriplet(@)
  191. {
  192. read_cputable() if (!@cpu);
  193. read_ostable() if (!@os);
  194. my ($abi, $os, $cpu) = @_;
  195. return undef unless defined($abi) && defined($os) && defined($cpu) &&
  196. exists($cputable{$cpu}) && exists($ostable{"$abi-$os"});
  197. return join("-", $cputable{$cpu}, $ostable{"$abi-$os"});
  198. }
  199. sub gnutriplet_to_debtriplet($)
  200. {
  201. my ($gnu) = @_;
  202. return undef unless defined($gnu);
  203. my ($gnu_cpu, $gnu_os) = split(/-/, $gnu, 2);
  204. return undef unless defined($gnu_cpu) && defined($gnu_os);
  205. read_cputable() if (!@cpu);
  206. read_ostable() if (!@os);
  207. my ($os, $cpu);
  208. foreach my $_cpu (@cpu) {
  209. if ($gnu_cpu =~ /^$cputable_re{$_cpu}$/) {
  210. $cpu = $_cpu;
  211. last;
  212. }
  213. }
  214. foreach my $_os (@os) {
  215. if ($gnu_os =~ /^(.*-)?$ostable_re{$_os}$/) {
  216. $os = $_os;
  217. last;
  218. }
  219. }
  220. return undef if !defined($cpu) || !defined($os);
  221. return (split(/-/, $os, 2), $cpu);
  222. }
  223. sub gnutriplet_to_multiarch($)
  224. {
  225. my ($gnu) = @_;
  226. my ($cpu, $cdr) = split('-', $gnu, 2);
  227. if ($cpu =~ /^i[456]86$/) {
  228. return "i386-$cdr";
  229. } else {
  230. return $gnu;
  231. }
  232. }
  233. sub debarch_to_multiarch($)
  234. {
  235. my ($arch) = @_;
  236. return gnutriplet_to_multiarch(debarch_to_gnutriplet($arch));
  237. }
  238. sub debtriplet_to_debarch(@)
  239. {
  240. read_triplettable() if (!%debtriplet_to_debarch);
  241. my ($abi, $os, $cpu) = @_;
  242. if (!defined($abi) || !defined($os) || !defined($cpu)) {
  243. return undef;
  244. } elsif (exists $debtriplet_to_debarch{"$abi-$os-$cpu"}) {
  245. return $debtriplet_to_debarch{"$abi-$os-$cpu"};
  246. } else {
  247. return undef;
  248. }
  249. }
  250. sub debarch_to_debtriplet($)
  251. {
  252. read_triplettable() if (!%debarch_to_debtriplet);
  253. local ($_) = @_;
  254. my $arch;
  255. if (/^linux-([^-]*)/) {
  256. # XXX: Might disappear in the future, not sure yet.
  257. $arch = $1;
  258. } else {
  259. $arch = $_;
  260. }
  261. my $triplet = $debarch_to_debtriplet{$arch};
  262. if (defined($triplet)) {
  263. return split('-', $triplet, 3);
  264. } else {
  265. return undef;
  266. }
  267. }
  268. sub debarch_to_gnutriplet($)
  269. {
  270. my ($arch) = @_;
  271. return debtriplet_to_gnutriplet(debarch_to_debtriplet($arch));
  272. }
  273. sub gnutriplet_to_debarch($)
  274. {
  275. my ($gnu) = @_;
  276. return debtriplet_to_debarch(gnutriplet_to_debtriplet($gnu));
  277. }
  278. sub debwildcard_to_debtriplet($)
  279. {
  280. local ($_) = @_;
  281. if (/any/) {
  282. if (/^([^-]*)-([^-]*)-(.*)/) {
  283. return ($1, $2, $3);
  284. } elsif (/^([^-]*)-([^-]*)$/) {
  285. return ('any', $1, $2);
  286. } else {
  287. return ($_, $_, $_);
  288. }
  289. } else {
  290. return debarch_to_debtriplet($_);
  291. }
  292. }
  293. sub debarch_to_cpuattrs($)
  294. {
  295. my ($arch) = @_;
  296. my ($abi, $os, $cpu) = debarch_to_debtriplet($arch);
  297. if (defined($cpu)) {
  298. abitable_load();
  299. return ($abibits{$abi} || $cpubits{$cpu}, $cpuendian{$cpu});
  300. } else {
  301. return undef;
  302. }
  303. }
  304. sub debarch_eq($$)
  305. {
  306. my ($a, $b) = @_;
  307. return 1 if ($a eq $b);
  308. my @a = debarch_to_debtriplet($a);
  309. my @b = debarch_to_debtriplet($b);
  310. return 0 if grep(!defined, (@a, @b));
  311. return ($a[0] eq $b[0] && $a[1] eq $b[1] && $a[2] eq $b[2]);
  312. }
  313. sub debarch_is($$)
  314. {
  315. my ($real, $alias) = @_;
  316. return 1 if ($alias eq $real or $alias eq 'any');
  317. my @real = debarch_to_debtriplet($real);
  318. my @alias = debwildcard_to_debtriplet($alias);
  319. return 0 if grep(!defined, (@real, @alias));
  320. if (($alias[0] eq $real[0] || $alias[0] eq 'any') &&
  321. ($alias[1] eq $real[1] || $alias[1] eq 'any') &&
  322. ($alias[2] eq $real[2] || $alias[2] eq 'any')) {
  323. return 1;
  324. }
  325. return 0;
  326. }
  327. 1;