File: emchain

package info (click to toggle)
emdebian-tools 1.4.3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 1,112 kB
  • ctags: 274
  • sloc: perl: 6,297; xml: 4,828; sh: 1,902; php: 406; ansic: 189; makefile: 15
file content (948 lines) | stat: -rwxr-xr-x 28,986 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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
#!/usr/bin/perl
#
#  emchain -- toolchain builder for cross compiling
#  Copyright (C) 2006-2008  Neil Williams <codehelp@debian.org>
#  Copyright (c) 2007 Simon Richter <sjr@debian.org>
#
#  This package 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 3 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, see <http://www.gnu.org/licenses/>.
#

use Cwd;
use File::HomeDir;
use File::Basename;
use Text::Wrap;
use Config::Auto;
use Debian::DpkgCross;
use Cache::Apt::Package;
use Cache::Apt::Config;
use Cache::Apt::Lookup;
use Term::ANSIColor qw(:constants);
use Text::Balanced qw(extract_bracketed);
use Emdebian::Tools;
use strict;
use warnings;
use vars qw/ $verbose $dpkg_cross_dir $suite $arch $line $aptcmd
$skip_gcc $skip_binutils $skip_libc $host $gcc_latest $gcc_vers
$gcc_pkg_v $binutils_vers $libc_latest $libc_vers $libc_pkg_v
$binutils_dir $gcc_dir $dev $binutils_deb @glibc_list @glibc_list2
@glibc_list3 $check $glibc_count $home $ourversion $progname $logfile
$date $log $cmdline $status $environ $result $workdir $msg $ignore
*OLDOUT *OLDERR $forcing $arglog $logdir $uclibc $uclibc_vers/;

$ourversion = &tools_version();
$progname = basename($0);
$verbose = 1;
$forcing = 0;
$suite = &get_targetsuite();
# read in dpkg-cross default arch
&read_config();
$arch = &get_architecture();
$dpkg_cross_dir = &get_aptcross_dir();
$workdir = &get_workdir;
$workdir = "." if ($workdir eq "");
$msg = &check_workdir($workdir);
die $msg if ($msg ne "");
chdir ("$workdir") if ($workdir ne ".");
chdir ("emchain") if ( -d "emchain");
$logdir = cwd;
$date = `date +%F`;
$date =~ s/\n//;

## Pseudo-code:
# $ apt-get source gcc-3.4 binutils libc
# $ cd binutils-2.17
# $ TARGET=$target_gnu_type fakeroot debian/rules binary-cross (replace arch by arm, alpha,...)
# dpkg -i built-package
# apt-cross -i libdb1-compat libc6 libc6-dev linux-kernel-headers
# $ export GCC_TARGET=$arch
# $ export DEB_CROSS_INDEPENDENT=yes (for gcc-4.1 and later).
# $ cd gcc-3.4-3.4.2
# $ debian/rules control
# $ dpkg-buildpackage -b -rfakeroot

sub usageversion {
	print(STDERR <<END)
emchain version $ourversion

Usage:
 emchain [-a|--arch ARCH] [-w|--workdir] [-l|--log] [-v|--verbose] [-q|--quiet]
 emchain [-a|--arch ARCH] [-w|--workdir] [-l|--log] [-v|--verbose] [-q|--quiet] -f|--force
 emchain [-a|--arch ARCH] [-w|--workdir] [-l|--log] [-v|--verbose] [-q|--quiet] -u|--uclibc
 emchain [-a|--arch ARCH] [-w|--workdir] [-l|--log] [-v|--verbose] [-q|--quiet] -i|--ignore
 emchain -?|-h|--help|--version

Commands:
 -f|--force:          continue the build even if the upstream native build has failed.
 -i|--ignore:         ignore the installed toolchain and build another one.
 -u|--uclibc:         attempt to create a uClibc toolchain link from the existing
                       gcc/glibc toolchain. (Experimental!)

Options:
 -a|--arch ARCH:      set architecture (default: defined by dpkg-cross).
 -w|--workdir DIR:    override the current working directory setting .
 -l|--log:            write a logfile in the working directory.
 -v|--verbose:        be verbose (repeat for more verbosity).
 -q|--quiet:          be quiet [default]
 -?|-h|--help:        print this usage message and exit
 --version:           print this usage message and exit

emchain implements the EmdebianSlind toolchain build process for
the latest versions of gcc, binutils and libc. To build toolchains
for older compilers, see the Emdebian website: www.emdebian.org

emchain uses dpkg-cross and apt-cross to determine the latest
versions of toolchain packages, downloads the source for each missing
package, builds the package using EmdebianSlind options and commands,
installs the cross-built packages and toolchain packages.

emchain needs to be run from a consistent working directory so that it
can check if new versions need to be downloaded. If an existing working
directory has not been specified using debconf, the current directory
will be used.

END
		|| die "$progname: failed to write usage: $!\n";
}

