# Template file for 'cross-arm-none-eabi'
_binutils_version=2.29.1
_gcc_version=7.3.0
_newlib_version=2.4.0.20161025

_triplet=arm-none-eabi
_sysroot="/usr/${_triplet}"

pkgname=cross-${_triplet}
version=0.2
revision=2
short_desc="GNU cross bare metal toolchain"
maintainer="Thomas Bernard <thomas@famillebernardgouriou.fr>"
homepage="http://www.voidlinux.eu"
license="GPL-3, GPL-2, LGPL-2.1"
distfiles="
 ${GNU_SITE}/binutils/binutils-${_binutils_version}.tar.bz2
 ${GNU_SITE}/gcc/gcc-${_gcc_version}/gcc-${_gcc_version}.tar.xz
 ftp://sources.redhat.com/pub/newlib/newlib-${_newlib_version}.tar.gz"

checksum="
 1509dff41369fb70aed23682351b663b56db894034773e6dbf7d5d6071fc55cc
 832ca6ae04636adbb430e865a1451adf6979ab44ca1c8374f61fba65645ce15c
 cbecbc637496fcec02829c12babf8441c6500eb1933c776d2fa4e0a9a35f081f"

lib32disabled=yes
nocross=yes
nopie=yes
nostrip=yes
nodebug=yes
create_wrksrc=yes

hostmakedepends="perl flex bison"
makedepends="zlib-devel gmp-devel mpfr-devel libmpc-devel isl16-devel"

_apply_patch() {
	local args="$1" pname="$(basename $2)"

	if [ ! -f ".${pname}_done" ]; then
		patch -N $args -i $2
		touch .${pname}_done
	fi
}

pre_build() {
	msg_normal "Preparing gcc source tree\n"
}

_binutils_build() {
	local _args

	[ -f ${wrksrc}/.binutils_build_done ] && return 0

	cd ${wrksrc}
	msg_normal "Building cross binutils\n"
	[ ! -d binutils-build ] && mkdir binutils-build
	cd binutils-build
	_args="--prefix=/usr"
	_args+=" --target=${_triplet}"
	_args+=" --host=${XBPS_CROSS_TRIPLET}"
	_args+=" --with-sysroot=${_sysroot}"
	_args+=" --enable-multilib"
	_args+=" --enable-interwork"
	_args+=" --with-gnu-as"
	_args+=" --with-gnu-ld"
	_args+=" --disable-nls"
	_args+=" --enable-ld=default"
	_args+=" --enable-plugins"
	_args+=" --enable-deterministic-archives"

	../binutils-${_binutils_version}/configure ${_args}

	make configure-host
	make ${makejobs}
	make DESTDIR= install

	touch ${wrksrc}/.binutils_build_done
}

_gcc_bootstrap() {
	local _args

	[ -f ${wrksrc}/.gcc_bootstrap_done ] && return 0

	cd ${wrksrc}/gcc-${_gcc_version}
	_apply_patch -p0 ${FILESDIR}/fix-cxxflags-passing.patch
	_apply_patch -p1 ${FILESDIR}/enable-with-multilib-list-for-arm.patch

	msg_normal "Building cross gcc bootstrap\n"

	[ ! -d ../gcc-bootstrap ] && mkdir ../gcc-bootstrap
	cd ../gcc-bootstrap

	_args="--prefix=/usr"
	_args+=" --target=${_triplet}"
	_args+=" --host=${XBPS_CROSS_TRIPLET}"
	_args+=" --without-headers"
	_args+=" --enable-internetwork"
	_args+=" --enable-multilib"
	_args+=" --enable-languages=c"
	_args+=" --with-newlib"
	_args+=" --disable-nls"
	_args+=" --disable-shared"
	_args+=" --disable-libquadmath"
	_args+=" --disable-decimal-float"
	_args+=" --disable-libgomp"
	_args+=" --disable-libmpx"
	_args+=" --disable-libmudflap"
	_args+=" --disable-libssp"
	_args+=" --disable-libitm"
	_args+=" --disable-libatomic"
	_args+=" --disable-threads"
	_args+=" --disable-sjlj-exceptions"
	_args+=" --with-gnu-ld"
	_args+=" --with-gnu-as"

	CFLAGS="-O0 -g0" CXXFLAGS="-O0 -g0" \
		../gcc-${_gcc_version}/configure ${_args}

	make ${makejobs} -s all-gcc
	make install-gcc

	touch ${wrksrc}/.gcc_bootstrap_done
}

