dpkg-architecture.pl 9.7 KB

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