$arglog = 0;
while( @ARGV ) {
	$_= shift( @ARGV );
	last if m/^--$/;
	if (!/^-/) {
		unshift(@ARGV,$_);
		last;
	}
	elsif (/^(-\?|-h|--help|--version)$/) {
		&usageversion();
		exit( 0 );
	}
	elsif (/^(-v|--verbose)$/) {
		$verbose++;
	}
	elsif (/^(-q|--quiet)$/) {
		$verbose--;
	}
	elsif (/^(-a|--arch)$/) {
		$arch = shift(@ARGV);
	}
	elsif (/^(-w|--workdir)$/) {
		$workdir = shift(@ARGV);
		chdir ("$workdir");
	}
	elsif (/^(-l|--log)$/) {
		$arglog = 1;
	}
	elsif (/^(-f|--force)$/) {
		$forcing = 1;
	}
	elsif (/^(-u|--uclibc)$/) {
		$uclibc = 1;
	}
	elsif (/^(-i|--ignore)$/) {
		$ignore = 1;
	}
}

$msg = "$progname: no default architecture found.\n";
$msg .= "Please use '$progname --arch ARCH'.\n";
die $msg if ((!$arch)||($arch eq ""));

my $target_gnu_type = &check_cache_arch($arch);
$logfile = "emchain-" . $arch . "-" . $date . ".log" if ($arglog == 1);

if ($verbose >= 2)
{
	print CYAN, "Building toolchain in '" . cwd . "'\n", RESET;
	print CYAN, "Options used:\n", RESET;
	print CYAN, "verbose=$verbose\n", RESET;
	print GREEN, "forcing=YES\n", RESET if ($forcing != 0);
	(defined $logfile) ? print CYAN, "logging to $logfile\n", RESET :
		print CYAN, "not logging output.\n", RESET;
}
if (!&check_arch($arch)){
	$msg = (qq;Error: dpkg-cross does not currently support "$arch".;);
	die RED, "\n$msg", RESET, "\n";
}

if ($logfile)
{
	open OLDOUT, ">&STDOUT" or die "cannot duplicate stdout: $!\n";
	open OLDERR, ">&STDERR" or die "cannot duplicate stderr: $!\n";
	open BUILD, "| tee $logfile" or die "could not open pipe to tee $logfile: $!";
	close STDOUT;
	close STDERR;
	open STDOUT, ">&BUILD" or die "cannot reopen stdout: $!";
	open STDERR, ">&BUILD" or die "cannot reopen stderr: $!";
	print "emchain $ourversion build log.\t$date\tArchitecture: $arch\n\n";
}

&setup_config;
$msg = "Checking apt-cross $suite cache for $arch.\n";
print CYAN, $msg, RESET if ($verbose >= 2);
my $val = &get_primary;
&set_mirror("ftp://$val/debian") if (defined $val);
&check_update($verbose);

# prepare variable names.
$skip_gcc = 0;
$skip_binutils = 0;
$skip_libc = 0;
$host = &host_arch("binutils");
$gcc_latest =  &find_latest_gcc("gcc", $arch, $suite);
if ($gcc_latest eq "0")
{
	print (RED, qq/Warning: $progname cannot find a native gcc package for $arch.\n/, RESET);
	print (GREEN, qq/$progname will try to build a toolchain from scratch using cache values for $host.\n/, RESET);
	$msg = "Refreshing apt-cross $suite cache for $host.\n";
	print CYAN, $msg, RESET if ($verbose >= 2);
	&use_hostsources;
	my $config = &init_host_cache(2);
	&cache_update($config);
	$gcc_latest = &find_latest_gcc("gcc", $host, $suite);
	$binutils_vers = &cache_version("binutils", $host);
	$libc_latest = &find_latest_libc("libc", $host, $suite);
	$gcc_vers = "gcc-" . $gcc_latest;
	# BUG $host should always work.
	my $bugmsg = "Internal error: Please rerun emchain with the --verbose --log options and ";
	$bugmsg .= " file a bug report against emdebian-tools with the results attached.";
	die (RED, wrap('','',$bugmsg), RESET) if ($gcc_vers eq "gcc-0");
	$gcc_pkg_v = &cache_version($gcc_vers, $host);
	$libc_vers = "libc" . $libc_latest;
	$libc_pkg_v = &cache_version($libc_vers, $host);
	$binutils_dir = &split_debian($binutils_vers);
	$gcc_dir = &parse_gcc_dir($host);
}
else
{
	$binutils_vers = &cache_version("binutils", $arch);
	$libc_latest = &find_latest_libc("libc", $arch, $suite);
	$gcc_vers = "gcc-" . $gcc_latest;
	$gcc_pkg_v = &cache_version($gcc_vers, $arch);
	$libc_vers = "libc" . $libc_latest;
	$libc_pkg_v = &cache_version($libc_vers, $arch);
	$binutils_dir = &split_debian($binutils_vers);
	$gcc_dir = &parse_gcc_dir($arch);
}

