# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../uclibc/uclibc.conf
# Copyright (C) 2006 - 2009 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE Project
#
# 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 ---

. $confdir/conffiles-functions.in

# generated set
var_append conffiles ' ' $builddir/.config
# add custom/target configuration from $targetdir
if [ -e $targetdir/uclibc.config ] ; then
	var_append conffiles ' ' $targetdir/uclibc.config
fi

uC_arch=$( echo $arch | arch2uname | sed -e 's/ppc/powerpc/' )

default_config() {
	echo "X TARGET_$uC_arch"

	if [ "$arch_bigendian" = "yes" ]; then
		echo "X ARCH_BIG_ENDIAN"
	else
		echo "X ARCH_LITTLE_ENDIAN"
	fi

	echo "X CROSS_COMPILER_PREFIX \"${archprefix}\""

	# PREFIX
	if atstage toolchain; then
		echo "X KERNEL_SOURCE \"$base/build/$SDECFG_ID/usr\""
	else
		echo "X KERNEL_SOURCE \"$root/usr\""
	fi
	echo "X RUNTIME_PREFIX \"/\""
	echo "X DEVEL_PREFIX \"/usr\""

	if [ "$SDECFG_STATIC" = 0 ]; then
		echo "X SHARED_LIB_LOADER_PREFIX \"/lib\""

		# ld.so preload
		echo "X LDSO_PRELOAD_FILE_SUPPORT"
		echo "X HAVE_SHARED"
	else
		echo "O HAVE_SHARED"
	fi

	# locale
	if [ "$SDECFG_DISABLE_NLS" = "0" ]; then
		echo "X UCLIBC_HAS_LOCALE"
	fi
	# turning off locale will break ncurses

	# util-linux needs it to build some mounts
	# FIXME: this has to be done in the oposite way (disable those mounts
	#        if libc doesn't have support)
	echo "X UCLIBC_HAS_RPC"
	echo "X UCLIBC_HAS_FULL_RPC"

	# to get rint (iproute2)
	echo "X DO_C99_MATH"

	# to make reiserfsprogs happy
	echo "X UCLIBC_HAS_GLIBC_CUSTOM_PRINTF"
	# to make sed happy
	echo "X UCLIBC_HAS_WCHAR"

	# I want V6
	echo "X UCLIBC_HAS_IPV6"

	# needed for attr
	echo "X UCLIBC_HAS_FTW"

	# needed for acl (among others)
	echo "X UCLIBC_HAS_GETTEXT_AWARENESS"

	# make make happy :)
	echo "X MALLOC_GLIBC_COMPAT"

	echo "X UCLIBC_HAS_GNU_GETOPT"
	echo "X UCLIBC_HAS_GLOB"
	# make our ldconfig patch happy :-/
	echo "X UCLIBC_HAS_GNU_GLOB"
	echo "X UCLIBC_HAS_FLOATS"

}

runconf=0

hook_add premake 5 "uclibc_config"
uclibc_config() {
	# generate default config rules
	default_config > $builddir/.config

	eval $MAKE TARGET_ARCH=$uC_arch ARCH=$uC_arch defconfig
	conffiles_loop 2 TARGET_ARCH=$uC_arch ARCH=$uC_arch
}

var_remove_regex makeopt ' ' 'prefix=[^ ]*'
if atstage toolchain; then
	var_append makeopt ' ' PREFIX=$base/build/$SDECFG_ID
else
	var_append makeopt ' ' "PREFIX=$root"
fi

if [ "$SDECFG_DISABLE_NLS" = "0" ]; then
	hook_add premake 3 'uclibc_locale'
	uclibc_locale() {
		find "$PWD/extra/locale/charmaps" -name "*.pairs" > "$PWD/extra/locale/codesets.txt"
		eval $MAKE -C extra/locale
	}
fi

makeinstopt="$makeopt -j 1"
var_append makeopt ' ' "CROSS=${archprefix}"

if atstage toolchain; then
	var_append makeopt ' ' "headers"
	var_append makeinstopt ' ' "install_headers"
else
	var_append makeopt ' ' "pregen"
	var_append makeopt ' ' "all"
	var_append makeopt ' ' "utils"

	var_append makeinstopt ' ' "install_headers"
	var_append makeinstopt ' ' "install"
	var_append makeinstopt ' ' "install_utils"
fi

# inject kernel symlinks
hook_add preconf 5 'uclibc_kernellinks'
uclibc_kernellinks() {
	local x=
#	rm -rvf include/scsi kernel does not provide scsi headers yet, so don't delete them!!!
	for x in $( ls -1d $root/usr/include/{asm,asm-*,linux} 2> /dev/null ); do
		ln -svf $x include/
	done
}

hook_add postdoc 5 'cp -vf .config $root/$docdir/'