File: dpkg-architecture.pl

package info (click to toggle)
dpkg 1.9.21
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,892 kB
  • ctags: 2,579
  • sloc: ansic: 19,380; perl: 4,921; sh: 4,406; cpp: 4,140; makefile: 1,028; yacc: 318
file content (481 lines) | stat: -rwxr-xr-x 15,668 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#! /usr/bin/perl
#
# dpkg-architecture
#
# Copyright 1999 Marcus Brinkmann <brinkmd@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

# History
#  0.0.1  Initial release.
#  0.0.2  Don't use dpkg to get default gnu system, so the default is
#         correct even on non-linux system.
#         Warn if the host gnu system does not match the gcc system.
#         Determine default from gcc if possible, else fall back to native
#         compilation.
#         Do not set environment variables which are already defined unless
#         force flag is given.
#  1.0.0  Changed target to host, because this complies with GNU
#         nomenclature.
#         Added command facility.
#  1.0.1  Moved to GNU nomenclature arch->cpu, system->type, os->system
#  1.0.2  Add facility to query single values, suggested by Richard Braakman.
#  1.0.3  Make it work with egcs, too.
#  1.0.4  Suppress single "export" with "-s" when all env variables are already set
#  1.0.5  Update default for rules files (i386->i486).
#         Print out overridden values, so make gets them, too.
#  1.0.6  Revert to i386 to comply with policy  5.1.
#  1.0.7  -q should not imply -f, because this prevents setting
#         make variables with non-standard names correctly.

$version="1.0.0";
$0 = `basename $0`; chomp $0;

$dpkglibdir="/usr/lib/dpkg";
push(@INC,$dpkglibdir);
require 'controllib.pl';

%archtable=('i386',      'i386-linux',
	    'sparc',     'sparc-linux',
	    'sparc64',   'sparc64-linux',
	    'alpha',     'alpha-linux',
	    'm68k',      'm68k-linux',
            'arm',       'arm-linux',
            'powerpc',   'powerpc-linux',
	    'mips',      'mips-linux',
	    'mipsel',    'mipsel-linux',
	    'sh',        'sh-linux',
	    'sheb',      'sheb-linux',
	    'hppa',      'hppa-linux',
	    'hurd-i386', 'i386-gnu',
	    's390',	 's390-linux',
	    'ia64',	 'ia64-linux',
	    'openbsd-i386', 'i386-openbsd',
	    'freebsd-i386', 'i386-freebsd');

sub usageversion {
    print STDERR
"Debian $0 $version.  Copyright (C) 1999,2000,2001 Marcus Brinkmann.
This is free software; see the GNU General Public Licence version 2
or later for copying conditions.  There is NO warranty.

Usage:
  $0 [<option> ...] [<action>]
Options:
       -a<debian-arch>    set Debian architecture
       -t<gnu-system>     set GNU system type 
       -f                 force flag (override variables set in environment)
Actions:
       -l                 list variables (default)
       -q<variable>       prints only the value of <variable>.
       -s                 print command to set environment variables
       -u                 print command to unset environment variables
       -c <command>       set environment and run the command in it.

Known Debian Architectures are ".join(", ",keys %archtable)."
Known GNU System Types are ".join(", ",map ($archtable{$_},keys %archtable))."
";
}

sub rewrite_gnu_cpu {
	local ($_) = @_;

	s/(?:i386|i486|i586|i686|pentium)(.*linux)/i386$1/;
	s/ppc/powerpc/;
	s/sh[34]/sh/;
	return $_;
}

sub gnu_to_debian {
	local ($gnu) = @_;
	local (@list);
	local ($a);

	$gnu = &rewrite_gnu_cpu($gnu);

	foreach $a (keys %archtable) {
		push @list, $a if $archtable{$a} eq $gnu;
	}
	return @list;
}

# Set default values:

$deb_build_arch = `dpkg --print-installation-architecture`;
if ($?>>8) {
	&syserr("dpkg --print-installation-architecture filed");
}
chomp $deb_build_arch;
$deb_build_gnu_type = $archtable{$deb_build_arch};
$deb_build_gnu_cpu = $deb_build_gnu_type;
$deb_build_gnu_system = $deb_build_gnu_type;
$deb_build_gnu_cpu =~ s/-.*$//;
$deb_build_gnu_system =~ s/^.*-//;

