#!/bin/sh
# $Id: setup-build-all,v 1.28 2011/10/10 22:03:32 tom Exp $
# vi:ts=4 sw=4

# common functions for "build-all-XXX" scripts, which know about the compiler
# variations that are useful on each platform/host.

# calling script must override $PROG and $FILE
PROG=
FILE=

failed() {
	echo "? $*"
	exit 1
}

rename_icc_logs()
{
	for name in LOGS/*-icc-*.log
	do
		mv $name `echo "$name" | sed -e 's/-icc-/-icc'$1'-/'`
	done
}

rename_icc9_logs()
{
	rename_icc_logs 9
}

rename_icc12_logs()
{
	rename_icc_logs 12
}

build_all() {
	ME=`partition 2>/dev/null`
	test -z "$ME" && ME=`hostname`
	test -z "$ME" && ME=`uname -a`
	export ME

	if test -z "$PROG" ; then
		failed "you did not give a value for PROG"
	elif test -z "$FILE" ; then
		failed "you did not give a value for FILE"
	elif test ! -f $FILE ; then
		failed "not in the build-directory"
	fi

	test -d bin &&
	test ! -f Makefile.in &&
	test ! -f makefile.in &&
	rm -rf bin

	USE=cc
	if $USE --version 2>/dev/null >/dev/null
	then
		TST=`$USE --version 2>&1|fgrep ignored`
		test -n "$TST" || USE=gcc
	fi
	CC=$USE build-$PROG "$@"

	case $ME in
	irix*)
		CC64=cc-irix64
		;;
	sunos-5_7)
		CC64=cc-solaris64
		;;
	sunos-5_8)
		CC64=cc-solaris64
		;;
	sunos-5_9)
		CC64=cc-solaris64
		;;
	sunos-5_10)
		CC64=cc-solaris64
		;;
	*)
		CC64=none
		;;
	esac

	if test "$CC64" != none ; then
		CC=$CC64 build-$PROG "$@"
	fi

	if ( rpm --version 2>/dev/null >/dev/null )
	then
		rpmbuild-$PROG
	fi

	if ( dpkg --version 2>/dev/null >/dev/null )
	then
		debbuild-$PROG
	fi

	if test -f /usr/bin/clang \
	|| test -f /usr/local/bin/clang \
	|| test -f /usr/pkg/bin/clang
	then
		with-clang build-$PROG
	fi

	case $ME in
	*bsd*)
		for port in /usr/local/bin/gcc*
		do
			PORT=`basename $port`
			if test -d $port && test -f $port/$PORT
			then
				PATH=$port:$PATH CC=$PORT build-$PROG
			fi
		done
		;;
	esac

	case $ME in
	stable-9|par-debian31)
		with-icc9 build-$PROG
		rename_icc9_logs
		with-icc build-$PROG
		;;
	testing-9|par-debian50-32|par-debian60-32)
		with-icc9 build-$PROG
		rename_icc9_logs
		;;
	par-fedora15a-64)
		with-icc12 build-$PROG
		rename_icc12_logs
		;;
	esac

	case $ME in
	xen-solaris|par-solaris0906)
		with-suncc build-$PROG
		CC=gcc-stricter build-$PROG
		CC=gcc-normal build-$PROG
		CC=gcc-normal-64 build-$PROG
		;;
	xen-*|par-*|*-9|ss90|callisto|marshall|cygwin64)
		CC=gcc-stricter build-$PROG
		CC=gcc-normal build-$PROG
		;;
	*)
		if with-gcc gcc --version 2>/dev/null ; then
			CC=gcc-normal with-gcc build-$PROG "$@"
			if test "$CC64" != none ; then
				CC=gcc-normal-64 with-gcc build-$PROG "$@"
			fi
		fi
		;;
	esac
	tput bel
}
