dpkg-architecture.pl 8.7 KB

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