summaryrefslogtreecommitdiff
path: root/bootstrap/stage1.sh
blob: 17076b5afd6ac9fef112131e0c5c38b70e3a43a8 (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
#!/bin/sh

cross_tools_kernel_headers () {
    src "https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-$LINUX_VER.tar.xz"

    make mrproper

    make ARCH=${ARCH} headers
    mkdir -p $CROSS_TOOLS/$TARGET/include

    cp -r usr/include/* $CROSS_TOOLS/$TARGET/include
    find $CROSS_TOOLS/$TARGET/include -name '.*.cmd' -exec rm -f {} \;
    rm $CROSS_TOOLS/$TARGET/include/Makefile
}

cross_tools_binutils () {
    src "https://ftp.gnu.org/gnu/binutils/binutils-$BINUTILS_VER.tar.xz"

    mkdir build &&
        cd build &&

    ../configure \
       --prefix=${CROSS_TOOLS} \
       --target=${TARGET} \
       --host=${HOST} \
       --with-sysroot=${CROSS_TOOLS}/${TARGET} \
       --disable-nls \
       --disable-multilib \
       --disable-werror \
       --enable-deterministic-archives \
       --disable-compressed-debug-sections &&

    make configure-host &&
    make && make install
}

cross_tools_gcc_static () {
    cd ${WD}
    src "https://ftp.gnu.org/gnu/mpfr/mpfr-$MPFR_VER.tar.xz"
    src "https://ftp.gnu.org/gnu/gmp/gmp-$GMP_VER.tar.xz"
    src "https://ftp.gnu.org/gnu/mpc/mpc-$MPC_VER.tar.gz"
    src "https://ftp.gnu.org/gnu/gcc/gcc-$GCC_VER/gcc-$GCC_VER.tar.xz"

    mv ../mpfr-$MPFR_VER mpfr &&
    mv ../gmp-$GMP_VER gmp &&
    mv ../mpc-$MPC_VER mpc &&

    mkdir build && cd  build &&

    CFLAGS='-g0 -O0' \
    CXXFLAGS=$CFLAGS \
    ../configure \
              --prefix=${CROSS_TOOLS} --build=${HOST} \
              --host=${HOST}   --target=${TARGET} \
              --with-sysroot=${CROSS_TOOLS}/${TARGET} \
              --disable-nls         --with-newlib  \
              --disable-libitm     --disable-libvtv \
              --disable-libssp     --disable-shared \
              --disable-libgomp    --without-headers \
              --disable-threads    --disable-multilib \
              --disable-libatomic  --disable-libstdcxx \
              --enable-languages=c --disable-libquadmath \
              --disable-libsanitizer --with-arch=${CPU} \
              --disable-decimal-float --enable-clocale=generic  &&

    make all-gcc all-target-libgcc &&
    make install-gcc install-target-libgcc 
}

cross_tools_musl () {
    src https://musl.libc.org/releases/musl-$MUSL_VER.tar.gz

    ./configure \
      CROSS_COMPILE=${TARGET}- \
      --prefix=/ \
      --target=${TARGET}  &&

    make && DESTDIR=${CROSS_TOOLS} make install &&

    # Add missing directory and link
    mkdir ${CROSS_TOOLS}/usr &&
    ln -s ../include  ${CROSS_TOOLS}/usr/include &&

    case $(uname -m) in
      x86_64) export ARCH="x86_64"
              ;;
      i686)   export ARCH="i386"
              ;;
      arm*)   export ARCH="arm"
              ;;
      aarch64) export ARCH="aarch64"
              ;;
    esac

    # Fix link
    rm -f ${CROSS_TOOLS}/lib/ld-musl-${ARCH}.so.1 &&
    ln -s libc.so ${CROSS_TOOLS}/lib/ld-musl-${ARCH}.so.1

    # Create link for ldd:
    ln -s ../lib/ld-musl-$ARCH.so.1 ${CROSS_TOOLS}/bin/ldd

    # Create config for dynamic library loading:
    mkdir ${CROSS_TOOLS}/etc

    echo $CROSS_TOOLS/lib >> ${CROSS_TOOLS}/etc/ld-musl-$ARCH.path 
    echo $TOOLS/lib >> ${CROSS_TOOLS}/etc/ld-musl-$ARCH.path 

    unset ARCH ARCH2
}

cross_tools_gcc_final () {
    cd ${WD}
    rm -rf *

    src "https://ftp.gnu.org/gnu/mpfr/mpfr-$MPFR_VER.tar.xz"
    src "https://ftp.gnu.org/gnu/gmp/gmp-$GMP_VER.tar.xz"
    src "https://ftp.gnu.org/gnu/mpc/mpc-$MPC_VER.tar.gz"
    src "https://ftp.gnu.org/gnu/gcc/gcc-$GCC_VER/gcc-$GCC_VER.tar.xz"

    mv ../mpfr-$MPFR_VER mpfr
    mv ../gmp-$GMP_VER gmp
    mv ../mpc-$MPC_VER mpc

    patch_gcc

# Configure in a dedicated build directory
mkdir build && cd  build &&
AR=ar LDFLAGS="-Wl,-rpath,${CROSS_TOOLS}/lib" \
../configure \
    --prefix=${CROSS_TOOLS} \
    --build=${HOST} \
    --host=${HOST} \
    --target=${TARGET} \
    --disable-multilib \
    --with-sysroot=${CROSS_TOOLS} \
    --disable-nls \
    --enable-shared \
    --enable-languages=c,c++ \
    --enable-threads=posix \
    --enable-clocale=generic \
    --enable-libstdcxx-time \
    --enable-fully-dynamic-string \
    --disable-symvers \
    --disable-libsanitizer \
    --disable-lto-plugin \
    --disable-libssp 

    # Build
    make AS_FOR_TARGET="${TARGET}-as" \
        LD_FOR_TARGET="${TARGET}-ld" &&

    # Install
    make install
}

cross_tools_file () {
    src "https://astron.com/pub/file/file-$FILE_VER.tar.gz"
    ./configure --prefix=${CROSS_TOOLS} --disable-libseccomp
    make && make install
}

for p in kernel_headers binutils gcc_static musl gcc_final file; do
    printf "${BLUE}building $p...\n${RESET}"
    cross_tools_$p || die "Failed building $p"
done
printf "${GREEN}finished building cross-tools${RESET}\n"