dpkg-architecture.pl 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #! /usr/bin/perl
  2. #
  3. # dpkg-architecture
  4. #
  5. # Copyright © 2004-2005 Scott James Remnant <scott@netsplit.com>,
  6. # Copyright © 1999 Marcus Brinkmann <brinkmd@debian.org>.
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. use strict;
  22. use warnings;
  23. our $progname;
  24. our $version = "1.0.0"; # This line modified by Makefile
  25. our $dpkglibdir = "."; # This line modified by Makefile
  26. push(@INC,$dpkglibdir);
  27. require 'controllib.pl';
  28. require 'dpkg-gettext.pl';
  29. textdomain("dpkg-dev");
  30. my $pkgdatadir = "..";
  31. sub version {
  32. printf _g("Debian %s version %s.\n"), $progname, $version;
  33. printf _g("
  34. Copyright (C) 1999-2001 Marcus Brinkmann <brinkmd\@debian.org>.
  35. Copyright (C) 2004-2005 Scott James Remnant <scott\@netsplit.com>.");
  36. printf _g("
  37. This is free software; see the GNU General Public Licence version 2 or
  38. later for copying conditions. There is NO warranty.
  39. ");
  40. }
  41. sub usage {
  42. printf _g(
  43. "Usage: %s [<option> ...] [<action>]
  44. Options:
  45. -a<debian-arch> set current Debian architecture.
  46. -t<gnu-system> set current GNU system type.
  47. -L list valid architectures.
  48. -f force flag (override variables set in environment).
  49. Actions:
  50. -l list variables (default).
  51. -e<debian-arch> compare with current Debian architecture.
  52. -i<arch-alias> check if current Debian architecture is <arch-alias>.
  53. -q<variable> prints only the value of <variable>.
  54. -s print command to set environment variables.
  55. -u print command to unset environment variables.
  56. -c <command> set environment and run the command in it.
  57. --help show this help message.
  58. --version show the version.
  59. "), $progname;
  60. }
  61. my (@cpu, @os);
  62. my (%cputable, %ostable);
  63. my (%cputable_re, %ostable_re);
  64. sub read_cputable {
  65. open CPUTABLE, "$pkgdatadir/cputable"
  66. or &syserr(_g("unable to open cputable"));
  67. while (<CPUTABLE>) {
  68. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)/) {
  69. $cputable{$1} = $2;
  70. $cputable_re{$1} = $3;
  71. push @cpu, $1;
  72. }
  73. }
  74. close CPUTABLE;
  75. }
  76. sub read_ostable {
  77. open OSTABLE, "$pkgdatadir/ostable"
  78. or &syserr(_g("unable to open ostable"));
  79. while (<OSTABLE>) {
  80. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)/) {
  81. $ostable{$1} = $2;
  82. $ostable_re{$1} = $3;
  83. push @os, $1;
  84. }
  85. }
  86. close OSTABLE;
  87. }
  88. sub split_debian {
  89. local ($_) = @_;
  90. if (/^([^-]*)-(.*)/) {
  91. return ($1, $2);
  92. } else {
  93. return ("linux", $_);
  94. }
  95. }
  96. sub debian_to_gnu {
  97. my ($arch) = @_;
  98. my ($os, $cpu) = split_debian($arch);
  99. return undef unless exists($cputable{$cpu}) && exists($ostable{$os});
  100. return join("-", $cputable{$cpu}, $ostable{$os});
  101. }
  102. sub split_gnu {
  103. local ($_) = @_;
  104. /^([^-]*)-(.*)/;
  105. return ($1, $2);
  106. }
  107. sub gnu_to_debian {
  108. my ($gnu) = @_;
  109. my ($cpu, $os);
  110. my ($gnu_cpu, $gnu_os) = split_gnu($gnu);
  111. foreach my $_cpu (@cpu) {
  112. if ($gnu_cpu =~ /^$cputable_re{$_cpu}$/) {
  113. $cpu = $_cpu;
  114. last;
  115. }
  116. }
  117. foreach my $_os (@os) {
  118. if ($gnu_os =~ /^(.*-)?$ostable_re{$_os}$/) {
  119. $os = $_os;
  120. last;
  121. }
  122. }
  123. return undef if !defined($cpu) || !defined($os);
  124. return debian_arch_fix($os, $cpu);
  125. }
  126. &read_cputable;
  127. &read_ostable;
  128. # Check for -L
  129. if (grep { m/^-L$/ } @ARGV) {
  130. foreach my $os (@os) {
  131. foreach my $cpu (@cpu) {
  132. print debian_arch_fix($os, $cpu)."\n";
  133. }
  134. }
  135. exit unless $#ARGV;
  136. }
  137. # Set default values:
  138. chomp (my $deb_build_arch = `dpkg --print-architecture`);
  139. &syserr("dpkg --print-architecture failed") if $?>>8;
  140. my $deb_build_gnu_type = debian_to_gnu($deb_build_arch);
  141. # Default host: Current gcc.
  142. my $gcc = `\${CC:-gcc} -dumpmachine`;
  143. if ($?>>8) {
  144. warning(_g("Couldn't determine gcc system type, falling back to default (native compilation)"));
  145. $gcc = '';
  146. } else {
  147. chomp $gcc;
  148. }
  149. my $deb_host_arch = undef;
  150. my $deb_host_gnu_type;
  151. if ($gcc ne '') {
  152. $deb_host_arch = &gnu_to_debian($gcc);
  153. unless (defined $deb_host_arch) {
  154. warning(sprintf(_g("Unknown gcc system type %s, falling back to default (native compilation)"), $gcc));
  155. $gcc = '';
  156. } else {
  157. $gcc = $deb_host_gnu_type = &debian_to_gnu($deb_host_arch);
  158. }
  159. }
  160. if (!defined($deb_host_arch)) {
  161. # Default host: Native compilation.
  162. $deb_host_arch = $deb_build_arch;
  163. $deb_host_gnu_type = $deb_build_gnu_type;
  164. }
  165. my $req_host_arch = '';
  166. my $req_host_gnu_type = '';
  167. my $req_build_gnu_type = '';
  168. my $req_eq_arch = '';
  169. my $req_is_arch = '';
  170. my $req_variable_to_print;
  171. my $action = 'l';
  172. my $force = 0;
  173. while (@ARGV) {
  174. $_=shift(@ARGV);
  175. if (m/^-a/) {
  176. $req_host_arch = "$'";
  177. } elsif (m/^-t/) {
  178. $req_host_gnu_type = "$'";
  179. } elsif (m/^-e/) {
  180. $req_eq_arch = "$'";
  181. $action = 'e';
  182. } elsif (m/^-i/) {
  183. $req_is_arch = "$'";
  184. $action = 'i';
  185. } elsif (m/^-[lsu]$/) {
  186. $action = $_;
  187. $action =~ s/^-//;
  188. } elsif (m/^-f$/) {
  189. $force=1;
  190. } elsif (m/^-q/) {
  191. $req_variable_to_print = "$'";
  192. $action = 'q';
  193. } elsif (m/^-c$/) {
  194. $action = 'c';
  195. last;
  196. } elsif (m/^-L$/) {
  197. # Handled already
  198. } elsif (m/^-(h|-help)$/) {
  199. &usage;
  200. exit 0;
  201. } elsif (m/^--version$/) {
  202. &version;
  203. exit 0;
  204. } else {
  205. usageerr(sprintf(_g("unknown option \`%s'"), $_));
  206. }
  207. }
  208. if ($req_host_arch ne '' && $req_host_gnu_type eq '') {
  209. $req_host_gnu_type = &debian_to_gnu ($req_host_arch);
  210. die (sprintf(_g("unknown Debian architecture %s, you must specify GNU system type, too"), $req_host_arch)) unless defined $req_host_gnu_type;
  211. }
  212. if ($req_host_gnu_type ne '' && $req_host_arch eq '') {
  213. $req_host_arch = &gnu_to_debian ($req_host_gnu_type);
  214. die (sprintf(_g("unknown GNU system type %s, you must specify Debian architecture, too"), $req_host_gnu_type)) unless defined $req_host_arch;
  215. }
  216. if ($req_host_gnu_type ne '' && $req_host_arch ne '') {
  217. my $dfl_host_gnu_type = debian_to_gnu($req_host_arch);
  218. warning(sprintf(_g("Default GNU system type %s for Debian arch %s does not match specified GNU system type %s"), $dfl_host_gnu_type, $req_host_arch, $req_host_gnu_type)) if $dfl_host_gnu_type ne $req_host_gnu_type;
  219. }
  220. $deb_host_arch = $req_host_arch if $req_host_arch ne '';
  221. $deb_host_gnu_type = $req_host_gnu_type if $req_host_gnu_type ne '';
  222. #$gcc = `\${CC:-gcc} --print-libgcc-file-name`;
  223. #$gcc =~ s!^.*gcc-lib/(.*)/\d+(?:.\d+)*/libgcc.*$!$1!s;
  224. warning(sprintf(_g("Specified GNU system type %s does not match gcc system type %s."), $deb_host_gnu_type, $gcc)) if !($req_is_arch or $req_eq_arch) && ($gcc ne '') && ($gcc ne $deb_host_gnu_type);
  225. # Split the Debian and GNU names
  226. my ($deb_host_arch_os, $deb_host_arch_cpu) = split_debian($deb_host_arch);
  227. my ($deb_build_arch_os, $deb_build_arch_cpu) = split_debian($deb_build_arch);
  228. my ($deb_host_gnu_cpu, $deb_host_gnu_system) = split_gnu($deb_host_gnu_type);
  229. my ($deb_build_gnu_cpu, $deb_build_gnu_system) = split_gnu($deb_build_gnu_type);
  230. my %env = ();
  231. if (!$force) {
  232. $deb_build_arch = $ENV{DEB_BUILD_ARCH} if (exists $ENV{DEB_BUILD_ARCH});
  233. $deb_build_arch_os = $ENV{DEB_BUILD_ARCH_OS} if (exists $ENV{DEB_BUILD_ARCH_OS});
  234. $deb_build_arch_cpu = $ENV{DEB_BUILD_ARCH_CPU} if (exists $ENV{DEB_BUILD_ARCH_CPU});
  235. $deb_build_gnu_cpu = $ENV{DEB_BUILD_GNU_CPU} if (exists $ENV{DEB_BUILD_GNU_CPU});
  236. $deb_build_gnu_system = $ENV{DEB_BUILD_GNU_SYSTEM} if (exists $ENV{DEB_BUILD_GNU_SYSTEM});
  237. $deb_build_gnu_type = $ENV{DEB_BUILD_GNU_TYPE} if (exists $ENV{DEB_BUILD_GNU_TYPE});
  238. $deb_host_arch = $ENV{DEB_HOST_ARCH} if (exists $ENV{DEB_HOST_ARCH});
  239. $deb_host_arch_os = $ENV{DEB_HOST_ARCH_OS} if (exists $ENV{DEB_HOST_ARCH_OS});
  240. $deb_host_arch_cpu = $ENV{DEB_HOST_ARCH_CPU} if (exists $ENV{DEB_HOST_ARCH_CPU});
  241. $deb_host_gnu_cpu = $ENV{DEB_HOST_GNU_CPU} if (exists $ENV{DEB_HOST_GNU_CPU});
  242. $deb_host_gnu_system = $ENV{DEB_HOST_GNU_SYSTEM} if (exists $ENV{DEB_HOST_GNU_SYSTEM});
  243. $deb_host_gnu_type = $ENV{DEB_HOST_GNU_TYPE} if (exists $ENV{DEB_HOST_GNU_TYPE});
  244. }
  245. my @ordered = qw(DEB_BUILD_ARCH DEB_BUILD_ARCH_OS DEB_BUILD_ARCH_CPU
  246. DEB_BUILD_GNU_CPU DEB_BUILD_GNU_SYSTEM DEB_BUILD_GNU_TYPE
  247. DEB_HOST_ARCH DEB_HOST_ARCH_OS DEB_HOST_ARCH_CPU
  248. DEB_HOST_GNU_CPU DEB_HOST_GNU_SYSTEM DEB_HOST_GNU_TYPE);
  249. $env{'DEB_BUILD_ARCH'}=$deb_build_arch;
  250. $env{'DEB_BUILD_ARCH_OS'}=$deb_build_arch_os;
  251. $env{'DEB_BUILD_ARCH_CPU'}=$deb_build_arch_cpu;
  252. $env{'DEB_BUILD_GNU_CPU'}=$deb_build_gnu_cpu;
  253. $env{'DEB_BUILD_GNU_SYSTEM'}=$deb_build_gnu_system;
  254. $env{'DEB_BUILD_GNU_TYPE'}=$deb_build_gnu_type;
  255. $env{'DEB_HOST_ARCH'}=$deb_host_arch;
  256. $env{'DEB_HOST_ARCH_OS'}=$deb_host_arch_os;
  257. $env{'DEB_HOST_ARCH_CPU'}=$deb_host_arch_cpu;
  258. $env{'DEB_HOST_GNU_CPU'}=$deb_host_gnu_cpu;
  259. $env{'DEB_HOST_GNU_SYSTEM'}=$deb_host_gnu_system;
  260. $env{'DEB_HOST_GNU_TYPE'}=$deb_host_gnu_type;
  261. if ($action eq 'l') {
  262. foreach my $k (@ordered) {
  263. print "$k=$env{$k}\n";
  264. }
  265. } elsif ($action eq 's') {
  266. foreach my $k (@ordered) {
  267. print "$k=$env{$k}; ";
  268. }
  269. print "export ".join(" ",@ordered)."\n";
  270. } elsif ($action eq 'u') {
  271. print "unset ".join(" ",@ordered)."\n";
  272. } elsif ($action eq 'e') {
  273. exit !debian_arch_eq($deb_host_arch, $req_eq_arch);
  274. } elsif ($action eq 'i') {
  275. exit !debian_arch_is($deb_host_arch, $req_is_arch);
  276. } elsif ($action eq 'c') {
  277. @ENV{keys %env} = values %env;
  278. exec @ARGV;
  279. } elsif ($action eq 'q') {
  280. if (exists $env{$req_variable_to_print}) {
  281. print "$env{$req_variable_to_print}\n";
  282. } else {
  283. die sprintf(_g("%s is not a supported variable name"), $req_variable_to_print);
  284. }
  285. }