Arch.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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_gcc_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 $gcc_host_gnu_type;
  91. sub get_gcc_host_gnu_type()
  92. {
  93. return $gcc_host_gnu_type if defined $gcc_host_gnu_type;
  94. $gcc_host_gnu_type = qx(\${CC:-gcc} -dumpmachine);
  95. if ($? >> 8) {
  96. $gcc_host_gnu_type = '';
  97. } else {
  98. chomp $gcc_host_gnu_type;
  99. }
  100. return $gcc_host_gnu_type;
  101. }
  102. =item $arch = get_raw_host_arch()
  103. Get the raw host Debian architecture, without taking into account variables
  104. from the environment.
  105. =cut
  106. sub get_raw_host_arch()
  107. {
  108. state $host_arch;
  109. return $host_arch if defined $host_arch;
  110. $gcc_host_gnu_type = get_gcc_host_gnu_type();
  111. if ($gcc_host_gnu_type eq '') {
  112. warning(g_("couldn't determine gcc system type, falling back to " .
  113. 'default (native compilation)'));
  114. } else {
  115. my (@host_archtriplet) = gnutriplet_to_debtriplet($gcc_host_gnu_type);
  116. $host_arch = debtriplet_to_debarch(@host_archtriplet);
  117. if (defined $host_arch) {
  118. $gcc_host_gnu_type = debtriplet_to_gnutriplet(@host_archtriplet);
  119. } else {
  120. warning(g_('unknown gcc system type %s, falling back to ' .
  121. 'default (native compilation)'), $gcc_host_gnu_type);
  122. $gcc_host_gnu_type = '';
  123. }
  124. }
  125. if (!defined($host_arch)) {
  126. # Switch to native compilation.
  127. $host_arch = get_raw_build_arch();
  128. }
  129. return $host_arch;
  130. }
  131. }
  132. =item $arch = get_host_arch()
  133. Get the host Debian architecture, using DEB_HOST_ARCH from the environment
  134. if available.
  135. =cut
  136. sub get_host_arch()
  137. {
  138. return Dpkg::BuildEnv::get('DEB_HOST_ARCH') || get_raw_host_arch();
  139. }
  140. =item @arch_list = get_valid_arches()
  141. Get an array with all currently known Debian architectures.
  142. =cut
  143. sub get_valid_arches()
  144. {
  145. read_cputable();
  146. read_ostable();
  147. my @arches;
  148. foreach my $os (@os) {
  149. foreach my $cpu (@cpu) {
  150. my $arch = debtriplet_to_debarch(split(/-/, $os, 2), $cpu);
  151. push @arches, $arch if defined($arch);
  152. }
  153. }
  154. return @arches;
  155. }
  156. my %table_loaded;
  157. sub load_table
  158. {
  159. my ($table, $loader) = @_;
  160. return if $table_loaded{$table};
  161. local $_;
  162. local $/ = "\n";
  163. open my $table_fh, '<', "$Dpkg::DATADIR/$table"
  164. or syserr(g_('cannot open %s'), $table);
  165. while (<$table_fh>) {
  166. $loader->($_);
  167. }
  168. close $table_fh;
  169. $table_loaded{$table} = 1;
  170. }
  171. sub read_cputable
  172. {
  173. load_table('cputable', sub {
  174. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) {
  175. $cputable{$1} = $2;
  176. $cputable_re{$1} = $3;
  177. $cpubits{$1} = $4;
  178. $cpuendian{$1} = $5;
  179. push @cpu, $1;
  180. }
  181. });
  182. }
  183. sub read_ostable
  184. {
  185. load_table('ostable', sub {
  186. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)/) {
  187. $ostable{$1} = $2;
  188. $ostable_re{$1} = $3;
  189. push @os, $1;
  190. }
  191. });
  192. }
  193. sub abitable_load()
  194. {
  195. load_table('abitable', sub {
  196. if (m/^(?!\#)(\S+)\s+(\S+)/) {
  197. $abibits{$1} = $2;
  198. }
  199. });
  200. }
  201. sub read_triplettable()
  202. {
  203. read_cputable();
  204. load_table('triplettable', sub {
  205. if (m/^(?!\#)(\S+)\s+(\S+)/) {
  206. my $debtriplet = $1;
  207. my $debarch = $2;
  208. if ($debtriplet =~ /<cpu>/) {
  209. foreach my $_cpu (@cpu) {
  210. (my $dt = $debtriplet) =~ s/<cpu>/$_cpu/;
  211. (my $da = $debarch) =~ s/<cpu>/$_cpu/;
  212. next if exists $debarch_to_debtriplet{$da}
  213. or exists $debtriplet_to_debarch{$dt};
  214. $debarch_to_debtriplet{$da} = $dt;
  215. $debtriplet_to_debarch{$dt} = $da;
  216. }
  217. } else {
  218. $debarch_to_debtriplet{$2} = $1;
  219. $debtriplet_to_debarch{$1} = $2;
  220. }
  221. }
  222. });
  223. }
  224. sub debtriplet_to_gnutriplet(@)
  225. {
  226. my ($abi, $os, $cpu) = @_;
  227. read_cputable();
  228. read_ostable();
  229. return unless defined($abi) && defined($os) && defined($cpu) &&
  230. exists($cputable{$cpu}) && exists($ostable{"$abi-$os"});
  231. return join('-', $cputable{$cpu}, $ostable{"$abi-$os"});
  232. }
  233. sub gnutriplet_to_debtriplet($)
  234. {
  235. my $gnu = shift;
  236. return unless defined($gnu);
  237. my ($gnu_cpu, $gnu_os) = split(/-/, $gnu, 2);
  238. return unless defined($gnu_cpu) && defined($gnu_os);
  239. read_cputable();
  240. read_ostable();
  241. my ($os, $cpu);
  242. foreach my $_cpu (@cpu) {
  243. if ($gnu_cpu =~ /^$cputable_re{$_cpu}$/) {
  244. $cpu = $_cpu;
  245. last;
  246. }
  247. }
  248. foreach my $_os (@os) {
  249. if ($gnu_os =~ /^(.*-)?$ostable_re{$_os}$/) {
  250. $os = $_os;
  251. last;
  252. }
  253. }
  254. return if !defined($cpu) || !defined($os);
  255. return (split(/-/, $os, 2), $cpu);
  256. }
  257. =item $multiarch = gnutriplet_to_multiarch($gnutriplet)
  258. Map a GNU triplet into a Debian multiarch triplet.
  259. =cut
  260. sub gnutriplet_to_multiarch($)
  261. {
  262. my $gnu = shift;
  263. my ($cpu, $cdr) = split(/-/, $gnu, 2);
  264. if ($cpu =~ /^i[4567]86$/) {
  265. return "i386-$cdr";
  266. } else {
  267. return $gnu;
  268. }
  269. }
  270. =item $multiarch = debarch_to_multiarch($arch)
  271. Map a Debian architecture into a Debian multiarch triplet.
  272. =cut
  273. sub debarch_to_multiarch($)
  274. {
  275. my $arch = shift;
  276. return gnutriplet_to_multiarch(debarch_to_gnutriplet($arch));
  277. }
  278. sub debtriplet_to_debarch(@)
  279. {
  280. my ($abi, $os, $cpu) = @_;
  281. read_triplettable();
  282. if (!defined($abi) || !defined($os) || !defined($cpu)) {
  283. return;
  284. } elsif (exists $debtriplet_to_debarch{"$abi-$os-$cpu"}) {
  285. return $debtriplet_to_debarch{"$abi-$os-$cpu"};
  286. } else {
  287. return;
  288. }
  289. }
  290. sub debarch_to_debtriplet($)
  291. {
  292. my $arch = shift;
  293. read_triplettable();
  294. if ($arch =~ /^linux-([^-]*)/) {
  295. # XXX: Might disappear in the future, not sure yet.
  296. $arch = $1;
  297. }
  298. my $triplet = $debarch_to_debtriplet{$arch};
  299. if (defined($triplet)) {
  300. return split(/-/, $triplet, 3);
  301. } else {
  302. return;
  303. }
  304. }
  305. =item $gnutriplet = debarch_to_gnutriplet($arch)
  306. Map a Debian architecture into a GNU triplet.
  307. =cut
  308. sub debarch_to_gnutriplet($)
  309. {
  310. my $arch = shift;
  311. return debtriplet_to_gnutriplet(debarch_to_debtriplet($arch));
  312. }
  313. =item $arch = gnutriplet_to_debarch($gnutriplet)
  314. Map a GNU triplet into a Debian architecture.
  315. =cut
  316. sub gnutriplet_to_debarch($)
  317. {
  318. my $gnu = shift;
  319. return debtriplet_to_debarch(gnutriplet_to_debtriplet($gnu));
  320. }
  321. sub debwildcard_to_debtriplet($)
  322. {
  323. my $arch = shift;
  324. my @tuple = split /-/, $arch, 3;
  325. if (any { $_ eq 'any' } @tuple) {
  326. if (scalar @tuple == 3) {
  327. return @tuple;
  328. } elsif (scalar @tuple == 2) {
  329. return ('any', @tuple);
  330. } else {
  331. return ('any', 'any', 'any');
  332. }
  333. } else {
  334. return debarch_to_debtriplet($arch);
  335. }
  336. }
  337. sub debarch_to_cpuattrs($)
  338. {
  339. my $arch = shift;
  340. my ($abi, $os, $cpu) = debarch_to_debtriplet($arch);
  341. if (defined($cpu)) {
  342. abitable_load();
  343. return ($abibits{$abi} // $cpubits{$cpu}, $cpuendian{$cpu});
  344. } else {
  345. return;
  346. }
  347. }
  348. =item $bool = debarch_eq($arch_a, $arch_b)
  349. Evaluate the equality of a Debian architecture, by comparing with another
  350. Debian architecture. No wildcard matching is performed.
  351. =cut
  352. sub debarch_eq($$)
  353. {
  354. my ($a, $b) = @_;
  355. return 1 if ($a eq $b);
  356. my @a = debarch_to_debtriplet($a);
  357. my @b = debarch_to_debtriplet($b);
  358. return 0 if scalar @a != 3 or scalar @b != 3;
  359. return ($a[0] eq $b[0] && $a[1] eq $b[1] && $a[2] eq $b[2]);
  360. }
  361. =item $bool = debarch_is($arch, $arch_wildcard)
  362. Evaluate the identity of a Debian architecture, by matchings with an
  363. architecture wildcard.
  364. =cut
  365. sub debarch_is($$)
  366. {
  367. my ($real, $alias) = @_;
  368. return 1 if ($alias eq $real or $alias eq 'any');
  369. my @real = debarch_to_debtriplet($real);
  370. my @alias = debwildcard_to_debtriplet($alias);
  371. return 0 if scalar @real != 3 or scalar @alias != 3;
  372. if (($alias[0] eq $real[0] || $alias[0] eq 'any') &&
  373. ($alias[1] eq $real[1] || $alias[1] eq 'any') &&
  374. ($alias[2] eq $real[2] || $alias[2] eq 'any')) {
  375. return 1;
  376. }
  377. return 0;
  378. }
  379. =item $bool = debarch_is_wildcard($arch)
  380. Evaluate whether a Debian architecture is an architecture wildcard.
  381. =cut
  382. sub debarch_is_wildcard($)
  383. {
  384. my $arch = shift;
  385. return 0 if $arch eq 'all';
  386. my @triplet = debwildcard_to_debtriplet($arch);
  387. return 0 if scalar @triplet != 3;
  388. return 1 if any { $_ eq 'any' } @triplet;
  389. return 0;
  390. }
  391. =item $bool = debarch_is_concerned($arch, @arches)
  392. Evaluate whether a Debian architecture applies to the list of architecture
  393. restrictions, as usually found in dependencies inside square brackets.
  394. =cut
  395. sub debarch_is_concerned
  396. {
  397. my ($host_arch, @arches) = @_;
  398. my $seen_arch = 0;
  399. foreach my $arch (@arches) {
  400. $arch = lc $arch;
  401. if ($arch =~ /^!/) {
  402. my $not_arch = $arch;
  403. $not_arch =~ s/^!//;
  404. if (debarch_is($host_arch, $not_arch)) {
  405. $seen_arch = 0;
  406. last;
  407. } else {
  408. # !arch includes by default all other arches
  409. # unless they also appear in a !otherarch
  410. $seen_arch = 1;
  411. }
  412. } elsif (debarch_is($host_arch, $arch)) {
  413. $seen_arch = 1;
  414. last;
  415. }
  416. }
  417. return $seen_arch;
  418. }
  419. 1;
  420. __END__
  421. =back
  422. =head1 CHANGES
  423. =head2 Version 1.00 (dpkg 1.18.2)
  424. Mark the module as public.
  425. =head1 SEE ALSO
  426. dpkg-architecture(1).