_newlib_build() {
	local _args

	[ -f ${wrksrc}/.newlib_build_done ] && return 0

	cd ${wrksrc}
	msg_normal "Building cross newlib\n"

	[ ! -d newlib-build ] && mkdir newlib-build
	cd newlib-build

	export CFLAGS_FOR_TARGET="-O2 -ffunction-sections -fdata-sections"

	_args="--prefix=/usr"
	_args+=" --target=${_triplet}"
	_args+=" --host=${XBPS_CROSS_TRIPLET}"
	_args+=" --enable-interwork"
	_args+=" --with-gnu-as"
	_args+=" --with-gnu-ld"
	_args+=" --disable-nls"
	_args+=" --disable-newlib-supplied-syscalls"
	_args+=" --enable-newlib-io-long-long"
	_args+=" --enable-newlib-register-fini"

	../newlib-${_newlib_version}/configure ${_args}

	make ${makejobs}
	make install

	touch ${wrksrc}/.newlib_build_done
}

_newlib_nano_build() {
	local _args

	[ -f ${wrksrc}/.newlib_nano_build_done ] && return 0

	cd ${wrksrc}
	msg_normal "Building cross newlib nano\n"

	[ ! -d newlib-nano-build ] && mkdir newlib-nano-build
	cd newlib-nano-build

	export CFLAGS_FOR_TARGET="-Os -ffunction-sections -fdata-sections"

	_args="--prefix=/usr"
	_args+=" --target=${_triplet}"
	_args+=" --enable-interwork"
	_args+=" --with-gnu-as"
	_args+=" --with-gnu-ld"
	_args+=" --disable-nls"
	_args+=" --disable-newlib-supplied-syscalls"
	_args+=" --disable-newlib-fvwrite-in-streamio"
	_args+=" --disable-newlib-fseek-optimization"
	_args+=" --disable-newlib-wide-orient"
	_args+=" --enable-newlib-nano-malloc"
	_args+=" --disable-newlib-unbuf-stream-opt"
	_args+=" --enable-lite-exit"
	_args+=" --enable-newlib-global-atexit"
	_args+=" --enable-newlib-nano-formatted-io"
	_args+=" --enable-newlib-reent-small"

	../newlib-${_newlib_version}/configure ${_args}

	make ${makejobs}

	touch ${wrksrc}/.newlib_nano_build_done
}


_gcc_build() {

	[ -f ${wrksrc}/.gcc_build_done ] && return 0

	cd ${wrksrc}
	msg_normal "Building cross gcc newlib\n"

	[ ! -d gcc-build ] && mkdir gcc-build
	cd gcc-build

	export CFLAGS='-g -O2 -pipe -ffunction-sections -fdata-sections'
	export CXXFLAGS='-g -O2 -pipe -ffunction-sections -fdata-sections'
	_do_gcc_build

	touch ${wrksrc}/.gcc_build_done
}

_gcc_nano_build() {

	[ -f ${wrksrc}/.gcc_nano_build_done ] && return 0

	cd ${wrksrc}
	msg_normal "Building cross gcc newlib nano\n"

	[ ! -d gcc-nano-build ] && mkdir gcc-nano-build
	cd gcc-nano-build

	export CFLAGS='-g -Os -pipe -ffunction-sections -fdata-sections'
	export CXXFLAGS='-g -Os -pipe -ffunction-sections -fdata-sections'
	_do_gcc_build

	touch ${wrksrc}/.gcc_nano_build_done
}


_do_gcc_build() {
	unset LD AS CPP

	local _args

	export CC="gcc"
	_args="--prefix=/usr"
	_args+=" --host=${XBPS_CROSS_TRIPLET}"
	_args+=" --libdir=/usr/lib"
	_args+=" --libexecdir=/usr/lib"
	_args+=" --target=${_triplet}"
	_args+=" --with-sysroot=${_sysroot}"
	_args+=" --quiet"
	_args+=" --enable-languages=c,c++"
	_args+=" --with-native-system-header-dir=/include"
	_args+=" --libexecdir=/usr/lib"
	_args+=" --enable-plugins"
	_args+=" --with-gmp"
	_args+=" --with-mpfr"
	_args+=" --with-mpc"
	_args+=" --disable-decimal-float"
	_args+=" --disable-libffi"
	_args+=" --disable-libgomp"
	_args+=" --disable-libmudflap"
	_args+=" --disable-libquadmath"
	_args+=" --disable-libssp"
	_args+=" --disable-libstdcxx-pch"
	_args+=" --disable-nls"
	_args+=" --disable-shared"
	_args+=" --disable-threads"
	_args+=" --disable-tls"
	_args+=" --with-gnu-as"
	_args+=" --with-gnu-ld"
	_args+=" --with-system-zlib"
	_args+=" --with-newlib"
	_args+=" --with-headers=${_sysroot}/include"
	_args+=" --with-python-dir=share/gcc-arm-none-eabi"
	_args+=" --with-isl"
	_args+=" --with-libelf"
	_args+=" --enable-gnu-indirect-function"
	_args+=" --with-host-libstdcxx='-static-libgcc'"
	_args+=" --enable-internetwork"
	_args+=" --enable-multilib"
	_args+=" --enable-long-long"
	_args+=" --enable-c99"
	_args+=" --enable-__cxa_atexit"
	_args+=" --disable-shared"
	_args+=" --disable-werror"
	_args+=" --with-multilib-list=armv6-m,armv7-m,armv7e-m,armv7-r"

	../gcc-${_gcc_version}/configure ${_args}

	make ${makejobs} INHIBIT_LIBC_CFLAGS='-DUSE_TM_CLONE_REGISTRY=0'

}

