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. 'sh', 'sh-linux',
  56. 'shed', 'shed-linux',
  57. 'hppa', 'hppa-linux',
  58. 'hurd-i386', 'i386-gnu',
  59. 's390', 's390-linux',
  60. 'ia64', 'ia64-linux',
  61. 'openbsd-i386', 'i386-openbsd',
  62. 'freebsd-i386', 'i386-freebsd');
  63. sub usageversion {
  64. print STDERR
  65. "Debian $0 $version. Copyright (C) 1999,2000,2001 Marcus Brinkmann.
  66. This is free software; see the GNU General Public Licence version 2
  67. or later for copying conditions. There is NO warranty.
  68. Usage:
  69. $0 [<option> ...] [<action>]
  70. Options:
  71. -a<debian-arch> set Debian architecture
  72. -t<gnu-system> set GNU system type
  73. -f force flag (override variables set in environment)
  74. Actions:
  75. -l list variables (default)
  76. -q<variable> prints only the value of <variable>.
  77. -s print command to set environment variables
  78. -u print command to unset environment variables
  79. -c <command> set environment and run the command in it.
  80. Known Debian Architectures are ".join(", ",keys %archtable)."
  81. Known GNU System Types are ".join(", ",map ($archtable{$_},keys %archtable))."
  82. ";
  83. }
  84. sub rewrite_gnu_cpu {
  85. local ($_) = @_;
  86. s/(?:i386|i486|i586|i686|pentium)(.*linux)/i386$1/;
  87. s/ppc/powerpc/;
  88. return $_;
  89. }
  90. sub gnu_to_debian {
  91. local ($gnu) = @_;
  92. local (@list);
  93. local ($a);
  94. $gnu = &rewrite_gnu_cpu($gnu);
  95. foreach $a (keys %archtable) {
  96. push @list, $a if $archtable{$a} eq $gnu;
  97. }
  98. return @list;
  99. }
  100. # Set default values:
  101. $deb_build_arch = `dpkg --print-installation-architecture`;
  102. chomp $deb_build_arch;
  103. $deb_build_gnu_type = $archtable{$deb_build_arch};
  104. $deb_build_gnu_cpu = $deb_build_gnu_type;
  105. $deb_build_gnu_system = $deb_build_gnu_type;
  106. $deb_build_gnu_cpu =~ s/-.*$//;
  107. $deb_build_gnu_system =~ s/^.*-//;
  108. # Default host: Current gcc.
  109. $gcc = `\${CC:-gcc} --print-libgcc-file-name`;
  110. if ($?>>8) {
  111. &warn("Couldn't determine gcc system type, falling back to default (native compilation)");
  112. $gcc = '';
  113. } else {
  114. $gcc =~ s!^.*gcc-lib/([^/]*)/(?:egcs-)?\d+(?:[.\da-z]*)/libgcc.*$!$1!s;
  115. if (defined $1 and $! ne '') {
  116. $gcc = $1;
  117. } else {
  118. &warn("Couldn't determine gcc system type, falling back to default (native compilation)");
  119. $gcc = '';
  120. }
  121. }
  122. if ($gcc ne '') {
  123. @list = &gnu_to_debian($gcc);
  124. if (!defined(@list)) {
  125. &warn ("Unknown gcc system type $gcc, falling back to default (native compilation)"),
  126. } elsif ($#list > 0) {
  127. &warn ("Ambiguous gcc system type $gcc, you must specify Debian architecture, too (one of ".join(", ",@list).")");
  128. } else {
  129. $gcc=$archtable{$list[0]};
  130. $deb_host_arch = $list[0];
  131. $deb_host_gnu_type = $gcc;
  132. ($deb_host_gnu_system = $gcc) =~ s/^.*-//;
  133. ($deb_host_gnu_cpu = $gcc ) =~ s/-.*$//;
  134. }
  135. }
  136. if (!defined($deb_host_arch)) {
  137. # Default host: Native compilation.
  138. $deb_host_arch = $deb_build_arch;
  139. $deb_host_gnu_cpu = $deb_build_gnu_cpu;
  140. $deb_host_gnu_system = $deb_build_gnu_system;
  141. $deb_host_gnu_type = $deb_build_gnu_type;
  142. }
  143. $req_host_arch = '';
  144. $req_host_gnu_type = '';
  145. $action='l';
  146. $force=0;
  147. while (@ARGV) {
  148. $_=shift(@ARGV);
  149. if (m/^-a/) {
  150. $req_host_arch = $';
  151. } elsif (m/^-t/) {
  152. $req_host_gnu_type = &rewrite_gnu_cpu($');
  153. } elsif (m/^-[lsu]$/) {
  154. $action = $_;
  155. $action =~ s/^-//;
  156. } elsif (m/^-f$/) {
  157. $force=1;
  158. } elsif (m/^-q/) {
  159. $req_variable_to_print = $';
  160. $action = 'q';
  161. } elsif (m/^-c$/) {
  162. $action = 'c';
  163. last;
  164. } else {
  165. usageerr("unknown option \`$_'");
  166. }
  167. }
  168. if ($req_host_arch ne '' && $req_host_gnu_type eq '') {
  169. die ("unknown Debian architecture $req_host_arch, you must specify GNU system type, too") if !exists $archtable{$req_host_arch};
  170. $req_host_gnu_type = $archtable{$req_host_arch}
  171. }
  172. if ($req_host_gnu_type ne '' && $req_host_arch eq '') {
  173. @list = &gnu_to_debian ($req_host_gnu_type);
  174. die ("unknown GNU system type $req_host_gnu_type, you must specify Debian architecture, too") if !defined(@list);
  175. die ("ambiguous GNU system type $req_host_gnu_type, you must specify Debian architecture, too (one of ".join(", ",@list).")") if $#list > 0;
  176. $req_host_arch = $list[0];
  177. }
  178. if (exists $archtable{$req_host_arch}) {
  179. &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;
  180. }
  181. 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})?$/;
  182. $deb_host_arch = $req_host_arch if $req_host_arch ne '';
  183. if ($req_host_gnu_type ne '') {
  184. $deb_host_gnu_cpu = $deb_host_gnu_system = $deb_host_gnu_type = $req_host_gnu_type;
  185. $deb_host_gnu_cpu =~ s/-.*$//;
  186. $deb_host_gnu_system =~ s/^.*-//;
  187. }
  188. #$gcc = `\${CC:-gcc} --print-libgcc-file-name`;
  189. #$gcc =~ s!^.*gcc-lib/(.*)/\d+(?:.\d+)*/libgcc.*$!$1!s;
  190. &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);
  191. %env = ();
  192. if (!$force) {
  193. $deb_build_arch = $ENV{DEB_BUILD_ARCH} if (exists $ENV{DEB_BUILD_ARCH});
  194. $deb_build_gnu_cpu = $ENV{DEB_BUILD_GNU_CPU} if (exists $ENV{DEB_BUILD_GNU_CPU});
  195. $deb_build_gnu_system = $ENV{DEB_BUILD_GNU_SYSTEM} if (exists $ENV{DEB_BUILD_GNU_SYSTEM});
  196. $deb_build_gnu_type = $ENV{DEB_BUILD_GNU_TYPE} if (exists $ENV{DEB_BUILD_GNU_TYPE});
  197. $deb_host_arch = $ENV{DEB_HOST_ARCH} if (exists $ENV{DEB_HOST_ARCH});
  198. $deb_host_gnu_cpu = $ENV{DEB_HOST_GNU_CPU} if (exists $ENV{DEB_HOST_GNU_CPU});
  199. $deb_host_gnu_system = $ENV{DEB_HOST_GNU_SYSTEM} if (exists $ENV{DEB_HOST_GNU_SYSTEM});
  200. $deb_host_gnu_type = $ENV{DEB_HOST_GNU_TYPE} if (exists $ENV{DEB_HOST_GNU_TYPE});
  201. }
  202. @ordered = qw(DEB_BUILD_ARCH DEB_BUILD_GNU_CPU
  203. DEB_BUILD_GNU_SYSTEM DEB_BUILD_GNU_TYPE
  204. DEB_HOST_ARCH DEB_HOST_GNU_CPU
  205. DEB_HOST_GNU_SYSTEM DEB_HOST_GNU_TYPE);
  206. $env{'DEB_BUILD_ARCH'}=$deb_build_arch;
  207. $env{'DEB_BUILD_GNU_CPU'}=$deb_build_gnu_cpu;
  208. $env{'DEB_BUILD_GNU_SYSTEM'}=$deb_build_gnu_system;
  209. $env{'DEB_BUILD_GNU_TYPE'}=$deb_build_gnu_type;
  210. $env{'DEB_HOST_ARCH'}=$deb_host_arch;
  211. $env{'DEB_HOST_GNU_CPU'}=$deb_host_gnu_cpu;
  212. $env{'DEB_HOST_GNU_SYSTEM'}=$deb_host_gnu_system;
  213. $env{'DEB_HOST_GNU_TYPE'}=$deb_host_gnu_type;
  214. if ($action eq 'l') {
  215. foreach $k (@ordered) {
  216. print "$k=$env{$k}\n";
  217. }
  218. } elsif ($action eq 's') {
  219. foreach $k (@ordered) {
  220. print "$k=$env{$k}; ";
  221. }
  222. print "export ".join(" ",@ordered)."\n";
  223. } elsif ($action eq 'u') {
  224. print "unset ".join(" ",@ordered)."\n";
  225. } elsif ($action eq 'c') {
  226. @ENV{keys %env} = values %env;
  227. exec @ARGV;
  228. } elsif ($action eq 'q') {
  229. if (exists $env{$req_variable_to_print}) {
  230. print "$env{$req_variable_to_print}\n"; # works because -q implies -f !
  231. } else {
  232. die "$req_variable_to_print is not a supported variable name";
  233. }
  234. }
  235. __END__
  236. =head1 NAME
  237. dpkg-architecture - set and determine the architecture for package building
  238. =head1 SYNOPSIS
  239. dpkg-architecture [options] [action]
  240. Valid options:
  241. B<-a>Debian-Architecture
  242. B<-t>Gnu-System-Type
  243. B<-f>
  244. Valid actions:
  245. B<-l>, B<-q>Variable-Name, B<-s>, B<-u>, B<-c> Command
  246. =head1 DESCRIPTION
  247. dpkg-architecture does provide a facility to determine and set the build and
  248. host architecture for package building.
  249. =head1 OVERVIEW
  250. The build architecture is always determined by an external call to dpkg, and
  251. can not be set at the command line.
  252. You can specify the host architecture by providing one or both of the options B<-a>
  253. and B<-t>. The default is determined by an external call to gcc, or the same as
  254. the build architecture if CC or gcc are both not available. One out of B<-a> and B<-t>
  255. is sufficient, the value of the other will be set to a usable default.
  256. Indeed, it is often better to only specify one, because dpkg-architecture
  257. will warn you if your choice doesn't match the default.
  258. The default action is B<-l>, which prints the environment variales, one each line,
  259. in the format VARIABLE=value. If you are only interested in the value of a
  260. single variable, you can use B<-q>. If you specify B<-s>, it will output an export
  261. command. This can be used to set the environment variables using eval. B<-u>
  262. does return a similar command to unset all variables. B<-c> does execute a
  263. command in an environment which has all variables set to the determined
  264. value.
  265. Existing environment variables with the same name as used by the scripts are
  266. not overwritten, except if the B<-f> force flag is present. This allows the user
  267. to override a value even when the call to dpkg-architecture is buried in
  268. some other script (for example dpkg-buildpackage).
  269. =head1 TERMS
  270. =over 4
  271. =item build machine
  272. The machine the package is build on.
  273. =item host machine
  274. The machine the package is build for.
  275. =item Debian Architecture
  276. The Debian archietcture string, which specifies the binary tree in the FTP
  277. archive. Examples: i386, sparc, hurd-i386.
  278. =item GNU System Type
  279. An architecture specification string consisting of two or three parts,
  280. cpu-system or cpu-vendor-system. Examples: i386-linux, sparc-linux, i386-gnu.
  281. =back
  282. =head1 EXAMPLES
  283. dpkg-buildpackage accepts the B<-a> option and passes it to dpkg-architecture.
  284. Other examples:
  285. CC=i386-gnu-gcc dpkg-architecture C<-c> debian/rules build
  286. eval `dpkg-architecture C<-u>`
  287. =head1 VARIABLES
  288. The following variables are set by dpkg-architecture:
  289. =over 4
  290. =item DEB_BUILD_ARCH
  291. The Debian architecture of the build machine.
  292. =item DEB_BUILD_GNU_TYPE
  293. The GNU system type of the build machine.
  294. =item DEB_BUILD_GNU_CPU
  295. The CPU part of DEB_BUILD_GNU_TYPE
  296. =item DEB_BUILD_GNU_SYSTEM
  297. The System part of DEB_BUILD_GNU_TYPE
  298. =item DEB_HOST_ARCH
  299. The Debian architecture of the host machine.
  300. =item DEB_HOST_GNU_TYPE
  301. The GNU system type of the host machine.
  302. =item DEB_HOST_GNU_CPU
  303. The CPU part of DEB_HOST_GNU_TYPE
  304. =item DEB_HOST_GNU_SYSTEM
  305. The System part of DEB_HOST_GNU_TYPE
  306. =back
  307. =head1 DEBIAN/RULES
  308. The environment variables set by dpkg-architecture are passed to
  309. debian/rules as make variables (see make documentation). You can and should
  310. use them in the build process as needed. Here are some examples, which also
  311. show how you can improve the cross compilation support in your package:
  312. Instead:
  313. ARCH=`dpkg --print-architecture`
  314. configure $(ARCH)-linux
  315. please use the following:
  316. B_ARCH=$(DEB_BUILD_GNU_TYPE)
  317. H_ARCH=$(DEB_HOST_GNU_TYPE)
  318. configure --build=$(B_ARCH) --host=$(H_ARCH)
  319. Instead:
  320. ARCH=`dpkg --print-architecture`
  321. ifeq ($(ARCH),alpha)
  322. ...
  323. endif
  324. please use:
  325. ARCH=$(DEB_HOST_ARCH)
  326. ifeq ($(ARCH),alpha)
  327. ...
  328. endif
  329. In general, calling dpkg in the rules file to get architecture information
  330. is deprecated (until you want to provide backward compatibility, see below).
  331. Especially the --print-architecture option is unreliable since we have
  332. Debian architectures which don't equal a processor name.
  333. =head1 BACKWARD COMPATIBILITY
  334. When providing a new facility, it is always a good idea to stay compatible with old
  335. versions of the programs. Note that dpkg-architecture does not affect old
  336. debian/rules files, so the only thing to consider is using old building
  337. scripts with new debian/rules files. The following does the job:
  338. DEB_BUILD_ARCH := $(shell dpkg --print-installation-architecture)
  339. DEB_BUILD_GNU_CPU := $(patsubst hurd-%,%,$(DEB_BUILD_ARCH))
  340. ifeq ($(filter-out hurd-%,$(DEB_BUILD_ARCH)),)
  341. DEB_BUILD_GNU_SYSTEM := gnu
  342. else
  343. DEB_BUILD_GNU_SYSTEM := linux
  344. endif
  345. DEB_BUILD_GNU_TYPE=$(DEB_BUILD_GNU_CPU)-$(DEB_BUILD_GNU_SYSTEM)
  346. DEB_HOST_ARCH=$(DEB_BUILD_ARCH)
  347. DEB_HOST_GNU_CPU=$(DEB_BUILD_GNU_CPU)
  348. DEB_HOST_GNU_SYSTEM=$(DEB_BUILD_GNU_SYSTEM)
  349. DEB_HOST_GNU_TYPE=$(DEB_BUILD_GNU_TYPE)
  350. Put a subset of these lines at the top of your debian/rules file; these
  351. default values will be overwritten if dpkg-architecture is used.
  352. You don't need the full set. Choose a consistent set which contains the
  353. values you use in the rules file. For example, if you only need the host
  354. Debian architecture, `DEB_HOST_ARCH=`dpkg --print-installation-architecture`
  355. is sufficient (this is indeed the Debian architecture of the build machine,
  356. but remember that we are only trying to be backward compatible with native
  357. compilation).
  358. You may not want to care about old build packages (for example, if you have
  359. sufficient source dependencies declared anyway). But you should at least
  360. support the traditional way to build packages by calling `debian/rules
  361. build' directly, without setting environment variables. To do this, use the
  362. B<-q> option to query suitable default values:
  363. DEB_BUILD_ARCH=`dpkg-architecture -qDEB_BUILD_ARCH`
  364. DEB_BUILD_GNU=`dpkg-architecture -qDEB_BUILD_GNU`
  365. etc. You get the idea. This way, you can ensure that the variables are never
  366. undeclared. Note that this breaks backwards compatibility with old build
  367. scripts, and you should only do that if source dependencies are implemented
  368. and declared accordingly.
  369. =head1 SEE ALSO
  370. dpkg-buildpackage
  371. dpkg-cross
  372. =head1 CONTACT
  373. If you have questions about the usage of the make variables in your rules
  374. files, or about cross compilation support in your packages, please email me.
  375. The address is Marcus Brinkmann <brinkmd@debian.org>.