# Template file for 'cross-${_triplet}-${_pkgname}'
_triplet=arm-none-eabi
_pkgname=newlib
pkgname=cross-${_triplet}-${_pkgname}
version=3.1.0.20181231
revision=3
build_style=gnu-configure
configure_args="
 --prefix=/usr --target=${_triplet} --host=${XBPS_CROSS_TRIPLET}
 --with-gnu-as --with-gnu-ld --disable-nls
 --disable-newlib-supplied-syscalls --enable-newlib-retargetable-locking
 --enable-interwork"
hostmakedepends="cross-arm-none-eabi-binutils cross-arm-none-eabi-gcc"
short_desc="C library intended for use on embedded systems"
maintainer="Orphaned <orphan@voidlinux.org>"
license="GPL-2.0-or-later, LGPL-2.1-or-later"
homepage="https://www.sourceware.org/newlib/"
distfiles="ftp://sourceware.org/pub/newlib/newlib-${version}.tar.gz"
checksum=9e12fea7297648b114434033ed4458755afe7b9b6c7d58123389e82bd37681c0
nostrip=yes

post_extract() {
	mkdir -p build-{newlib,nano}
}

do_configure() {
	pushd build-newlib
	export CFLAGS_FOR_TARGET="-g -O2 -ffunction-sections -fdata-sections"
	../configure ${configure_args} \
		--enable-newlib-io-long-long \
		--enable-newlib-register-fini
	popd

	pushd build-nano
	export CFLAGS_FOR_TARGET="-g -Os -ffunction-sections -fdata-sections"
	../configure ${configure_args} \
		--enable-newlib-reent-small \
		--disable-newlib-fvwrite-in-streamio \
		--disable-newlib-fseek-optimization \
		--disable-newlib-wide-orient \
		--enable-newlib-nano-malloc \
		--disable-newlib-unbuf-stream-opt \
		--enable-lite-exit \
		--enable-newlib-global-atexit \
		--enable-newlib-nano-formatted-io
	popd
}

do_build() {
	pushd build-newlib
	make ${makejobs}
	popd

	pushd build-nano
	make ${makejobs}
	popd
}

do_install() {
	pushd build-nano
	make DESTDIR=${DESTDIR} install
	find ${DESTDIR} -regex ".*/lib\(c\|g\|rdimon\)\.a" \
		-exec rename .a _nano.a '{}' \;
	install -d ${DESTDIR}/usr/${_triplet}/include/newlib-nano
	install -m644 -t ${DESTDIR}/usr/${_triplet}/include/newlib-nano \
		${DESTDIR}/usr/${_triplet}/include/newlib.h
	popd

	pushd build-newlib
	make DESTDIR=${DESTDIR} install
	popd
}
