# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../dietlibc/dietlibc.conf
# Copyright (C) 2007 - 2009 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE Project
# Copyright (C) 1998 - 2004 Clifford Wolf
#
# More information can be found in the files COPYING and README.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

# Apply debian patches (debian/diff/*.diff)
hook_add prepatch 3 'dietlibc_debian_patches'
dietlibc_debian_patches() {
	local x=
	for x in $( match_source_file -p diff ); do
		case "$x" in
			*.bz2)	bzcat "$x" | patch ${patchopt} ;;
			*) patch ${patchopt} < "$x" ;;
		esac

	done
	for x in debian/diff/*.diff; do
		patch ${patchopt} < $x
	done
}
hook_add prepatch 5 'var_append patchfiles $confdir/ttyname_r.diff'
dietlibc_initial_fixup() {
	sed -i /WANT_SYSENTER/d dietfeatures.h
	sed -i "s,(CROSS)strip,(STRIP),g" Makefile
	sed -i "s,(CROSS)ar,(AR),g" Makefile
	sed -i "s,(CROSS)ld,(LD),g" Makefile

	case "$arch_machine" in
		powerpc)	ln -vs bin-ppc bin-powerpc	;;
		powerpc64)	ln -vs bin-ppc64 bin-powerpc64	;;
	esac

	if atstage cross || [ "$toolsdir" == tools.cross ] ; then
		mkdir -p bin-$cpu bin-$hostcpu
		ln -s bin-$cpu "bin-${CC%%-*}"
	fi
}

dietlibc_install_headers() {
	find include/* -name '*.orig' -exec rm -v '{}' \;

	mkdir -p $base/build/${SDECFG_ID}${includedir}/
	cp -rv include/* $base/build/${SDECFG_ID}${includedir}/
}

dietlibc_make_wrappers() {
	rm -rf $toolsbin; mkdir -p $toolsbin
	for x in gcc as ld ar ranlib nm ; do # wrapper
		cat <<-EOT > $toolsbin/${arch_target}-$x
		#!/bin/sh
		#PATH=\${PATH/\$(dirname \$( type -p \$0 ))/}; PATH=\${PATH/::/:}
		exec $dietbin ${pkg_dietlibc_orig_target}-$x "\$@"
		EOT
		chmod +x $toolsbin/${arch_target}-$x
	done
}

dietlibc_install_toolscross() {
	local diet=bin-$1/diet
	cp -v $diet-i $toolsbin/diet
	[ ! -f $diet-dyn-i ] ||
		cp -v $diet-dyn-i $toolsbin/diet-dyn
}

dietlibc_build_nativewrapper() {
	local DOPTCOMMON="-Os -DVERSION=\"$ver\" $dietfeatures"
	local DOPT="$DOPTCOMMON  -DDIETHOME=\"$PWD\""
	local DOPTI="$DOPTCOMMON -DDIETHOME=\"$root/$prefix\" -DINSTALLVERSION"

	patch -p1 < $confdir/hostlibc.diff
	$HOSTCC $DOPT  diet.c -o bin-$hostcpu/diet
	$HOSTCC $DOPT  diet.c -o bin-$hostcpu/diet-dyn -D__DYN_LIB
	$HOSTCC $DOPTI diet.c -o bin-$hostcpu/diet-i
	$HOSTCC $DOPTI diet.c -o bin-$hostcpu/diet-dyn-i -D__DYN_LIB
	patch -Rp1 < $confdir/hostlibc.diff
}

dietlibc_final_fixup() {
	for x in ppc ppc64; do
		if [ -d $root/$libdir-$x ] ; then
			rm -f $root/$libdir-${x/ppc/powerpc}
			ln -sfnv lib-$x $root/$libdir-${x/ppc/powerpc}
		fi
	done
}

prefix=usr/dietlibc
set_confopt
bindir=/usr/bin
libdir=/$prefix/lib

if [ "$SDECFG_LIBC" = "dietlibc" ] ; then
	includedir=/usr/include
fi

var_append makeopt     ' ' prefix=/$prefix
var_append makeinstopt ' ' prefix=/$prefix
var_append makeopt     ' ' LIBDIR=$libdir
var_append makeinstopt ' ' LIBDIR=$libdir
var_append makeinstopt ' ' BINDIR=$bindir
var_append makeinstopt ' ' MAN1DIR=$mandir/man1

cpu="`echo $arch | arch2uname | sed -e s/sh.$/sh/`"
hostcpu="`/bin/uname -m | uname2arch | arch2uname`"

toolsbin=$base/build/$SDECFG_ID/TOOLCHAIN/$toolsdir/diet-bin

if [ "$SDECFG_DIETLIBC_DYN" = 1 ]; then
	case $cpu in
		arm|i386|x86_64)
			var_append makeopt " " "dyn"
			;;
	esac
fi

# dietlibc contains non Thumb aware assembly ...
var_remove GCC_WRAPPER_INSERT " " "-mthumb"

# special features
dietfeatures='-DWANT_DYNAMIC -DWANT_MALLOC_ZERO'
[ "$SDECFG_DIETWANT_SYSENTER" = "1" ] &&
	var_append dietfeatures ' ' '-DWANT_SYSENTER'
var_append GCC_WRAPPER_APPEND ' ' "$dietfeatures"

if atstage toolchain; then
	makeopt=
	makeinstopt=
	hook_add postmake 5 'dietlibc_install_headers'
else
	hook_add postpatch 5 'dietlibc_initial_fixup'

	[ $toolsdir != tools.cross ] ||
		hook_add premake 5 'dietlibc_make_wrappers'

	var_append makeopt ' ' all

	if atstage cross || [ "$toolsdir" == tools.cross ]; then
		# first build a native diet wrapper
		hook_add premake 7 "dietlibc_build_nativewrapper"
		hook_add premake 8 "dietlibc_install_toolscross $hostcpu"
	fi

	if atstage native; then
		# dietlibc uses uname - so we always need to set MYARCH
		var_append makeopt     ' ' "MYARCH=$cpu"
		var_append makeinstopt ' ' "MYARCH=$cpu"
	else
		var_append makeopt     ' ' "CROSS=$archprefix CC=cc"
		var_append makeopt     ' ' "MYARCH=$hostcpu"
		var_append makeopt     ' ' "ARCH=$cpu"
		var_append makeinstopt ' ' "MYARCH=$hostcpu"
		var_append makeinstopt ' ' "ARCH=$cpu"
	fi
	hook_add postmake  9 'dietlibc_final_fixup'
fi