# Default host: Current gcc.
$gcc = `\${CC:-gcc} --print-libgcc-file-name`;
if ($?>>8) {
    &warn("Couldn't determine gcc system type, falling back to default (native compilation)");
    $gcc = '';
} else {
    $gcc =~ s!^.*gcc-lib/([^/]*)/(?:egcs-)?\d+(?:[.\da-z]*)/libgcc.*$!$1!s;
    if (defined $1 and $1 ne '') {
	$gcc = $1;
    } else {
	&warn("Couldn't determine gcc system type, falling back to default (native compilation)");
	$gcc = '';
    }
}

if ($gcc ne '') {
    @list = &gnu_to_debian($gcc);
    if ($#list == -1) {
	&warn ("Unknown gcc system type $gcc, falling back to default (native compilation)"),
    } elsif ($#list > 0) {
	&warn ("Ambiguous gcc system type $gcc, you must specify Debian architecture, too (one of ".join(", ",@list).")");
    } else {
	$gcc=$archtable{$list[0]};
	$deb_host_arch = $list[0];
	$deb_host_gnu_type = $gcc;
        ($deb_host_gnu_system = $gcc) =~ s/^.*-//;
        ($deb_host_gnu_cpu = $gcc ) =~ s/-.*$//;
    }
}
if (!defined($deb_host_arch)) {
    # Default host: Native compilation.
    $deb_host_arch = $deb_build_arch;
    $deb_host_gnu_cpu = $deb_build_gnu_cpu;
    $deb_host_gnu_system = $deb_build_gnu_system;
    $deb_host_gnu_type = $deb_build_gnu_type;
}


$req_host_arch = '';
$req_host_gnu_type = '';
$action='l';
$force=0;

while (@ARGV) {
    $_=shift(@ARGV);
    if (m/^-a/) {
	$req_host_arch = $';
    } elsif (m/^-t/) {
	$req_host_gnu_type = &rewrite_gnu_cpu($');
    } elsif (m/^-[lsu]$/) {
	$action = $_;
	$action =~ s/^-//;
    } elsif (m/^-f$/) {
        $force=1;
    } elsif (m/^-q/) {
        $req_variable_to_print = $';
        $action = 'q';
    } elsif (m/^-c$/) {
       $action = 'c';
       last;
    } else {
	usageerr("unknown option \`$_'");
    }
}

if ($req_host_arch ne '' && $req_host_gnu_type eq '') {
    die ("unknown Debian architecture $req_host_arch, you must specify GNU system type, too") if !exists $archtable{$req_host_arch};
    $req_host_gnu_type = $archtable{$req_host_arch}
}

if ($req_host_gnu_type ne '' && $req_host_arch eq '') {
    @list = &gnu_to_debian ($req_host_gnu_type);
    die ("unknown GNU system type $req_host_gnu_type, you must specify Debian architecture, too") if $#list == -1;
    die ("ambiguous GNU system type $req_host_gnu_type, you must specify Debian architecture, too (one of ".join(", ",@list).")") if $#list > 0;
    $req_host_arch = $list[0];
}

if (exists $archtable{$req_host_arch}) {
    &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;
}

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})?$/;

$deb_host_arch = $req_host_arch if $req_host_arch ne '';
if ($req_host_gnu_type ne '') {
    $deb_host_gnu_cpu = $deb_host_gnu_system = $deb_host_gnu_type = $req_host_gnu_type;
    $deb_host_gnu_cpu =~ s/-.*$//;
    $deb_host_gnu_system =~ s/^.*-//;
}

#$gcc = `\${CC:-gcc} --print-libgcc-file-name`;
#$gcc =~ s!^.*gcc-lib/(.*)/\d+(?:.\d+)*/libgcc.*$!$1!s;
&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);

