Merge branch 'master' into sim-target-tree
[kugel-rb.git] / tools / rockboxdev.sh
blob82f20ef98fbd48730fb8857444dacdc694aff64e
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=ftp://ftp.gnu.org/pub/gnu
36 # If detection fails, override the value of make manually:
37 # make="make"
39 ##############################################################################
42 # These are the tools this script requires and depends upon.
43 reqtools="gcc bzip2 make patch"
46 findtool(){
47 file="$1"
49 IFS=":"
50 for path in $PATH
52 # echo "Checks for $file in $path" >&2
53 if test -f "$path/$file"; then
54 echo "$path/$file"
55 return
57 done
60 input() {
61 read response
62 echo $response
65 #$1 file
66 #$2 URL"root
67 getfile() {
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
95 for t in $reqtools; do
96 tool=`findtool $t`
97 if test -z "$tool"; then
98 echo "ROCKBOXDEV: \"$t\" is required for this script to work."
99 echo "ROCKBOXDEV: Please install \"$t\" and re-run the script."
100 exit
102 done
104 ###########################################################################
105 # Verify download directory or create it
106 if test -d "$dlwhere"; then
107 if ! test -w "$dlwhere"; then
108 echo "$dlwhere exists, but doesn't seem to be writable for you"
109 exit
111 else
112 mkdir $dlwhere
113 if test $? -ne 0; then
114 echo "$dlwhere is missing and we failed to create it!"
115 exit
117 echo "$dlwhere has been created to store downloads in"
120 echo "Download directory: $dlwhere (set RBDEV_DOWNLOAD to change dir)"
121 echo "Install prefix: $prefix/[target] (set RBDEV_PREFIX to change dir)"
122 echo "Build dir: $builddir (set RBDEV_BUILD to change dir)"
124 ###########################################################################
125 # Verify that the prefix dir exists and that we can write to it,
126 # as otherwise we will hardly be able to install there!
127 if test ! -d $prefix; then
128 echo "ERROR: The installation destination does not exist."
129 echo "Please create it and re-run this script"
130 exit
132 if test ! -w $prefix; then
133 echo "ERROR: This script is set to install in $prefix but has no write permissions for it"
134 echo "Please fix this and re-run this script"
135 exit
140 cleardir () {
141 # $1 is the name of the build dir
142 # $2 is the arch
143 # delete the build dirs and the source dirs
144 echo "Cleaning up build folder"
145 rm -rf $1/build-gcc-$2 $1/build-binu-$2
148 buildone () {
150 arch=$1
151 gccpatch="" # default is no gcc patch
152 gccver="4.0.3" # default gcc version
153 binutils="2.16.1" # The binutils version to use
154 gccconfigure="" #default is nothing added to configure
155 binutilsconf="" #default is nothing added to configure
156 gcctarget="" #default make target
157 gccinstalltarget="install" #default install target
159 system=`uname -s`
160 gccurl="http://www.rockbox.org/gcc"
162 case $arch in
163 [Ss])
164 target="sh-elf"
165 gccpatch="gcc-4.0.3-rockbox-1.diff"
167 [Mm])
168 target="m68k-elf"
169 gccver="3.4.6"
170 case $system in
171 CYGWIN* | Darwin | FreeBSD | Interix | SunOS)
172 gccpatch="gcc-3.4.6.patch"
174 Linux)
175 machine=`uname -m`
176 case $machine in
177 x86_64)
178 gccpatch="gcc-3.4.6-amd64.patch"
180 esac
183 echo "Unknown host system $system detected - check necessity of" \
184 "patch for ${target}-gcc $gccver and add it."
185 echo "Press enter to continue..."
186 read ignore
188 esac
190 [Aa])
191 target="arm-elf"
192 gccpatch="rockbox-multilibs-arm-elf-gcc-4.0.3_3.diff"
194 [Ee])
195 target="arm-elf-eabi"
196 gccpatch="rockbox-multilibs-noexceptions-arm-elf-eabi-gcc-4.4.2_1.diff"
197 gccver="4.4.3"
198 # needed to build a bare-metal gcc-4.4.2
199 gcctarget="all-gcc all-target-libgcc"
200 gccinstalltarget="install-gcc install-target-libgcc"
201 binutils="2.20.1"
203 [Ii])
204 target="mipsel-elf"
205 gccver="4.1.2"
206 binutils="2.17"
207 gccconfigure="--disable-libssp"
208 binutilsconf="--disable-werror"
209 # necessary to make binutils build on gcc 4.3+
210 case $system in
211 Interix)
212 gccpatch="gcc-4.1.2-interix.diff"
216 esac
219 echo "An unsupported architecture option: $arch"
220 exit
222 esac
224 bindir="$prefix/$target/bin"
225 if test -n $pathadd; then
226 pathadd="$pathadd:$bindir"
227 else
228 pathadd="$bindir"
231 echo ""
232 echo "In case you encounter a slow internet connection, you can use an alternative mirror."
233 echo "A list of other GNU mirrors is available here: http://www.gnu.org/prep/ftp.html"
234 echo ""
235 echo "Usage: GNU_MIRROR=[URL] ./rockboxdev.sh"
236 echo ""
237 echo "Example:"
238 echo "$ GNU_MIRROR=http://mirrors.kernel.org/gnu ./rockboxdev.sh"
239 echo ""
241 if test -f "$dlwhere/binutils-$binutils.tar.bz2"; then
242 echo "binutils $binutils already downloaded"
243 else
244 getfile binutils-$binutils.tar.bz2 $GNU_MIRROR/binutils
247 if test -f "$dlwhere/gcc-core-$gccver.tar.bz2"; then
248 echo "gcc $gccver already downloaded"
249 else
250 getfile gcc-core-$gccver.tar.bz2 $GNU_MIRROR/gcc/gcc-$gccver
253 if test -n "$gccpatch"; then
254 if test -f "$dlwhere/$gccpatch"; then
255 echo "$gccpatch already downloaded"
256 else
257 getfile "$gccpatch" "$gccurl"
261 ###########################################################################
262 # If there's already a build dir, we don't overwrite or delete it
263 if test -d $builddir; then
264 if test ! -w $builddir; then
265 echo "ERROR: No write permissions for the build directory!"
266 exit
268 else
269 mkdir -p $builddir
272 cd $builddir
274 ###########################################################################
275 # Create a summary file for each toolchain, containing info about the version
276 # and a remainder to append the compiler path to PATH
278 summary="summary-$1"
280 echo "============================ Summary ============================" > $summary
281 echo "target: $target" >> $summary
282 echo "gcc version: $gccver" >> $summary
283 if test -n "$gccpatch"; then
284 echo "gcc patch: $gccpatch" >> $summary
286 echo "binutils: $binutils" >> $summary
287 echo "installation target: $prefix/$target" >> $summary
288 echo "" >> $summary
289 echo "When done, append $bindir to PATH" >> $summary
290 echo "=================================================================" >> $summary
292 cat $summary
294 echo "ROCKBOXDEV: extracting binutils-$binutils in $builddir"
295 bunzip2 < $dlwhere/binutils-$binutils.tar.bz2 | tar xf -
296 echo "ROCKBOXDEV: extracting gcc-$gccver in $builddir"
297 bunzip2 < $dlwhere/gcc-core-$gccver.tar.bz2 | tar xf -
299 if test -n "$gccpatch"; then
300 echo "ROCKBOXDEV: applying gcc patch"
301 # apply the patch and hope it runs well - don't be dependant on the
302 # exact gcc version, thus strip the gcc folder
303 (cd $builddir/gcc-$gccver && patch -p1 < "$dlwhere/$gccpatch")
304 if [ $? -gt 0 ]; then # check if the applied cleanly
305 echo "ROCKBOXDEV: failed to apply the gcc patch"
306 exit
310 echo "ROCKBOXDEV: mkdir build-binu-$1"
311 mkdir build-binu-$1
312 echo "ROCKBOXDEV: cd build-binu-$1"
313 cd build-binu-$1
314 echo "ROCKBOXDEV: binutils/configure"
315 # we undefine _FORTIFY_SOURCE to make the binutils built run fine on recent
316 # Ubuntu installations
317 CFLAGS=-U_FORTIFY_SOURCE ../binutils-$binutils/configure --target=$target --prefix=$prefix/$target $binutilsconf
318 echo "ROCKBOXDEV: binutils/make"
319 # We add -r when building binutils to fix compilation on OSX - Apple's make has
320 # extra built-ins which cause problems.
321 $make -r
322 echo "ROCKBOXDEV: binutils/make install to $prefix/$target"
323 $make install
324 cd .. # get out of build-binu-$1
325 PATH="$bindir:${PATH}"
326 SHELL=/bin/sh # seems to be needed by the gcc build in some cases
328 echo "ROCKBOXDEV: mkdir build-gcc-$1"
329 mkdir build-gcc-$1
330 echo "ROCKBOXDEV: cd build-gcc-$1"
331 cd build-gcc-$1
332 echo "ROCKBOXDEV: gcc/configure"
333 # we undefine _FORTIFY_SOURCE to make the gcc build go through fine on
334 # recent Ubuntu installations
335 CFLAGS=-U_FORTIFY_SOURCE ../gcc-$gccver/configure --target=$target --prefix=$prefix/$target --enable-languages=c $gccconfigure
336 echo "ROCKBOXDEV: gcc/make"
337 $make $gcctarget
338 echo "ROCKBOXDEV: gcc/make install to $prefix/$target"
339 $make $gccinstalltarget
340 cd .. # get out of build-gcc
341 cd .. # get out of $builddir
343 } # buildone()
345 echo ""
346 echo "Select target arch:"
347 echo "s - sh (Archos models)"
348 echo "m - m68k (iriver h1x0/h3x0, ifp7x0 and iaudio)"
349 echo "a - arm (ipods, iriver H10, Sansa, etc)"
350 echo "e - arm-eabi (same as above, new testing toolchain)"
351 echo "i - mips (Jz4740 and ATJ-based players)"
352 echo "separate multiple targets with spaces"
353 echo "(Example: \"s m a\" will build sh, m86k and arm)"
354 echo ""
356 selarch=`input`
358 for arch in $selarch
360 echo ""
361 case $arch in
362 [Ss])
363 buildone $arch
364 cleardir $builddir $arch
366 [Ii])
367 buildone $arch
368 cleardir $builddir $arch
370 [Mm])
371 buildone $arch
372 cleardir $builddir $arch
374 [Aa])
375 buildone $arch
376 cleardir $builddir $arch
378 [Ee])
379 buildone $arch
380 cleardir $builddir $arch
383 echo "An unsupported architecture option: $arch"
384 exit
386 esac
387 done
389 # show the summaries:
390 cat $builddir/summary-*
391 rm $builddir/summary-*
393 echo ""
394 echo "Done!"
395 echo ""
396 echo "Make your PATH include $pathadd"
397 echo ""