summaryrefslogtreecommitdiff
path: root/toolchain/build-toolchain
blob: c872e6520559331959210f1abb6aba087fb219db (plain)
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
#!/usr/bin/perl
use strict;
use warnings;
use FindBin qw($Bin $Script);
use File::Path;

my $GNU_MIRROR = 'ftp://ftp.fu-berlin.de/unix/gnu';
my $NEWLIB_URL = 'ftp://sources.redhat.com/pub/newlib';

my $GCC_VERSION = '4.6.3';
my $GDB_VERSION = '7.4';
my $BINUTILS_VERSION = '2.22';
my $NEWLIB_VERSION = '1.20.0';
my $OPENOCD_VERSION = '0.5.0';

my $GCC_URL = "$GNU_MIRROR/gcc/gcc-$GCC_VERSION";
my $GDB_URL = "$GNU_MIRROR/gdb";
my $BINUTILS_URL = "$GNU_MIRROR/binutils";

my $issue = 'this OS';
if (open I, "</etc/issue") {
	$issue = <I>;
	close I;

	$issue =~ s/\\.*$//g;
	$issue =~ s/\s+$//g;
}

if ($issue =~ /^Ubuntu/) {

	if ($issue !~ /^Ubuntu 11.10/) {
		warn "Notice that this script hasn't been tested on $issue, if it fails, try Ubuntu 11.10 in stead\n";
	}

	my @missing;
	for my $p qw'build-essential zlib1g-dev libgmp-dev libmpfr-dev libmpc-dev texinfo libftdi-dev libncurses5-dev libreadline-dev libexpat1-dev' {
		print "Checking that you have $p\n";
		push @missing, $p if system("dpkg -s $p > /dev/null");
	}

	if (@missing) {
		my $cmd = "sudo apt-get install ".join(' ', @missing);
		print "You're missing some packages, trying to fix that with: $cmd\n";
		system($cmd) and die "Failed to install missing packages, please run this command manually: $cmd";		
	}

} else {
	warn "Notice that this script hasn't been tested on $issue, if it fails, try Ubuntu 11.10 in stead\n";
}

my @DIRS = (
    {
	dir => "binutils-$BINUTILS_VERSION",
	url => $BINUTILS_URL,
	cfg => ['--target=arm-none-eabi', '--enable-interwork', '--enable-multilib'],
	make => 'make all install',
    },

    {
	dir => "gcc-$GCC_VERSION",
	url => $GCC_URL,
	cfg => ['--target=arm-none-eabi', '--enable-interwork', '--enable-multilib', '--enable-languages=c,c++',
		'--with-newlib',
		"--with-headers=../../src/newlib-$NEWLIB_VERSION/newlib/libc/include",
		'--with-system-zlib',
		'--with-cpu=cortex-m3', '--with-mode=thumb',
	    ],
	make=>'make all-gcc install-gcc',

	make2=>'make all install',
    },

    {
	dir => "gcc-core-$GCC_VERSION",
	url => $GCC_URL,
    },

    {
	dir => "gcc-g++-$GCC_VERSION",
	url => $GCC_URL,
    },

    {
	dir => "newlib-$NEWLIB_VERSION",
	file => "newlib-$NEWLIB_VERSION.tar.gz",
	url => $NEWLIB_URL,

	cfg => ['--target=arm-none-eabi', '--enable-interwork', '--enable-multilib',
		'--with-cpu=cortex-m3', '--with-mode=thumb', '--disable-newlib-supplied-syscalls'
	    ],
	make => 'make all install',
    },

    {
	dir => "gdb-$GDB_VERSION",
	url => $GDB_URL,
	
	cfg2=>['--target=arm-none-eabi', '--enable-interwork', '--enable-multilib'],
	make2=>'make all install',
    },

    {
	dir=> "openocd-$OPENOCD_VERSION",
	url=>"http://downloads.sourceforge.net/project/openocd/openocd/$OPENOCD_VERSION/openocd-$OPENOCD_VERSION.tar.bz2?use_mirror=autoselect",

	inplace=>1,
	patch=>'openocd-0.5.0-fix.patch',
	cfg=>['--enable-ft2232_libftdi', '--enable-jlink', 
	      '--enable-buspirate'
	    ],
	make=>'make all install',
    },
);

