Cleanup gitignore.
[Qnixx.git] / tools / cross.sh
blobfae955a8daf721496b650d7cb7948094bae694b2
1 #!/usr/bin/env bash
2 ################################################################
3 # A script to build a GNU GCC and Binutils cross-compiler
4 # Based on https://wiki.osdev.org/GCC_Cross-Compiler
6 # Copyright (c) 2022, the Native authors.
7 # SPDX-License-Identifier: BSD-2-Clause
8 ################################################################
10 ################################################################
11 # Configuration
12 ################################################################
14 # Exit if there is an error
15 set -e
17 # Target to use
18 TARGET=x86_64-elf
20 # Versions to build
21 # Always use the latest working version (test before updating)
22 BUT_VER=2.39
23 GCC_VER=12.2.0
25 # Tar file extension to use
26 # Always use the one with the smallest file size (check when updating version)
27 BUT_EXT=xz
28 GCC_EXT=xz
30 # Multicore builds
31 # Currrently automatic using nproc
32 CORES=$(($(nproc) + 1))
33 LOAD=$(nproc)
35 PREFIX="$(pwd)/cross"
36 export PATH="$PREFIX/bin:$PATH"
38 echo "Building $TARGET Binutils $BUT_VER and GCC $GCC_VER..."
39 echo "Cores: $CORES, load: $LOAD"
41 ################################################################
42 # Source Tarballs
43 ################################################################
45 BUT_TARBALL=binutils-$BUT_VER.tar.$BUT_EXT
46 GCC_TARBALL=gcc-$GCC_VER.tar.$GCC_EXT
48 mkdir -p buildcc
49 cd buildcc
51 # Download tarballs
52 echo "Downloading Binutils tarball..."
53 if [ ! -f $BUT_TARBALL ]; then
54 wget https://ftp.gnu.org/gnu/binutils/$BUT_TARBALL
57 echo "Downloading GCC tarball..."
58 if [ ! -f $GCC_TARBALL ]; then
59 wget https://ftp.gnu.org/gnu/gcc/gcc-$GCC_VER/$GCC_TARBALL
62 # Unzip tarballs
63 printf "%s" "Unzipping Binutils tarball"
64 tar -xf $BUT_TARBALL --checkpoint=.400
65 echo "" # Newline :~)
66 printf "%s" "Unzipping GCC tarball"
67 tar -xf $GCC_TARBALL --checkpoint=.400
69 ################################################################
70 # Building
71 ################################################################
73 echo "Removing old build directories..."
74 rm -rf buildcc-gcc build-binutils
76 # Build binutils
77 mkdir buildcc-binutils
78 cd buildcc-binutils
79 echo "Configuring Binutils..."
80 ../binutils-$BUT_VER/configure --target=$TARGET --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror
81 echo "Building Binutils..."
82 make -j$CORES -l$LOAD
83 echo "Installing Binutils..."
84 make install -j$CORES -l$LOAD
85 cd ..
87 # Build gcc
88 cd gcc-$GCC_VER
89 echo "Downloading prerequisites for GCC..."
90 contrib/download_prerequisites
91 cd ..
92 mkdir buildcc-gcc
93 cd buildcc-gcc
94 echo "Configuring GCC..."
95 ../gcc-$GCC_VER/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c --without-headers
96 echo "Building all-gcc..."
97 make all-gcc -j$CORES -l$LOAD
98 echo "Building all-target-libgcc..."
99 make all-target-libgcc -j$CORES -l$LOAD
100 echo "Installing GCC..."
101 make install-gcc -j$CORES -l$LOAD
102 echo "Installing target-libgcc..."
103 make install-target-libgcc -j$CORES -l$LOAD
104 cd ../..
106 echo "Removing build directory..."
107 rm -rf buildcc
109 echo "Build complete, binaries are in $PREFIX/bin"
111 ################################################################
112 # Basic Testing (just prints info for now)
113 ################################################################
115 echo "Testing GCC..."
116 $TARGET-gcc -v
118 echo "Testing LD..."
119 $TARGET-ld -v
121 echo "Done!"