Arch.pm 9.4 KB

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