my $tarDir = "$Bin/tarballs";
mkdir $tarDir;

# Do all the downloading up front, so we get that out of the way.
for my $d (@DIRS) {
    $d->{file} //= "$d->{dir}.tar.bz2";

    my $fn = "$tarDir/$d->{file}";
    my $url = "$d->{url}/$d->{file}";

    if (-f $fn) {
	print "You already have $d->{file}, skipping\n";
    } else {
	system("wget", "-O", $fn, $url) and die "Failed to fetch $d->{file} from $url";
	die "Did not find the expected file: $fn" unless -f $fn;
    }
}


# Unpack, configure, build and install each package.
my $srcDir = "$Bin/src";
mkdir $srcDir;
my $buildDir = "$Bin/build";
mkdir $buildDir;
my $installDir = "$Bin/arm-toolchain";
mkdir $installDir;

$ENV{PATH} = "$ENV{PATH}:$installDir/bin";

# Unpack all at sources once.
for my $d (@DIRS) {
    my $fn = "$tarDir/$d->{file}";
    my $dn = "$srcDir/$d->{dir}";
 
    if (-d $dn) {
	print "Already unpacked $d->{dir}, skipping unpack\n";
    } else {
	print "Unpacking $fn\n";
	chdir $srcDir;
	system("tar", "xf", $fn) and die "Failed to unpack $fn into $srcDir";
	mkdir $dn;
	
	if ($d->{patch}) {
	    chdir $dn;
	    system("patch -p 1 < $Bin/$d->{patch}") and die "Failed to apply patch: $d->{patch}";
	}
    }
}

# Do the actual building, first pass
for my $d (@DIRS) {
    my $fn = "$tarDir/$d->{file}";
    my $dn = "$srcDir/$d->{dir}";
    my $bn = "$buildDir/$d->{dir}";
    
    next if -f "$bn.done"; # Lazyness ftw.
    
    rmtree $bn;
    mkdir $bn;
    if ($d->{inplace}) {
	chdir $dn; # Build in the source directory, needed for borken packages
    } else {
	chdir $bn; # Build in a separate tree, because it's nice not to mess up the source tree.
    }    

    next unless $d->{make} and $d->{cfg};

    my @cfg = (@{$d->{cfg}}, "--prefix=$installDir");
    print "Running: $bn> $dn/configure ".join(' ',@cfg)."\n";
    system("$dn/configure", @cfg) and die "Failed to configure in $bn";

    print "Running: $bn> $d->{make}\n";
    system($d->{make}) and die "Failed to run $d->{make} in $bn";  
    open D, ">$bn.done";
    close D;
}

# Second pass, needed because we need to hit gcc twice, before and after building newlib.
for my $d (@DIRS) {
    my $fn = "$tarDir/$d->{file}";
    my $dn = "$srcDir/$d->{dir}";
    my $bn = "$buildDir/$d->{dir}";
    
    next unless $d->{make2};

    next if -f "$bn.done2"; # Lazyness ftw.
    chdir $bn;
    
    if ($d->{cfg2}) {
	my @cfg = (@{$d->{cfg2}}, "--prefix=$installDir");
	print "Running2: $bn> $dn/configure ".join(' ',@cfg)."\n";
	system("$dn/configure", @cfg) and die "Failed to configure in $bn";
    }
    print "Running2: $bn> $d->{make2}\n";
    system($d->{make2}) and die "Failed to run2 $d->{make2} in $bn";  
    open D, ">$bn.done2";
    close D;
}

print "\n\nAll done, toolchain can be found in $installDir\n";
print "Add to path with: export PATH=\${PATH}:$installDir/bin\n";
exit 0;