Arch.pm 9.3 KB

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