Arch.pm 9.2 KB

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