dpkg-architecture.pl 9.3 KB

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