if (defined $uclibc)
{
	&uclibc_build_chain;
	exit (0);
}
elsif (not defined $ignore)
{
	my $check = &check_toolchains ($arch, $target_gnu_type);
	$msg = "$progname: You already have a usable toolchain installed ";
	$msg .= "for $arch on $suite using glibc. Use '$progname -a $arch --ignore' ";
	$msg .= " if you want to build the toolchain again or ";
	$msg .= "'$progname -a $arch --uclibc' if you want the uClibc extension.\n";
	if ($check eq "true")
	{
		system ("emsetup -a $arch --report");
		warn RED, wrap('','',"\n$msg"), RESET, "\n";
		exit (1);
	}
}

$dev = $libc_vers . "-dev";
$binutils_deb = "binutils-${target_gnu_type}_${binutils_vers}_${host}.deb";
@glibc_list=qw//;
@glibc_list2=qw//;
@glibc_list3=qw//;
$glibc_count = 0;
&check_source_version("binutils");
&check_source_version("linux-kernel-headers");
&check_source_version($libc_vers);
&check_source_version($gcc_vers);

$log = "\nThis is emchain $ourversion.\n Toolchain builder for cross compiling.\n\n";
print CYAN, $log, RESET if ($verbose >= 3);
$log = "Using : \n";
# catch errors where package-version does not match the unpacked directory name.
if(! -d "$gcc_dir")
{
	opendir(DIR, ".") or die ("Cannot open current directory: $!");
	my @dirs=readdir(DIR);
	closedir(DIR);
	foreach my $f (@dirs)
	{
		if ((-d $f) && ($f =~ /^$gcc_dir.*/) && (-d "$f/debian"))
		{
			$gcc_dir = $f;
		}
	}
}
$log .= "$gcc_vers ($gcc_pkg_v) in $gcc_dir/\n";
$log .= "binutils $binutils_vers in binutils-$binutils_dir/\n";
$log .= "$libc_vers v$libc_pkg_v\n";
print CYAN, $log, RESET if ($verbose >= 2);

# apt-get source will skip existing downloads if up to date.
$log = "Running apt-get source . . . \n";
print GREEN, $log, RESET if ($verbose >= 2);
if ($verbose < 2) { $aptcmd = "apt-get -q"; }
else { $aptcmd = "apt-get"; }
&set_suite($suite);
&check_cache_arch($arch);
my $config = &init_cache(0);
my $emp = AptCrossPackage->new();
$emp->Package($gcc_vers);
$emp = &lookup_pkg($emp);
my $gcc_src = $$emp->Version;
$log = "$aptcmd source $gcc_vers=$gcc_src binutils=$binutils_vers glibc=$libc_pkg_v";
print CYAN, "$log\n", RESET if ($verbose >= 2);
my $exitval = system "$log";
# catch all in case there was an unknown error.
exit($exitval) if ($exitval != 0);

print GREEN, "Running sudo - enter your sudo password if prompted.\n", RESET;

if (! -f $binutils_deb) {
	my $dir = &get_src_dir("binutils");
	my $cwd = cwd;
	chdir ($dir);
	&check_emdebian_control();
	print CYAN, "Building $binutils_deb in $cwd/$dir\n", RESET if ($verbose >= 2);
	system ("TARGET=$target_gnu_type fakeroot debian/rules binary-cross");
	chdir ("../");
	system ("sudo dpkg -i $binutils_deb") if ($forcing == 0);
}
# apt-cross also skips download/conversion of existing debs
# but does not skip installation. :-(
if ($verbose < 2) { $aptcmd = "apt-cross -i"; }
else { $aptcmd = "apt-cross -v -i"; }
# check with dpkg-cross --status for Status: install ok installed\n
$cmdline = &check_cross("libdb1-compat");
$cmdline .= &check_cross("$libc_vers");
$cmdline .= &check_cross("$dev");
$cmdline .= &check_cross("linux-kernel-headers");
# only run cmdline if there is something for apt-cross to do
if (($cmdline ne "") && ($forcing == 0))
{
	system("sudo $aptcmd -a $arch $cmdline");
}
$log = "";

