Declare du_flush_pipe in or1200_top
[openrisc.git] / toolchain_install_scripts / crossbuild-1.0.sh
blob945ad129f7dee1d117de6fc2240b44fac9528411
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="${CROSSBUILD_DOWNLOAD:-$PWD/crossbuild-dl}"
13 # This directory is used to extract all files and to build everything in. It
14 # must not exist before this script is invoked (as a security measure).
15 # This dir should be an absolute path
16 builddir="${CROSSBUILD_BUILD:-$PWD/crossbuild-build}"
18 # will append the target string to the prefix dir mentioned here
19 # Note that the user running this script must be able to do make install in
20 # this given prefix directory. Also make sure that this given root dir
21 # exists.
22 prefix="${CROSSBUILD_PREFIX:-/usr/local}"
24 # This script needs to use GNU Make. On Linux systems, GNU Make is invoked
25 # by running the "make" command, on most BSD systems, GNU Make is invoked
26 # by running the "gmake" command. Set the "make" variable accordingly.
27 if [ -f "`which gmake 2>/dev/null`" ]; then
28 make="gmake"
29 else
30 make="make"
33 ##############################################################################
34 # Variables:
36 target="or32-elf"
38 releasever="or32-1.0rc1"
39 releasever_gcc="or32-1.0rc2"
41 binutilsver="2.20.1"
42 binutils="binutils-$binutilsver.tar.bz2"
43 binutils_url="http://mirrors.kernel.org/sources.redhat.com/binutils/releases"
44 binutils_patch="binutils-$binutilsver-$releasever.patch.bz2"
45 binutils_patch_url="ftp://ocuser:oc@opencores.org/toolchain"
47 newlibver="1.18.0"
48 newlib="newlib-$newlibver.tar.gz"
49 newlib_url="http://mirrors.kernel.org/sourceware/newlib"
50 newlib_patch="newlib-$newlibver-$releasever.patch.bz2"
51 newlib_patch_url="ftp://ocuser:oc@opencores.org/toolchain"
53 gccver="4.5.1"
54 gcc="gcc-$gccver.tar.bz2"
55 gcc_url="http://mirrors.kernel.org/gnu/gcc/gcc-$gccver";
56 gcc_patch="gcc-$gccver-$releasever_gcc.patch.bz2"
57 gcc_patch_url="ftp://ocuser:oc@opencores.org/toolchain"
59 gdbver="7.2"
60 gdb="gdb-$gdbver.tar.bz2"
61 gdb_url="http://mirrors.kernel.org/gnu/gdb";
62 gdb_patch="gdb-$gdbver-$releasever.patch.bz2"
63 gdb_patch_url="ftp://ocuser:oc@opencores.org/toolchain"
65 # Options passed to all GNU tools during configure
66 or32configure="--disable-checking --enable-fast-install=N/A --disable-libssp --enable-languages=c,c++ --with-or1ksim=$prefix/or1ksim --with-newlib"
68 simver="0.5.0rc2"
69 sim="or1ksim-$simver.tar.bz2"
70 sim_url="ftp://ocuser:oc@opencores.org/toolchain"
72 linuxver="2.6.35"
73 linux="linux-$linuxver.tar.bz2"
74 linux_url="http://www.kernel.org/pub/linux/kernel/v2.6"
75 linux_patch="linux-$linuxver-or32.patch.bz2"
76 linux_patch_url="ftp://ocuser:oc@opencores.org/toolchain"
78 uclibcver="0.9.31"
79 uclibc="uClibc-$uclibcver.tar.bz2"
80 uclibc_url="http://www.uclibc.org/downloads"
81 uclibc_patch="uClibc-$uclibcver-or32.patch.bz2"
82 uclibc_patch_url="ftp://ocuser:oc@opencores.org/toolchain"
83 uclibc_config="extra/Configs/defconfigs/or32"
85 # These are the tools this script requires and depends upon.
86 reqtools="gcc bzip2 make patch file makeinfo"
88 ##############################################################################
89 # Functions:
91 findtool(){
92 file="$1"
94 IFS=":"
95 for path in $PATH
97 # echo "Checks for $file in $path" >&2
98 if test -f "$path/$file"; then
99 echo "$path/$file"
100 return
102 done
105 getfile() {
106 # $1 file
107 # $2 URL
109 tool=`findtool curl`
110 if test -z "$tool"; then
111 tool=`findtool wget`
112 if test -n "$tool"; then
113 # wget download
114 echo "CROSSBUILD: Downloading $2/$1 using wget"
115 $tool -O "$dlwhere/$1" "$2/$1"
117 else
118 # curl download
119 echo "CROSSBUILD: Downloading $2/$1 using curl"
120 $tool -Lo "$dlwhere/$1" "$2/$1"
123 if [ $? -ne 0 ] ; then
124 echo "CROSSBUILD: couldn't download the file!"
125 echo "CROSSBUILD: check your internet connection"
126 exit
129 if test -z "$tool"; then
130 echo "CROSSBUILD: No downloader tool found!"
131 echo "CROSSBUILD: Please install curl or wget and re-run the script"
132 exit
136 extract_tool() {
137 toolname="$1"
138 version="$2"
139 file="$3"
141 cd $builddir
143 # if [ ! -d $toolname-$version ]; then
144 echo "CROSSBUILD: extracting $file"
145 if [ `file $dlwhere/$file | grep -c bzip2` -ne 0 ]; then
146 tar xjf $dlwhere/$file
148 if [ `file $dlwhere/$file | grep -c gzip` -ne 0 ]; then
149 tar xzf $dlwhere/$file
151 # else
152 # echo "CROSSBUILD: $file already extracted"
153 # fi
157 patch_tool() {
158 toolname="$1"
159 version="$2"
160 patch="$3"
162 # do we have a patch?
163 if test -n "$patch"; then
164 echo "CROSSBUILD: applying patch $patch"
166 # apply the patch
167 (cd $builddir/$toolname-$version && bzcat "$dlwhere/$patch" | patch -p1)
169 # check if the patch applied cleanly
170 if [ $? -gt 0 ]; then
171 echo "CROSSBUILD: failed to apply patch $patch"
172 exit
179 download_extract_patch () {
180 toolname="$1"
181 version="$2"
182 file="$3"
183 file_url="$4"
184 patch="$5"
185 patch_url="$6"
187 if test -f "$dlwhere/$file"; then
188 echo "CROSSBUILD: $file already downloaded"
189 else
190 getfile "$file" "$file_url"
193 if test -n "$patch"; then
194 if test -f "$dlwhere/$patch"; then
195 echo "CROSSBUILD: $patch already downloaded"
196 else
197 getfile "$patch" "$patch_url"
201 extract_tool "$toolname" "$version" "$file"
203 patch_tool "$toolname" "$version" "$patch"
207 build_gnu_tool() {
208 toolname="$1"
209 version="$2"
210 file="$3"
211 file_url="$4"
212 patch="$5"
213 patch_url="$6"
214 configure_prefix="$7"
215 configure_params="$8"
217 download_extract_patch "$toolname" "$version" "$file" "$file_url" "$patch" \
218 "$patch_url"
220 cd $builddir
222 echo "CROSSBUILD: mkdir build-$toolname"
223 mkdir -p build-$toolname
224 echo "CROSSBUILD: cd build-$toolname"
225 cd build-$toolname
226 echo "CROSSBUILD: $toolname/configure"
227 echo "CROSSBUILD: "../$toolname-$version/configure --target=$target \
228 --prefix=$configure_prefix \
229 --with-pkgversion=$releasever-built-`date +%Y%m%d` \
230 --with-bugurl=http://www.opencores.org/ \
231 "$configure_params"
232 ../$toolname-$version/configure --target=$target \
233 --prefix=$configure_prefix \
234 --with-pkgversion=$releasever-built-`date +%Y%m%d` \
235 --with-bugurl=http://www.opencores.org/ \
236 $configure_params
237 echo "CROSSBUILD: $toolname/make"
238 $make
239 echo "CROSSBUILD: $toolname/make install"
240 $make install
243 # For now, just prep the GCC path, extract newlib, link in newlib and libgloss
244 # paths.
245 prepare_gcc() {
246 prepare_gccver="$1"
247 file="$2"
248 newlibver="$3"
249 file_url="$4"
250 patch="$5"
251 patch_url="$6"
253 download_extract_patch "newlib" "$newlibver" "$file" "$file_url" "$patch" \
254 "$patch_url"
256 cd $builddir
258 echo "CROSSBUILD: creating GCC source directory"
260 mkdir -p gcc-$prepare_gccver
262 echo "CROSSBUILD: linking newlib and libgloss into GCC source directory"
263 if [ ! -L gcc-$prepare_gccver/newlib ]; then
264 ln -s $PWD/newlib-$newlibver/newlib $PWD/gcc-$prepare_gccver/newlib
267 if [ ! -L gcc-$prepare_gccver/libgloss ]; then
268 ln -s $PWD/newlib-$newlibver/libgloss $PWD/gcc-$prepare_gccver/libgloss
270 echo "CROSSBUILD: newlib and libgloss linked into GCC source directory"
273 # Move newlib libraries and includes to own sysroot
274 post_gcc_install() {
276 mkdir -p ${prefix}/or32-elf/newlib
277 rm -rf ${prefix}/or32-elf/newlib-include
279 if [ -d ${prefix}/or32-elf/include ]
280 then
281 mv ${prefix}/or32-elf/include \
282 ${prefix}/or32-elf/newlib-include
285 if [ -d ${prefix}/or32-elf/lib ]
286 then
287 afiles=`ls -1 ${prefix}/or32-elf/lib | grep '\.a' | head -1`
289 if [ "x$afiles" != "x" ]
290 then
291 mv ${prefix}/or32-elf/lib/*.a ${prefix}/or32-elf/newlib
295 if [ -f ${prefix}/or32-elf/lib/crt0.o ]
296 then
297 mv ${prefix}/or32-elf/lib/crt0.o ${prefix}/or32-elf/newlib
301 # Download, patch kernel, install headers to $prefix/$target
302 install_linux_headers() {
303 file="$linux"
304 file_url="$linux_url"
305 patch="$linux_patch"
306 patch_url="$linux_patch_url"
307 toolname="linux"
308 version=$linuxver
310 download_extract_patch "$toolname" "$version" "$file" "$file_url" "$patch" \
311 "$patch_url"
313 cd $builddir/linux-$linuxver
315 echo "CROSSBUILD: defconfig kernel"
316 $make ARCH=or32 defconfig
317 echo "CROSSBUILD: installing headers to $prefix/or32-elf"
318 $make INSTALL_HDR_PATH=$prefix/$target headers_install
321 build_uclibc () {
322 file="$uclibc"
323 file_url="$uclibc_url"
324 patch="$uclibc_patch"
325 patch_url="$uclibc_patch_url"
326 toolname="uClibc"
327 version="$uclibcver"
329 download_extract_patch "$toolname" "$version" "$file" "$file_url" "$patch" \
330 "$patch_url"
332 cd $builddir
334 cd uClibc-$uclibcver
335 # Substitute appropriate paths for kernel headers and install path
336 echo "CROSSBUILD: configuring .config for install path"
337 # Create a single variable with all paths expanded
338 kheaders="KERNEL_HEADERS=\\\"$prefix\\/$target\\/include\\\""
339 # In-place SED
340 sed -i -e "s|KERNEL_HEADERS.*|$kheaders|g" $uclibc_config
341 develprefix="DEVEL_PREFIX=\\\"$prefix\\/$target\\\""
342 sed -i -e "s|DEVEL_PREFIX.*|$develprefix|g" $uclibc_config
344 echo "CROSSBUILD: defconfig uClibc"
345 $make ARCH=or32 defconfig
346 echo "CROSSBUILD: building uClibc"
347 $make
348 echo "CROSSBUILD: installing uClibc"
349 $make ARCH=or32 all
350 $make install
351 # TODO - maybe move uClibc's libc.a where GCC can find it ($prefix/$target/lib)
352 echo "CROSSBUILD: uClibc build complete"
356 ##############################################################################
357 # Code:
359 for t in $reqtools; do
360 tool=`findtool $t`
361 if test -z "$tool"; then
362 echo "CROSSBUILD: \"$t\" is required for this script to work."
363 echo "CROSSBUILD: Please install \"$t\" and re-run the script."
364 exit
366 done
368 # Verify build directory or create it
369 if test -d $builddir; then
370 if test ! -w $builddir; then
371 echo "CROSSBUILD: ERROR: No write permissions for the build directory!"
372 exit
374 else
375 mkdir -p $builddir
378 # Verify download directory or create it
379 if test -d "$dlwhere"; then
380 if ! test -w "$dlwhere"; then
381 echo "CROSSBUILD: $dlwhere exists, but doesn't seem to be writable for you"
382 exit
384 else
385 mkdir -p $dlwhere
386 if test $? -ne 0; then
387 echo "CROSSBUILD: $dlwhere is missing and we failed to create it!"
388 exit
390 echo "CROSSBUILD: $dlwhere has been created"
393 # Verify installation directory
394 if test ! -d $prefix; then
395 mkdir -p $prefix
396 if test $? -ne 0; then
397 echo "CROSSBUILD: $prefix is missing and we failed to create it!"
398 exit
401 if test ! -w $prefix; then
402 echo "CROSSBUILD: ERROR: This script is set to install in $prefix but has no write permissions for it"
403 echo "CROSSBUILD: Please fix this and re-run this script"
404 exit
407 echo "CROSSBUILD: Download dir: $dlwhere"
408 echo "CROSSBUILD: Build dir : $builddir"
409 echo "CROSSBUILD: Install dir : $prefix"
411 ### start the builds
413 build_gnu_tool "or1ksim" "$simver" "$sim" "$sim_url" "" "" "$prefix/or1ksim" "--enable-ethphy"
415 build_gnu_tool "binutils" "$binutilsver" "$binutils" "$binutils_url" "$binutils_patch" "$binutils_patch_url" "$prefix" "$or32configure"
417 PATH="$prefix/bin:${PATH}" # add binutils to $PATH for gcc build
419 prepare_gcc "$gccver" "$newlib" "$newlibver" "$newlib_url" "$newlib_patch" "$newlib_patch_url"
421 build_gnu_tool "gcc" "$gccver" "$gcc" "$gcc_url" "$gcc_patch" "$gcc_patch_url" "$prefix" "$or32configure"
423 post_gcc_install
425 build_gnu_tool "gdb" "$gdbver" "$gdb" "$gdb_url" "$gdb_patch" "$gdb_patch_url" "$prefix" "$or32configure"
427 install_linux_headers
429 build_uclibc
431 ### builds done
433 #echo "CROSSBUILD: Deleting build folder"
434 #rm -rf $builddir
436 echo ""
437 echo "CROSSBUILD: Done!"
438 echo ""
439 echo "CROSSBUILD: Remove temporary build directory, $builddir, manually"
440 echo ""
441 echo "CROSSBUILD: Now add $prefix/bin and $prefix/or1ksim/bin to your \$PATH."
442 echo ""