1#!/bin/bash
2
3set -e
4
5TARGET=nios2-linux-gnu
6LINUX_ARCH=nios2
7
8J=$(expr $(nproc) / 2)
9TOOLCHAIN_INSTALL=/usr/local
10TOOLCHAIN_BIN=${TOOLCHAIN_INSTALL}/bin
11CROSS_SYSROOT=${TOOLCHAIN_INSTALL}/$TARGET/sys-root
12
13export PATH=${TOOLCHAIN_BIN}:$PATH
14
15#
16# Grab all of the source for the toolchain bootstrap.
17#
18
19wget https://ftp.gnu.org/gnu/binutils/binutils-2.37.tar.xz
20wget https://ftp.gnu.org/gnu/gcc/gcc-11.2.0/gcc-11.2.0.tar.xz
21wget https://ftp.gnu.org/gnu/glibc/glibc-2.34.tar.xz
22wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.10.70.tar.xz
23
24tar axf binutils-2.37.tar.xz
25tar axf gcc-11.2.0.tar.xz
26tar axf glibc-2.34.tar.xz
27tar axf linux-5.10.70.tar.xz
28
29mv binutils-2.37 src-binu
30mv gcc-11.2.0 src-gcc
31mv glibc-2.34 src-glibc
32mv linux-5.10.70 src-linux
33
34mkdir -p bld-hdr bld-binu bld-gcc bld-glibc
35mkdir -p ${CROSS_SYSROOT}/usr/include
36
37#
38# Install kernel and glibc headers
39#
40
41cd src-linux
42make headers_install ARCH=${LINUX_ARCH} INSTALL_HDR_PATH=${CROSS_SYSROOT}/usr
43cd ..
44
45cd bld-hdr
46../src-glibc/configure --prefix=/usr --host=${TARGET}
47make install-headers DESTDIR=${CROSS_SYSROOT}
48touch ${CROSS_SYSROOT}/usr/include/gnu/stubs.h
49cd ..
50
51#
52# Build binutils
53#
54
55cd bld-binu
56../src-binu/configure --disable-werror \
57  --prefix=${TOOLCHAIN_INSTALL} --with-sysroot --target=${TARGET}
58make -j${J}
59make install
60cd ..
61
62#
63# Build gcc, without shared libraries, because we do not yet
64# have a shared libc against which to link.
65#
66
67cd bld-gcc
68../src-gcc/configure --disable-werror --disable-shared \
69  --prefix=${TOOLCHAIN_INSTALL} --with-sysroot --target=${TARGET} \
70  --enable-languages=c --disable-libssp --disable-libsanitizer \
71  --disable-libatomic --disable-libgomp --disable-libquadmath
72make -j${J}
73make install
74cd ..
75
76#
77# Build glibc
78# There are a few random things that use c++ but we didn't build that
79# cross-compiler.  We can get away without them.  Disable CXX so that
80# glibc doesn't try to use the host c++ compiler.
81#
82
83cd bld-glibc
84CXX=false ../src-glibc/configure --prefix=/usr --host=${TARGET}
85make -j${j}
86make install DESTDIR=${CROSS_SYSROOT}
87cd ..
88