push @glibc_list, "gcc-${gcc_latest}-${target_gnu_type}-base_${gcc_pkg_v}_${host}.deb";
push @glibc_list, "libstdc++${libc_latest}-${arch}-cross_${gcc_pkg_v}_all.deb";
push @glibc_list, "gcc-${gcc_latest}-${target_gnu_type}_${gcc_pkg_v}_${host}.deb";
push @glibc_list, "cpp-${gcc_latest}-${target_gnu_type}_${gcc_pkg_v}_${host}.deb";
push @glibc_list, "libgcc1-${arch}-cross_${gcc_pkg_v}_all.deb";
$glibc_count += scalar @glibc_list;

foreach $check (@glibc_list) {
	$log = "checking for $check\n";
	print CYAN, $log, RESET if ($verbose >= 3);
	if (-f $check) {
		$skip_gcc++;
	}
	else {
		$log = "missing $check\n";
		print CYAN, $log, RESET if ($verbose >= 2);
	}
}

push @glibc_list2, "g++-${gcc_latest}-${target_gnu_type}_${gcc_pkg_v}_${host}.deb";
push @glibc_list2,
	"libstdc++${libc_latest}-${gcc_latest}-dev-${arch}-cross_${gcc_pkg_v}_all.deb";

$glibc_count += scalar @glibc_list2;
foreach $check (@glibc_list2) {
	$log = "checking for $check\n";
	print CYAN, $log, RESET if ($verbose >= 3);
	if (-f $check) {
		$skip_gcc++;
	}
	else {
		$log = "missing $check\n";
		print CYAN, $log, RESET if ($verbose >= 2);
	}
}

push @glibc_list3,
	"libstdc++${libc_latest}-${gcc_latest}-pic-${arch}-cross_${gcc_pkg_v}_all.deb";
push @glibc_list3,
	"libstdc++${libc_latest}-${gcc_latest}-dbg-${arch}-cross_${gcc_pkg_v}_all.deb";

$glibc_count += scalar @glibc_list3;
foreach $check (@glibc_list3) {
	$log = "checking for $check\n";
	print CYAN, $log, RESET if ($verbose >= 3);
	if (-f $check) {
		$skip_gcc++;
	}
	else {
		$log = "missing $check\n";
		print CYAN, $log, RESET if ($verbose >= 2);
	}
}

if ($skip_gcc < $glibc_count) {
	die ("Cannot find $gcc_dir\n") if ( not -d "$gcc_dir");
	chdir ($gcc_dir);
	$environ = "GCC_TARGET=$arch DEB_CROSS=yes";
	system ("$environ debian/rules control");
	# Drop depends on gcc-*-TRIPLET-base
	open (CTRL, "debian/control") or die ("Cannot read debian/control!: $!\n");
	my @ctrl=<CTRL>;
	close (CTRL);
	my @out=();
	foreach my $deps (@ctrl)
	{
		$deps =~ s/gcc-${gcc_latest}-${target_gnu_type}-base \(= \${gcc:Version}\),//g;
		push @out, $deps;
	}
	open (CTRL, ">debian/control") or die ("Cannot write debian/control!: $!\n");
	print CTRL @out;
	close (CTRL);
	$log = "Building gcc in $gcc_dir . . . (this will take a while)\n";
	print GREEN, $log, RESET if ($verbose >= 2);
	$result = 0;
	$result = system "$environ dpkg-buildpackage -b -uc -us -rfakeroot";
	if ($result != 0) {
		print RED, "$progname: Build failed.", RESET;
		($logfile) ? print GREEN, " See $logdir/$logfile for more info.\n", RESET : print "\n";
		if ($logfile)
		{
			close STDOUT;
			close STDERR;
			close BUILD;
			open STDOUT, ">&OLDOUT";
			open STDERR, ">&OLDERR";
		}
		die ("\n");
	}
	chdir ("../");
	foreach $result (@glibc_list)
	{
		$log = "$progname: Failed to create $result package\n";
		if (! -r $result)
		{
			if ($logfile)
			{
				close STDOUT;
				close STDERR;
				close BUILD;
				open STDOUT, ">&OLDOUT";
				open STDERR, ">&OLDERR";
			}
			die ($log);
		}
	}
	foreach $result (@glibc_list2)
	{
		$log = "$progname: Failed to create $result package\n";
		if (! -r $result)
		{
			if ($logfile)
			{
				close STDOUT;
				close STDERR;
				close BUILD;
				open STDOUT, ">&OLDOUT";
				open STDERR, ">&OLDERR";
			}
			die ($log);
		}
	}
	foreach $result (@glibc_list3)
	{
		$log = "$progname: Failed to create $result package\n";
		if (! -r $result)
		{
			if ($logfile)
			{
				close STDOUT;
				close STDERR;
				close BUILD;
				open STDOUT, ">&OLDOUT";
				open STDERR, ">&OLDERR";
			}
			die ($log);
		}
	}
	$log = "$gcc_vers ($gcc_pkg_v) built successfully.\n";
	print GREEN, $log, RESET if ($verbose >= 2);
	print CYAN, "Packages built when using --force are not installed automatically.", RESET if ($forcing == 1);
	if ($forcing == 0)
	{
		my $install = (join (' ', @glibc_list));
		$log = `sudo dpkg -i $install`;
		print $log if ($verbose >= 3);
		$install = (join (' ', @glibc_list2));
		$log = `sudo dpkg -i $install`;
		print $log if ($verbose >= 3);
		$install = (join (' ', @glibc_list3));
		$log = `sudo dpkg -i $install`;
		print $log if ($verbose >= 3);
	}
}
if ($logfile)
{
	close STDOUT;
	close STDERR;
	close BUILD;
	open STDOUT, ">&OLDOUT";
	open STDERR, ">&OLDERR";
}