%env = ();
if (!$force) {
    $deb_build_arch = $ENV{DEB_BUILD_ARCH} if (exists $ENV{DEB_BUILD_ARCH});
    $deb_build_gnu_cpu = $ENV{DEB_BUILD_GNU_CPU} if (exists $ENV{DEB_BUILD_GNU_CPU});
    $deb_build_gnu_system = $ENV{DEB_BUILD_GNU_SYSTEM} if (exists $ENV{DEB_BUILD_GNU_SYSTEM});
    $deb_build_gnu_type = $ENV{DEB_BUILD_GNU_TYPE} if (exists $ENV{DEB_BUILD_GNU_TYPE});
    $deb_host_arch = $ENV{DEB_HOST_ARCH} if (exists $ENV{DEB_HOST_ARCH});
    $deb_host_gnu_cpu = $ENV{DEB_HOST_GNU_CPU} if (exists $ENV{DEB_HOST_GNU_CPU});
    $deb_host_gnu_system = $ENV{DEB_HOST_GNU_SYSTEM} if (exists $ENV{DEB_HOST_GNU_SYSTEM});
    $deb_host_gnu_type = $ENV{DEB_HOST_GNU_TYPE} if (exists $ENV{DEB_HOST_GNU_TYPE});
}

@ordered = qw(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);

$env{'DEB_BUILD_ARCH'}=$deb_build_arch;
$env{'DEB_BUILD_GNU_CPU'}=$deb_build_gnu_cpu;
$env{'DEB_BUILD_GNU_SYSTEM'}=$deb_build_gnu_system;
$env{'DEB_BUILD_GNU_TYPE'}=$deb_build_gnu_type;
$env{'DEB_HOST_ARCH'}=$deb_host_arch;
$env{'DEB_HOST_GNU_CPU'}=$deb_host_gnu_cpu;
$env{'DEB_HOST_GNU_SYSTEM'}=$deb_host_gnu_system;
$env{'DEB_HOST_GNU_TYPE'}=$deb_host_gnu_type;

if ($action eq 'l') {
    foreach $k (@ordered) {
	print "$k=$env{$k}\n";
    }
} elsif ($action eq 's') {
    foreach $k (@ordered) {
	print "$k=$env{$k}; ";
    }
    print "export ".join(" ",@ordered)."\n";
} elsif ($action eq 'u') {
    print "unset ".join(" ",@ordered)."\n";
} elsif ($action eq 'c') {
    @ENV{keys %env} = values %env;
    exec @ARGV;
} elsif ($action eq 'q') {
    if (exists $env{$req_variable_to_print}) {
        print "$env{$req_variable_to_print}\n";
    } else {
        die "$req_variable_to_print is not a supported variable name";
    }
}

__END__

=head1 NAME

dpkg-architecture - set and determine the architecture for package building

=head1 SYNOPSIS

dpkg-architecture [options] [action]

Valid options:
B<-a>Debian-Architecture
B<-t>Gnu-System-Type
B<-f>

Valid actions:
B<-l>, B<-q>Variable-Name, B<-s>, B<-u>, B<-c> Command

=head1 DESCRIPTION

dpkg-architecture does provide a facility to determine and set the build and
host architecture for package building.

=head1 OVERVIEW

The build architecture is always determined by an external call to dpkg, and
can not be set at the command line.

You can specify the host architecture by providing one or both of the options B<-a>
and B<-t>. The default is determined by an external call to gcc, or the same as
the build architecture if CC or gcc are both not available. One out of B<-a> and B<-t>
is sufficient, the value of the other will be set to a usable default.
Indeed, it is often better to only specify one, because dpkg-architecture
will warn you if your choice doesn't match the default.

The default action is B<-l>, which prints the environment variales, one each line,
in the format VARIABLE=value. If you are only interested in the value of a
single variable, you can use B<-q>. If you specify B<-s>, it will output an export
command. This can be used to set the environment variables using eval. B<-u>
does return a similar command to unset all variables. B<-c> does execute a
command in an environment which has all variables set to the determined
value.

Existing environment variables with the same name as used by the scripts are
not overwritten, except if the B<-f> force flag is present. This allows the user
to override a value even when the call to dpkg-architecture is buried in
some other script (for example dpkg-buildpackage).

=head1 TERMS

=over 4

=item build machine

The machine the package is build on.

=item host machine

The machine the package is build for.

=item Debian Architecture

The Debian archietcture string, which specifies the binary tree in the FTP
archive. Examples: i386, sparc, hurd-i386.

=item GNU System Type

An architecture specification string consisting of two or three parts,
cpu-system or cpu-vendor-system. Examples: i386-linux, sparc-linux, i386-gnu.

=back

=head1 EXAMPLES

dpkg-buildpackage accepts the B<-a> option and passes it to dpkg-architecture.
Other examples:

CC=i386-gnu-gcc dpkg-architecture C<-c> debian/rules build

eval `dpkg-architecture C<-u>`

=head1 VARIABLES

