#!/bin/sh

source ../Config.sh

eval toolchain_`cat ../toolchain.cfg`

pp_clean_local () {
    pp_clean_default "$@"
    cd "pp${toolchain_version}/libstdc++-v3" && {
	pp_clean_default "$@"
	rm -f include/stamp-*
	rm -rf "include/${HOST_SYSTEM}-uclibc"
	find . -type l -exec rm -f {} \;
	rm -rf "${DESTDIR}"/include/c++
	rm -rf "${DESTDIR}"/lib/libstdc++.*
	rm -rf "${DESTDIR}"/lib/libsupc++.*
	cd ../..
    }
}

pp_build_local () {
    pp_build_default "$@"

    # build libstdc++ only for kx2 & kxng or in non-final builds
    [ "$PP_PRODUCT" = "kx2" -o "$PP_PRODUCT" = "kxng" -o "$PP_BUILD_TYPE" != "final" ] || exit 0

    cd "pp${toolchain_version}/libstdc++-v3" && {
	# force configure
	if [ "$PP_FORCE_FULL_BUILD" = "1" ]; then
	    rm -f Makefile
	fi

	export CXXFLAGS="$FW_CXXFLAGS_3RDPARTY -Os"

	# configure
	if [ ! -f Makefile ]; then
	    echo "Running configure ..."
	    ./configure \
		--host=${HOST_SYSTEM}-uclibc \
		--with-cross-host=${HOST_SYSTEM}-uclibc \
		--prefix=/ \
		--disable-static \
		--enable-shared \
		--enable-sjlj-exceptions \
		--enable-clocale=generic \
		--disable-nls
	    sed -i -e "s,^\(sys_lib_search_path_spec=\).*,\1\"$DESTDIR/lib $DESTDIR/usr/lib\"," libtool
	fi

	# make
	echo "Running make ..."
	make toolexeclibdir='$(exec_prefix)/lib' DESTDIR="$DESTDIR" all install

	# delete installed libtool files
	for f in `find . -name \*.la`; do
	    rm -f $DESTDIR/lib/`basename $f`
	done
	cd ../..
    }
}

pp_do "$@"
