# --- ROCK-COPYRIGHT-NOTE-BEGIN ---
# 
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# Please add additional copyright information _after_ the line containing
# the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
# the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
# 
# ROCK Linux: rock-src/package/base/dietlibc/parse-config-9
# ROCK Linux is Copyright (C) 1998 - 2005 Clifford Wolf
# 
# 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; either version 2 of the License, or
# (at your option) any later version. A copy of the GNU General Public
# License can be found at Documentation/COPYING.
# 
# Many people helped and are helping developing ROCK Linux. Please
# have a look at http://www.rocklinux.org/ and the Documentation/TEAM
# file for details.
# 
# --- ROCK-COPYRIGHT-NOTE-END ---

# --- FUNCTIONS-BEGIN ---
function set_wrapper_and_misc () {
	var_append CC_WRAPPER_INSERT " " "-idirafter $root/usr/include"
	hook_add preconf 8 'echo ac_cv_header_wchar_h=no >> config.cache'
	hook_add preconf 8 'echo ac_cv_header_wctype_h=no >> config.cache'
	hook_add preconf 8 'echo ac_cv_func_clock_gettime=no >> config.cache'
	if [ "$stagelevel" -le 1 -o "$ROCKCFG_DIETLIBC_ALL" -ne 1 ] ; then
		var_append CC_WRAPPER_INSERT " " "-nostdinc"
		# all dietlibc pathes are redirected into the build system
		var_insert CC_WRAPPER_FILTER "|" "sed -e 's,/usr/dietlibc/,$root&,'"
		var_append CC_WRAPPER_OTHERS ":" $dietbin
	else
		if [ "$diet_dynamic_static" == "static"  ] ; then
			var_append GCC_WRAPPER_INSERT " " "-static"
		fi
		var_append GCC_WRAPPER_INSERT " " "-D__dietlibc__"
	fi
}

function add_lib () {
	if [ "$stagelevel" -le 1 ] ; then var_append CC_WRAPPER_APPEND_PO " " "$1"
	else var_append CC_WRAPPER_APPEND " " "$1"; fi
}
# --- FUNCTIONS-END --

# decide wether to compile the packet with dietlibc or not
if [ "$ROCKCFG_DIETLIBC_ALL" = 1 -o "$pkg" = "dietlibc" ]; then
	pkg_dietlibc_useit=1
else
	x="${pkg//-/_}" ; x="${x//+/_}"
	eval "pkg_dietlibc_useit=\$ROCKCFG_DIETLIBC_${x}"
fi
[ "$pkg_dietlibc_useit" != 1 ] && pkg_dietlibc_useit=0
[ "$stagelevel" -eq 0 ] && pkg_dietlibc_useit=0		# never use dietlibc in stage 0

if [ "$pkg_dietlibc_useit" = 1 ]; then

	# If we use something like 'i386-pc-linux-gnu' as architecture name,
	# some programs start using gnu extensions. So we use ..-gnulibc1
	# instead.
	#
	pkg_dietlibc_orig_target="$arch_target"
	arch_target="${arch_target}libc1"
	[ $ROCKCFG_CROSSBUILD = 0 ] && arch_build="${arch_target}"

	if [ -d $base/build/$ROCKCFG_ID/$toolsdir/diet-bin ] ; then
		PATH="$base/build/$ROCKCFG_ID/$toolsdir/diet-bin:$PATH"
	fi

	if [ "$ROCKCFG_DIETLIBC_DYN" = "1" ]
	then dietbin="diet-dyn" ; diet_dynamic_static="dynamic"
	else dietbin="diet" ; diet_dynamic_static="static" ; fi

	echo_status "Preparing configuration to build this package with dietlibc ("${diet_dynamic_static}"ally)."

	if [ "$stagelevel" -gt 1 ]; then
		var_remove CC_WRAPPER_INSERT " " "-pipe" ;
		var_append CC_WRAPPER_REMOVE " " "-pipe" ;

		var_remove GCC_WRAPPER_INSERT " " "-pipe" ;
		var_append GCC_WRAPPER_REMOVE " " "-pipe" ;

		var_remove KCC_WRAPPER_INSERT " " "-pipe" ;
		var_append KCC_WRAPPER_REMOVE " " "-pipe" ;

	fi

	# patch has problems with Plan A patching so we switch with -x 16 to Plan B
	[ "$stagelevel" -ge 9 ] && patchopt="-bfp1 -x 16 -z .orig"

	case "$pkg" in
		dietlibc)
			echo_status "This package does not utilize a C library ..."
			;;

		linux*)
			echo_status "This package is currently blacklisted and not build using dietlibc!"
			[ "$stagelevel" -gt 1 -a "$diet_dynamic_static" == "static" ] && \
			  var_append GCC_WRAPPER_INSERT " " "-static"
			;;

		e2fsprogs|util-linux|net-tools)
			# the packets that have BSD problems and/or GNU problems
			echo_status "Setting dietlibc special GNU/BSD settings!"
			var_append CC_WRAPPER_INSERT " " "-D_BSD_SOURCE"
			var_append CC_WRAPPER_INSERT " " "-D_GNU_SOURCE"
			set_wrapper_and_misc
			;;

		sed|lilo)
			echo_status "Setting dietlibc special lcompat settings!"
			add_lib "-lcompat"
			set_wrapper_and_misc
			;;

		coreutils|findutils)
			echo_status "Setting dietlibc special lcompat+BSD settings!"
			var_append CC_WRAPPER_INSERT " " "-D_BSD_SOURCE"
			add_lib "-lcompat"
			set_wrapper_and_misc
			;;

		*)
			echo_status "Using default dietlibc compiler options!"
			set_wrapper_and_misc
			;;
	esac

	case "$pkg" in
		coreutils)
			var_remove patchfiles " " "*acl-xattr.patch"
			;;
		curl)
			[ "$diet_dynamic_static" == "static"  ] && \
			  var_append extraconfopt " " "--disable-shared"
			;;
		ncurses)
			var_append extraconfopt " " \
			  "--without-cxx-binding --without-cxx"
			;;
		util-linux)
			add_lib "-lrpc"
			add_lib "-lcompat"
			;;
		dump)
			var_append extraconfopt " " "--disable-rmt"
			;;
		perl5)
			add_lib "-lm -lcompat"
			;;
	esac

	var_append patchfiles " " \
	  `ls $base/package/base/dietlibc/pkg_patch/pkg_$pkg.patch 2>/dev/null`
	[ "$stagelevel" -ge 1 -a "$ROCKCFG_DIETLIBC_ALL" = "1" ] && \
	  var_append patchfiles " " \
	  `ls $base/package/base/dietlibc/pkg_patch/pkg_$pkg.chroot.patch 2>/dev/null`
fi

