dpkg-architecture.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #! /usr/bin/perl
  2. #
  3. # dpkg-architecture
  4. #
  5. # Copyright 1999 Marcus Brinkmann <brinkmd@debian.org>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. # History
  21. # 0.0.1 Initial release.
  22. # 0.0.2 Don't use dpkg to get default gnu system, so the default is
  23. # correct even on non-linux system.
  24. # Warn if the host gnu system does not match the gcc system.
  25. # Determine default from gcc if possible, else fall back to native
  26. # compilation.
  27. # Do not set environment variables which are already defined unless
  28. # force flag is given.
  29. # 1.0.0 Changed target to host, because this complies with GNU
  30. # nomenclature.
  31. # Added command facility.
  32. # 1.0.1 Moved to GNU nomenclature arch->cpu, system->type, os->system
  33. # 1.0.2 Add facility to query single values, suggested by Richard Braakman.
  34. # 1.0.3 Make it work with egcs, too.
  35. # 1.0.4 Suppress single "export" with "-s" when all env variables are already set
  36. # 1.0.5 Update default for rules files (i386->i486).
  37. # Print out overridden values, so make gets them, too.
  38. # 1.0.6 Revert to i386 to comply with policy § 5.1.
  39. # 1.0.7 -q should not imply -f, because this prevents setting
  40. # make variables with non-standard names correctly.
  41. $version="1.0.0";
  42. $0 = `basename $0`; chomp $0;
  43. $dpkglibdir="/usr/lib/dpkg";
  44. push(@INC,$dpkglibdir);
  45. require 'controllib.pl';
  46. %archtable=('i386', 'i386-linux',
  47. 'sparc', 'sparc-linux',
  48. 'sparc64', 'sparc64-linux',
  49. 'alpha', 'alpha-linux',
  50. 'm68k', 'm68k-linux',
  51. 'arm', 'arm-linux',
  52. 'powerpc', 'powerpc-linux',
  53. 'mips', 'mips-linux',
  54. 'mipsel', 'mipsel-linux',
  55. 'sh3', 'sh3-linux',
  56. 'sh4', 'sh4-linux',
  57. 'sh3eb', 'sh3eb-linux',
  58. 'sh4eb', 'sh4eb-linux',
  59. 'hppa', 'hppa-linux',
  60. 'hurd-i386', 'i386-gnu',
  61. 's390', 's390-linux',
  62. 's390x', 's390x-linux',
  63. 'ia64', 'ia64-linux',
  64. 'amd64', 'x86_64-linux',
  65. 'openbsd-i386', 'i386-openbsd',
  66. 'freebsd-i386', 'i386-kfreebsd-gnu',
  67. 'netbsd-i386', 'i386-netbsdelf-gnu',
  68. 'darwin-powerpc', 'powerpc-darwin',
  69. 'darwin-i386', 'i386-darwin');
  70. sub usageversion {
  71. print STDERR
  72. "Debian $0 $version. Copyright (C) 1999,2000,2001 Marcus Brinkmann.
  73. This is free software; see the GNU General Public Licence version 2
  74. or later for copying conditions. There is NO warranty.
  75. Usage:
  76. $0 [<option> ...] [<action>]
  77. Options:
  78. -a<debian-arch> set Debian architecture
  79. -t<gnu-system> set GNU system type
  80. -f force flag (override variables set in environment)
  81. Actions:
  82. -l list variables (default)
  83. -q<variable> prints only the value of <variable>.
  84. -s print command to set environment variables
  85. -u print command to unset environment variables
  86. -c <command> set environment and run the command in it.
  87. Known Debian Architectures are ".join(", ",keys %archtable)."
  88. Known GNU System Types are ".join(", ",map ($archtable{$_},keys %archtable))."
  89. ";
  90. }
  91. sub rewrite_gnu {
  92. local ($_) = @_;
  93. s/(?:i386|i486|i586|i686|pentium)(.*linux)/i386$1/;
  94. s/(?:i386|i486|i586|i686|pentium)(.*gnu)/i386$1/;
  95. s/ppc/powerpc/;
  96. s/openbsd([\d\.]+$)/openbsd/;
  97. s/-unknown-/-/;
  98. return $_;
  99. }
  100. sub gnu_to_debian {
  101. local ($gnu) = @_;
  102. local (@list);
  103. local ($a);
  104. $gnu = &rewrite_gnu($gnu);
  105. foreach $a (keys %archtable) {
  106. push @list, $a if $archtable{$a} eq $gnu;
  107. }
  108. return @list;
  109. }
  110. # Set default values:
  111. $deb_build_arch = `dpkg --print-installation-architecture`;
  112. if ($?>>8) {
  113. &syserr("dpkg --print-installation-architecture filed");
  114. }
  115. chomp $deb_build_arch;
  116. $deb_build_gnu_type = $archtable{$deb_build_arch};
  117. @deb_build_gnu_triple = split(/-/, $deb_build_gnu_type, 2);
  118. $deb_build_gnu_cpu = $deb_build_gnu_triple[0];
  119. $deb_build_gnu_system = $deb_build_gnu_triple[1];
  120. # Default host: Current gcc.
  121. $gcc = `\${CC:-gcc} -dumpmachine`;
  122. if ($?>>8) {
  123. &warn("Couldn't determine gcc system type, falling back to default (native compilation)");
  124. $gcc = '';
  125. } else {
  126. chomp $gcc;
  127. }
  128. if ($gcc ne '') {
  129. @list = &gnu_to_debian($gcc);
  130. if ($#list == -1) {
  131. &warn ("Unknown gcc system type $gcc, falling back to default (native compilation)"),
  132. } elsif ($#list > 0) {
  133. &warn ("Ambiguous gcc system type $gcc, you must specify Debian architecture, too (one of ".join(", ",@list).")");
  134. } else {
  135. $gcc=$archtable{$list[0]};
  136. $deb_host_arch = $list[0];
  137. $deb_host_gnu_type = $gcc;
  138. @deb_host_gnu_triple = split(/-/, $deb_host_gnu_type, 2);
  139. $deb_host_gnu_cpu = $deb_host_gnu_triple[0];
  140. $deb_host_gnu_system = $deb_host_gnu_triple[1];
  141. }
  142. }
  143. if (!defined($deb_host_arch)) {
  144. # Default host: Native compilation.
  145. $deb_host_arch = $deb_build_arch;
  146. $deb_host_gnu_cpu = $deb_build_gnu_cpu;
  147. $deb_host_gnu_system = $deb_build_gnu_system;
  148. $deb_host_gnu_type = $deb_build_gnu_type;
  149. }
  150. $req_host_arch = '';
  151. $req_host_gnu_type = '';
  152. $action='l';
  153. $force=0;
  154. while (@ARGV) {
  155. $_=shift(@ARGV);
  156. if (m/^-a/) {
  157. $req_host_arch = $';
  158. } elsif (m/^-t/) {
  159. $req_host_gnu_type = &rewrite_gnu($');
  160. } elsif (m/^-[lsu]$/) {
  161. $action = $_;
  162. $action =~ s/^-//;
  163. } elsif (m/^-f$/) {
  164. $force=1;
  165. } elsif (m/^-q/) {
  166. $req_variable_to_print = $';
  167. $action = 'q';
  168. } elsif (m/^-c$/) {
  169. $action = 'c';
  170. last;
  171. } else {
  172. usageerr("unknown option \`$_'");
  173. }
  174. }
  175. if ($req_host_arch ne '' && $req_host_gnu_type eq '') {
  176. die ("unknown Debian architecture $req_host_arch, you must specify GNU system type, too") if !exists $archtable{$req_host_arch};
  177. $req_host_gnu_type = $archtable{$req_host_arch}
  178. }
  179. if ($req_host_gnu_type ne '' && $req_host_arch eq '') {
  180. @list = &gnu_to_debian ($req_host_gnu_type);
  181. die ("unknown GNU system type $req_host_gnu_type, you must specify Debian architecture, too") if $#list == -1;
  182. die ("ambiguous GNU system type $req_host_gnu_type, you must specify Debian architecture, too (one of ".join(", ",@list).")") if $#list > 0;
  183. $req_host_arch = $list[0];
  184. }
  185. if (exists $archtable{$req_host_arch}) {
  186. &warn("Default GNU system type $archtable{$req_host_arch} for Debian arch $req_host_arch does not match specified GNU system type $req_host_gnu_type\n") if $archtable{$req_host_arch} ne $req_host_gnu_type;
  187. }
  188. die "couldn't parse GNU system type $req_host_gnu_type, must be arch-os or arch-vendor-os" if $req_host_gnu_type !~ m/^([\w\d]+(-[\w\d]+){1,2})?$/;
  189. $deb_host_arch = $req_host_arch if $req_host_arch ne '';
  190. if ($req_host_gnu_type ne '') {
  191. $deb_host_gnu_type = $req_host_gnu_type;
  192. @deb_host_gnu_triple = split(/-/, $deb_host_gnu_type, 2);
  193. $deb_host_gnu_cpu = $deb_host_gnu_triple[0];
  194. $deb_host_gnu_system = $deb_host_gnu_triple[1];
  195. }
  196. #$gcc = `\${CC:-gcc} --print-libgcc-file-name`;
  197. #$gcc =~ s!^.*gcc-lib/(.*)/\d+(?:.\d+)*/libgcc.*$!$1!s;
  198. &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);
  199. %env = ();
  200. if (!$force) {
  201. $deb_build_arch = $ENV{DEB_BUILD_ARCH} if (exists $ENV{DEB_BUILD_ARCH});
  202. $deb_build_gnu_cpu = $ENV{DEB_BUILD_GNU_CPU} if (exists $ENV{DEB_BUILD_GNU_CPU});
  203. $deb_build_gnu_system = $ENV{DEB_BUILD_GNU_SYSTEM} if (exists $ENV{DEB_BUILD_GNU_SYSTEM});
  204. $deb_build_gnu_type = $ENV{DEB_BUILD_GNU_TYPE} if (exists $ENV{DEB_BUILD_GNU_TYPE});
  205. $deb_host_arch = $ENV{DEB_HOST_ARCH} if (exists $ENV{DEB_HOST_ARCH});
  206. $deb_host_gnu_cpu = $ENV{DEB_HOST_GNU_CPU} if (exists $ENV{DEB_HOST_GNU_CPU});
  207. $deb_host_gnu_system = $ENV{DEB_HOST_GNU_SYSTEM} if (exists $ENV{DEB_HOST_GNU_SYSTEM});
  208. $deb_host_gnu_type = $ENV{DEB_HOST_GNU_TYPE} if (exists $ENV{DEB_HOST_GNU_TYPE});
  209. }
  210. @ordered = qw(DEB_BUILD_ARCH DEB_BUILD_GNU_CPU
  211. DEB_BUILD_GNU_SYSTEM DEB_BUILD_GNU_TYPE
  212. DEB_HOST_ARCH DEB_HOST_GNU_CPU
  213. DEB_HOST_GNU_SYSTEM DEB_HOST_GNU_TYPE);
  214. $env{'DEB_BUILD_ARCH'}=$deb_build_arch;
  215. $env{'DEB_BUILD_GNU_CPU'}=$deb_build_gnu_cpu;
  216. $env{'DEB_BUILD_GNU_SYSTEM'}=$deb_build_gnu_system;
  217. $env{'DEB_BUILD_GNU_TYPE'}=$deb_build_gnu_type;
  218. $env{'DEB_HOST_ARCH'}=$deb_host_arch;
  219. $env{'DEB_HOST_GNU_CPU'}=$deb_host_gnu_cpu;
  220. $env{'DEB_HOST_GNU_SYSTEM'}=$deb_host_gnu_system;
  221. $env{'DEB_HOST_GNU_TYPE'}=$deb_host_gnu_type;
  222. if ($action eq 'l') {
  223. foreach $k (@ordered) {
  224. print "$k=$env{$k}\n";
  225. }
  226. } elsif ($action eq 's') {
  227. foreach $k (@ordered) {
  228. print "$k=$env{$k}; ";
  229. }
  230. print "export ".join(" ",@ordered)."\n";
  231. } elsif ($action eq 'u') {
  232. print "unset ".join(" ",@ordered)."\n";
  233. } elsif ($action eq 'c') {
  234. @ENV{keys %env} = values %env;
  235. exec @ARGV;
  236. } elsif ($action eq 'q') {
  237. if (exists $env{$req_variable_to_print}) {
  238. print "$env{$req_variable_to_print}\n";
  239. } else {
  240. die "$req_variable_to_print is not a supported variable name";
  241. }
  242. }
  243. __END__
  244. =head1 NAME
  245. dpkg-architecture - set and determine the architecture for package building
  246. =head1 SYNOPSIS
  247. dpkg-architecture [options] [action]
  248. Valid options:
  249. B<-a>Debian-Architecture
  250. B<-t>Gnu-System-Type
  251. B<-f>
  252. Valid actions:
  253. B<-l>, B<-q>Variable-Name, B<-s>, B<-u>, B<-c> Command
  254. =head1 DESCRIPTION
  255. dpkg-architecture does provide a facility to determine and set the build and
  256. host architecture for package building.
  257. =head1 OVERVIEW
  258. The build architecture is always determined by an external call to dpkg, and
  259. can not be set at the command line.
  260. You can specify the host architecture by providing one or both of the options B<-a>
  261. and B<-t>. The default is determined by an external call to gcc, or the same as
  262. the build architecture if CC or gcc are both not available. One out of B<-a> and B<-t>
  263. is sufficient, the value of the other will be set to a usable default.
  264. Indeed, it is often better to only specify one, because dpkg-architecture
  265. will warn you if your choice doesn't match the default.
  266. The default action is B<-l>, which prints the environment variales, one each line,
  267. in the format VARIABLE=value. If you are only interested in the value of a
  268. single variable, you can use B<-q>. If you specify B<-s>, it will output an export
  269. command. This can be used to set the environment variables using eval. B<-u>
  270. does return a similar command to unset all variables. B<-c> does execute a
  271. command in an environment which has all variables set to the determined
  272. value.
  273. Existing environment variables with the same name as used by the scripts are
  274. not overwritten, except if the B<-f> force flag is present. This allows the user
  275. to override a value even when the call to dpkg-architecture is buried in
  276. some other script (for example dpkg-buildpackage).
  277. =head1 TERMS
  278. =over 4
  279. =item build machine
  280. The machine the package is built on.
  281. =item host machine
  282. The machine the package is built for.
  283. =item Debian Architecture
  284. The Debian archietcture string, which specifies the binary tree in the FTP
  285. archive. Examples: i386, sparc, hurd-i386.
  286. =item GNU System Type
  287. An architecture specification string consisting of two or three parts,
  288. cpu-system or cpu-vendor-system. Examples: i386-linux, sparc-linux, i386-gnu.
  289. =back
  290. =head1 EXAMPLES
  291. dpkg-buildpackage accepts the B<-a> option and passes it to dpkg-architecture.
  292. Other examples:
  293. CC=i386-gnu-gcc dpkg-architecture C<-c> debian/rules build
  294. eval `dpkg-architecture C<-u>`
  295. =head1 VARIABLES
  296. The following variables are set by dpkg-architecture:
  297. =over 4
  298. =item DEB_BUILD_ARCH
  299. The Debian architecture of the build machine.
  300. =item DEB_BUILD_GNU_TYPE
  301. The GNU system type of the build machine.
  302. =item DEB_BUILD_GNU_CPU
  303. The CPU part of DEB_BUILD_GNU_TYPE
  304. =item DEB_BUILD_GNU_SYSTEM
  305. The System part of DEB_BUILD_GNU_TYPE
  306. =item DEB_HOST_ARCH
  307. The Debian architecture of the host machine.
  308. =item DEB_HOST_GNU_TYPE
  309. The GNU system type of the host machine.
  310. =item DEB_HOST_GNU_CPU
  311. The CPU part of DEB_HOST_GNU_TYPE
  312. =item DEB_HOST_GNU_SYSTEM
  313. The System part of DEB_HOST_GNU_TYPE
  314. =back
  315. =head1 DEBIAN/RULES
  316. The environment variables set by dpkg-architecture are passed to
  317. debian/rules as make variables (see make documentation). However, you
  318. should not rely on them, as this breaks manual invocation of the
  319. script. Instead, you should always initialize them using
  320. dpkg-architecture with the -q option. Here are some examples, which
  321. also show how you can improve the cross compilation support in your
  322. package:
  323. Instead of:
  324. ARCH=`dpkg --print-architecture`
  325. configure $(ARCH)-linux
  326. please use the following:
  327. DEB_BUILD_GNU_TYPE := $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
  328. DEB_HOST_GNU_TYPE := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
  329. configure --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
  330. Instead of:
  331. ARCH=`dpkg --print-architecture`
  332. ifeq ($(ARCH),alpha)
  333. ...
  334. endif
  335. please use:
  336. DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)
  337. ifeq ($(DEB_HOST_ARCH),alpha)
  338. ...
  339. endif
  340. In general, calling dpkg in the rules file to get architecture information
  341. is deprecated (until you want to provide backward compatibility, see below).
  342. Especially the --print-architecture option is unreliable since we have
  343. Debian architectures which don't equal a processor name.
  344. =head1 BACKWARD COMPATIBILITY
  345. When providing a new facility, it is always a good idea to stay
  346. compatible with old versions of the programs. Note that
  347. dpkg-architecture does not affect old debian/rules files, so the only
  348. thing to consider is using old versions of dpkg-dev with new
  349. debian/rules files. The following does the job:
  350. DEB_BUILD_ARCH := $(shell dpkg --print-installation-architecture)
  351. DEB_BUILD_GNU_CPU := $(patsubst hurd-%,%,$(DEB_BUILD_ARCH))
  352. ifeq ($(filter-out hurd-%,$(DEB_BUILD_ARCH)),)
  353. DEB_BUILD_GNU_SYSTEM := gnu
  354. else
  355. DEB_BUILD_GNU_SYSTEM := linux
  356. endif
  357. DEB_BUILD_GNU_TYPE=$(DEB_BUILD_GNU_CPU)-$(DEB_BUILD_GNU_SYSTEM)
  358. DEB_HOST_ARCH := $(DEB_BUILD_ARCH)
  359. DEB_HOST_GNU_CPU := $(DEB_BUILD_GNU_CPU)
  360. DEB_HOST_GNU_SYSTEM := $(DEB_BUILD_GNU_SYSTEM)
  361. DEB_HOST_GNU_TYPE := $(DEB_BUILD_GNU_TYPE)
  362. Put a subset of these lines at the top of your debian/rules file; these
  363. default values will be overwritten if dpkg-architecture is used.
  364. You don't need the full set. Choose a consistent set which contains the
  365. values you use in the rules file. For example, if you only need the host
  366. Debian architecture, `DEB_HOST_ARCH=`dpkg --print-installation-architecture`
  367. is sufficient (this is indeed the Debian architecture of the build machine,
  368. but remember that we are only trying to be backward compatible with native
  369. compilation).
  370. =head1 SEE ALSO
  371. dpkg-buildpackage
  372. dpkg-cross
  373. =head1 CONTACT
  374. If you have questions about the usage of the make variables in your rules
  375. files, or about cross compilation support in your packages, please email me.
  376. The address is Marcus Brinkmann <brinkmd@debian.org>.