Arch.pm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 $cputable_loaded = 0;
  116. sub read_cputable
  117. {
  118. return if ($cputable_loaded);
  119. local $_;
  120. local $/ = "\n";
  121. open my $cputable_fh, '<', "$Dpkg::DATADIR/cputable"
  122. or syserr(g_('cannot open %s'), 'cputable');
  123. while (<$cputable_fh>) {
  124. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) {
  125. $cputable{$1} = $2;
  126. $cputable_re{$1} = $3;
  127. $cpubits{$1} = $4;
  128. $cpuendian{$1} = $5;
  129. push @cpu, $1;
  130. }
  131. }
  132. close $cputable_fh;
  133. $cputable_loaded = 1;
  134. }
  135. my $ostable_loaded = 0;
  136. sub read_ostable
  137. {
  138. return if ($ostable_loaded);
  139. local $_;
  140. local $/ = "\n";
  141. open my $ostable_fh, '<', "$Dpkg::DATADIR/ostable"
  142. or syserr(g_('cannot open %s'), 'ostable');
  143. while (<$ostable_fh>) {
  144. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)/) {
  145. $ostable{$1} = $2;
  146. $ostable_re{$1} = $3;
  147. push @os, $1;
  148. }
  149. }
  150. close $ostable_fh;
  151. $ostable_loaded = 1;
  152. }
  153. my $abitable_loaded = 0;
  154. sub abitable_load()
  155. {
  156. return if ($abitable_loaded);
  157. local $_;
  158. local $/ = "\n";
  159. # Because the abitable is only for override information, do not fail if
  160. # it does not exist, as that will only mean the other tables do not have
  161. # an entry needing to be overridden. This way we do not require a newer
  162. # dpkg by libdpkg-perl.
  163. if (open my $abitable_fh, '<', "$Dpkg::DATADIR/abitable") {
  164. while (<$abitable_fh>) {
  165. if (m/^(?!\#)(\S+)\s+(\S+)/) {
  166. $abibits{$1} = $2;
  167. }
  168. }
  169. close $abitable_fh;
  170. } elsif ($! != ENOENT) {
  171. syserr(g_('cannot open %s'), 'abitable');
  172. }
  173. $abitable_loaded = 1;
  174. }
  175. my $triplettable_loaded = 0;
  176. sub read_triplettable()
  177. {
  178. return if ($triplettable_loaded);
  179. read_cputable();
  180. local $_;
  181. local $/ = "\n";
  182. open my $triplettable_fh, '<', "$Dpkg::DATADIR/triplettable"
  183. or syserr(g_('cannot open %s'), 'triplettable');
  184. while (<$triplettable_fh>) {
  185. if (m/^(?!\#)(\S+)\s+(\S+)/) {
  186. my $debtriplet = $1;
  187. my $debarch = $2;
  188. if ($debtriplet =~ /<cpu>/) {
  189. foreach my $_cpu (@cpu) {
  190. (my $dt = $debtriplet) =~ s/<cpu>/$_cpu/;
  191. (my $da = $debarch) =~ s/<cpu>/$_cpu/;
  192. next if exists $debarch_to_debtriplet{$da}
  193. or exists $debtriplet_to_debarch{$dt};
  194. $debarch_to_debtriplet{$da} = $dt;
  195. $debtriplet_to_debarch{$dt} = $da;
  196. }
  197. } else {
  198. $debarch_to_debtriplet{$2} = $1;
  199. $debtriplet_to_debarch{$1} = $2;
  200. }
  201. }
  202. }
  203. close $triplettable_fh;
  204. $triplettable_loaded = 1;
  205. }
  206. sub debtriplet_to_gnutriplet(@)
  207. {
  208. my ($abi, $os, $cpu) = @_;
  209. read_cputable();
  210. read_ostable();
  211. return unless defined($abi) && defined($os) && defined($cpu) &&
  212. exists($cputable{$cpu}) && exists($ostable{"$abi-$os"});
  213. return join('-', $cputable{$cpu}, $ostable{"$abi-$os"});
  214. }
  215. sub gnutriplet_to_debtriplet($)
  216. {
  217. my ($gnu) = @_;
  218. return unless defined($gnu);
  219. my ($gnu_cpu, $gnu_os) = split(/-/, $gnu, 2);
  220. return unless defined($gnu_cpu) && defined($gnu_os);
  221. read_cputable();
  222. read_ostable();
  223. my ($os, $cpu);
  224. foreach my $_cpu (@cpu) {
  225. if ($gnu_cpu =~ /^$cputable_re{$_cpu}$/) {
  226. $cpu = $_cpu;
  227. last;
  228. }
  229. }
  230. foreach my $_os (@os) {
  231. if ($gnu_os =~ /^(.*-)?$ostable_re{$_os}$/) {
  232. $os = $_os;
  233. last;
  234. }
  235. }
  236. return if !defined($cpu) || !defined($os);
  237. return (split(/-/, $os, 2), $cpu);
  238. }
  239. sub gnutriplet_to_multiarch($)
  240. {
  241. my ($gnu) = @_;
  242. my ($cpu, $cdr) = split(/-/, $gnu, 2);
  243. if ($cpu =~ /^i[4567]86$/) {
  244. return "i386-$cdr";
  245. } else {
  246. return $gnu;
  247. }
  248. }
  249. sub debarch_to_multiarch($)
  250. {
  251. my ($arch) = @_;
  252. return gnutriplet_to_multiarch(debarch_to_gnutriplet($arch));
  253. }
  254. sub debtriplet_to_debarch(@)
  255. {
  256. my ($abi, $os, $cpu) = @_;
  257. read_triplettable();
  258. if (!defined($abi) || !defined($os) || !defined($cpu)) {
  259. return;
  260. } elsif (exists $debtriplet_to_debarch{"$abi-$os-$cpu"}) {
  261. return $debtriplet_to_debarch{"$abi-$os-$cpu"};
  262. } else {
  263. return;
  264. }
  265. }
  266. sub debarch_to_debtriplet($)
  267. {
  268. my $arch = shift;
  269. read_triplettable();
  270. if ($arch =~ /^linux-([^-]*)/) {
  271. # XXX: Might disappear in the future, not sure yet.
  272. $arch = $1;
  273. }
  274. my $triplet = $debarch_to_debtriplet{$arch};
  275. if (defined($triplet)) {
  276. return split(/-/, $triplet, 3);
  277. } else {
  278. return;
  279. }
  280. }
  281. sub debarch_to_gnutriplet($)
  282. {
  283. my ($arch) = @_;
  284. return debtriplet_to_gnutriplet(debarch_to_debtriplet($arch));
  285. }
  286. sub gnutriplet_to_debarch($)
  287. {
  288. my ($gnu) = @_;
  289. return debtriplet_to_debarch(gnutriplet_to_debtriplet($gnu));
  290. }
  291. sub debwildcard_to_debtriplet($)
  292. {
  293. my ($arch) = @_;
  294. my @tuple = split /-/, $arch, 3;
  295. if (any { $_ eq 'any' } @tuple) {
  296. if (scalar @tuple == 3) {
  297. return @tuple;
  298. } elsif (scalar @tuple == 2) {
  299. return ('any', @tuple);
  300. } else {
  301. return ('any', 'any', 'any');
  302. }
  303. } else {
  304. return debarch_to_debtriplet($arch);
  305. }
  306. }
  307. sub debarch_to_cpuattrs($)
  308. {
  309. my ($arch) = @_;
  310. my ($abi, $os, $cpu) = debarch_to_debtriplet($arch);
  311. if (defined($cpu)) {
  312. abitable_load();
  313. return ($abibits{$abi} // $cpubits{$cpu}, $cpuendian{$cpu});
  314. } else {
  315. return;
  316. }
  317. }
  318. sub debarch_eq($$)
  319. {
  320. my ($a, $b) = @_;
  321. return 1 if ($a eq $b);
  322. my @a = debarch_to_debtriplet($a);
  323. my @b = debarch_to_debtriplet($b);
  324. return 0 if scalar @a != 3 or scalar @b != 3;
  325. return ($a[0] eq $b[0] && $a[1] eq $b[1] && $a[2] eq $b[2]);
  326. }
  327. sub debarch_is($$)
  328. {
  329. my ($real, $alias) = @_;
  330. return 1 if ($alias eq $real or $alias eq 'any');
  331. my @real = debarch_to_debtriplet($real);
  332. my @alias = debwildcard_to_debtriplet($alias);
  333. return 0 if scalar @real != 3 or scalar @alias != 3;
  334. if (($alias[0] eq $real[0] || $alias[0] eq 'any') &&
  335. ($alias[1] eq $real[1] || $alias[1] eq 'any') &&
  336. ($alias[2] eq $real[2] || $alias[2] eq 'any')) {
  337. return 1;
  338. }
  339. return 0;
  340. }
  341. sub debarch_is_wildcard($)
  342. {
  343. my ($arch) = @_;
  344. return 0 if $arch eq 'all';
  345. my @triplet = debwildcard_to_debtriplet($arch);
  346. return 0 if scalar @triplet != 3;
  347. return 1 if any { $_ eq 'any' } @triplet;
  348. return 0;
  349. }
  350. sub debarch_is_concerned
  351. {
  352. my ($host_arch, @arches) = @_;
  353. my $seen_arch = 0;
  354. foreach my $arch (@arches) {
  355. $arch = lc $arch;
  356. if ($arch =~ /^!/) {
  357. my $not_arch = $arch;
  358. $not_arch =~ s/^!//;
  359. if (debarch_is($host_arch, $not_arch)) {
  360. $seen_arch = 0;
  361. last;
  362. } else {
  363. # !arch includes by default all other arches
  364. # unless they also appear in a !otherarch
  365. $seen_arch = 1;
  366. }
  367. } elsif (debarch_is($host_arch, $arch)) {
  368. $seen_arch = 1;
  369. last;
  370. }
  371. }
  372. return $seen_arch;
  373. }
  374. 1;