do_build() {
	for f in include lib bin; do
		if [ ! -d ${_sysroot}/usr/${f} ]; then
			mkdir -p ${_sysroot}/usr/${f}
		fi
		if [ ! -h ${_sysroot}/${f} ]; then
			ln -sfr ${_sysroot}/usr/${f} ${_sysroot}/${f}
		fi
	done

	# Ensure we use sane environment
	unset CC CXX CPP LD AS AR RANLIB OBJDUMP READELF NM
	unset CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
	export CFLAGS="-Os" CXXFLAGS="-Os"

	_binutils_build
	_gcc_bootstrap
	_newlib_nano_build
	_newlib_build
	_gcc_build
	_gcc_nano_build
}

do_install() {
	for f in include lib bin; do
		if [ ! -d ${DESTDIR}/${_sysroot}/usr/${f} ]; then
			echo "Creating directory ${f}"
			mkdir -p ${DESTDIR}/${_sysroot}/usr/${f}
		fi
		if [ ! -h ${DESTDIR}/${_sysroot}/${f} ]; then
			echo "Linking directory ${f}"
			ln -sfr ${DESTDIR}/${_sysroot}/usr/${f} \
				${DESTDIR}/${_sysroot}/${f}
		fi
	done
	# install cross binutils
	cd ${wrksrc}/binutils-build
	make DESTDIR=${DESTDIR} install

	# install cross gcc
	cd ${wrksrc}/gcc-build
	make DESTDIR=${DESTDIR} install

	cd ${wrksrc}/gcc-nano-build
	make DESTDIR=${DESTDIR}.nano install

	# we need only libstdc nano files
	multilibs=( $(${DESTDIR}/usr/bin/${_triplet}-gcc -print-multi-lib 2>/dev/null)  )
	for multilib in "${multilibs[@]}"; do
		dir="${multilib%%;*}"
		from_dir=${DESTDIR}.nano/usr/${_triplet}/lib/$dir
		to_dir=${DESTDIR}/${_sysroot}/lib/$dir
		cp -f $from_dir/libstdc++.a $to_dir/libstdc++_nano.a
		cp -f $from_dir/libsupc++.a $to_dir/libsupc++_nano.a
	done

	# install newlib for target
	cd ${wrksrc}/newlib-nano-build
	make DESTDIR=${DESTDIR} install
	find "$DESTDIR" -regex ".*/lib\(c\|g\|rdimon\)\.a" -exec rename .a _nano.a '{}' \;

	cd ${wrksrc}/newlib-build
	make DESTDIR=${DESTDIR} install

	# strip target binaries
	find ${DESTDIR}/${_sysroot}/usr/lib \( -name "*.a" -or -name "*.o" \) -exec ${_triplet}-strip '{}' \;
	find ${DESTDIR}/usr/lib/gcc/${_triplet}/${_gcc_version} \( -name "*.a" -or -name "*.o" \) -exec ${_triplet}-strip '{}' \;

	# strip host binaries
	find ${DESTDIR}/usr/lib/gcc/${_triplet}/${_gcc_version} -type f -and \( -executable \) -exec /usr/bin/strip '{}' \;
	find ${DESTDIR}/usr/bin -type f -and \( -executable \) -exec /usr/bin/strip '{}' \;

	# Remove unnecessary stuff
	rm -rf ${DESTDIR}.nano
	rm -f ${DESTDIR}/usr/lib*/libiberty.a
	rm -rf ${DESTDIR}/usr/share
	rm -rf ${DESTDIR}/${_sysroot}/{sbin,etc,var}
	rm -rf ${DESTDIR}/${_sysroot}/usr/{sbin,share,libexec}
	rm -rf ${DESTDIR}/${_sysroot}/usr/lib/gconv
	rm -f ${DESTDIR}/usr/lib/libcc1.*
	rm -f ${DESTDIR}/${_sysroot}/libexec
}
