dpkg-architecture.pl 8.9 KB

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