# end : show confirmation.
die ("Force used! Toolchain might not be usable!\n") if ($forcing == 1);
print GREEN, "\nSuccess! \nInstalled toolchains:\n\n", RESET;
my $all_vers = $gcc_vers;
$all_vers =~ s/gcc/'\?\?\?'/;
$all_vers .= "* 'libstdc*' 'libc6*' 'libgcc1*' 'linux-kernel-headers*'";
my $success = `dpkg -l 'binutils*' $all_vers`;
my @s = split (/\n/, $success);
foreach my $s (@s)
{
	if ($s =~ /^ii +(.*)/)
	{
		print "$1\n";
	}
}
print "\n";
exit 0;

# subroutines.
sub check_cross()
{
	# needs a versioned check!
	# TODO the format has changed!
	$log = "checking dpkg-cross status for $_[0] on $arch . . . ";
	$status = `apt-cross -a $arch -s $_[0]`;
	if ((!$status =~ /Status: install ok installed\n/g) || (!$status))
	{
		$log .= "\n$_[0] cross-build not installed yet. Installing ...\n";
		print $log if ($verbose >= 2);
		return "$_[0] ";
	}
	else {
		$log .= "installed ok.\n$status";
	}
	print $log if ($verbose >= 2);
	return "";
}

sub split_debian()
{
	return "" if (!$_[0]);
	$_[0] =~ /([0-9\.a-z]*)-[0-9].*/;
	return $1;
}

sub cache_version()
{
	my $result;
	if ($forcing == 0)
	{
		$result = &binlookup($_[0]);
		return $result->{VerStr};
	}
	else
	{
		$result = `apt-cache show $_[0] 2>/dev/null`;
	}
	$result =~ /Version: (.*)/g;
	return $1;
}

sub check_source_version()
{
	# check the SOURCE version of the main apt cache
	my $main_src = `apt-cache showsrc $_[0] 2>/dev/null`;
	return if (!$main_src);
	$main_src =~ /Version: (.*)\n/g;
	$main_src = $1;

	# compare with the BINARY version of the arch selected.
	my $cross_src = &binlookup($_[0]);
	my $cross_ver = $cross_src->{VerStr};
	if ($main_src ne $cross_ver) {
		my $msg = "\n$progname: Error. Mismatch in source versions\n";
		$msg .= "$arch does not appear to have built version $main_src of $_[0] successfully yet.";
		$msg .= " Therefore it is unlikely that $progname will be able to build a usable cross-compiler";
		$msg .= " using the current upstream source of $_[0]. Only version $cross_ver is available on";
		$msg .= " $arch and $progname is unable to proceed.\n";
		$msg .= "Please run $progname again when the $arch port has updated.\n";
		if ($logfile)
		{
			close STDOUT;
			close STDERR;
			close BUILD;
			open STDOUT, ">&OLDOUT";
			open STDERR, ">&OLDERR";
		}
		($forcing == 0) ? die (RED, wrap('','',$msg), RESET, "\n")
			: print CYAN, wrap('','',"$msg\nForce enabled, trying to continue ...\n"), RESET;
		print GREEN, "Forced packages will not be installed by $progname.\n", RESET
			if ($forcing == 1);
	}
}

