Arch.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. =encoding utf8
  17. =head1 NAME
  18. Dpkg::Arch - handle architectures
  19. =head1 DESCRIPTION
  20. The Dpkg::Arch module provides functions to handle Debian architectures,
  21. wildcards, and mapping from and to GNU triplets.
  22. =cut
  23. use strict;
  24. use warnings;
  25. use feature qw(state);
  26. our $VERSION = '1.00';
  27. our @EXPORT_OK = qw(
  28. get_raw_build_arch
  29. get_raw_host_arch
  30. get_build_arch
  31. get_host_arch
  32. get_host_gnu_type
  33. get_valid_arches
  34. debarch_eq
  35. debarch_is
  36. debarch_is_wildcard
  37. debarch_is_concerned
  38. debarch_to_cpuattrs
  39. debarch_to_gnutriplet
  40. debarch_to_debtriplet
  41. debarch_to_multiarch
  42. debtriplet_to_debarch
  43. debtriplet_to_gnutriplet
  44. gnutriplet_to_debarch
  45. gnutriplet_to_debtriplet
  46. gnutriplet_to_multiarch
  47. );
  48. use Exporter qw(import);
  49. use POSIX qw(:errno_h);
  50. use Dpkg ();
  51. use Dpkg::Gettext;
  52. use Dpkg::ErrorHandling;
  53. use Dpkg::Util qw(:list);
  54. use Dpkg::BuildEnv;
  55. my (@cpu, @os);
  56. my (%cputable, %ostable);
  57. my (%cputable_re, %ostable_re);
  58. my (%cpubits, %cpuendian);
  59. my %abibits;
  60. my %debtriplet_to_debarch;
  61. my %debarch_to_debtriplet;
  62. =head1 FUNCTIONS
  63. =over 4
  64. =item $arch = get_raw_build_arch()
  65. Get the raw build Debian architecture, without taking into account variables
  66. from the environment.
  67. =cut
  68. sub get_raw_build_arch()
  69. {
  70. state $build_arch;
  71. return $build_arch if defined $build_arch;
  72. # Note: We *always* require an installed dpkg when inferring the
  73. # build architecture. The bootstrapping case is handled by
  74. # dpkg-architecture itself, by avoiding computing the DEB_BUILD_
  75. # variables when they are not requested.
  76. $build_arch = qx(dpkg --print-architecture);
  77. syserr('dpkg --print-architecture failed') if $? >> 8;
  78. chomp $build_arch;
  79. return $build_arch;
  80. }
  81. =item $arch = get_build_arch()
  82. Get the build Debian architecture, using DEB_BUILD_ARCH from the environment
  83. if available.
  84. =cut
  85. sub get_build_arch()
  86. {
  87. return Dpkg::BuildEnv::get('DEB_BUILD_ARCH') || get_raw_build_arch();
  88. }
  89. {
  90. my %cc_host_gnu_type;
  91. sub get_host_gnu_type()
  92. {
  93. my $CC = $ENV{CC} || 'gcc';
  94. return $cc_host_gnu_type{$CC} if defined $cc_host_gnu_type{$CC};
  95. $cc_host_gnu_type{$CC} = qx($CC -dumpmachine);
  96. if ($? >> 8) {
  97. $cc_host_gnu_type{$CC} = '';
  98. } else {
  99. chomp $cc_host_gnu_type{$CC};
  100. }
  101. return $cc_host_gnu_type{$CC};
  102. }
  103. sub set_host_gnu_type
  104. {
  105. my ($host_gnu_type) = @_;
  106. my $CC = $ENV{CC} || 'gcc';
  107. $cc_host_gnu_type{$CC} = $host_gnu_type;
  108. }
  109. }
  110. =item $arch = get_raw_host_arch()
  111. Get the raw host Debian architecture, without taking into account variables
  112. from the environment.
  113. =cut
  114. sub get_raw_host_arch()
  115. {
  116. state $host_arch;
  117. return $host_arch if defined $host_arch;
  118. my $host_gnu_type = get_host_gnu_type();
  119. if ($host_gnu_type eq '') {
  120. warning(g_('cannot determine CC system type, falling back to ' .
  121. 'default (native compilation)'));
  122. } else {
  123. my (@host_archtriplet) = gnutriplet_to_debtriplet($host_gnu_type);
  124. $host_arch = debtriplet_to_debarch(@host_archtriplet);
  125. if (defined $host_arch) {
  126. $host_gnu_type = debtriplet_to_gnutriplet(@host_archtriplet);
  127. } else {
  128. warning(g_('unknown CC system type %s, falling back to ' .
  129. 'default (native compilation)'), $host_gnu_type);
  130. $host_gnu_type = '';
  131. }
  132. set_host_gnu_type($host_gnu_type);
  133. }
  134. if (!defined($host_arch)) {
  135. # Switch to native compilation.
  136. $host_arch = get_raw_build_arch();
  137. }
  138. return $host_arch;
  139. }
  140. =item $arch = get_host_arch()
  141. Get the host Debian architecture, using DEB_HOST_ARCH from the environment
  142. if available.
  143. =cut
  144. sub get_host_arch()
  145. {
  146. return Dpkg::BuildEnv::get('DEB_HOST_ARCH') || get_raw_host_arch();
  147. }
  148. =item @arch_list = get_valid_arches()
  149. Get an array with all currently known Debian architectures.
  150. =cut
  151. sub get_valid_arches()
  152. {
  153. read_cputable();
  154. read_ostable();
  155. my @arches;
  156. foreach my $os (@os) {
  157. foreach my $cpu (@cpu) {
  158. my $arch = debtriplet_to_debarch(split(/-/, $os, 2), $cpu);
  159. push @arches, $arch if defined($arch);
  160. }
  161. }
  162. return @arches;
  163. }
  164. my %table_loaded;
  165. sub load_table
  166. {
  167. my ($table, $loader) = @_;
  168. return if $table_loaded{$table};
  169. local $_;
  170. local $/ = "\n";
  171. open my $table_fh, '<', "$Dpkg::DATADIR/$table"
  172. or syserr(g_('cannot open %s'), $table);
  173. while (<$table_fh>) {
  174. $loader->($_);
  175. }
  176. close $table_fh;
  177. $table_loaded{$table} = 1;
  178. }
  179. sub read_cputable
  180. {
  181. load_table('cputable', sub {
  182. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) {
  183. $cputable{$1} = $2;
  184. $cputable_re{$1} = $3;
  185. $cpubits{$1} = $4;
  186. $cpuendian{$1} = $5;
  187. push @cpu, $1;
  188. }
  189. });
  190. }
  191. sub read_ostable
  192. {
  193. load_table('ostable', sub {
  194. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)/) {
  195. $ostable{$1} = $2;
  196. $ostable_re{$1} = $3;
  197. push @os, $1;
  198. }
  199. });
  200. }
  201. sub abitable_load()
  202. {
  203. load_table('abitable', sub {
  204. if (m/^(?!\#)(\S+)\s+(\S+)/) {
  205. $abibits{$1} = $2;
  206. }
  207. });
  208. }
  209. sub read_triplettable()
  210. {
  211. read_cputable();
  212. load_table('triplettable', sub {
  213. if (m/^(?!\#)(\S+)\s+(\S+)/) {
  214. my $debtriplet = $1;
  215. my $debarch = $2;
  216. if ($debtriplet =~ /<cpu>/) {
  217. foreach my $_cpu (@cpu) {
  218. (my $dt = $debtriplet) =~ s/<cpu>/$_cpu/;
  219. (my $da = $debarch) =~ s/<cpu>/$_cpu/;
  220. next if exists $debarch_to_debtriplet{$da}
  221. or exists $debtriplet_to_debarch{$dt};
  222. $debarch_to_debtriplet{$da} = $dt;
  223. $debtriplet_to_debarch{$dt} = $da;
  224. }
  225. } else {
  226. $debarch_to_debtriplet{$2} = $1;
  227. $debtriplet_to_debarch{$1} = $2;
  228. }
  229. }
  230. });
  231. }
  232. sub debtriplet_to_gnutriplet(@)
  233. {
  234. my ($abi, $os, $cpu) = @_;
  235. read_cputable();
  236. read_ostable();
  237. return unless defined($abi) && defined($os) && defined($cpu) &&
  238. exists($cputable{$cpu}) && exists($ostable{"$abi-$os"});
  239. return join('-', $cputable{$cpu}, $ostable{"$abi-$os"});
  240. }
  241. sub gnutriplet_to_debtriplet($)
  242. {
  243. my $gnu = shift;
  244. return unless defined($gnu);
  245. my ($gnu_cpu, $gnu_os) = split(/-/, $gnu, 2);
  246. return unless defined($gnu_cpu) && defined($gnu_os);
  247. read_cputable();
  248. read_ostable();
  249. my ($os, $cpu);
  250. foreach my $_cpu (@cpu) {
  251. if ($gnu_cpu =~ /^$cputable_re{$_cpu}$/) {
  252. $cpu = $_cpu;
  253. last;
  254. }
  255. }
  256. foreach my $_os (@os) {
  257. if ($gnu_os =~ /^(.*-)?$ostable_re{$_os}$/) {
  258. $os = $_os;
  259. last;
  260. }
  261. }
  262. return if !defined($cpu) || !defined($os);
  263. return (split(/-/, $os, 2), $cpu);
  264. }
  265. =item $multiarch = gnutriplet_to_multiarch($gnutriplet)
  266. Map a GNU triplet into a Debian multiarch triplet.
  267. =cut
  268. sub gnutriplet_to_multiarch($)
  269. {
  270. my $gnu = shift;
  271. my ($cpu, $cdr) = split(/-/, $gnu, 2);
  272. if ($cpu =~ /^i[4567]86$/) {
  273. return "i386-$cdr";
  274. } else {
  275. return $gnu;
  276. }
  277. }
  278. =item $multiarch = debarch_to_multiarch($arch)
  279. Map a Debian architecture into a Debian multiarch triplet.
  280. =cut
  281. sub debarch_to_multiarch($)
  282. {
  283. my $arch = shift;
  284. return gnutriplet_to_multiarch(debarch_to_gnutriplet($arch));
  285. }
  286. sub debtriplet_to_debarch(@)
  287. {
  288. my ($abi, $os, $cpu) = @_;
  289. read_triplettable();
  290. if (!defined($abi) || !defined($os) || !defined($cpu)) {
  291. return;
  292. } elsif (exists $debtriplet_to_debarch{"$abi-$os-$cpu"}) {
  293. return $debtriplet_to_debarch{"$abi-$os-$cpu"};
  294. } else {
  295. return;
  296. }
  297. }
  298. sub debarch_to_debtriplet($)
  299. {
  300. my $arch = shift;
  301. return if not defined $arch;
  302. read_triplettable();
  303. if ($arch =~ /^linux-([^-]*)/) {
  304. # XXX: Might disappear in the future, not sure yet.
  305. $arch = $1;
  306. }
  307. my $triplet = $debarch_to_debtriplet{$arch};
  308. if (defined($triplet)) {
  309. return split(/-/, $triplet, 3);
  310. } else {
  311. return;
  312. }
  313. }
  314. =item $gnutriplet = debarch_to_gnutriplet($arch)
  315. Map a Debian architecture into a GNU triplet.
  316. =cut
  317. sub debarch_to_gnutriplet($)
  318. {
  319. my $arch = shift;
  320. return debtriplet_to_gnutriplet(debarch_to_debtriplet($arch));
  321. }
  322. =item $arch = gnutriplet_to_debarch($gnutriplet)
  323. Map a GNU triplet into a Debian architecture.
  324. =cut
  325. sub gnutriplet_to_debarch($)
  326. {
  327. my $gnu = shift;
  328. return debtriplet_to_debarch(gnutriplet_to_debtriplet($gnu));
  329. }
  330. sub debwildcard_to_debtriplet($)
  331. {
  332. my $arch = shift;
  333. my @tuple = split /-/, $arch, 3;
  334. if (any { $_ eq 'any' } @tuple) {
  335. if (scalar @tuple == 3) {
  336. return @tuple;
  337. } elsif (scalar @tuple == 2) {
  338. return ('any', @tuple);
  339. } else {
  340. return ('any', 'any', 'any');
  341. }
  342. } else {
  343. return debarch_to_debtriplet($arch);
  344. }
  345. }
  346. sub debarch_to_cpuattrs($)
  347. {
  348. my $arch = shift;
  349. my ($abi, $os, $cpu) = debarch_to_debtriplet($arch);
  350. if (defined($cpu)) {
  351. abitable_load();
  352. return ($abibits{$abi} // $cpubits{$cpu}, $cpuendian{$cpu});
  353. } else {
  354. return;
  355. }
  356. }
  357. =item $bool = debarch_eq($arch_a, $arch_b)
  358. Evaluate the equality of a Debian architecture, by comparing with another
  359. Debian architecture. No wildcard matching is performed.
  360. =cut
  361. sub debarch_eq($$)
  362. {
  363. my ($a, $b) = @_;
  364. return 1 if ($a eq $b);
  365. my @a = debarch_to_debtriplet($a);
  366. my @b = debarch_to_debtriplet($b);
  367. return 0 if scalar @a != 3 or scalar @b != 3;
  368. return ($a[0] eq $b[0] && $a[1] eq $b[1] && $a[2] eq $b[2]);
  369. }
  370. =item $bool = debarch_is($arch, $arch_wildcard)
  371. Evaluate the identity of a Debian architecture, by matchings with an
  372. architecture wildcard.
  373. =cut
  374. sub debarch_is($$)
  375. {
  376. my ($real, $alias) = @_;
  377. return 1 if ($alias eq $real or $alias eq 'any');
  378. my @real = debarch_to_debtriplet($real);
  379. my @alias = debwildcard_to_debtriplet($alias);
  380. return 0 if scalar @real != 3 or scalar @alias != 3;
  381. if (($alias[0] eq $real[0] || $alias[0] eq 'any') &&
  382. ($alias[1] eq $real[1] || $alias[1] eq 'any') &&
  383. ($alias[2] eq $real[2] || $alias[2] eq 'any')) {
  384. return 1;
  385. }
  386. return 0;
  387. }
  388. =item $bool = debarch_is_wildcard($arch)
  389. Evaluate whether a Debian architecture is an architecture wildcard.
  390. =cut
  391. sub debarch_is_wildcard($)
  392. {
  393. my $arch = shift;
  394. return 0 if $arch eq 'all';
  395. my @triplet = debwildcard_to_debtriplet($arch);
  396. return 0 if scalar @triplet != 3;
  397. return 1 if any { $_ eq 'any' } @triplet;
  398. return 0;
  399. }
  400. =item $bool = debarch_is_concerned($arch, @arches)
  401. Evaluate whether a Debian architecture applies to the list of architecture
  402. restrictions, as usually found in dependencies inside square brackets.
  403. =cut
  404. sub debarch_is_concerned
  405. {
  406. my ($host_arch, @arches) = @_;
  407. my $seen_arch = 0;
  408. foreach my $arch (@arches) {
  409. $arch = lc $arch;
  410. if ($arch =~ /^!/) {
  411. my $not_arch = $arch;
  412. $not_arch =~ s/^!//;
  413. if (debarch_is($host_arch, $not_arch)) {
  414. $seen_arch = 0;
  415. last;
  416. } else {
  417. # !arch includes by default all other arches
  418. # unless they also appear in a !otherarch
  419. $seen_arch = 1;
  420. }
  421. } elsif (debarch_is($host_arch, $arch)) {
  422. $seen_arch = 1;
  423. last;
  424. }
  425. }
  426. return $seen_arch;
  427. }
  428. 1;
  429. __END__
  430. =back
  431. =head1 CHANGES
  432. =head2 Version 1.00 (dpkg 1.18.2)
  433. Mark the module as public.
  434. =head1 SEE ALSO
  435. dpkg-architecture(1).