dpkg-architecture.pl 8.8 KB

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