sub parse_gcc_dir()
{
	my $result;
	my $r;
	my $dir;
	if ($forcing == 0)
	{
		$result = &binlookup($gcc_vers);
		$dir = $result->{VerStr};
	}
	else
	{
		$result = `apt-cache show $gcc_vers 2>/dev/null`;
		return "" if (!$result);
		$r = $result;
		$r =~ /Version: (.*)\n/g;
		$dir = $1;
		if (! -d $dir)
		{
			$dir =~ s/-[0-9]+$//;
		}
	}
	my $dsc = "${gcc_vers}_${dir}.dsc";
	$dsc =~ s/\n//;
	my $pat = "${gcc_vers}_.*orig\.tar.*";
	my $orig;
	if ( -f $dsc)
	{
		open (F, "$dsc");
		while(<F>)
		{
			if (/$pat/)
			{
				$orig = $_;
			}
		}
	}
	if (defined $orig)
	{
		$orig =~ s/\.orig\.tar\..*//;
		$orig =~ s/.* //;
		$orig =~ s/\_/\-/;
		$orig =~ s/\n//;
		return $orig if (-d $orig);
	}
	# less favourable method.
	if (! -d $dir) {
		$r = $result;
		$r =~ /Source: $gcc_vers \((.*)\)\n/;
		my $t = &split_debian($1);
		if (-d "$gcc_vers-$t") {
			return "$gcc_vers-$t"
		}
	}
	return "$gcc_vers-$dir";
}

sub get_src_dir()
{
	my $pkg=$_[0];
	my $res = &binlookup($pkg);
	my $dir = $res->{VerStr};
	$dir =~ s/-.*\n?$//;
	return "$pkg-$dir";
}

sub uclibc_add_alias
{
	$_ = shift;
	my $src = shift;
	my $tgt = shift;
	s/^(([^!{]*\|)*static-libgcc)((\|[^:|]*)*:)/$1|uclibc$3/;
	s/^(!static-libgcc:)(.*)$/$1\%{!uclibc:$2}/;
	my $ret;
	for(;;)
	{
		my @res = extract_bracketed($_, '{}', '[^{]*');
		if(!$res[0])
		{
			$ret .= $_;
			return $ret;
		}
		$ret .= $res[2];
		my $child = $res[0];
		$child =~ s/^{(.*)}$/$1/;
		$ret .= '{' . &uclibc_add_alias($child, $src, $tgt) . '}';
		$_ = $res[1];
	}
}

sub uclibc_munge_specs
{
	print GREEN, "Converting gcc specs for use with uClibc.\n", RESET
		if ($verbose >= 1);
	my @s = split(/\n/, shift);
	my @ret = ();
	foreach my $line (@s)
	{
		if($line =~ /^\*([0-9A-Za-z_]*):$/)
		{
			push @ret, $line;
			next;
		}
		$line =~ s/(\%{I\*[^}]*})/$1 \%{uclibc:-I\/usr\/$arch\/include}/g;
		$line =~ s/(\%{L\*[^}]*})/$1 \%{uclibc:-L\/usr\/$arch\/lib}/g;
		$line =~ s/((S|g|)crt(0|1|i|n)(.o|\%O))(\%s)?/\%{uclibc:\/usr\/$arch\/lib\/$1;:$1$5}/g;
		$line =~ s/(\/lib\/ld-linux.so.2)/\%{uclibc:\/lib\/ld-uClibc.so.0;:$1}/g;
		if($line =~ /static-libgcc/)
		{
			# "uclibc" behaves like "static-libgcc"
			$line = &uclibc_add_alias($line, 'uclibc', 'static-libgcc');
		}
		push @ret, $line;
	}
	return join("\n", @ret);
}

sub uclibc_translate_triplet
{
	my $glibc_triplet = shift;
	return undef unless (defined $glibc_triplet);
	my $uclibc_triplet = $glibc_triplet;
	$uclibc_triplet =~ s/([[:alnum:]]*)-([[:alnum:]]*)-gnu(.*)/$1-$2-uclibc$3/;
	return undef if($glibc_triplet eq $uclibc_triplet);
	return $uclibc_triplet;
}

