addto_CPPFLAGS() {
    local f
    for f; do
	case " $CPPFLAGS " in
	    *\ $f\ *) ;;
	    *) CPPFLAGS="$CPPFLAGS $f" ;;
	esac
    done
}

realdirpath() {
    [ -z "$1" ] && return 1
    (cd "$1" 2>/dev/null && pwd)
    if test $? -ne 0; then
	return 1
    else
	return 0
    fi
}

for d in . .. ../.. ../../..; do
    if test -e "$srcdir/$d/configure.cygwin"; then
	winsup_srcdir=$(realdirpath "$srcdir/$d")
	break
    fi
done

if test -z "$winsup_srcdir"; then
    echo "cannot find directory containing configure.cygwin" 1>&2
    exit 1
fi

case "$srcdir" in
    */newlib*)	lookfor=newlib ;;
    */winsup*)	lookfor=winsup ;;
    *)		lookfor=. ;;
esac

for d in . .. ../.. ../../..; do
    if test -d "$d/$lookfor"; then
	target_builddir=$(realdirpath "$d")
	break
    fi
done

if test -z "$target_builddir"; then
    target_builddir=$(pwd)
fi