# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../uclibc/uclibc.conf
# Copyright (C) 2006 - 2013 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"
		echo "X ARCH_WANTS_BIG_ENDIAN"
	else
		echo "X ARCH_LITTLE_ENDIAN"
		echo "X ARCH_WANTS_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

	# thread support
	if [ "$SDECFG_PKG_UCLIBC_THREADSUPPORT" != "none" ]; then
		echo "X UCLIBC_HAS_THREADS"
		# choose the thread implementation
		case "$SDECFG_PKG_UCLIBC_THREADSUPPORT" in
			old) echo "X LINUXTHREADS_OLD" ;;
			new) echo "X LINUXTHREADS_NEW" ;;
			nptl) echo "X UCLIBC_HAS_THREADS_NATIVE" ;;
		esac
	else
		echo "X HAS_NO_THREADS"
	fi

	# SuSv3 legacy support
	if [ "$SDECFG_PKG_UCLIBC_SUSV3_LEGACY" = "1" ]; then
		echo "X UCLIBC_SUSV3_LEGACY"
	fi
	
	# SuSv4 legacy support
	if [ "$SDECFG_PKG_UCLIBC_SUSV4_LEGACY" = "1" ]; then
		echo "X UCLIBC_SUSV4_LEGACY"
	fi

	# libresolv support
	echo "X UCLIBC_HAS_RESOLVER_SUPPORT"
	echo "X UCLIBC_HAS_LIBRESOLV_STUB"
	
	# provide libutil library
	# forkpty(), login(), login_tty(), logout(), logwtmp(), openpty()
	echo "X UCLIBC_HAS_LIBUTIL"

	if [ "$SDECFG_DEBUG" = 1 ]; then
		echo "X DODEBUG"
	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 mdadm
	echo "X UCLIBC_HAS_NFTW"

	# 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"

	# libssp needs __progname
	if [ "$SDECFG_PKG_GCC_STACKPRO" = "1" ]; then
		echo "X UCLIBC_HAS___PROGNAME"
		echo "X UCLIBC_HAS_PROGRAM_INVOCATION_NAME"
	fi
}

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=
	for x in $( ls -1d $root/usr/include/{asm,asm-*,scsi,linux} 2> /dev/null ); do
		rm -rvf "include/${x##*/}"
		ln -svf $x include/
	done
}

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