Fix a typo.
[maemo-rb.git] / tools / rockboxdev.sh
blob99ac46157270b84d73e6782f950266c395c67385
1 #!/bin/sh
3 # Abort execution as soon as an error is encountered
4 # That way the script do not let the user think the process completed correctly
5 # and leave the opportunity to fix the problem and restart compilation where
6 # it stopped
7 set -e
9 # this is where this script will store downloaded files and check for already
10 # downloaded files
11 dlwhere="${RBDEV_DOWNLOAD:-/tmp/rbdev-dl}"
13 # will append the target string to the prefix dir mentioned here
14 # Note that the user running this script must be able to do make install in
15 # this given prefix directory. Also make sure that this given root dir
16 # exists.
17 prefix="${RBDEV_PREFIX:-/usr/local}"
19 # This directory is used to extract all files and to build everything in. It
20 # must not exist before this script is invoked (as a security measure).
21 builddir="${RBDEV_BUILD:-/tmp/rbdev-build}"
23 # This script needs to use GNU Make. On Linux systems, GNU Make is invoked
24 # by running the "make" command, on most BSD systems, GNU Make is invoked
25 # by running the "gmake" command. Set the "make" variable accordingly.
26 if [ -f "`which gmake 2>/dev/null`" ]; then
27 make="gmake"
28 else
29 make="make"
32 if [ -z $GNU_MIRROR ] ; then
33 GNU_MIRROR=http://www.nic.funet.fi/pub/gnu/ftp.gnu.org/pub/gnu
36 # These are the tools this script requires and depends upon.
37 reqtools="gcc bzip2 gzip make patch makeinfo"
39 ##############################################################################
40 # Functions:
42 findtool(){
43 file="$1"
45 IFS=":"
46 for path in $PATH
48 # echo "Checks for $file in $path" >&2
49 if test -f "$path/$file"; then
50 echo "$path/$file"
51 return
53 done
56 input() {
57 read response
58 echo $response
61 #$1 file
62 #$2 URL"root
63 getfile() {
64 if test -f $dlwhere/$1; then
65 echo "ROCKBOXDEV: Skipping download of $2/$1: File already exists"
66 return
68 tool=`findtool curl`
69 if test -z "$tool"; then
70 tool=`findtool wget`
71 if test -n "$tool"; then
72 # wget download
73 echo "ROCKBOXDEV: Downloading $2/$1 using wget"
74 $tool -O $dlwhere/$1 $2/$1
76 else
77 # curl download
78 echo "ROCKBOXDEV: Downloading $2/$1 using curl"
79 $tool -Lo $dlwhere/$1 $2/$1
82 if [ $? -ne 0 ] ; then
83 echo "ROCKBOXDEV: couldn't download the file!"
84 echo "ROCKBOXDEV: check your internet connection"
85 exit
88 if test -z "$tool"; then
89 echo "ROCKBOXDEV: No downloader tool found!"
90 echo "ROCKBOXDEV: Please install curl or wget and re-run the script"
91 exit
96 build() {
97 toolname="$1"
98 target="$2"
99 version="$3"
100 patch="$4"
101 configure_params="$5"
102 needs_libs="$6"
104 patch_url="http://www.rockbox.org/gcc"
106 case $toolname in
107 gcc)
108 file="gcc-core-$version.tar.bz2"
109 url="$GNU_MIRROR/gcc/gcc-$version"
112 binutils)
113 file="binutils-$version.tar.bz2"
114 url="$GNU_MIRROR/binutils"
117 ctng)
118 file="crosstool-ng-$version.tar.bz2"
119 url="http://crosstool-ng.org/download/crosstool-ng"
122 echo "ROCKBOXDEV: Bad toolname $toolname"
123 exit
125 esac
127 # create build directory
128 if test -d $builddir; then
129 if test ! -w $builddir; then
130 echo "ROCKBOXDEV: No write permission for $builddir"
131 exit
133 else
134 mkdir -p $builddir
137 # download source tarball
138 if test ! -f "$dlwhere/$file"; then
139 getfile "$file" "$url"
142 # download patch
143 if test -n "$patch"; then
144 if test ! -f "$dlwhere/$patch"; then
145 getfile "$patch" "$patch_url"
149 cd $builddir
151 echo "ROCKBOXDEV: extracting $file"
152 tar xjf $dlwhere/$file
154 # do we have a patch?
155 if test -n "$patch"; then
156 echo "ROCKBOXDEV: applying patch $patch"
158 # apply the patch
159 (cd $builddir/$toolname-$version && patch -p1 < "$dlwhere/$patch")
161 # check if the patch applied cleanly
162 if [ $? -gt 0 ]; then
163 echo "ROCKBOXDEV: failed to apply patch $patch"
164 exit
168 # kludge to avoid having to install GMP, MPFR and MPC, for new gcc
169 if test -n "$needs_libs"; then
170 cd "gcc-$version"
171 if (echo $needs_libs | grep -q gmp && test ! -d gmp); then
172 echo "ROCKBOXDEV: Getting GMP"
173 if test ! -f $dlwhere/gmp-4.3.2.tar.bz2; then
174 getfile "gmp-4.3.2.tar.bz2" "$GNU_MIRROR/gmp"
176 tar xjf $dlwhere/gmp-4.3.2.tar.bz2
177 ln -s gmp-4.3.2 gmp
180 if (echo $needs_libs | grep -q mpfr && test ! -d mpfr); then
181 echo "ROCKBOXDEV: Getting MPFR"
182 if test ! -f $dlwhere/mpfr-2.4.2.tar.bz2; then
183 getfile "mpfr-2.4.2.tar.bz2" "$GNU_MIRROR/mpfr"
185 tar xjf $dlwhere/mpfr-2.4.2.tar.bz2
186 ln -s mpfr-2.4.2 mpfr
189 if (echo $needs_libs | grep -q mpc && test ! -d mpc); then
190 echo "ROCKBOXDEV: Getting MPC"
191 if test ! -f $dlwhere/mpc-0.8.1.tar.gz; then
192 getfile "mpc-0.8.1.tar.gz" "http://www.multiprecision.org/mpc/download"
194 tar xzf $dlwhere/mpc-0.8.1.tar.gz
195 ln -s mpc-0.8.1 mpc
197 cd $builddir
200 echo "ROCKBOXDEV: mkdir build-$toolname"
201 mkdir build-$toolname
203 echo "ROCKBOXDEV: cd build-$toolname"
204 cd build-$toolname
206 echo "ROCKBOXDEV: $toolname/configure"
207 case $toolname in
208 ctng) # ct-ng doesnt support out-of-tree build and the src folder is named differently
209 toolname="crosstool-ng"
210 cp -r ../$toolname-$version/* ../$toolname-$version/.version .
211 ./configure --prefix=$prefix $configure_params
214 CFLAGS=-U_FORTIFY_SOURCE ../$toolname-$version/configure --target=$target --prefix=$prefix --enable-languages=c --disable-libssp --disable-docs $configure_params
216 esac
218 echo "ROCKBOXDEV: $toolname/make"
219 $make
221 echo "ROCKBOXDEV: $toolname/make install"
222 $make install
224 echo "ROCKBOXDEV: rm -rf build-$toolname $toolname-$version"
225 cd ..
226 rm -rf build-$toolname $toolname-$version
230 make_ctng() {
231 if test -f "`which ct-ng 2>/dev/null`"; then
232 ctng="ct-ng"
233 else
234 ctng=""
237 if test ! -n "$ctng"; then
238 if test ! -f "$prefix/bin/ct-ng"; then # look if we build it already
239 build "ctng" "" "1.13.2"
242 ctng=`PATH=$prefix/bin:$PATH which ct-ng`
245 build_ctng() {
246 ctng_target="$1"
247 extra="$2"
248 tc_arch="$3"
249 tc_host="$4"
251 make_ctng
253 dlurl="http://www.rockbox.org/gcc/$ctng_target"
255 # download
256 getfile "ct-ng-config" "$dlurl"
258 test -n "$extra" && getfile "$extra" "$dlurl"
260 # create build directory
261 if test -d $builddir; then
262 if test ! -w $builddir; then
263 echo "ROCKBOXDEV: No write permission for $builddir"
264 exit
266 else
267 mkdir -p $builddir
270 # copy config and cd to $builddir
271 mkdir $builddir/build-$ctng_target
272 ctng_config="$builddir/build-$ctng_target/.config"
273 cat "$dlwhere/ct-ng-config" | sed -e "s,\(CT_PREFIX_DIR=\).*,\1$prefix," > $ctng_config
274 cd $builddir/build-$ctng_target
276 $ctng "build"
278 # install extras
279 if test -e "$dlwhere/$extra"; then
280 # verify the toolchain has sysroot support
281 if test -n `cat $ctng_config | grep CT_USE_SYSROOT\=y`; then
282 sysroot=`cat $ctng_config | grep CT_SYSROOT_NAME | sed -e 's,CT_SYSROOT_NAME\=\"\([a-zA-Z0-9]*\)\",\1,'`
283 tar xf "$dlwhere/$extra" -C "$prefix/$tc_arch-$ctng_target-$tc_host/$sysroot"
287 # cleanup
288 cd $builddir
289 rm -rf $builddir/build-$ctng_target
292 ##############################################################################
293 # Code:
295 # Verify required tools
296 for t in $reqtools; do
297 tool=`findtool $t`
298 if test -z "$tool"; then
299 echo "ROCKBOXDEV: \"$t\" is required for this script to work."
300 echo "ROCKBOXDEV: Please install \"$t\" and re-run the script."
301 exit
303 done
305 echo "Download directory : $dlwhere (set RBDEV_DOWNLOAD to change)"
306 echo "Install prefix : $prefix (set RBDEV_PREFIX to change)"
307 echo "Build dir : $builddir (set RBDEV_BUILD to change)"
308 echo "Make options : $MAKEFLAGS (set MAKEFLAGS to change)"
309 echo ""
311 # Verify download directory
312 if test -d "$dlwhere"; then
313 if ! test -w "$dlwhere"; then
314 echo "ROCKBOXDEV: No write permission for $dlwhere"
315 exit
317 else
318 mkdir $dlwhere
319 if test $? -ne 0; then
320 echo "ROCKBOXDEV: Failed creating directory $dlwhere"
321 exit
326 # Verify the prefix dir
327 if test ! -d $prefix; then
328 mkdir -p $prefix
329 if test $? -ne 0; then
330 echo "ROCKBOXDEV: Failed creating directory $prefix"
331 exit
334 if test ! -w $prefix; then
335 echo "ROCKBOXDEV: No write permission for $prefix"
336 exit
339 echo "Select target arch:"
340 echo "s - sh (Archos models)"
341 echo "m - m68k (iriver h1x0/h3x0, iaudio m3/m5/x5 and mpio hd200)"
342 echo "a - arm (ipods, iriver H10, Sansa, D2, Gigabeat, etc)"
343 echo "i - mips (Jz4740 and ATJ-based players)"
344 echo "r - arm-app (Samsung ypr0)"
345 echo "separate multiple targets with spaces"
346 echo "(Example: \"s m a\" will build sh, m68k and arm)"
347 echo ""
349 selarch=`input`
350 system=`uname -s`
352 # add target dir to path to ensure the new binutils are used in gcc build
353 PATH="$prefix/bin:${PATH}"
355 for arch in $selarch
357 echo ""
358 case $arch in
359 [Ss])
360 # For binutils 2.16.1 builtin rules conflict on some systems with a
361 # default rule for Objective C. Disable the builtin make rules. See
362 # http://sourceware.org/ml/binutils/2005-12/msg00259.html
363 export MAKEFLAGS="-r $MAKEFLAGS"
364 build "binutils" "sh-elf" "2.16.1" "" "--disable-werror"
365 build "gcc" "sh-elf" "4.0.3" "gcc-4.0.3-rockbox-1.diff"
368 [Ii])
369 build "binutils" "mipsel-elf" "2.17" "" "--disable-werror"
370 patch=""
371 if [ "$system" = "Interix" ]; then
372 patch="gcc-4.1.2-interix.diff"
374 build "gcc" "mipsel-elf" "4.1.2" "$patch"
377 [Mm])
378 build "binutils" "m68k-elf" "2.20.1" "" "--disable-werror"
379 build "gcc" "m68k-elf" "4.5.2" "" "--with-arch=cf" "gmp mpfr mpc"
382 [Aa])
383 binopts=""
384 gccopts=""
385 case $system in
386 Darwin)
387 binopts="--disable-nls"
388 gccopts="--disable-nls"
390 esac
391 build "binutils" "arm-elf-eabi" "2.20.1" "binutils-2.20.1-ld-thumb-interwork-long-call.diff" "$binopts --disable-werror"
392 build "gcc" "arm-elf-eabi" "4.4.4" "rockbox-multilibs-noexceptions-arm-elf-eabi-gcc-4.4.2_1.diff" "$gccopts" "gmp mpfr"
394 [Rr])
395 build_ctng "ypr0" "alsalib.tar.gz" "arm" "linux-gnueabi"
398 echo "ROCKBOXDEV: Unsupported architecture option: $arch"
399 exit
401 esac
402 done
404 echo ""
405 echo "ROCKBOXDEV: Done!"
406 echo ""
407 echo "ROCKBOXDEV: Make sure your PATH includes $prefix/bin"
408 echo ""