Arch.pm 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. # Copyright © 2006-2013 Guillem Jover <guillem@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. package Dpkg::Arch;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '0.01';
  19. use Exporter qw(import);
  20. our @EXPORT_OK = qw(get_raw_build_arch get_raw_host_arch
  21. get_build_arch get_host_arch get_gcc_host_gnu_type
  22. get_valid_arches debarch_eq debarch_is debarch_is_wildcard
  23. debarch_is_concerned
  24. debarch_to_cpuattrs
  25. debarch_to_gnutriplet gnutriplet_to_debarch
  26. debtriplet_to_gnutriplet gnutriplet_to_debtriplet
  27. debtriplet_to_debarch debarch_to_debtriplet
  28. gnutriplet_to_multiarch debarch_to_multiarch);
  29. use POSIX qw(:errno_h);
  30. use Dpkg ();
  31. use Dpkg::Gettext;
  32. use Dpkg::ErrorHandling;
  33. use Dpkg::Util qw(:list);
  34. use Dpkg::BuildEnv;
  35. my (@cpu, @os);
  36. my (%cputable, %ostable);
  37. my (%cputable_re, %ostable_re);
  38. my (%cpubits, %cpuendian);
  39. my %abibits;
  40. my %debtriplet_to_debarch;
  41. my %debarch_to_debtriplet;
  42. {
  43. my $build_arch;
  44. my $host_arch;
  45. my $gcc_host_gnu_type;
  46. sub get_raw_build_arch()
  47. {
  48. return $build_arch if defined $build_arch;
  49. # Note: We *always* require an installed dpkg when inferring the
  50. # build architecture. The bootstrapping case is handled by
  51. # dpkg-architecture itself, by avoiding computing the DEB_BUILD_
  52. # variables when they are not requested.
  53. $build_arch = `dpkg --print-architecture`;
  54. syserr('dpkg --print-architecture failed') if $? >> 8;
  55. chomp $build_arch;
  56. return $build_arch;
  57. }
  58. sub get_build_arch()
  59. {
  60. return Dpkg::BuildEnv::get('DEB_BUILD_ARCH') || get_raw_build_arch();
  61. }
  62. sub get_gcc_host_gnu_type()
  63. {
  64. return $gcc_host_gnu_type if defined $gcc_host_gnu_type;
  65. $gcc_host_gnu_type = `\${CC:-gcc} -dumpmachine`;
  66. if ($? >> 8) {
  67. $gcc_host_gnu_type = '';
  68. } else {
  69. chomp $gcc_host_gnu_type;
  70. }
  71. return $gcc_host_gnu_type;
  72. }
  73. sub get_raw_host_arch()
  74. {
  75. return $host_arch if defined $host_arch;
  76. $gcc_host_gnu_type = get_gcc_host_gnu_type();
  77. if ($gcc_host_gnu_type eq '') {
  78. warning(g_("couldn't determine gcc system type, falling back to " .
  79. 'default (native compilation)'));
  80. } else {
  81. my (@host_archtriplet) = gnutriplet_to_debtriplet($gcc_host_gnu_type);
  82. $host_arch = debtriplet_to_debarch(@host_archtriplet);
  83. if (defined $host_arch) {
  84. $gcc_host_gnu_type = debtriplet_to_gnutriplet(@host_archtriplet);
  85. } else {
  86. warning(g_('unknown gcc system type %s, falling back to ' .
  87. 'default (native compilation)'), $gcc_host_gnu_type);
  88. $gcc_host_gnu_type = '';
  89. }
  90. }
  91. if (!defined($host_arch)) {
  92. # Switch to native compilation.
  93. $host_arch = get_raw_build_arch();
  94. }
  95. return $host_arch;
  96. }
  97. sub get_host_arch()
  98. {
  99. return Dpkg::BuildEnv::get('DEB_HOST_ARCH') || get_raw_host_arch();
  100. }
  101. }
  102. sub get_valid_arches()
  103. {
  104. read_cputable();
  105. read_ostable();
  106. my @arches;
  107. foreach my $os (@os) {
  108. foreach my $cpu (@cpu) {
  109. my $arch = debtriplet_to_debarch(split(/-/, $os, 2), $cpu);
  110. push @arches, $arch if defined($arch);
  111. }
  112. }
  113. return @arches;
  114. }
  115. my %table_loaded;
  116. sub load_table
  117. {
  118. my ($table, $loader) = @_;
  119. return if $table_loaded{$table};
  120. local $_;
  121. local $/ = "\n";
  122. open my $table_fh, '<', "$Dpkg::DATADIR/$table"
  123. or syserr(g_('cannot open %s'), $table);
  124. while (<$table_fh>) {
  125. $loader->($_);
  126. }
  127. close $table_fh;
  128. $table_loaded{$table} = 1;
  129. }
  130. sub read_cputable
  131. {
  132. load_table('cputable', sub {
  133. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) {
  134. $cputable{$1} = $2;
  135. $cputable_re{$1} = $3;
  136. $cpubits{$1} = $4;
  137. $cpuendian{$1} = $5;
  138. push @cpu, $1;
  139. }
  140. });
  141. }
  142. sub read_ostable
  143. {
  144. load_table('ostable', sub {
  145. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)/) {
  146. $ostable{$1} = $2;
  147. $ostable_re{$1} = $3;
  148. push @os, $1;
  149. }
  150. });
  151. }
  152. sub abitable_load()
  153. {
  154. load_table('abitable', sub {
  155. if (m/^(?!\#)(\S+)\s+(\S+)/) {
  156. $abibits{$1} = $2;
  157. }
  158. });
  159. }
  160. sub read_triplettable()
  161. {
  162. read_cputable();
  163. load_table('triplettable', sub {
  164. if (m/^(?!\#)(\S+)\s+(\S+)/) {
  165. my $debtriplet = $1;
  166. my $debarch = $2;
  167. if ($debtriplet =~ /<cpu>/) {
  168. foreach my $_cpu (@cpu) {
  169. (my $dt = $debtriplet) =~ s/<cpu>/$_cpu/;
  170. (my $da = $debarch) =~ s/<cpu>/$_cpu/;
  171. next if exists $debarch_to_debtriplet{$da}
  172. or exists $debtriplet_to_debarch{$dt};
  173. $debarch_to_debtriplet{$da} = $dt;
  174. $debtriplet_to_debarch{$dt} = $da;
  175. }
  176. } else {
  177. $debarch_to_debtriplet{$2} = $1;
  178. $debtriplet_to_debarch{$1} = $2;
  179. }
  180. }
  181. });
  182. }
  183. sub debtriplet_to_gnutriplet(@)
  184. {
  185. my ($abi, $os, $cpu) = @_;
  186. read_cputable();
  187. read_ostable();
  188. return unless defined($abi) && defined($os) && defined($cpu) &&
  189. exists($cputable{$cpu}) && exists($ostable{"$abi-$os"});
  190. return join('-', $cputable{$cpu}, $ostable{"$abi-$os"});
  191. }
  192. sub gnutriplet_to_debtriplet($)
  193. {
  194. my $gnu = shift;
  195. return unless defined($gnu);
  196. my ($gnu_cpu, $gnu_os) = split(/-/, $gnu, 2);
  197. return unless defined($gnu_cpu) && defined($gnu_os);
  198. read_cputable();
  199. read_ostable();
  200. my ($os, $cpu);
  201. foreach my $_cpu (@cpu) {
  202. if ($gnu_cpu =~ /^$cputable_re{$_cpu}$/) {
  203. $cpu = $_cpu;
  204. last;
  205. }
  206. }
  207. foreach my $_os (@os) {
  208. if ($gnu_os =~ /^(.*-)?$ostable_re{$_os}$/) {
  209. $os = $_os;
  210. last;
  211. }
  212. }
  213. return if !defined($cpu) || !defined($os);
  214. return (split(/-/, $os, 2), $cpu);
  215. }
  216. sub gnutriplet_to_multiarch($)
  217. {
  218. my $gnu = shift;
  219. my ($cpu, $cdr) = split(/-/, $gnu, 2);
  220. if ($cpu =~ /^i[4567]86$/) {
  221. return "i386-$cdr";
  222. } else {
  223. return $gnu;
  224. }
  225. }
  226. sub debarch_to_multiarch($)
  227. {
  228. my $arch = shift;
  229. return gnutriplet_to_multiarch(debarch_to_gnutriplet($arch));
  230. }
  231. sub debtriplet_to_debarch(@)
  232. {
  233. my ($abi, $os, $cpu) = @_;
  234. read_triplettable();
  235. if (!defined($abi) || !defined($os) || !defined($cpu)) {
  236. return;
  237. } elsif (exists $debtriplet_to_debarch{"$abi-$os-$cpu"}) {
  238. return $debtriplet_to_debarch{"$abi-$os-$cpu"};
  239. } else {
  240. return;
  241. }
  242. }
  243. sub debarch_to_debtriplet($)
  244. {
  245. my $arch = shift;
  246. read_triplettable();
  247. if ($arch =~ /^linux-([^-]*)/) {
  248. # XXX: Might disappear in the future, not sure yet.
  249. $arch = $1;
  250. }
  251. my $triplet = $debarch_to_debtriplet{$arch};
  252. if (defined($triplet)) {
  253. return split(/-/, $triplet, 3);
  254. } else {
  255. return;
  256. }
  257. }
  258. sub debarch_to_gnutriplet($)
  259. {
  260. my $arch = shift;
  261. return debtriplet_to_gnutriplet(debarch_to_debtriplet($arch));
  262. }
  263. sub gnutriplet_to_debarch($)
  264. {
  265. my $gnu = shift;
  266. return debtriplet_to_debarch(gnutriplet_to_debtriplet($gnu));
  267. }
  268. sub debwildcard_to_debtriplet($)
  269. {
  270. my $arch = shift;
  271. my @tuple = split /-/, $arch, 3;
  272. if (any { $_ eq 'any' } @tuple) {
  273. if (scalar @tuple == 3) {
  274. return @tuple;
  275. } elsif (scalar @tuple == 2) {
  276. return ('any', @tuple);
  277. } else {
  278. return ('any', 'any', 'any');
  279. }
  280. } else {
  281. return debarch_to_debtriplet($arch);
  282. }
  283. }
  284. sub debarch_to_cpuattrs($)
  285. {
  286. my $arch = shift;
  287. my ($abi, $os, $cpu) = debarch_to_debtriplet($arch);
  288. if (defined($cpu)) {
  289. abitable_load();
  290. return ($abibits{$abi} // $cpubits{$cpu}, $cpuendian{$cpu});
  291. } else {
  292. return;
  293. }
  294. }
  295. sub debarch_eq($$)
  296. {
  297. my ($a, $b) = @_;
  298. return 1 if ($a eq $b);
  299. my @a = debarch_to_debtriplet($a);
  300. my @b = debarch_to_debtriplet($b);
  301. return 0 if scalar @a != 3 or scalar @b != 3;
  302. return ($a[0] eq $b[0] && $a[1] eq $b[1] && $a[2] eq $b[2]);
  303. }
  304. sub debarch_is($$)
  305. {
  306. my ($real, $alias) = @_;
  307. return 1 if ($alias eq $real or $alias eq 'any');
  308. my @real = debarch_to_debtriplet($real);
  309. my @alias = debwildcard_to_debtriplet($alias);
  310. return 0 if scalar @real != 3 or scalar @alias != 3;
  311. if (($alias[0] eq $real[0] || $alias[0] eq 'any') &&
  312. ($alias[1] eq $real[1] || $alias[1] eq 'any') &&
  313. ($alias[2] eq $real[2] || $alias[2] eq 'any')) {
  314. return 1;
  315. }
  316. return 0;
  317. }
  318. sub debarch_is_wildcard($)
  319. {
  320. my $arch = shift;
  321. return 0 if $arch eq 'all';
  322. my @triplet = debwildcard_to_debtriplet($arch);
  323. return 0 if scalar @triplet != 3;
  324. return 1 if any { $_ eq 'any' } @triplet;
  325. return 0;
  326. }
  327. sub debarch_is_concerned
  328. {
  329. my ($host_arch, @arches) = @_;
  330. my $seen_arch = 0;
  331. foreach my $arch (@arches) {
  332. $arch = lc $arch;
  333. if ($arch =~ /^!/) {
  334. my $not_arch = $arch;
  335. $not_arch =~ s/^!//;
  336. if (debarch_is($host_arch, $not_arch)) {
  337. $seen_arch = 0;
  338. last;
  339. } else {
  340. # !arch includes by default all other arches
  341. # unless they also appear in a !otherarch
  342. $seen_arch = 1;
  343. }
  344. } elsif (debarch_is($host_arch, $arch)) {
  345. $seen_arch = 1;
  346. last;
  347. }
  348. }
  349. return $seen_arch;
  350. }
  351. 1;