Arch.pm 7.6 KB

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