dpkg-architecture.pl 8.9 KB

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