Debian Codegraph @

debian/dpkg-cross

raw
1 #!/usr/bin/perl -I debian
2
3 use POSIX;
4 use POSIX qw(:errno_h :signal_h);
5 use POSIX qw( locale_h);
6 use Locale::gettext;
7 use IO::Handle;
8 use File::Basename;
9 use Text::Wrap;
10 use Debian::DpkgCross;
11 use strict;
12 use warnings;
13 use vars qw($verbose $str @removedeps $package %builds $arch $exclude
14 @keepdeps $dpkg_statfile $progname $debname $anyway $cross2cross $crosstype
15 $crossdir $crosslib $crosslib64 $crosslib32 $crosslibhf $crosslibn32
16 $crosslibo32 $crosslibsf $crosslibx32 $crossinc $data $len
17 $retval $dpkg_cmd $mode $pkg @exlist $conffile $removedeps $keepdeps
18 $DPKGCROSSVERSION $keep_temp $msg $multiarchpackage $multiarch
19 $multiarchtriplet $multiarchconv);
20
21 setlocale(LC_MESSAGES, "");
22 textdomain("dpkg-cross");
23
24 =pod
25
26 =head1 Name
27
28 dpkg-cross - manage libraries for cross compiling
29
30 =cut
31
32 =head1 Copyright and Licence
33
34 Copyright (C) 1997-2000 Roman Hodek <[email protected]>
35 Copyright (C) 2000-2002 Colin Watson <[email protected]>
36 Copyright (C) 2002-2004 David Schleef <[email protected]>
37 Copyright (C) 2004 Nikita Youshchenko <[email protected]>
38 Copyright (C) 2004 Raphael Bossek <[email protected]>
39 Copyright (c) 2007-2011 Neil Williams <[email protected]>
40 Copyright (c) 2011 Wookey <[email protected]>
41
42 This program is free software; you can redistribute it and/or modify
43 it under the terms of the GNU General Public License as published by
44 the Free Software Foundation; either version 2 of the License, or
45 (at your option) any later version.
46
47 This program is distributed in the hope that it will be useful,
48 but WITHOUT ANY WARRANTY; without even the implied warranty of
49 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 GNU General Public License for more details.
51
52 You should have received a copy of the GNU General Public License
53 along with this program; if not, write to the Free Software
54 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
55
56 =cut
57
58 =head1 SYNOPSIS
59
60 dpkg-cross [OPTIONS] [--install|-i] <files...>
61 dpkg-cross [OPTIONS] [--build|-b] <files...>
62 dpkg-cross [OPTIONS] [--remove|--purge|-r] <packages...>
63 dpkg-cross [OPTIONS] [--status|-s] <packages...>
64 dpkg-cross [OPTIONS] [--list|-l] <packages...>
65 dpkg-cross [OPTIONS] [--list-files|-L] <packages...>
66 dpkg-cross [OPTIONS] [--query|-Q] <pkgpath>
67 dpkg-cross [OPTIONS] [--update|-u] <pkgpath>
68
69 OPTIONS: [ -v | --verbose ] [ -q | --quiet ]
70 { [ -a | --arch ] architecture } { [ -X | --exclude ] PACKAGE }
71 [ -A | --convert-anyway ] [ -M | --convert-multiarch ] [ -k | --keep-temp ]
72
73 =cut
74
75 =head1 DESCRIPTION
76
77 dpkg-cross is a tool to install and manage libraries and header files
78 for cross compiling. dpkg-cross converts native Debian packages for the
79 target architecture to cross compiling support packages that can be
80 installed on any architecture, but in different paths to avoid
81 conflicts. It then calls dpkg to install the converted package. The
82 conversion step alone can be done with the --build option. Other
83 options are wrappers around corresponding dpkg functionality.
84
85 dpkg-cross is intended to make it easier for you to keep your cross
86 compiling libraries up-to-date, as it works directly on Debian
87 packages. It saves you copying the libs and headers from a machine
88 with your target architecture, or extracting them via
89 dpkg-deb --fsys-tarfile.
90
91 dpkg-cross also collects and install the .shlibs files in -dev packages,
92 which are needed by dpkg-shlibdeps.
93
94 =cut
95
96 =head1 CMAKE
97
98 CMake cross-building support is experimental!
99
100 CMake requires a little support from dpkg-cross to cross-build. The
101 included cmake support file is an example for Linux kernels. Use:
102
103 rm CMakeCache.txt
104 cmake -DCMAKE_TOOLCHAIN_FILE=/etc/dpkg-cross/cmake/CMakeCross.txt
105
106 The main changes involve setting PKG_CONFIG_LIBDIR within CMake an
107 setting the include directories to locate the cross libraries installed
108 by dpkg-cross e.g.
109
110 #Make pkg-config look in the right place
111 SET(ENV{PKG_CONFIG_LIBDIR} ${CMAKE_FIND_ROOT_PATH}/lib/pkgconfig/)
112 ELSE (CMAKE_CROSSCOMPILING)
113
114 Packages using cmake may need some tweaks to debian/rules, e.g.
115
116 ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
117 export CC=$(DEB_HOST_GNU_TYPE)-gcc
118 export CXX=$(DEB_HOST_GNU_TYPE)-g++
119 export CMAKE_TOOLCHAIN_FILE=/etc/dpkg-cross/cmake/CMakeCross.txt
120 else
121 export CC=gcc
122 export CXX=g++
123 endif
124
125 =cut
126
127 =head1 OPTIONS
128
129 dpkg-cross follows the usual GNU command line syntax, with long options
130 starting with two dashes ('-').
131
132 -h|--help
133
134 Show summary of options.
135
136 -v|--verbose
137
138 Be more verbose.
139
140 -q|--quiet
141
142 Be more quiet.
143
144 -a|--arch architecture
145
146 Install for architecture architecture.
147
148 -i|--install
149
150 Install Debian packages (.deb) named on the command line. Only files in
151 the directories /lib, /usr/lib and /usr/include are extracted, since
152 only they can be relevant for cross compiling. In the lib directories,
153 also no subdirectories are extracted.
154
155 -A|--convert-anyway
156
157 Convert Debian package even if it does not provide any files useful for
158 cross-compile environment.
159
160 -M|--convert-multiarch
161
162 Convert package even if it is a multiarch package. If the package is
163 not a multiarch package, processing continues as normal.
164
165 -X|--exclude PACKAGE
166
167 Removes the specified package from the dependencies of the current
168 package. Typically used to drop dependencies on packages that are not
169 required within a cross-build environment, e.g. packages containing
170 only executables or architecture independent files.
171
172 dpkg-cross can only check the current package and information about a
173 dependency is not available (for that, use xapt). By default,
174 dpkg-cross converts all dependencies to specify the cross version of
175 the dependency package.
176
177 If you know that a particular dependency should not be converted (it
178 provides no useful files or is architecture independent), that
179 dependency can be excluded and dpkg-cross will remove that package from
180 the dependency information of the cross package. -X has replaced the use
181 of keepdeps and removedeps in /etc/dpkg-cross/cross-compile.
182
183 -X|--exclude PACKAGE needs to be repeated for each package to be excluded.
184
185 -k|--keep-temp
186
187 Keep the built and installed package instead of deleting it.
188 Requires --install.
189
190 -b|--build
191
192 Just build the converted Debian package, but do not install it with dpkg.
193
194 -r|--remove
195
196 Remove the cross compiling packages named on the command line.
197
198 -s|--status
199
200 Print status of the named packages.
201
202 -l|--list
203
204 Print short version of status of named packages or package name patterns.
205
206 -L|--list-files
207
208 List files belonging to the named packages.
209
210 -u|--update
211
212 Update current cross-installation with Debian packages found in/under
213 the paths given as arguments. Will check all packages there if they are
214 installed already as cross-compiling packages and if they are really
215 updates. Those packages will be installed as with -i.
216
217 -Q|--query
218
219 Much like --update, but just prints available update packages and does
220 not install them.
221
222 =cut
223
224 =head1 FILES
225
226 /etc/dpkg-cross/cross-compile defines the default_arch for dpkg-cross:
227
228 The default architecture for dpkg-cross is normally set by debconf.
229 To change the system-wide value, use:
230
231 $ sudo dpkg-reconfigure dpkg-cross
232
233 Alternatively, the default can be overridden on a per-user basis using
234 the optional file in $HOME/.dpkg-cross/
235
236 =cut
237
238 $dpkg_statfile = "/var/lib/dpkg/status";
239 &read_config;
240 $arch = &get_architecture;
241 $DPKGCROSSVERSION = &get_version;
242 $progname = basename($0);
243
244 # packages to omit in dependencies
245 @removedeps = (); #&get_removedeps;
246 @keepdeps = (); #&get_keepdeps;
247
248 sub usageversion {
249 warn sprintf(_g("
250 dpkg-cross version %s
251
252 Usage:
253 dpkg-cross [OPTIONS] [--install|-i] <files...>
254 dpkg-cross [OPTIONS] [--build|-b] <files...>
255 dpkg-cross [OPTIONS] [--remove|--purge|-r] <packages...>
256 dpkg-cross [OPTIONS] [--status|-s] <packages...>
257 dpkg-cross [OPTIONS] [--list|-l] <packages...>
258 dpkg-cross [OPTIONS] [--list-files|-L] <packages...>
259 dpkg-cross [OPTIONS] [--query|-Q] <pkgpath>
260 dpkg-cross [OPTIONS] [--update|-u] <pkgpath>
261
262 Commands:
263 -?|-h|--help|--version: print this message.
264
265 Options:
266 -a|--arch ARCH: set architecture (default: defined in configuration file)
267 -v|--verbose: be verbose
268 -q|--quiet: be quiet
269 -A|--convert-anyway: convert package even if it does not provide any
270 development files
271 -M|--convert-multiarch: convert package even if it is a multiarch package
272 -X|--exclude PACKAGE: Exclude this package from the dependency list of the
273 built package.
274 -k|--keep-temp: Keep the temporary archives when installing.
275
276 dpkg-cross installs or removes libraries and include files for
277 cross-compiling Debian packages. It reads /etc/dpkg-cross/cross-compile
278 to determine the base directory of the cross compiler installation, and
279 works in the subdirectories lib and include there.
280
281 "), $DPKGCROSSVERSION)
282 || die "$progname: failed to write usage: $!\n";
283 }
284
285 $verbose = 1;
286 $anyway = 0;
287 $exclude = 0;
288 $keep_temp = 0;
289 @exlist=();
290 %builds=();
291 $cross2cross = 1;
292 while( @ARGV ) {
293 $_= shift( @ARGV );
294 last if m/^--$/;
295 if (!/^-/) {
296 unshift(@ARGV,$_);
297 last;
298 } elsif (/^(-\?|-h|--help|--version)$/) {
299 &usageversion();
300 exit( 0 );
301 } elsif (/^(-v|--verbose)$/) {
302 $verbose = 2;
303 } elsif (/^(-q|--quiet)$/) {
304 $verbose = 0;
305 } elsif (/^(-k|--keep-temp)$/) {
306 $keep_temp = 1;
307 } elsif (/^(-i|--install)$/) {
308 die sprintf(_g("%s: Only one action can be specified!\n"), $progname) if $mode;
309 $mode = "install";
310 } elsif (/^(-r|--remove|--purge)$/) {
311 die sprintf(_g("%s: Only one action can be specified!\n"), $progname) if $mode;
312 $mode = "remove";
313 } elsif (/^(-s|--status)$/) {
314 die sprintf(_g("%s: Only one action can be specified!\n"), $progname) if $mode;
315 $mode = "status";
316 } elsif (/^(-l|--list)$/) {
317 die sprintf(_g("%s: Only one action can be specified!\n"), $progname) if $mode;
318 $mode = "list";
319 } elsif (/^(-L|--listfiles)$/) {
320 die sprintf(_g("%s: Only one action can be specified!\n"), $progname) if $mode;
321 $mode = "listfiles";
322 } elsif (/^(-b|--build)$/) {
323 die sprintf(_g("%s: Only one action can be specified!\n"), $progname) if $mode;
324 $mode = "build";
325 } elsif (/^(-Q|--query)$/) {
326 die sprintf(_g("%s: Only one action can be specified!\n"), $progname) if $mode;
327 $mode = "query";
328 } elsif (/^(-u|--update)$/) {
329 die sprintf(_g("%s: Only one action can be specified!\n"), $progname) if $mode;
330 $mode = "update";
331 } elsif (/^(-a|--arch$)/) {
332 $arch = shift(@ARGV);
333 die sprintf(_g("%s: --arch needs an argument.\n"), $progname) if (!($arch));
334 } elsif (/^(-X|--exclude)$/) {
335 $pkg = shift (@ARGV);
336 die sprintf(_g("%s: --exclude needs an argument.\n"), $progname) if ($pkg =~ /^\-/);
337 push @removedeps, $pkg;
338 } elsif (/^(-A|--convert-anyway)$/) {
339 $anyway = 1;
340 } elsif (/^(-M|--convert-multiarch)$/) {
341 $multiarchconv = 1;
342 } else {
343 die sprintf (_g("%s: Unknown option %s.\n"), $progname, $_);
344 }
345 }
346
347 if (!defined($mode) || (!@ARGV && $mode ne "list")) {
348 die sprintf(_g("%s: Too few arguments.\n"), $progname);
349 }
350
351 if (not defined ($arch)) {
352 die sprintf(_g("%s: No default architecture has been set.\n"), $progname);
353 }
354
355 if (!&check_arch($arch)) {
356 die sprintf(_g("%s: Unrecognised architecture: %s\n"), $progname, $arch);
357 }
358 &setup;
359
360 $retval = 0;
361
362 if ($mode eq "query" || $mode eq "update") {
363 my %update_list = get_update_list( @ARGV );
364 if (!%update_list) {
365 printf _g("No updates available.\n");
366 exit 0;
367 }
368 if ($mode eq "query") {
369 printf _g("Available updates:\n");
370 foreach (sort keys %update_list) {
371 $msg = sprintf(_g("%s (from %s to %s)\n"),
372 $_, $update_list{$_}->{'Oldver'}, $update_list{$_}->{'Newver'});
373 print wrap('','',$msg);
374 }
375 } else {
376 $mode = "install";
377 @ARGV = ();
378 foreach (sort keys %update_list) {
379 push( @ARGV, $update_list{$_}->{'Path'} );
380 }
381 }
382 }
383
384 if ($mode eq "status") {
385 $dpkg_cmd = "--status";
386 } elsif ($mode eq "list") {
387 unshift( @ARGV, "*" ) if !@ARGV; # list all packages if no arg given
388 $dpkg_cmd = "--list";
389 } elsif ($mode eq "listfiles") {
390 $dpkg_cmd = "--listfiles";
391 } elsif ($mode eq "remove") {
392 $dpkg_cmd = "--purge";
393 } elsif ($mode eq "install") {
394 my( @debs, $deb );
395 printf (_g("Converting packages:\n")) if $verbose >= 2;
396 &check_exclude(@ARGV);
397 foreach $package ( keys %builds) {
398 $deb = &sub_build( $package, "/tmp" );
399 if ($deb) {
400 push( @debs, "/tmp/$deb" );
401 } else {
402 $retval = 1;
403 }
404 }
405 if (@debs) {
406 printf (_g("Installing converted packages with dpkg\n")) if $verbose >= 2;
407 open( PIPE, "LC_ALL=C dpkg -i @debs 2>&1 |" );
408 while( <PIPE> ) {
409 if ($verbose == 1 && /^Unpacking (replacement )?(\S+)/) {
410 printf (_g("Unpacking %s\n"), $2);
411 next;
412 }
413 print if ($verbose >= 1 && /^Setting up/) ||
414 $verbose >= 2 ||
415 !/^(\(Reading\sdatabase|
416 Selecting\spreviously\sdeselected\spackage|
417 Unpacking|
418 Preparing\sto\sreplace|
419 Setting\sup)/xi;
420 }
421 close( PIPE );
422 if ($?) {
423 warn sprintf (_g("dpkg -i failed.\n"));
424 $retval = 1;
425 }
426 if ($keep_temp == 0) {
427 printf (_g("Removing tmp packages\n")) if $verbose >= 2;
428 if (unlink( @debs ) != @debs) {
429 warn sprintf(_g("Removing %s failed: %s\n"), @debs, $!);
430 $retval = 1;
431 }
432 }
433 }
434 } elsif ($mode eq "build") {
435 &check_exclude(@ARGV);
436 foreach $package ( keys %builds ) {
437 printf (_g("Trying to build: %s\n"), $package) if ($verbose >= 2);
438 $retval = 1 if !sub_build( $package, "." );
439 }
440 }
441
442 if ($dpkg_cmd) {
443 my $cmdline = "dpkg $dpkg_cmd " .
444 join( " ", map( rewrite_pkg_name($_), @ARGV ));
445 printf (_g("Calling %s\n"), $cmdline) if $verbose >= 2;
446 system( $cmdline );
447 $retval = $? >> 8;
448 }
449 exit $retval;
450
451 sub sub_build {
452 my $package = shift(@_);
453 my $debpath = shift(@_);
454
455 # first of all, check if the file exists
456 if (not -r $package) {
457 warn sprintf (_g("%s: cannot access %s: %s\n"), $progname, $package, $!);
458 return "";
459 }
460
461 printf (_g("Going to convert %s\n"), $package) if $verbose >= 2;
462 my $nofailmsg = 0;
463
464 # set the umask (it may be bad by default)
465 umask(0022);
466
467 my $tmpdir = &create_tmpdir('dpkg-cross');
468 if (!$tmpdir) {
469 warn sprintf(_g("%s: failed to create temporary directory: %s\n"), $progname, $!);
470 return "";
471 }
472 my ($src, $dst) = ("$tmpdir/src", "$tmpdir/dst");
473 if (!(mkdir("$tmpdir/src") && mkdir("$tmpdir/dst"))) {
474 warn sprintf(_g("%s: failed to prepare temporary directory: %s\n"), $progname, $!);
475 system("rm -rf $tmpdir");
476 return "";
477 }
478
479 # remove tmp files on C-c
480 $SIG{'INT'} = sub {
481 printf (_g("Removing tmp files...\n")) if $verbose >= 2;
482 system "rm -rf $tmpdir";
483 die sprintf(_g("Interrupted.\n"));
484 };
485
486 printf (_g("Extracting %s\n"), $package) if $verbose >= 2;
487
488 # extract package to $src
489 if (system("dpkg --extract $package $src && dpkg --control $package $src/DEBIAN") != 0) {
490 goto fail;
491 }
492
493 printf (_g("Extracting information from control file\n")) if $verbose >= 2;
494
495 # extract useful information from control file
496 if (!(open(CONTROL, "$src/DEBIAN/control"))) {
497 warn sprintf(_g("%s: cannot open package control file: %s\n"), $progname, $!);
498 goto fail;
499 }
500 my $field;
501 my %control;
502 while (<CONTROL>) {
503 chomp;
504 if (/^ /) {
505 if (defined($field)) {
506 $control{$field} .= ("\n" . $_);
507 }
508 } elsif (/^(\S+):\s*(.*)$/i) {
509 $field = lc($1);
510 $control{$field} = $2;
511 }
512 }
513 close(CONTROL);
514
515 if (defined ($control{'multi-arch'})) {
516 if ($multiarchconv) {
517 # Carry on and process file anyway if --convert-multiarch given
518 warn sprintf(_g("%s: Multi-Arch package detected; processing anyway as --convert-multiarch specified.\n"), $progname);
519 $multiarchpackage=1;
520 } else {
521 my $output = basename ($package);
522 warn sprintf(_g("%s: Skipping Multi-Arch package '%s'.\n"), $progname, $output);
523 if (not -f $output) {
524 open(CP, "$package");
525 my @cp=<CP>;
526 close (CP);
527 open (CP, ">$output");
528 print CP @cp;
529 close (CP);
530 }
531 exit 0;
532 }
533 }
534 # check for existence of required fields
535 for $field (qw(package version architecture)) {
536 if (!defined($control{$field})) {
537 warn sprintf(_g("%s: required field \'%s\' missing in control file\n"), $progname, $field);
538 goto fail;
539 }
540 }
541 # check for package already processed by dpkg-cross, or created by
542 # cross-gcc compilation
543 if (($control{"architecture"} eq "all") && (
544 ($control{"description"} =~ /generated by dpkg-cross/) ||
545 ($control{"description"} =~ /contains files for.*cross-compile/)
546 )) {
547 warn sprintf(_g("%s: %s already looks like a cross-compile package\n"), $progname, $package);
548 goto fail;
549 }
550
551 # check package architecture
552 my $control_arch = $control{"architecture"};
553 if (($control_arch ne $arch) and
554 ($control_arch ne "all")) {
555 warn sprintf(_g("%s: %s has wrong architecture (%s)\n"), $progname, $package, $control_arch);
556 goto fail;
557 }
558
559 # prepare destination filename
560 my $evers = $control{"version"};
561 $evers =~ s/^\d+://; # strip epoch for filename
562 $debname = $control{"package"} . "-" . $arch . "-cross_" . $evers . "_all.deb";
563
564 # now ready to start preparing destination package
565
566 printf _g("Creating destination package tree\n") if $verbose >= 2;
567
568 my $objects = 0; # Number of objects (files and symlinks) in the converted package
569
570 # Helper: create directory tree if it does not exist.
571 sub ensure_dir {
572 my $dir = $_[0];
573 while ($dir =~ m:/:g) {
574 next if (length($`) == 0);
575 next if -d $`;
576 if (! mkdir($`)) {
577 $msg = sprintf(_g("%s: failed to create %s: %s\n"), $progname, $`, $!);
578 warn ($msg);
579 return 0;
580 }
581 }
582 return 1;
583 }
584
585 # Helper: link a file
586 sub link_file {
587 my ($from, $to) = @_;
588 ensure_dir($to) or return 0;
589 if (! link($from, $to)) {
590 $msg = sprintf(_g("%s: failed to link %s to %s: %s"), $progname, $from, $to, $!);
591 warn ($msg);
592 return 0;
593 }
594 return 1;
595 }
596
597 # Helper: detect ldscript
598 # Assumes that any *.so* file in library directory that is not ELF is
599 # ldscript
600 sub is_ldscript {
601 my $file = $_[0];
602 if (! open( FILE, $file )) {
603 $msg = sprintf(_g("%s: failed to open: %s\n"), $progname, $file, $!);
604 warn ($msg);
605 return 0;
606 }
607 $len = sysread( FILE, $data, 7 );
608 close( FILE );
609 return 0 if ($len != 7);
610 return 0 if ($data =~ /^!<arch>$/);
611 return 0 if ($data =~ /^.ELF/);
612 return 1 if ($data =~ /^\/\* GNU $/);
613 return 0;
614 }
615
616 # Helper: fix ldscript.
617 # Map all ocurances of /lib, /usr/lib, and (/usr)/lib<something>
618 # to corresponding $crosslib<something>
619 # FIXME!: remove all this repetition.
620 # Uses global $multiarch regexp modifier
621 sub fix_ldscript {
622 my ($from, $to) = @_;
623 ensure_dir($to) or return 0;
624 if (! open(FROM, $from)) {
625 $msg = sprintf(_g("%s: failed to open %s: %s"), $progname, $from, $!);
626 warn ($msg);
627 return 0;
628 }
629 if (! open(TO, ">$to")) {
630 $msg = sprintf(_g("%s: failed to open %s for writing: %s"), $progname, $to, $!);
631 warn ($msg);
632 close(FROM);
633 return 0;
634 }
635 while (<FROM>) {
636 if ($multiarch =~ m/mips(isa)?64.*-linux.*-gnuabi64.*/) {
637 s:(^|[^-\w/])(/usr)?/lib/${multiarch}ld\.so\.1:$1$crosslib64/ld.so.1:g;
638 s:(^|[^-\w/])(/usr)?/lib/${multiarch}ld-linux-mipsn8\.so\.1:$1$crosslib64/ld-linux-mipsn8\.so\.1:g;
639 } elsif ($multiarch =~ m/^mips(isa)?64.*-linux.*-gnuabin32.*/) {
640 s:(^|[^-\w/])(/usr)?/lib/${multiarch}ld\.so\.1:$1$crosslibn32/ld.so.1:g;
641 s:(^|[^-\w/])(/usr)?/lib/${multiarch}ld-linux-mipsn8\.so\.1:$1$crosslibn32/ld-linux-mipsn8\.so\.1:g;
642 } elsif ($multiarch =~ m/^mips(isa32)?.*-linux.*-gnu.*/) {
643 s:(^|[^-\w/])(/usr)?/lib/${multiarch}ld\.so\.1:$1$crosslib/ld\.so\.1:g;
644 s:(^|[^-\w/])(/usr)?/lib/${multiarch}ld-linux-mipsn8\.so\.1:$1$crosslib/ld-linux-mipsn8\.so\.1:g;
645 } elsif ($multiarchtriplet eq "sparc64-linux-gnu") {
646 s:(^|[^-\w/])(/usr)?/lib/${multiarch}ld-linux\.so\.2:$1$crosslib64/ld-linux.so.2:g;
647 }
648 s:(^|[^-\w/])(/usr)?/lib/$multiarch:$1$crosslib/:g;
649 unless ($multiarch) {
650 s:(^|[^-\w/])(/usr)?/lib32/:$1$crosslib32/:g;
651 s:(^|[^-\w/])(/usr)?/lib64/:$1$crosslib64/:g;
652 s:(^|[^-\w/])(/usr)?/libhf/:$1$crosslibhf/:g;
653 s:(^|[^-\w/])(/usr)?/libn32/:$1$crosslibn32/:g;
654 s:(^|[^-\w/])(/usr)?/libo32/:$1$crosslibo32/:g;
655 s:(^|[^-\w/])(/usr)?/libsf/:$1$crosslibsf/:g;
656 s:(^|[^-\w/])(/usr)?/libx32/:$1$crosslibx32/:g;
657 }
658 # Even though libc is multiarch, it ships non-multiarch loaders.
659 s:(^|[^-\w/])/lib(/ld[^/ ]*\.so):$1$crosslib$2:g;
660 s:(^|[^-\w/])/lib32(/ld[^/ ]*\.so):$1$crosslib32$2:g;
661 s:(^|[^-\w/])/lib64(/ld[^/ ]*\.so):$1$crosslib64$2:g;
662 s:(^|[^-\w/])/libx32(/ld[^/ ]*\.so):$1$crosslibx32$2:g;
663 print TO;
664 }
665 close(FROM);
666 close(TO);
667 return 1;
668 }
669
670 # Helper: fix .la file:
671 # - set libdir to $crosslib
672 # - change dependency_libs:
673 # - remove any -L (because dpkg-cross never allows any libraries
674 # outside $crosslib)
675 # - replace any references to .la files to files in $crosslib
676 # To make this work both for $crosslib and $crosslib64, one of those is passed
677 # as 3rd argumengt
678 # Also adapted to work for $crosslib32
679 sub fix_la_file {
680 my ($from, $to, $crosslib) = @_;
681 ensure_dir($to) or return 0;
682 if (! open(FROM, $from)) {
683 $msg = sprintf(_g("%s: failed to open %s: %s\n"), $progname, $from, $!);
684 warn ($msg);
685 return 0;
686 }
687 if (! open(TO, ">$to")) {
688 $msg = sprintf(_g("%s: failed to open %s for writing: %s"), $progname, $to, $!);
689 warn ($msg);
690 close(FROM);
691 return 0;
692 }
693 while (<FROM>) {
694 if (/^libdir=/) {
695 print TO "libdir=\'$crosslib\'\n";
696 } elsif (/^dependency_libs='(.*)'$/ or /^dependency_libs=(.*)/) {
697 my $deplibs = $1;
698 $deplibs =~ s/( )?-L\S+//g;
699 $deplibs =~ s:\S+/([\w\.\-]+\.la)( |$):$crosslib/$1$2:g;
700 print TO "dependency_libs='$deplibs'\n";
701 } else {
702 print TO;
703 }
704 }
705 close(FROM);
706 close(TO);
707 return 1;
708 }
709
710 # Helper: fixup pkgconfig file
711 # Set prefix and exec_prefix to $crossdir, libdir to $crosslib, includedir to $crossinc
712 sub fix_pc_file {
713 my ($from, $to) = @_;
714 ensure_dir($to) or return 0;
715 if (! open(FROM, $from)) {
716 $msg = sprintf(_g("%s: failed to open %s: %s\n"), $progname, $from, $!);
717 warn ($msg);
718 return 0;
719 }
720 if (! open(TO, ">$to")) {
721 $msg = sprintf(_g("%s: failed to open %s for writing: %s"), $progname, $to, $!);
722 warn ($msg);
723 close(FROM);
724 return 0;
725 }
726 #
727 my $orig_prefix = '/usr';
728 while (<FROM>) {
729 # fix for #483076
730 if ((m:^prefix="/usr(.*)"$:) or (m:^prefix=/usr(.*)$:)) {
731 print TO "prefix=${crossdir}${1}\n";
732 $orig_prefix = "/usr${1}";
733 } elsif (/^exec_prefix=/) {
734 print TO "exec_prefix=\${prefix}\n";
735 } elsif (/^libdir=/) {
736 if ($crosslib eq "$crossdir/lib") {
737 print TO "libdir=\${exec_prefix}/lib\n";
738 } else {
739 print TO "libdir=$crosslib\n";
740 }
741 # preserve original prefix for datarootdir entries
742 } elsif (/^datarootdir="\$\{prefix}(.*)"$/ or /^datarootdir=\$\{prefix}(.*)$/) {
743 print TO "datarootdir=${orig_prefix}${1}\n";
744 # preserve original (build arch) prefix for ${exec_prefix}/bin entries
745 # (e.g. orbit_idl).
746 } elsif (m:\$\{exec_prefix}/bin:) {
747 s:\$\{exec_prefix}/bin:${orig_prefix}/bin:;
748 print TO;
749 } elsif (/^includedir=/) {
750 my $inc = ($crossinc eq "$crossdir/include") ? "\${prefix}/include" : $crossinc;
751 s:\$\{(exec_)?prefix}/include:$inc:;
752 print TO "includedir=$inc\n";
753 } elsif (m#^Cflags:.*(-I/usr/include)#) {
754 # dpkg-cross unconditionally moves /usr/include to /usr/$host/include
755 # adapt pc file to match See bug #697695
756 s:-I/usr/include:-I/usr/$crosstype/include:;
757 print TO;
758 } else {
759 print TO;
760 }
761 }
762 close(FROM);
763 close(TO);
764 return 1;
765 }
766 # Helper: fix shlibs file
767 # - arch-qualify dependencies
768 sub fix_shlibs($$) {
769 my ($from, $to) = @_;
770 ensure_dir($to) or return 0;
771 if (! open(FROM, $from)) {
772 $msg = sprintf(_g("%s: failed to open %s: %s\n"), $progname, $from, $!);
773 warn ($msg);
774 return 0;
775 }
776 if (! open(TO, ">$to")) {
777 $msg = sprintf(_g("%s: failed to open %s for writing: %s\n"), $progname, $to, $!);
778 warn ($msg);
779 close(FROM);
780 return 0;
781 }
782 while (<FROM>) {
783 if (m/^#/) {
784 print TO;
785 } elsif (m/((?:\S+:\s*)?\S+\s+\S+\s+)(.*)/) {
786 print TO ($1 . join(",", map { s/\S+/$&:$arch/; $_; } split(/,/, $2)) . "\n");
787 } else {
788 print TO;
789 }
790 }
791 close(FROM);
792 close(TO);
793 return 1;
794 }
795 my $config = &get_config;
796 $crosstype = `CC="" dpkg-architecture -f -a$arch -qDEB_HOST_GNU_TYPE 2> /dev/null`;
797 chomp ($crosstype);
798 $multiarchtriplet = `CC="" dpkg-architecture -f -a$arch -qDEB_HOST_MULTIARCH 2> /dev/null`;
799 chomp ($multiarchtriplet);
800 # FIXME!: remove all this repetition (do we really need all these config vars?)
801 $crossinc = $$config{'crossinc'};
802 $crossdir = $$config{'crossdir'};
803 $crosslib = $$config{'crosslib'};
804 $crosslib64 = $$config{'crosslib64'};
805 $crosslib32 = $$config{'crosslib32'};
806 $crosslibhf = $$config{'crosslibhf'};
807 $crosslibn32 = $$config{'crosslibn32'};
808 $crosslibo32 = $$config{'crosslibo32'};
809 $crosslibsf = $$config{'crosslibsf'};
810 $crosslibx32 = $$config{'crosslibx32'};
811 # add extra regexp component for multiarch packages
812 if ($multiarchpackage) { $multiarch="\Q$multiarchtriplet/\E" } else {$multiarch=""};
813
814 # Now process regular files ...
815 open(PIPE, "find $src/ -type f -print |") or goto fail;
816 while(<PIPE>) {
817 chomp;
818 s/^$src//;
819 /^DEBIAN/ && next;
820 # if we have library, or header, files on multiarch paths,
821 # treat deb like a multiarch package
822 if (m:^((/usr)?/lib/|/usr/include/)\Q$multiarchtriplet/\E:) {
823 $multiarch="\Q$multiarchtriplet/\E"
824 }
825 # special support for generated cache data
826 if (m:(/etc/dpkg-cross/cross-config.d/($arch)?/.*):) {
827 &ensure_dir("$dst$1");
828 if (! open(FROM, "$src$_")) {
829 $msg = sprintf(_g("%s: failed to open %s: %s\n"), $progname, "$src$_", $!);
830 warn ($msg);
831 return 0;
832 }
833 if (! open(TO, ">$dst$1")) {
834 $msg = sprintf(_g("%s: failed to open %s for writing: %s"), $progname, "$dst$1", $!);
835 warn ($msg);
836 close(FROM);
837 return 0;
838 }
839 while (<FROM>) {
840 print TO;
841 }
842 close(FROM);
843 close(TO);
844 } elsif (m:^/usr(/share/.*/.*\.cmake)$:) {
845 &ensure_dir("$dst/$crossdir/$1");
846 if (! open(FROM, "$src$_")) {
847 $msg = sprintf(_g("%s: failed to open %s: %s\n"), $progname, "$src$_", $!);
848 warn ($msg);
849 return 0;
850 }
851 if (! open(TO, ">$dst/$crossdir/$1")) {
852 $msg = sprintf(_g("%s: failed to open %s for writing: %s"), $progname, "$dst$1", $!);
853 warn ($msg);
854 close(FROM);
855 return 0;
856 }
857 while (<FROM>) {
858 print TO;
859 }
860 close(FROM);
861 close(TO);
862 } elsif (m:^/usr/include/($multiarch)?:) {
863 # regular file under /usr/include/ or /usr/include/<triplet>
864 link_file("$src$_", "$dst$crossinc/$'") or goto fail;
865 # FIXME!: We should be able to do better than all this repetitive code!
866 } elsif (m:^/usr/(lib/)$multiarch(.*\.([hH]|[hH][hH]|[hH][pP][pP]))$:) {
867 # regular .h, .hh, or .hpp file under /usr/lib or /usr/lib/<triplet>
868 link_file("$src$_", "$dst$crossdir/$1/$2") or goto fail;
869 } elsif (m:^/usr/lib/$multiarch([^/]+\.o)$:) {
870 # regular .o file under /usr/lib or /usr/lib/<triplet>
871 link_file("$src$_", "$dst$crosslib/$1") or goto fail;
872 } elsif (m:^/usr/lib64/([^/]+\.o)$:) {
873 # regular .o file under /usr/lib64
874 link_file("$src$_", "$dst$crosslib64/$1") or goto fail;
875 } elsif (m:^/usr/lib32/([^/]+\.o)$:) {
876 # regular .o file under /usr/lib32
877 link_file("$src$_", "$dst$crosslib32/$1") or goto fail;
878 } elsif ((m:^/emul/ia32-linux/usr/lib/([^/]+\.[ao])$:)) {
879 # regular .a or .o file under /emul/ia32-linux/ for #463588
880 link_file("$src$_", "$dst$crosslib32/$1") or goto fail;
881 } elsif (m:^/usr/libhf/([^/]+\.[ao])$:) {
882 # regular .a or .o file under /usr/libhf
883 link_file("$src$_", "$dst$crosslibhf/$1") or goto fail;
884 } elsif (m:^/usr/libn32/([^/]+\.o)$:) {
885 # regular .o file under /usr/libn32
886 link_file("$src$_", "$dst$crosslibn32/$1") or goto fail;
887 } elsif (m:^/usr/libo32/([^/]+\.o)$:) {
888 # regular .o file under /usr/libo32
889 link_file("$src$_", "$dst$crosslibo32/$1") or goto fail;
890 } elsif (m:^/usr/libsf/([^/]+\.[ao])$:) {
891 # regular .a or .o file under /usr/libsf
892 link_file("$src$_", "$dst$crosslibsf/$1") or goto fail;
893 } elsif (m:^/usr/libx32/([^/]+\.o)$:) {
894 # regular .o file under /usr/libx32
895 link_file("$src$_", "$dst$crosslibx32/$1") or goto fail;
896 } elsif (m:^(/usr)?/lib/($multiarch)?([^/]+\.(a|so[^/]*))$:) {
897 # regular .a or .so* file under /lib, /usr/lib, /lib/<triplet>, /usr/lib/<triplet>
898 if (is_ldscript("$src$_")) {
899 fix_ldscript("$src$_", "$dst$crosslib/$3") or goto fail;
900 } else {
901 link_file("$src$_", "$dst$crosslib/$3") or goto fail;
902 }
903 } elsif (m:^/usr/\w+-\w+(-\w+(-\w+)?)?/(lib|include)/:) {
904 # regular file under /usr/<host>/lib or include
905 if ($cross2cross) {
906 link_file("$src$_", "$dst$_");
907 } else {
908 next;
909 }
910 } elsif (m:^(/usr)?/lib64/($multiarch)?([^/]+\.(a|so[^/]*))$:) {
911 # regular .a or .so* file under /lib64, /usr/lib64, /lib64/<triplet>, /usr/lib64/<triplet>
912 if (is_ldscript("$src$_")) {
913 fix_ldscript("$src$_", "$dst$crosslib64/$3") or goto fail;
914 } else {
915 link_file("$src$_", "$dst$crosslib64/$3") or goto fail;
916 }
917 } elsif (m:^(/usr)?/lib32/([^/]+\.(a|so[^/]*))$:) {
918 # regular .a or .so* file under /lib32, /usr/lib32
919 if (is_ldscript("$src$_")) {
920 fix_ldscript("$src$_", "$dst$crosslib32/$2") or goto fail;
921 } else {
922 link_file("$src$_", "$dst$crosslib32/$2") or goto fail;
923 }
924 } elsif ((m:^/emul/ia32-linux/usr/lib/([^/]+\.so[^/]*)$:)) {
925 # regular .so* file under /emul/ia32-linux/usr/lib
926 if (is_ldscript("$src$_")) {
927 fix_ldscript("$src$_", "$dst$crosslib32/$1") or goto fail;
928 } else {
929 link_file("$src$_", "$dst$crosslib32/$1") or goto fail;
930 }
931 } elsif (m:^(/usr)?/libhf/([^/]+\.so[^/]*)$:) {
932 # regular .so* file under /libhf, /usr/libhf
933 if (is_ldscript("$src$_")) {
934 fix_ldscript("$src$_", "$dst$crosslibhf/$2") or goto fail;
935 } else {
936 link_file("$src$_", "$dst$crosslibhf/$2") or goto fail;
937 }
938 } elsif (m:^(/usr)?/libn32/([^/]+\.(a|so[^/]*))$:) {
939 # regular .a or .so* file under /libn32, /usr/libn32
940 if (is_ldscript("$src$_")) {
941 fix_ldscript("$src$_", "$dst$crosslibn32/$2") or goto fail;
942 } else {
943 link_file("$src$_", "$dst$crosslibn32/$2") or goto fail;
944 }
945 } elsif (m:^(/usr)?/libo32/([^/]+\.(a|so[^/]*))$:) {
946 # regular .a or .so* file under /libo32, /usr/libo32
947 my $libf = $2;
948 if (is_ldscript("$src$_")) {
949 fix_ldscript("$src$_", "$dst$crosslibo32/$libf") or goto fail;
950 } else {
951 link_file("$src$_", "$dst$crosslibo32/$libf") or goto fail;
952 if ($_ =~ m:libo32/ld.*\.so\.1$:) {
953 my $relative = create_relative("$dst$crosslib/$libf", "$dst$crosslibo32/$libf");
954 ensure_dir("$dst$crosslib/$2") or goto fail;
955 if (! symlink($relative, "$dst$crosslib/$libf")) {
956 warn ("Failed to symlink lib/ld.so.1");
957 goto fail;
958 }
959 }
960 }
961 } elsif (m:^(/usr)?/libsf/([^/]+\.so[^/]*)$:) {
962 # regular .so* file under /libsf, /usr/libsf
963 if (is_ldscript("$src$_")) {
964 fix_ldscript("$src$_", "$dst$crosslibsf/$2") or goto fail;
965 } else {
966 link_file("$src$_", "$dst$crosslibsf/$2") or goto fail;
967 }
968 } elsif (m:^(/usr)?/libx32/([^/]+\.(a|so[^/]*))$:) {
969 # regular .a or .so* file under /libx32, /usr/libx32
970 if (is_ldscript("$src$_")) {
971 fix_ldscript("$src$_", "$dst$crosslibx32/$2") or goto fail;
972 } else {
973 link_file("$src$_", "$dst$crosslibx32/$2") or goto fail;
974 }
975 } elsif (m:^/usr/lib/$multiarch([^/]+\.la)$:) {
976 # regular .la file under /usr/lib
977 fix_la_file("$src$_", "$dst$crosslib/$1", $crosslib) or goto fail;
978 } elsif (m:^/usr/lib64/([^/]+\.la)$:) {
979 # regular .la file under /usr/lib64
980 fix_la_file("$src$_", "$dst$crosslib64/$1", $crosslib64) or goto fail;
981 } elsif (m:^/usr/lib32/([^/]+\.la)$:) {
982 # regular .la file under /usr/lib32
983 fix_la_file("$src$_", "$dst$crosslib32/$1", $crosslib32) or goto fail;
984 } elsif ((m:^/emul/ia32-linux/usr/lib/([^/]+\.la)$:)) {
985 # regular .la file under /emul-ia32-linux/usr/lib/
986 fix_la_file("$src$_", "$dst$crosslib32/$1", $crosslib32) or goto fail;
987 } elsif (m:/usr/lib/$multiarch(pkgconfig/[^/]+.pc)$:) {
988 # regular .pc file in /usr/lib/pkgconfig
989 fix_pc_file("$src$_", "$dst$crosslib/$1") or goto fail;
990 # not for lib64: I don't know if there is any rationale
991 } elsif (m:/usr/(share/pkgconfig/[^/]+.pc)$:) {
992 # regular .pc file in /usr/share/pkgconfig
993 fix_pc_file("$src$_", "$dst$crossdir/$1") or goto fail;
994 } elsif (m:^/usr/(src/.*):) {
995 # any files under /usr/src/
996 link_file("$src$_", "$dst$crossdir/$1") or goto fail;
997 } elsif (m:^/usr/(lib/debug/.*):) {
998 # any files under /usr/lib/debug
999 link_file("$src$_", "$dst$crossdir/$1") or goto fail;
1000 } elsif (m:^/usr/(share/fonts/.*):) {
1001 # any files under /usr/share/fonts/
1002 link_file("$src$_", "$dst$crossdir/$1") or goto fail;
1003 } elsif (m:^/usr/(lib/.*/tclConfig.sh):) {
1004 # the tcl cross-configure script
1005 link_file("$src$_", "$dst$crossdir/$1") or goto fail;
1006 } elsif (m:^/usr/(lib/perl/\d+\.\d+\.\d/(Config.pm|Config_heavy.pl)):) {
1007 # the perl cross-configure scripts
1008 link_file("$src$_", "$dst$crossdir/$1") or goto fail;
1009 } else {
1010 # everything else
1011 next;
1012 }
1013
1014 $objects++;
1015 }
1016 close(PIPE);
1017
1018 # Helper: create shortest relative symlink
1019 sub create_relative {
1020 my ($from, $to) = @_;
1021 # First remove common prefix from $from and $to
1022 while (1) {
1023 $from =~ m:^(/[^/]+): or last;
1024 my ($p1, $s1) = ($1, $');
1025 $to =~ m:^(/[^/]+): or last;
1026 my ($p2, $s2) = ($1, $');
1027 last if ($p1 ne $p2);
1028 ($from, $to) = ($s1, $s2);
1029 }
1030 # Now $from one more slashes than "../"'s should be added before $to
1031 # Example: /usr/lib/a/b -> /usr/lib/c
1032 # After loop: $from is "/a/b", $to is "/c"
1033 # $from has 2 slashes, so one "../" should be added before $to
1034 # Result is: /usr/lib/a/b -> ../c
1035 $from =~ s:[^/]::g; # remove all but slashes
1036 $from =~ s:^/::; # remove first slash (at least one slash always exists)
1037 $from =~ s:/:../:g; # replace each of other slashes with ../
1038 $to =~ s:^/::; # remove leading slash from $to
1039 return $from . $to;
1040 }
1041
1042 # ... next process symlinks.
1043 open(PIPE, "find $src/ -type l -print |") or goto fail;
1044 while(<PIPE>) {
1045 chomp;
1046 s/^$src//;
1047 # Ignore any symlinks not under /usr or /lib or /lib64 (or /emul/ia32-linux)
1048 m:^/(usr|lib|lib64|emul/ia32-linux): or next;
1049 next if (m:^/usr/\w+-\w+(-\w+(-\w+)?)?/: && !$cross2cross);
1050 # Find out (absolute) symlink destination
1051 my $lv = readlink("$src$_");
1052 if ($lv ne "." && $lv =~ m:^[^/]:) {
1053 m:^(.*)/[^/]*$:;
1054 $lv = "$1/$lv";
1055 }
1056 # Ignore any symlinks pointing outside /usr and /lib and /lib64 (or /emul/ia32-linux)
1057 $lv =~ m:^/(usr|lib|lib64|emul/ia32-linux): or next;
1058
1059 # Calculate corresponding DESTINATION path
1060 $lv = convert_path($multiarch,$lv);
1061
1062 # Check if destination object exists.
1063 # FIXME: this code is not correct for the case of symlink chains.
1064 # If converting symlink chains will be ever needed, this
1065 # should be rewritten
1066 if (! -e "$dst$lv") {
1067 # Non-existing destination allowed only if it is .so link,
1068 # or if both source and destination is under /usr/src
1069 next unless (/.*\.so$/ or
1070 (m:/usr/src/: and $lv =~ m:/usr/:));
1071 }
1072
1073 # Calculate corresponding SOURCE path
1074 $_ = convert_path($multiarch,$_);
1075
1076 # Skip links that are going to point to themselves
1077 next if ($lv eq $_);
1078
1079 # MIPS psABI requires the ld.so.1 in hardcoded directories:
1080 # O32 -> lib/ld.so.1
1081 # N32 -> lib32/ld.so.1
1082 # N64 -> lib64/ld.so.1
1083 # So to avoid conflicts of converting /usr/lib/<triple>/ld.so.1 to /usr/<triple>/lib,
1084 # some hack will be needed.
1085 # In libc6_VER_mips64*/mipsn32*, /usr/lib64/ld.so.1 is symlink of /usr/lib/<triple>/ld.so.1
1086 # So we need to delete /usr/<triple>/lib64/ld.so.1 and move /usr/<triple>/lib/ld.so.1 to
1087 # /usr/<triple>/lib64/ld.so.1
1088 if ($multiarch =~ m/^mips(isa)?64/ && ($_ =~ m:lib64/ld.*\.so\.1$: || $_ =~ m:lib32/ld.*\.so\.1$:)) {
1089 ensure_dir("$dst$_") or goto fail;
1090 unlink("$dst$_");
1091 rename("$dst$lv", "$dst$_");
1092 next;
1093 }
1094 # On MIPS N32 or MIPS64, lib/ld.so.1 is symlink to libo32/ld.so.1 now, we can just skip them.
1095 next if ($multiarch =~ m/^mips(isa)?64/ && $_ =~ m:lib/ld\.so\.1$:);
1096 next if ($multiarch =~ m/^mips(isa)?64/ && $_ =~ m:lib/ld-linux-mipsn8\.so\.1$:);
1097 next if ($multiarchtriplet eq "sparc64-linux-gnu" && $_ =~ m:lib/ld-linux\.so\.2$:);
1098
1099 # skip links to private modules and plugins that are not
1100 # useful or packaged in the -cross package, basically anything
1101 # in a directory beneath /usr/lib/. See #499292
1102 # except pkgconfig symlinks, see #506956
1103 next if (($lv =~ m:$crosslib/.*/:) and ($lv !~ m:$crosslib/pkgconfig/:));
1104 $lv =~ m:$crosslib/(.*)$:;
1105 # Translators, retain the -> to indicate the direction of the link.
1106 printf (_g("Creating symlink %s -> %s\n"), $_, $1) if ($verbose >= 2);
1107
1108 # Skip if destination already exists - for the case if a symlink maps
1109 # to same destination as a regular file
1110 next if (-e "$dst$_");
1111
1112 # Previous line seems not to catch situation when "$dst$_" is a symlink
1113 # pointing to non-existant (external to the package being created) file
1114 unlink("$dst$_");
1115
1116 # Create a relative link
1117 my $relative = create_relative($_, $lv);
1118 ensure_dir("$dst$_") or goto fail;
1119 if (! symlink($relative, "$dst$_")) {
1120 $msg = sprintf(_g("%s: failed to create symlink %s -> %s: %s\n"), $progname, "$dst$_", $relative, $!);
1121 warn ($msg);
1122 goto fail;
1123 }
1124 $objects++;
1125 }
1126 close(PIPE);
1127
1128 # At this point, $dst should be ready, and $objects should contain number
1129 # or files and symlinks under $dst
1130 if ($objects == 0) {
1131 if ($anyway) {
1132 $msg = sprintf(_g("package %s doesn't provide any useful files, but processing it anyway as requested\n"), $control{"package"});
1133 print wrap('','',"$progname: $msg") if ($verbose >= 1);
1134 } else {
1135 $msg = sprintf(_g("package %s doesn't provide any useful files. Skipping.\n"), $control{"package"});
1136 warn ("$progname: $msg");
1137 $nofailmsg = 1;
1138 goto fail;
1139 }
1140 }
1141
1142 # Create README in /usr/share/doc/ ...
1143 my $docpath = "/usr/share/doc/" . $control{"package"} . "-$arch-cross";
1144 my $docfile = "$docpath/README";
1145 printf (_g("Creating %s\n"), $docfile) if $verbose >= 2;
1146 ensure_dir("$dst$docfile") or goto fail;
1147 if (! open(DOC, ">$dst$docfile")) {
1148 $msg = sprintf(_g("%s: failed to open %s for writing: %s\n"), $progname, "$dst$docfile", $!);
1149 warn ($msg);
1150 goto fail;
1151 }
1152 print DOC "Package " . $control{"package"} .
1153 "-$arch-cross is a part of the cross-compiling environment \n" .
1154 "for $arch target. It was created from " . $control{"package"} .
1155 " package using the dpkg-cross tool.\n\nTo get more information about " .
1156 $control{"package"} . " or dpkg-cross packages,\n" .
1157 "please install those and read the provided documentation.\n";
1158 close(DOC);
1159
1160 if (! mkdir("$dst/DEBIAN")) {
1161 $msg = sprintf(_g("%s: failed to create %s/DEBIAN: %s\n"), $progname, $dst, $!);
1162 warn ($msg);
1163 goto fail;
1164 }
1165
1166 # Link the shlibs file
1167 if (-f "$src/DEBIAN/shlibs") {
1168 print "Installing shlibs file\n" if $verbose >= 2;
1169 link_file("$src/DEBIAN/shlibs", "$dst/DEBIAN/shlibs");
1170 # FIXME: not enabling in this copy. needed?
1171 #fix_shlibs("$src/DEBIAN/shlibs", "$dst/DEBIAN/shlibs");
1172 }
1173
1174 # Create the control file.
1175 print "Creating control file\n" if $verbose >= 2;
1176 if (! open(CONTROL, ">$dst/DEBIAN/control")) {
1177 $msg = sprintf(_g("%s: failed to open %s/DEBIAN/control for writing: %s\n"), $progname, $dst, $!);
1178 warn ($msg);
1179 goto fail;
1180 }
1181
1182 print CONTROL "Package: " . $control{"package"} . "-$arch-cross\n";
1183 print CONTROL "Version: " . $control{"version"} . "\n";
1184 print CONTROL "Section: " . $control{"section"} . "\n";
1185 print CONTROL "Priority: optional\n";
1186 print CONTROL "Architecture: all\n";
1187 print CONTROL "Multi-Arch: foreign\n";
1188
1189 if (defined($control{"maintainer"})) {
1190 print CONTROL "Maintainer: " . $control{"maintainer"} . "\n";
1191 }
1192 if (defined($control{"source"})) {
1193 print CONTROL "Source: " . $control{"source"} . "\n";
1194 } else {
1195 print CONTROL "Source: " . $control{"package"} . "\n";
1196 }
1197
1198 # Turn Pre-Depends into Depends
1199 if (defined($control{"pre-depends"})) {
1200 if (defined($control{"depends"})) {
1201 $control{"depends"} = $control{"pre-depends"} . ", " . $control{"depends"};
1202 } else {
1203 $control{"depends"} = $control{"pre-depends"};
1204 }
1205 }
1206
1207 # Rewrite dependency fields
1208 # Make 'provides' field to exist always to all $package-$arch-dcv1
1209 # but create no dependencies on dcv1 - see #514249
1210 my $prov = (exists $control{"provides"}) ? $control{"provides"}: "";
1211 my $rewritten = rewrite_dependencies($prov);
1212 my $joiner = (length($rewritten) > 0) ? ", " : "";
1213 $rewritten .= $joiner.$control{"package"} ."-$arch-dcv1";
1214 print CONTROL ucfirst("provides") . ": $rewritten\n";
1215 for $field (qw(depends conflicts replaces)) {
1216 next if not defined $control{$field};
1217 my $rewritten = rewrite_dependencies($control{$field});
1218 if (length($rewritten) > 0) {
1219 # Capitalize first letter of field name
1220 print CONTROL ucfirst($field) . ": " . $rewritten . "\n";
1221 }
1222 }
1223
1224 # Output modified description
1225 if (defined($control{"description"})) {
1226 $control{"description"} =~ /(.*)/; # match first line
1227 print CONTROL "Description: $1 (for cross-compiling)\n";
1228 print CONTROL " This package was generated by dpkg-cross for cross compiling.\n .$'\n";
1229 } else {
1230 print CONTROL "Description: " . $control{"package"} . " for cross-compiling\n";
1231 print CONTROL " This package was generated by dpkg-cross for cross compiling.\n" .
1232 " .\n Source package " . $control{"package"} . " provided no description.\n"
1233 }
1234
1235 close(CONTROL);
1236
1237 # Create md5sums file
1238 printf (_g("Creating md5sums file\n")) if $verbose >= 2;
1239 # Code shamelessly stolen from dh_md5sums
1240 system("cd $dst && find . -type f ! -regex '.*/DEBIAN/.*' -printf '%P\\0' | xargs -r0 md5sum > $dst/DEBIAN/md5sums");
1241
1242 # Find out if fakeroot is needed and if it is available
1243 my $wrapper = "";
1244 if (geteuid() != 0) {
1245 $wrapper = "/usr/bin/fakeroot";
1246 if (! -x $wrapper) {
1247 $msg = sprintf(_g("%s: %s is not available, package files will not be owned by root\n"), $progname, $wrapper);
1248 warn ($msg);
1249 $wrapper = "";
1250 }
1251 }
1252
1253 # Build the .deb
1254 printf (_g("Building %s\n"), $debname) if $verbose == 1;
1255 if (system( "$wrapper dpkg-deb -b $dst $debpath")) {
1256 $msg = sprintf (_g("%s: building package with dpkg-deb -b failed.\n"), $progname);
1257 warn ($msg);
1258 goto fail;
1259 }
1260
1261 $SIG{'INT'} = 'DEFAULT';
1262 system "rm -rf $tmpdir";
1263 return $debname;
1264
1265 fail:
1266 system("rm -rf $tmpdir");
1267 $SIG{'INT'} = 'DEFAULT';
1268 warn sprintf(_g("%s: conversion of %s failed.\n"), $progname, $package) unless $nofailmsg;
1269 return "";
1270 }
1271
1272 sub get_update_list {
1273 my( %installed, %available, %av_path, $pkg, %update_list, $cnt );
1274 local( *F );
1275
1276 if ($verbose >= 2) {
1277 printf (_g("Determining installed packages "));
1278 STDOUT->flush();
1279 }
1280 open( F, "<$dpkg_statfile" ) or die sprintf(_g("Can't open %s: %s"), $dpkg_statfile, $!);
1281 %installed = parse_pkg_list();
1282 if ($verbose >= 2) {
1283 $cnt = %installed;
1284 $cnt = $cnt+0;
1285 printf (ngettext("(%d package)\n", "(%d packages)\n", $cnt), $cnt);
1286 }
1287
1288 foreach (@_) {
1289 scan_available( \%available, \%av_path, $_ );
1290 }
1291
1292 foreach $pkg (keys %installed) {
1293 if (exists($available{$pkg}) &&
1294 version_less_p( $installed{$pkg}, $available{$pkg} )) {
1295 $update_list{$pkg}->{'Path'} = $av_path{$pkg};
1296 $update_list{$pkg}->{'Oldver'} = $installed{$pkg};
1297 $update_list{$pkg}->{'Newver'} = $available{$pkg};
1298 }
1299 }
1300 return %update_list;
1301 }
1302
1303 sub scan_available {
1304 my $av_ref = shift;
1305 my $path_ref = shift;
1306 my $pkgpath = shift;
1307 my( @pkglist, %available, $file, $pkg, $cnt );
1308
1309 if ($verbose >= 2) {
1310 printf (_g("Scanning .deb files under %s "), $pkgpath);
1311 STDOUT->flush();
1312 }
1313 @pkglist = `find $pkgpath -type f -a -name '*.deb' -print`;
1314 die sprintf(_g("find command returned error status %s\n"), $?) if $?;
1315 if (!@pkglist) {
1316 printf (_g("No .deb files found under %s\n"), $pkgpath) if $verbose >= 1;
1317 return;
1318 }
1319 chomp @pkglist;
1320 $cnt = 0;
1321 foreach $file (@pkglist) {
1322 open( F, "dpkg --field $file |" )
1323 or die sprintf (_g("Can't run dpkg --field %s: %s\n"), $file, $!);
1324 if (%available = parse_pkg_list(1)) {
1325 $pkg = (keys %available)[0];
1326 $av_ref->{$pkg} = $available{$pkg};
1327 $path_ref->{$pkg} = $file;
1328 ++$cnt;
1329 }
1330 }
1331 printf(ngettext("(%d package)\n", "(%d packages)\n", $cnt), $cnt)
1332 if ($verbose >= 2);
1333 }
1334
1335 sub parse_pkg_list {
1336 my $avail_pkg = shift;
1337 my( $name, $version, %result );
1338 local($/) = ""; # read in paragraph mode
1339
1340 while( <F> ) {
1341 /^Package:\s*(\S+)\s*$/mi || next; $name = $1;
1342 if ($avail_pkg) {
1343 # available package: check architecture
1344 /^Architecture:\s*(\S+)\s*$/mi || next;
1345 next if $1 ne $arch;
1346 } else {
1347 # package from status file: check if installed at all, and
1348 # if cross-compiling package; strip suffix from name
1349 next if /^Status:.*\s+(\S+)\s*$/mi && $1 ne 'installed';
1350 next if $name !~ /-$arch-cross$/;
1351 $name =~ s/-$arch-cross$//;
1352 }
1353 /^Version:\s*(\S+)\s*$/mi || next; $version = $1;
1354 $result{$name} = $version;
1355 }
1356 close( F ) or die sprintf(_g("Error status from dpkg\n"));;
1357 return %result;
1358 }
1359
1360 sub version_less_p {
1361 my $vers1 = shift;
1362 my $vers2 = shift;
1363
1364 system( "dpkg --compare-versions $vers1 '<<' $vers2" );
1365 return $? == 0;
1366 }
1367
1368 sub rewrite_dependencies {
1369 my $str = shift;
1370
1371 my @list = map( rewrite_alternatives($_), split( /\s*,\s*/, $str));
1372 # remove empty elements
1373 @list = map { $_ ? ( $_ ) : () } @list;
1374 return join(", ", @list );
1375 }
1376
1377 sub rewrite_alternatives {
1378 my $str = shift;
1379
1380 my @list = map( rewrite_item($_), split( /\s*\|\s*/, $str ));
1381 # if any of the alternatives became empty (because of @removedeps),
1382 # the complete dependency should be removed
1383 @list = ();
1384 for my $item (split( /\s*\|\s*/, $str )) {
1385 $item = rewrite_item($item);
1386 return () if not $item;
1387 push @list, $item
1388 }
1389 return join( " | ", @list );
1390 }
1391
1392 sub rewrite_item {
1393 my $str = shift;
1394 $str =~ /^([^ (]+)/;
1395 my $name = $1;
1396 return () if grep { $_ eq $name } @removedeps;
1397 return $str if grep { $_ eq $name } @keepdeps;
1398 if ($name =~ /^([^:]*):(.*)/) {
1399 my $replacement = "$1-$2-cross";
1400 $str =~ s/^[^ (]+/$replacement/;
1401 } else {
1402 $str =~ s/^([^ (]+)/$name-$arch-cross/;
1403 }
1404 return $str;
1405 }
1406
1407 sub check_exclude {
1408 foreach $package ( @ARGV ) {
1409 if ($package =~ /^\-X/) {
1410 $exclude = 1;
1411 next;
1412 }
1413 if (($exclude == 1) && ($package !~ /^\-/) && (!defined $builds{$package})) {
1414 push @removedeps, $package;
1415 next;
1416 }
1417 $builds{"$package"}++;
1418 }
1419 $str = join(' ', @removedeps);
1420 $msg = sprintf (_g("Excluding: %s\n"), $str);
1421 print wrap('','', $msg) if ($verbose >= 2);
1422 }
1423
1424 =head1 Conversion process
1425
1426 There is no safe way for dpkg-cross to mangle certain files in -dev
1427 packages, particularly files in F</usr/share> or to determine precisely
1428 which of these files could be parsed and which cannot. Special
1429 cases do not help - dpkg-cross already has too many of those.
1430
1431 It is not safe to leave files in the package nor is it safe to move
1432 files into arbitrary locations when there is no reliable and
1433 standardised way to determine the usefulness of a particular kind of
1434 file. Package specific config scripts cannot be supported and even
1435 build-system specific ones are not necessarily going to work without a
1436 lot of ongoing maintenance.
1437
1438 Therefore, C<dpkg-cross> is very restrictive on which files are
1439 retained in the -cross package.
1440
1441 =head2 Files to be converted
1442
1443 All files in
1444
1445 /usr/include/*
1446
1447 Also /usr/src and /usr/lib are searched for includes - files with
1448 .h, .hh, .hpp extensions (case insensitive).
1449
1450 Library files - *.so* and *.a and *.o files from /lib and /usr/lib
1451 (and all the multilib eqivalents (lib64, lib32, libo32, libhf, libx32 etc).
1452 Other library files are not copied. *.la files in
1453 library directories are also copied, and library and paths are
1454 modified there. Same for /usr/lib/pkgconfig/*.pc files.
1455
1456 Symlinks are copied (and modified appropriately) if their
1457 destination is copied. Also, symlinks to non-existing shared libraries
1458 are copied (this is common case for libdevel packages)
1459
1460 Directories are created only to hold some files or symlinks.
1461 No empty directories are copied.
1462
1463 =head1 Multiarch behaviour
1464
1465 By default <dpkg-cross> does nothing with packages from Debian which already
1466 support Multi-Arch - the package is simply copied to the current work
1467 directory, if it does not already exist. Any package containing a Multi-Arch:
1468 field in DEBIAN/control is skipped in this manner.
1469
1470 --convert-multiarch can be specified to instead force the generation of a
1471 -<arch>-cross package with the files moved into the conventional
1472 dpkg-cross locations.
1473
1474 The Multiarch spec ensures that multiarch packages will only be
1475 available from Debian mirrors once dpkg itself is capable of installing
1476 the packages and installing a Multiarch package means being able to
1477 install a 32bit multiarch package alongside a 64bit multiarch package.
1478 Installing an armel multiarch package alongside an x86 multiarch
1479 package is no different to 32bit vs 64bit. (What changes is how the
1480 multiarch package itself handles the header files and pkg-config
1481 files.)
1482
1483 =cut