#!/bin/sh
# Copyright (C) 2022 Jone Bontus
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# ``Software''), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT.	IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

set -e

TARGET=arm-none-symbianelf

extract_sources ()
{
	if [ ! -d ./$3 ] ; then
		# wget $1/$2
		tar -xf $2
	fi
}

copy_sources ()
{
	if [ ! -d $2 ] ; then
		cp -Rp $1 $2
	fi
}

GCC_VERSION=10.2.0
BINUTILS_VERSION=2.33.1
GDB_VERSION=8.3.1
OSNAME=$(uname -s)

extract_sources http://ftp.gnu.org/pub/gnu/gdb gdb-$GDB_VERSION.tar.xz gdb-$GDB_VERSION
extract_sources http://gcc.gnu.org/pub/gcc/releases/gcc-$GCC_VERSION gcc-$GCC_VERSION.tar.xz gcc-$GCC_VERSION
extract_sources http://ftp.gnu.org/pub/gnu/binutils binutils-$BINUTILS_VERSION.tar.xz binutils-$BINUTILS_VERSION

PREFIX=$(pwd)/output/gcc-$GCC_VERSION
export PATH=$PATH:$PREFIX/bin
export CFLAGS="-pipe"

# Build binutils

patch --no-backup-if-mismatch --remove-empty-files --fuzz=0 --forward --strip=0 --reject-file=- < binutils-$BINUTILS_VERSION.diff

cd binutils-$BINUTILS_VERSION

./configure --target=$TARGET --prefix=$PREFIX --enable-ld --enable-gold --enable-lto --disable-nls --disable-shared \
	--enable-plugins --enable-multilib

make
make install-strip

cd ..

# Build gcc

MPC_VERSION=1.1.0
ISL_VERSION=0.22.1
GMP_VERSION=6.1.2
MPFR_VERSION=4.0.2

extract_sources http://ftp.gnu.org/pub/gnu/mpc mpc-$MPC_VERSION.tar.gz mpc-$MPC_VERSION
extract_sources http://isl.gforge.inria.fr isl-$ISL_VERSION.tar.xz isl-$ISL_VERSION
extract_sources http://ftp.gnu.org/pub/gnu/gmp gmp-$GMP_VERSION.tar.xz gmp-$GMP_VERSION
extract_sources http://ftp.gnu.org/pub/gnu/mpfr mpfr-$MPFR_VERSION.tar.xz mpfr-$MPFR_VERSION
copy_sources mpc-$MPC_VERSION gcc-$GCC_VERSION/mpc
copy_sources isl-$ISL_VERSION gcc-$GCC_VERSION/isl
copy_sources gmp-$GMP_VERSION gcc-$GCC_VERSION/gmp
copy_sources mpfr-$MPFR_VERSION gcc-$GCC_VERSION/mpfr

patch --no-backup-if-mismatch --remove-empty-files --fuzz=0 --forward --strip=0 --reject-file=- < gcc-$GCC_VERSION.diff

cd gcc-$GCC_VERSION

./configure  --target=$TARGET --prefix=$PREFIX --enable-languages="c,c++,lto" --enable-lto --with-newlib \
	--with-gnu-as --with-gnu-ld --with-dwarf2 --disable-nls --disable-libssp --disable-shared --enable-interwork --enable-multilib \
	--enable-wchar_t --enable-long-long

make
make install-strip

cd ..

# Build gdb

# Additional libraries for building GDB with MinGW64.
if [ $OSNAME != Linux ]; then
	export LIBS="-lssp"
fi

patch --no-backup-if-mismatch --remove-empty-files --fuzz=0 --forward --strip=0 --reject-file=- < gdb-$GDB_VERSION.diff

cd gdb-$GDB_VERSION

./configure --target=$TARGET --prefix=$PREFIX --disable-nls --disable-shared
make
make install