The following variables are set by dpkg-architecture:

=over 4

=item DEB_BUILD_ARCH

The Debian architecture of the build machine.

=item DEB_BUILD_GNU_TYPE

The GNU system type of the build machine.

=item DEB_BUILD_GNU_CPU

The CPU part of DEB_BUILD_GNU_TYPE

=item DEB_BUILD_GNU_SYSTEM

The System part of DEB_BUILD_GNU_TYPE

=item DEB_HOST_ARCH

The Debian architecture of the host machine.

=item DEB_HOST_GNU_TYPE

The GNU system type of the host machine.

=item DEB_HOST_GNU_CPU

The CPU part of DEB_HOST_GNU_TYPE

=item DEB_HOST_GNU_SYSTEM

The System part of DEB_HOST_GNU_TYPE

=back

=head1 DEBIAN/RULES

The environment variables set by dpkg-architecture are passed to
debian/rules as make variables (see make documentation). You can and should
use them in the build process as needed. Here are some examples, which also
show how you can improve the cross compilation support in your package:

Instead:

ARCH=`dpkg --print-architecture`
configure $(ARCH)-linux

please use the following:

B_ARCH=$(DEB_BUILD_GNU_TYPE)
H_ARCH=$(DEB_HOST_GNU_TYPE)
configure --build=$(B_ARCH) --host=$(H_ARCH)

Instead:

ARCH=`dpkg --print-architecture`
ifeq ($(ARCH),alpha)
  ...
endif

please use:

ARCH=$(DEB_HOST_ARCH)
ifeq ($(ARCH),alpha)
  ...
endif

In general, calling dpkg in the rules file to get architecture information
is deprecated (until you want to provide backward compatibility, see below).
Especially the --print-architecture option is unreliable since we have
Debian architectures which don't equal a processor name.

=head1 BACKWARD COMPATIBILITY

When providing a new facility, it is always a good idea to stay compatible with old
versions of the programs. Note that dpkg-architecture does not affect old
debian/rules files, so the only thing to consider is using old building
scripts with new debian/rules files. The following does the job:

DEB_BUILD_ARCH := $(shell dpkg --print-installation-architecture)
DEB_BUILD_GNU_CPU := $(patsubst hurd-%,%,$(DEB_BUILD_ARCH))
ifeq ($(filter-out hurd-%,$(DEB_BUILD_ARCH)),)
  DEB_BUILD_GNU_SYSTEM := gnu
else
  DEB_BUILD_GNU_SYSTEM := linux
endif
DEB_BUILD_GNU_TYPE=$(DEB_BUILD_GNU_CPU)-$(DEB_BUILD_GNU_SYSTEM)

DEB_HOST_ARCH=$(DEB_BUILD_ARCH)
DEB_HOST_GNU_CPU=$(DEB_BUILD_GNU_CPU)
DEB_HOST_GNU_SYSTEM=$(DEB_BUILD_GNU_SYSTEM)
DEB_HOST_GNU_TYPE=$(DEB_BUILD_GNU_TYPE)

Put a subset of these lines at the top of your debian/rules file; these
default values will be overwritten if dpkg-architecture is used.

You don't need the full set. Choose a consistent set which contains the
values you use in the rules file. For example, if you only need the host
Debian architecture, `DEB_HOST_ARCH=`dpkg --print-installation-architecture`
is sufficient (this is indeed the Debian architecture of the build machine,
but remember that we are only trying to be backward compatible with native
compilation).

You may not want to care about old build packages (for example, if you have
sufficient source dependencies declared anyway). But you should at least
support the traditional way to build packages by calling `debian/rules
build' directly, without setting environment variables. To do this, use the
B<-q> option to query suitable default values:

DEB_BUILD_ARCH=`dpkg-architecture -qDEB_BUILD_ARCH`
DEB_BUILD_GNU=`dpkg-architecture -qDEB_BUILD_GNU`

etc. You get the idea. This way, you can ensure that the variables are never
undeclared. Note that this breaks backwards compatibility with old build
scripts, and you should only do that if source dependencies are implemented
and declared accordingly.

=head1 SEE ALSO

dpkg-buildpackage
dpkg-cross

=head1 CONTACT

If you have questions about the usage of the make variables in your rules
files, or about cross compilation support in your packages, please email me.
The address is Marcus Brinkmann <brinkmd@debian.org>.