#!/bin/sh
# $Id: setup-build-all,v 1.68 2025/07/13 18:15: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
}

only_gcc() {
	CC=gcc-stricter build-$PROG "$@"
	CC=gcc-normal build-$PROG "$@"
	CC=gcc-c11 build-$PROG "$@"
	CC=gcc-c23 build-$PROG "$@"
}

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

	. openbsd-egcc

	USE=cc
	TST=`path cc|head -n 1`
	if test -n "$TST"
		then
		if $USE --version 2>/dev/null >/dev/null
		then
			TST=`$USE --version 2>&1| ${FGREP-grep -F} ignored`
			test -n "$TST" || USE=$ACTUAL_GCC
		fi
		CC=$USE build-$PROG "$@"
	fi

	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 ( dpkg --version 2>&1 >/dev/null )
	then
		HAVE_DEB=yes
		debbuild-$PROG
	else
		HAVE_DEB=no
		echo "** DPKG not found"
	fi

	MY_SYS=`uname -sr|sed -e 's/ /-/g' | tr '[A-Z]' '[a-z]'`
	if test $HAVE_DEB = yes
	then
		# Debian provides rpm, but it's hit or miss whether it works.
		echo "...skipping RPM check, since this is Debian-based"
	elif test -f /sbin/installpkg && test -f /sbin/removepkg
	then
		HAVE_RPM=no
		echo "** RPM not useful (Slackware)"
		MY_SYS=slackware
	else
		case `uname -s` in
		Linux)
			if ( rpm --version 2>&1 >/dev/null )
			then
				HAVE_RPM=yes
				rpmbuild-$PROG
			else
				HAVE_RPM=no
				echo "** RPM not found"
			fi
			;;
		*)
			HAVE_RPM=no
			echo "** RPM not useful"
			;;
		esac
	fi

	# handle nonportable packages here
	case x$MY_SYS in
	xcygwin*)
		HOST=cygwin
		;;
	xslack*)
		HOST=slack
		;;
	xsunos*)
		HOST=solaris
		;;
	xlinux-*arch|xlinux-*arch[1-9]*)
		HOST=arch
		;;
	*)
		HOST=
		;;
	esac
	if test -n "$HOST"
	then
		echo "** build special packages for $MY_SYS"
		mkpkg-$HOST-$PROG 2>/dev/null || mkpkg-$HOST
	else
		echo "** no special packages to build for $MY_SYS"
	fi

	if ( clang --version 2>/dev/null >/dev/null )
	then
		with-clang build-$PROG "$@"
	else
		echo "** CLANG not found"
	fi

	if [ -d /opt/intel ]
	then
		with-icc build-$PROG "$@"
	fi

	if [ -d /opt/pgi ] || [ -d /opt/nvidia ]
	then
		with-pgi build-$PROG "$@"
	fi

	for std in c89 c99
	do
		[ -f /usr/bin/$std ] && CC=$std build-$PROG "$@"
	done

	if ( cfg-mingw --version 2>/dev/null >/dev/null )
	then
		mingwbuild-$PROG && run-makensis */*.nsi || rmdir BUILD-W32
		test "$HAVE_RPM" = yes && rpmbuild-mingw-$PROG
		test "$HAVE_DEB" = yes && debbuild-mingw-$PROG
	else
		echo "** MINGW-32 not found"
	fi

	if ( cfg-mingw64 --version 2>/dev/null >/dev/null )
	then
		mingw64build-$PROG && run-makensis */*.nsi || rmdir BUILD-W64
		test "$HAVE_RPM" = yes && rpmbuild-mingw64-$PROG
		test "$HAVE_DEB" = yes && debbuild-mingw64-$PROG
	else
		echo "** MINGW-64 not found"
	fi

	MYCC=`which gcc 2>/dev/null`
	case $ME in
	*bsd*)
		for port in /usr/local/bin/egcc[1-9]* /usr/local/bin/gcc[1-9]*
		do
			PORT=`basename $port`
			if test -d $port && test -f $port/$PORT
			then
				PATH=$port:$PATH CC=$PORT build-$PROG "$@"
			elif test -f $port
			then
				MYCC=$port
			fi
		done
		;;
	*darwin*|michener|marshall)
		for port in /opt/local/bin/gcc-mp-[1-9]*
		do
			if test -f $port
			then
				MYCC=$port
			fi
		done
		;;
	esac

	case "x$MYCC" in
	x/*)
		if test -f "$MYCC"
		then
			ACTUAL_GCC=`basename $MYCC`
			export ACTUAL_GCC
			echo "...using $ACTUAL_GCC as 'gcc'"
		fi
		;;
	esac

	case $ME in
	par-solaris10*|prl-solaris10*|vbx-solaris10*)
		CC=gcc-stricter build-$PROG "$@"
		CC=gcc-normal build-$PROG "$@"
		CC=c89 build-$PROG "$@"
		CC=c99 build-$PROG "$@"
		;;
	vbx-*|vmw-*|xen-*|par-*|prl-*|magpie|marshall|michener|mischief|muskrat|mundane|cygwin64)
		only_gcc "$@"
		case ".:$PATH" in
		*:/usr/pkg/gcc*)
			;;
		*)
			for TOP in /usr/pkg/gcc*/bin
			do
				[ -n "$TOP" ] || continue
				[ -d "$TOP" ] || continue
				PATH=$TOP:$PATH only_gcc "$@"
			done
			;;
		esac
		;;
	*)
		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
}