sub uclibc_build_chain
{
	my $check = &check_toolchains($arch, $target_gnu_type);
	if($check ne "true")
	{
	# need to identify the relevant gcc version from the installed toolchain.
		my @dpkg_gcc = `dpkg -l "gcc-???-$arch-linux-gnu"`;
		my %gcc_list=();
		my $choice = 0;
		foreach my $install (@dpkg_gcc)
		{
			next unless $install =~ /^ii/;
			$install =~ s/^ii\s+//;
			$install =~ s/ +/ /g;
			my @bits = split(/ /, $install);
			if ($bits[0] =~ /gcc-([0-9\.]+)\-${target_gnu_type}$/)
			{
				if ($1 > $choice)
				{
					$choice = $1;
					$gcc_list{$choice}=$bits[1];
				}
			}
		}
		die ("check_toolchains failed") if ($choice == 0);
		$gcc_latest = $choice;
		$gcc_pkg_v = $gcc_list{$choice};
		my @dpkg_binutils = `dpkg -l "binutils-$arch-linux-gnu"`;
		foreach my $install (@dpkg_gcc)
		{
			next unless $install =~ /^ii/;
			$install =~ s/^ii\s+//;
			$install =~ s/ +/ /g;
			my @bits = split(/ /, $install);
			$binutils_vers = $bits[1];
		}
		print "binutils_vers=$binutils_vers\n";
	}
	print CYAN, "Creating uclibc toolchain packages for : $target_gnu_type\n", RESET
		if ($verbose >= 1);
	my $dir = "uclibc-${target_gnu_type}";
	mkdir ($dir) if (! -d "debian");
	chdir($dir);
	`rm -rf ./debian` if (-d "debian");
	my $uclibc_target = &uclibc_translate_triplet($target_gnu_type);
	my $gcc_package = "gcc-${gcc_latest}-${uclibc_target}";
	my $binutils_pkg = "binutils-${uclibc_target}";
	my $gpp_package = "g++-${gcc_latest}-${uclibc_target}";
	print GREEN, "Building $binutils_pkg for $uclibc_target\n", RESET
		if ($verbose >= 2);
	system ("mkdir -p debian/${binutils_pkg}/usr/bin");
	system ("mkdir -p debian/${binutils_pkg}/usr/share/doc/${binutils_pkg}");
	my (@binutils) = `dpkg -L binutils-${target_gnu_type}`;
	foreach my $file (@binutils)
	{
		chomp($file);
		next unless $file =~ m:/usr/bin/${target_gnu_type}:;
		$file =~ s:/usr/bin/${target_gnu_type}-::;
		print CYAN, "symlinking : $file\n", RESET if ($verbose >= 3);
		unlink ("debian/${binutils_pkg}/usr/bin/${uclibc_target}-$file") if
			(-f "debian/${binutils_pkg}/usr/bin/${uclibc_target}-$file");
		my $e = symlink $file, "debian/${binutils_pkg}/usr/bin/${uclibc_target}-$file";
	}
	$uclibc_vers = $binutils_vers;
	mkdir ("debian/${binutils_pkg}/DEBIAN");
	open (CTRL, ">debian/control");
	print CTRL &uclibc_control_info ("$binutils_pkg");
	close (CTRL);
	system ("dpkg-gencontrol -p${binutils_pkg} -Pdebian/${binutils_pkg} ".
		"-cdebian/control");
	system ("dpkg-deb --build debian/${binutils_pkg} ..");
	`rm -rf ./debian` if (-d "debian");
	# end of binutils package.

	print GREEN, "Building $gcc_package for $uclibc_target\n", RESET
		if ($verbose >= 2);
	system ("mkdir -p debian/${gcc_package}/usr/bin");
	system ("mkdir -p debian/${gcc_package}/usr/share/doc/${gcc_package}");
	my $gcc_shell = "debian/${gcc_package}/usr/bin/${uclibc_target}-gcc";
	open(GCC, ">$gcc_shell");
	print (GCC "#!/bin/sh\n");
	print (GCC "${target_gnu_type}-gcc -uclibc \"\$\$@\"\n");
	close (GCC);
	chmod (0755, $gcc_shell);
	my @search = `${target_gnu_type}-gcc -print-search-dirs`;
	my $target_compiler;
	foreach my $dir (@search)
	{
		next unless $dir =~ /^install: /;
		$dir =~ s/install: //;
		$target_compiler = $dir;
		chomp($target_compiler);
	}
	die unless (defined $target_compiler);
	print GREEN, "Creating target_compiler directory tree : $target_compiler\n",
		RESET if ($verbose >= 3);
	system ("mkdir -p debian/${gcc_package}/${target_compiler}");
	my $gccspecs = `$arch-linux-gnu-gcc -dumpspecs`;
	my $s = &uclibc_munge_specs($gccspecs);
	open (SPECS, ">debian/${gcc_package}/${target_compiler}/specs");
	print SPECS "$s\n";
	close (SPECS);
	mkdir ("debian/${gcc_package}/DEBIAN");
	open (PREINST, ">debian/${gcc_package}/DEBIAN/preinst") or die (
	"Cannot create debian/$gcc_package/DEBIAN/preinst: $!");
	print PREINST "#!/bin/sh\n";
	print PREINST "dpkg-divert --package ${gcc_package} --add --rename --divert ${target_compiler}specs.glibc ${target_compiler}specs\n";
	close (PREINST);
	chmod (0755, "debian/${gcc_package}/DEBIAN/preinst");
	open (POSTRM, ">debian/${gcc_package}/DEBIAN/postrm");
	print POSTRM "#!/bin/sh\n";
	print POSTRM "dpkg-divert --package ${gcc_package} --rename --remove ${target_compiler}specs\n";
	close (POSTRM);
	chmod (0755, "debian/${gcc_package}/DEBIAN/postrm");
	open (CTRL, ">debian/control");
	$uclibc_vers = $gcc_pkg_v;
	print CTRL &uclibc_control_info ("$gcc_package");
	close (CTRL);
	system ("dpkg-gencontrol -p${gcc_package} -Pdebian/${gcc_package} ".
		"-cdebian/control");
	system ("dpkg-deb --build debian/${gcc_package} ..");
}

