dpkg-architecture.pl 15 KB

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