# Template file for 'klibc'
pkgname=klibc
version=2.0.11
revision=1
hostmakedepends="perl"
makedepends="kernel-libc-headers"
short_desc="Minimal libc subset for use with initramfs"
maintainer="Đoàn Trần Công Danh <congdanhqx@gmail.com>"
license="GPL-2.0-only"
homepage="https://git.kernel.org/pub/scm/libs/klibc/klibc.git/"
distfiles="https://git.kernel.org/pub/scm/libs/klibc/klibc.git/snapshot/klibc-${version}.tar.gz"
checksum=bb9c40fb9b20335fbdc44904d7c579d43e39cb0b59252e1a56ce67194fb38541

_make() {
	local _arch _mkflags=

	KCFLAGS="-g -ffile-prefix-map=$wrksrc=."
	case "$XBPS_TARGET_MACHINE" in
		i686*) _arch=i386 ;;
		x86_64*) _arch=x86_64 ;;
		arm*)
			_arch=arm
			_mkflags+=" CONFIG_AEABI=y"
			case "$XBPS_TARGET_MACHINE" in
			armv5*) _mkflags+=" CPU_ARCH=armv5te" ;;
			armv6*) _mkflags+=" CPU_ARCH=armv6" ;;
			armv7*) _mkflags+=" CPU_ARCH=armv7-a" ;;
			esac
			;;
		aarch64*) _arch=arm64 ;;
		ppc64*) _arch=ppc64 ;;
		ppc*) _arch=ppc ;;
		mips*64*) _arch=mips64 ;;
		mips*) _arch=mips ;;
		*) msg_error "not supported" ;;
	esac
	PATH=/usr/bin
	make ARCH=$_arch INSTALLROOT="$DESTDIR" V=1 \
		${CROSS_BUILD:+CROSS_COMPILE=${XBPS_CROSS_TRIPLET}-} \
		$_mkflags \
		KCFLAGS="${KCFLAGS}" \
		HOSTCC="${CC_FOR_BUILD}" \
		CONFIG_DEBUG_INFO=y \
		KLIBC_SRC="$wrksrc/usr/klibc" \
		mandir='$(prefix)/share/man' \
		SHLIBDIR=/usr/lib \
		"$@"
}

do_configure() {
	local _f
	rm -rf linux
	mkdir -p linux/include
	for _f in asm asm-generic linux; do
		ln -s "${XBPS_CROSS_BASE}/usr/include/$_f" linux/include/
	done
}

_gen_native_klcc() {
	mv klcc/klcc klcc/${XBPS_CROSS_TRIPLET}-klcc
	sed "s/${XBPS_CROSS_TRIPLET}-//" \
		klcc/klibc.config >klcc/native-klibc.config
	PATH=/usr/bin
	perl klcc/makeklcc.pl klcc/klcc.in klcc/native-klibc.config \
		/usr/bin/perl >klcc/klcc
	chmod +x klcc/klcc
}

_gen_cross_klcc() {
	local _triplet _f
	_triplet=${XBPS_TRIPLET/unknown-/}
	sed -E "/^(CROSS|CC|LD|STRIP)=/s/=/=${_triplet}-/" \
		klcc/klibc.config >klcc/cross-klibc.config
	mkdir -p /tmp/bin
	for _f in gcc ld strip; do
		ln -sf /usr/bin/$_f /tmp/bin/${_triplet}-$_f
	done
	PATH=/usr/bin:/tmp/bin
	perl klcc/makeklcc.pl klcc/klcc.in klcc/cross-klibc.config \
		/usr/bin/perl >"klcc/${_triplet}-klcc"

	sed -iE "/^(CC|LD|STRIP)=/s/tmp/usr/" "klcc/${_triplet}-klcc"
	chmod +x "klcc/${_triplet}-klcc"
}

do_build() {
	_make all
	! grep ccache klcc/klcc
	if [ "$CROSS_BUILD" ]; then
		_gen_native_klcc
	else
		_gen_cross_klcc
	fi
}

do_install() {
	local _f
	_make install DESTDIR="${DESTDIR}"
	for _f in asm asm-generic linux; do
		rm -rf "$DESTDIR/usr/lib/klibc/include/$_f"
		ln -s "/usr/include/$_f" "$DESTDIR/usr/lib/klibc/include/"
	done
	if [ "$CROSS_BUILD" ]; then
		vbin klcc/${XBPS_CROSS_TRIPLET}-klcc
	else
		vbin klcc/${XBPS_TRIPLET/unknown-/}-klcc
	fi
}

klibc-devel_package() {
	short_desc+=" - development files"
	depends="perl kernel-libc-headers ${sourcepkg}>=${version}_${revision}
	 ${sourcepkg}<${version}_999"
	pkg_install() {
		vmove usr/bin
		vmove usr/share/man/man1
		vmove usr/lib/klibc/include
	}
}