sub uclibc_control_info
{
	my ($target, $target_gcc_vers, $target_arch, $std_ver);
	$std_ver = &get_standards_version;
#	echo >>debian/$(cross_package).substvars 'target:GCCVersion=$(shell dpkg -s
# gcc-4.1-$(glibc_target) | sed -ne "s/Version: //p")'
	$target = shift;
	$target_gcc_vers = $gcc_pkg_v;
#	$target_arch = "foo";
	my $maint = defined($ENV{DEBFULLNAME}) ? $ENV{DEBFULLNAME} : "Simon Richter";
	$maint .= defined($ENV{DEBEMAIL}) ? " <".$ENV{DEBEMAIL}.">" : '<sjr@debian.org>';
	print CYAN, "changelog maintainer : $maint\n", RESET if ($verbose >= 2);
	my $pkgpath = "debian/${target}";
	my $docpath = "/usr/share/doc/${target}/";
	unlink ("${pkgpath}${docpath}/changelog");
	unlink ("${pkgpath}${docpath}/copyright");
	&uclibc_changelog("$target");
	open (COPY, "<debian/changelog");
	my @c=<COPY>;
	close (COPY);
	open (CLOG, ">${pkgpath}${docpath}/changelog");
	print CLOG @c;
	close (CLOG);
	unlink ("${pkgpath}${docpath}/changelog.gz");
	`gzip -9 "${pkgpath}${docpath}/changelog"`;
	print CYAN, "Auto-generating copyright information.\n", RESET if ($verbose >= 2);
	open (COPY, ">${pkgpath}${docpath}/copyright");
	print COPY &uclibc_copyright;
	close (COPY);
	my $ctrl = qq%Source: ${target}
Section: devel
Priority: extra
Maintainer: $maint
Build-Depends: debhelper (>= 5)
Standards-Version: $std_ver

Package: ${target}
Architecture: all%;
	if ($target =~ /^binutils/)
	{
		$ctrl .= qq%
Depends: binutils-${target_gnu_type}
Description: binutils support for building against uClibc
 This package provides wrapper scripts for the toolchain, providing an
 alternate host triplet for building against uclibc.
%;
	}
	if ($target =~ /^gcc/)
	{
		$ctrl .= qq%
Depends: gcc-${gcc_latest}-${target_gnu_type} (= ${gcc_pkg_v}),
 libuclibc-dev-${arch}-cross, binutils-${arch}-linux-uclibc
Description: Toolchain alias for building against uclibc
 This package provides wrapper scripts for the toolchain, providing an
 alternate host triplet for building against uclibc.
%;
	}
	return $ctrl;
}

sub uclibc_changelog
{
	my $name = defined($ENV{DEBFULLNAME}) ? $ENV{DEBFULLNAME} : "Simon Richter";
	my $email .= defined($ENV{DEBEMAIL}) ? $ENV{DEBEMAIL} : 'sjr@debian.org';
	my $maint = "$name <$email>";
	my $cross_package = shift;
	# dch doesn't support entirely automated changelogs from new :-(
	open (CLOG, ">debian/changelog");
	print CLOG "$cross_package ($uclibc_vers) UNRELEASED; urgency=low\n\n";
	print CLOG "  * Automated package.\n";
	print CLOG " -- $maint  Sat, 29 Mar 2008 14:22:41 +0000\n";
	close (CLOG);
	# ensure the timestamp is updated.
	`DEBFULLNAME="$name" DEBEMAIL="$email" dch -r automated package`;
}

sub uclibc_copyright
{
	my $copy = qq%This package was automatically generated by emdebian-tools for use with uClibc.

Files: *
Licence: GPL-2+
Copyright: 2007-2008 Neil Williams <codehelp\@debian.org>
           2007-2008 Simon Richter <sjr\@debian.org>

   This package 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; version 2 dated June, 1991, or
   (at your option) any later version.

   This package 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 package; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
   MA 02110-1301, USA.

On Debian systems, the complete text of the GNU General
Public License can be found in `/usr/share/common-licenses/GPL-2'
%;
	return $copy;
}