#!/bin/sh
###############################################################################
#
#  Copyright (C) 2003-2005  Anders Gavare.  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#  3. The name of the author may not be used to endorse or promote products
#     derived from this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#  SUCH DAMAGE.
#
#
#  $Id: configure,v 1.79 2005/01/09 06:06:58 debug Exp $
#
#  This is a minimal configure script, hardcoded for mips64emul. This script
#  figures out which compiler flags will work, and creates Makefiles in
#  sub-directories. A config.h file is also created.
#
#
#  --->   FOR NORMAL USE, JUST RUN ./configure WITHOUT OPTIONS!
#
#
#  To compile the emulator with profiling (during development), use
#  CFLAGS="-pg", run the emulator, and then run 'gprof mips64emul' and study
#  the results.
#
#
#  The main things that are detected by this script:
#
#    o)  which compiler to use  (overridden by setting CC)
#    o)  which compiler flags to use  (overridden by setting CFLAGS)
#    o)  X11 flags and libraries  (TODO: should be possible to override)
#    o)  special hacks for some OSes
#    o)  binary translation (on supported platforms)
#    o)  prefetch capability  (TODO: this is assumed on Alpha, but not all
#                              Alphas have it...)
#
###############################################################################


if [ z"$*" != z ]; then
	#  Parse command line options:
	for a in $*; do
		if [ z$a = z--nox11 ]; then
			NOX11=YES
		else if [ z$a = z--nobintrans ]; then
			NOBINTRANS=YES
		else if [ z$a = z--mips16 ]; then
			MIPS16=YES
		else if [ z$a = z--userland ]; then
			USERLAND=YES
		else if [ z$a = z--gui ]; then
			GUI=YES
		else if [ z$a = z--delays ]; then
			DELAYS=YES
		else if [ z$a = z--caches ]; then
			CACHES=YES
		else
			echo "usage: $0 [options]"
			echo "  --caches       enable cache emulation (experimental)"
			echo "  --delays       enable instruction latency/delay emulation"
#			echo "  --gui          compile with GTK+ 2.x"
			echo "  --mips16       enable MIPS16 instruction support"
			echo "  --nobintrans   configure without bintrans, even if the host supports it"
			echo "  --nox11        configure without X11 support"
			echo "  --userland     enable userland emulation (experimental)"
			exit
		fi; fi; fi; fi; fi; fi; fi
	done
fi


#############################################################################
#
#  Configure options:
#
#  This creates a config.h file, which is the included from include/misc.h.
#
#############################################################################

#  Head of config.h:
printf "/*
 *  THIS FILE IS AUTOMATICALLY CREATED BY configure!
 *  DON'T EDIT THIS FILE MANUALLY, IT WILL BE OVERWRITTEN.
 */
\n#ifndef CONFIG_H\n#define CONFIG_H\n\n" > config.h


#  Figure out if VERSION should be defined.
X=`basename \`pwd\`|cut -d \- -f 2-`
if [ z"$X" = zmips64emul ]; then
	echo '#  No VERSION defined.' >> _Makefile.header
else
	printf "#define VERSION \"$X\"\n" >> config.h
fi


ZZ=`echo compiled on \`date\``
printf "#define COMPILE_DATE \"$ZZ\"\n" >> config.h



#  MIPS16 support:
if [ z$MIPS16 = zYES ]; then
	echo 'Enabling MIPS16 support.   NOTE: MIPS16 support is not really working yet.'
	printf "#define ENABLE_MIPS16\n" >> config.h
fi


#  Userland emulation support:
if [ z$USERLAND = zYES ]; then
	echo 'Enabling userland emulation support. (Experimental.)'
	printf "#define ENABLE_USERLAND\n" >> config.h
fi


#  Instruction delay/latency emulation:
if [ z$DELAYS = zYES ]; then
	echo 'Enabling Instruction delay/latency emulation.'
	printf "#define ENABLE_INSTRUCTION_DELAYS\n" >> config.h
fi


#  Cache emulation:
if [ z$CACHES = zYES ]; then
	echo 'Enabling Cache emulation. (EXPERIMENTAL)'
	printf "#define ENABLE_CACHE_EMULATION\n" >> config.h

	if [ z$DELAYS != zYES ]; then
		printf 'WARNING. Cache emulation without instruction delay/latency emulation
(--delays) will not produce correct cache miss penalties and such.\n'
	fi
fi


#  GUI:
if [ z$GUI = zYES ]; then
	#  Check for GTK+ 2.x:
	printf 'Checking for GTK+ 2.x... '
	A=`pkg-config --modversion gtk+-2.0|grep 2\\..`
	if [ z$A = z ]; then
		echo no
	else
		echo yes, $A
	fi

	echo 'Sorry, there is no GUI yet. Aborting.'
	exit
fi


#############################################################################
#
#  Create the Makefile header:
#
#############################################################################

rm -f _Makefile.header

printf "#
#  DO NOT EDIT THIS FILE! It is automagically created by
#  the configure script, based on Makefile.skel.
#\n\n" >> _Makefile.header


echo 'int main(int argc, char *argv[]) { return 0; }' > _testprog.c


#  Try to detect which C compiler to use, if CC is not set:
printf "checking which C compiler to use... "

if [ z"`uname|cut -c 1-6`" = zCYGWIN ]; then
	CYGWIN=YES

	if [ z"$CC" = z ]; then
		#  Assume gcc on Cygwin (Windows) systems.
		CC=gcc
	fi
fi

if [ z"$CC" = z ]; then
	CC=cc

	#  Try gcc first:
	printf "#!/bin/sh\ngcc _testprog.c -o _testprog > /dev/null 2> /dev/null\n" > _test.sh
	chmod 755 _test.sh
	./_test.sh > /dev/null 2> /dev/null
	if [ -x _testprog ]; then
		CC=gcc
	fi
	rm -f _testprog

	#  If both gcc and cc exist, then cc might be a vendor specific
	#  compiler which produces faster code than gcc (eg on Solaris):
	printf "#!/bin/sh\ncc _testprog.c -o _testprog > /dev/null 2> /dev/null\n" > _test.sh
	chmod 755 _test.sh
	./_test.sh > /dev/null 2> /dev/null
	if [ -x _testprog ]; then
		CC=cc
	fi
	rm -f _testprog

	#  Try ccc (FreeBSD/Alpha):
	printf "#!/bin/sh\nccc _testprog.c -o _testprog > /dev/null 2> /dev/null\n" > _test.sh
	chmod 755 _test.sh
	./_test.sh > /dev/null 2> /dev/null
	if [ -x _testprog ]; then
		CC="ccc"
	fi
	rm -f _testprog

	rm -f _test.sh
fi


rm -f _testprog
printf "$CC $CFLAGS\n"


CCTYPE="generic"

if $CC -V 2> /dev/null | grep ompaq 1> /dev/null 2> /dev/null; then
	COMPAQCC=YES
	CCTYPE="Compaq CC"
fi

if $CC -V 2>&1 | grep Sun 1> /dev/null 2> /dev/null; then
	SUNCC=YES
	CCTYPE="Solaris CC"
fi

if $CC -v 2>&1 | grep gcc 1> /dev/null 2> /dev/null; then
	GCCCC=YES
	CCTYPE="GNU CC"
fi

if [ z$CYGWIN = zYES ]; then
	CCTYPE="$CCTYPE (Cygwin)"
fi

echo "C compiler type: $CCTYPE"


if [ z$NOX11 = z ]; then
	printf "checking for X11 headers and libs\n"

	#  Try to compile a small X11 test program:
	printf "#include <X11/Xlib.h>
	#include <stdio.h>
	Display *dis;
	void f(void) {
		dis = XOpenDisplay(NULL);
	}
	int main(int argc, char *argv[])
	{ return 0; }
	" > _test_x11.c

	XOK=0

	XINCLUDE=-I/usr/X11R6/include
	$CC $CFLAGS _test_x11.c -c -o _test_x11.o $XINCLUDE 2> /dev/null

	XLIB="-L/usr/X11R6/lib -lX11"
	$CC $CFLAGS _test_x11.o -o _test_x11 $XLIB 2> /dev/null

	if [ -x _test_x11 ]; then
		XOK=1
	fi

	rm -f _test_x11 _test_x11.o

	if [ z$XOK = z0 ]; then
		XINCLUDE=""
		$CC $CFLAGS _test_x11.c -c -o _test_x11.o $XINCLUDE 2> /dev/null

		#  -lsocket for Solaris
		XLIB="-lX11 -lsocket"
		$CC $CFLAGS _test_x11.o -o _test_x11 $XLIB 2> /dev/null

		if [ -x _test_x11 ]; then
			XOK=1
		fi
		rm -f _test_x11 _test_x11.o
	fi

	if [ z`uname` = zNetBSD ]; then
		echo "Using NetBSD hack for X11 libs..."
		XLIB="$XLIB -Wl,-rpath,/usr/X11R6/lib"
	fi

	if [ z$XOK = z0 ]; then
		echo Failed to compile X11 test program. Configuring without X11.
	else
		printf "X11 headers: $XINCLUDE\n"
		printf "X11 libraries: $XLIB\n"
		echo "XINCLUDE=$XINCLUDE" >> _Makefile.header
		echo "XLIB=$XLIB" >> _Makefile.header
		printf "#define WITH_X11\n" >> config.h
	fi

	rm -f _test_x11.c
fi


#  CWARNINGS:
printf "checking whether -Wall can be used... "
$CC $CFLAGS _testprog.c -o _testprog -Wall 2> /dev/null
if [ -x _testprog ]; then
	printf "yes\n"
	CWARNINGS="-Wall $CWARNINGS"

	#  Compaq's compiler always seems to warn about the long long type,
	#  and unusedtop suppresses warnings about include files being
	#  included more than once.
	if [ z"$COMPAQCC" = zYES ]; then
		CWARNINGS="$CWARNINGS -msg_disable longlongtype,unusedtop"
	fi
else
	printf "no\n"
fi
rm -f _testprog


#  -lrt for nanosleep?
printf "checking whether -lrt is required for nanosleep... "
printf "#include <time.h>\n#include <stdio.h>
int main(int argc, char *argv[]){nanosleep(NULL,NULL);return 0;}\n" > _testns.c
$CC $CFLAGS _testns.c -o _testns 2> /dev/null
if [ ! -x _testns ]; then
	$CC $CFLAGS -lrt _testns.c -o _testns 2> /dev/null
	if [ ! -x _testns ]; then
		printf "WARNING! COULD NOT COMPILE WITH nanosleep AT ALL!\n"
	else
		#  -lrt for nanosleep
		OTHERLIBS="-lrt $OTHERLIBS"
		printf "yes\n"
	fi
else
	printf "no\n"
fi
rm -f _testns.c _testns


#  -lresolv for inet_aton?
printf "checking whether -lresolv is required for inet_aton... "
printf "extern void (*inet_aton)(void); int main(int argc, char *argv[]) { void *x; x = inet_aton; return 0; }\n" > _testr.c
$CC $CFLAGS _testr.c -o _testr 2> /dev/null
if [ ! -x _testr ]; then
	$CC $CFLAGS -lresolv _testr.c -o _testr 2> /dev/null
	if [ ! -x _testr ]; then
		printf "WARNING! COULD NOT COMPILE WITH inet_aton AT ALL!\n"
	else
		#  -lresolv for inet_aton
		OTHERLIBS="-lresolv $OTHERLIBS"
		printf "yes\n"
	fi
else
	printf "no\n"
fi
rm -f _testr.[co] _testr


#  strtoll/strtoull missing?
printf "checking for strtoll... "
printf "#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
  long long x = strtoll(argv[1], NULL, 0); return 0;}\n" > _tests.c
$CC $CFLAGS _tests.c -o _tests 2> /dev/null
if [ ! -x _tests ]; then
	#  No strtoll, but let's check the size of long:
	printf "#include <stdio.h>\nint main(int argc, char *argv[]){printf(\"%%i\\\n\", sizeof(long));return 0;}\n" > _testlong.c
	$CC $COPTIM _testlong.c -o _testlong 2> /dev/null
	if [ ! -x _testlong ]; then
		printf "\nWARNING! COULD NOT COMPILE sizeof(long) TEST PROGRAM!\n\n"
		printf "Something is broken.\n\n"
		exit
	else
		XX=`./_testlong`
		if [ z$XX = z8 ]; then
			printf "missing (but OK, because sizeof(long) = 8)\n"
		else
			printf "missing\n"
			printf "WARNING! strtoll missing from libc. Hack: using strtoll = strtol and\n"
			printf "strtoull = strtoul. This will only work correctly on a machine where\n"
			printf "sizeof(long long) = sizeof(long), which does not seem to be the case here.\n\n"
		fi
		printf "#define HACK_STRTOLL\n" >> config.h
	fi
	rm -f _testlong*
else
	printf "yes\n"
fi
rm -f _tests.[co] _tests


#  fseeko missing?
printf "checking for fseeko... "
printf "#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <sys/types.h>
int main(int argc, char *argv[]) { fseeko(NULL, 0, 0); return 0;}\n" > _tests.c
$CC $CFLAGS _tests.c -o _tests 2> /dev/null
if [ ! -x _tests ]; then
	printf "no\n"
	printf "WARNING! fseeko missing from libc. Using a hack, which probably doesn't work.\n"
	printf "#define HACK_FSEEKO\n" >> config.h
else
	printf "yes\n"
fi
rm -f _tests.[co] _tests


#  MAP_ANON missing? (On some HP-UX systems? Use MAP_ANONYMOUS instead.)
printf "checking for MAP_ANON... "
rm -f _tests
printf "#include <stdio.h>
#include <sys/types.h>
#include <sys/mman.h>
int main(int argc, char *argv[]) { int x = MAP_ANON; return 0;}\n" > _tests.c
$CC $CFLAGS _tests.c -o _tests 2> /dev/null
if [ ! -x _tests ]; then
	printf "#include <stdio.h>
#include <sys/types.h>
#include <sys/mman.h>
int main(int argc, char *argv[]) { int x = MAP_ANONYMOUS; return 0;}\n" > _tests.c
	$CC $CFLAGS _tests.c -o _tests 2> /dev/null
	if [ ! -x _tests ]; then
		printf "no\n\n"
		printf "WARNING! Neither MAP_ANON nor MAP_ANONYMOUS work on this system.\n"
		printf "Please try to compile anyway, and report to me whether it builds/runs or not.\n\n"
		printf "#define MAP_ANON 0\n" >> config.h
		printf "#define NO_MAP_ANON\n" >> config.h
	else
		printf "using MAP_ANONYMOUS\n"
		printf "#define MAP_ANON MAP_ANONYMOUS\n" >> config.h
	fi
else
	printf "yes\n"
fi
rm -f _tests.[co] _tests


#############################################################################
#
#  Dynamic binary translation (BINTRANS):
#
#############################################################################

printf "checking for bintrans backend... "

BINTRANS=NO

if [ z$NOBINTRANS = zYES ]; then
	printf "disabled\n"
else
	#  Alpha:
	if [ z"`uname -m`" = zalpha ]; then
		printf "#define ALPHA\n" >> config.h
		printf "#define BINTRANS\n" >> config.h
		printf "alpha\n"
		BINTRANS=YES
	fi

	#  x86:  TODO: interpret all i?86 as i386!
	if [ z"`uname -m`" = zi386 ]; then
		printf "#define I386\n" >> config.h
		printf "#define BINTRANS\n" >> config.h
		printf "i386\n"
		BINTRANS=YES
	else
		if [ z"`uname -m`" = zi686 ]; then
			printf "#define I386\n" >> config.h
			printf "#define BINTRANS\n" >> config.h
			printf "i386\n"
			BINTRANS=YES
		fi
	fi

# MIPS not yet
#	#  MIPS:
#	if [ z"`uname -m`" = zmips ]; then
#		printf "#define MIPS\n" >> config.h
#		printf "#define BINTRANS\n" >> config.h
#		printf "mips\n"
#		BINTRANS=YES
#	fi

	#  UltraSPARC:
	if [ z"`uname -m`" = zsun4u ]; then
		if [ z$SPARCEXPERIMENTAL = z ]; then
			echo "no"
			printf "NOTE! Set the SPARCEXPERIMENTAL environment variable to YES to\n"
			printf "enable experimental bintrans. (It isn't really working yet.)\n"
		else
			printf "#define SPARCV9\n" >> config.h
			printf "#define BINTRANS\n" >> config.h
			printf "sparcv9\n"
		fi
		BINTRANS=YES
	else
		if [ z"`uname -m`" = zsparc64 ]; then
			if [ z$SPARCEXPERIMENTAL = z ]; then
				echo "no"
				printf "NOTE! Set the SPARCEXPERIMENTAL environment variable to YES to\n"
				printf "enable experimental bintrans. (It isn't really working yet.)\n"
			else
				printf "#define SPARCV9\n" >> config.h
				printf "#define BINTRANS\n" >> config.h
				printf "sparcv9\n"
			fi
			BINTRANS=YES
		fi
	fi

	if [ z$BINTRANS = zNO ]; then
		printf "not supported yet on this arch\n"
	fi
fi


#############################################################################
#
#  COPTIM:
#
#############################################################################

COPTIM=$CFLAGS


if [ z"`uname`" = zHP-UX ]; then
	COPTIM="-D_XOPEN_SOURCE_EXTENDED $COPTIM"
	printf "#define HPUX\n" >> config.h
fi


if [ z"`uname`" = zOSF1 ]; then
	COPTIM="-D_XOPEN_SOURCE=500 -D_OSF_SOURCE -D_POSIX_PII_SOCKET $COPTIM"
fi


#  Prefetch support?
printf "checking for prefetch support... "
if [ z"`uname -m`" = zalpha ]; then
	rm -f _alpha_asm_test.c _alpha_asm_test
	printf 'int main(int argc, char *argv[]) { int x; int *y = &x; asm ("ldl $31,0(%%0)" : : "g" (y)); return 0; }\n' > _alpha_asm_test.c
	$CC $COPTIM -O2 _alpha_asm_test.c -o _alpha_asm_test > /dev/null 2> /dev/null
	if [ -x _alpha_asm_test ]; then
#		printf "#define HAVE_PREFETCH\n" >> config.h
		printf "#define PREFETCH(x) asm(\"ldl \$31,0(%%0)\" : : \"g\" (x))\n" >> config.h
		echo "yes"
		PREFSUP=YES
	fi
	rm -f _alpha_asm_test.c _alpha_asm_test
fi
if [ z$PREFSUP = z ]; then
	printf "#define PREFETCH(x) { }\n" >> config.h
	echo "no"
fi


#  Check for 64-bit off_t:
printf "checking for 64-bit off_t... "
printf "#include <stdio.h>\n#include <inttypes.h>\n#include <sys/types.h>\n
int main(int argc, char *argv[]){printf(\"%%i\\\n\", sizeof(off_t));return 0;}\n" > _testoff.c
$CC $COPTIM _testoff.c -o _testoff 2> /dev/null
if [ ! -x _testoff ]; then
	printf "\nWARNING! COULD NOT COMPILE off_t TEST PROGRAM AT ALL!\n"
else
	if [ z`./_testoff` = z8 ]; then
		printf "yes\n"
	else
		$CC $COPTIM -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE _testoff.c -o _testoff 2> /dev/null
		if [ ! -x _testoff ]; then
			printf "\nWARNING! COULD NOT COMPILE off_t TEST PROGRAM!\n"
		else
			if [ z`./_testoff` = z8 ]; then
				COPTIM="-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE $COPTIM"
				printf "using -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE\n"
			else
				printf "NO\n"
				printf "Warning! No 64-bit off_t. Continuing anyway.\n"
			fi
		fi
	fi
fi
rm -f _testoff.c _testoff


#  Check for u_int8_t etc:
printf "checking for u_int8_t... "
printf "#include <stdio.h>\n#include <inttypes.h>\n#include <sys/types.h>\n
int main(int argc, char *argv[]){printf(\"%%i\\\n\", sizeof(u_int8_t));return 0;}\n" > _testuint.c
$CC $COPTIM _testuint.c -o _testuint 2> /dev/null
if [ ! -x _testuint ]; then
	rm -f _testuint*
	printf "#include <stdio.h>\n#include <inttypes.h>\n#include <sys/types.h>\n
	int main(int argc, char *argv[]){printf(\"%%i\\\n\", sizeof(uint8_t));return 0;}\n" > _testuint.c
	$CC $COPTIM _testuint.c -o _testuint 2> /dev/null
	if [ ! -x _testuint ]; then
		printf "no\n\nERROR: No u_int8_t or uint8_t. Aborting\n"
		exit
	fi

	printf "typedef uint8_t u_int8_t;\ntypedef uint16_t u_int16_t;\n" >> config.h
	printf "typedef uint32_t u_int32_t;\ntypedef uint64_t u_int64_t;\n" >> config.h
	printf "uint8_t\n"
else
	printf "yes\n"
fi
rm -f _testuint.c _testuint


if [ z"$COMPAQCC" = zYES ]; then
	#  -O4 is possible, but is -O3 better?
	COPTIM="-O4 $COPTIM"
else
	if [ z"`uname`" = zSunOS ]; then
		#  "cc", the system's default compiler:
		if [ z"$SUNCC" = zYES ]; then
			COPTIM="-xO5 -xdepend $COPTIM"
		fi
		printf "#define SOLARIS\n" >> config.h
		OTHERLIBS="-lsocket $OTHERLIBS"
	else
		#  gcc or something else:
		$CC $COPTIM _testprog.c -o _testprog -O 2> /dev/null
		if [ -x _testprog ]; then
			rm -f _testprog
			$CC $COPTIM _testprog.c -o _testprog -O2 2> /dev/null
			if [ -x _testprog ]; then
				COPTIM="-O2 $COPTIM"
			else
				COPTIM="-O $COPTIM"
			fi
		fi
	fi
fi
rm -f _testprog


#  -fschedule-insns causes bugs on i386 with gcc,
#  but works OK on my alpha with ccc (compaq's cc).
if [ z"$COMPAQCC" = zYES ]; then
	printf "checking whether -fschedule-insns2 can be used... "
	$CC $COPTIM _testprog.c -o _testprog -fschedule-insns2 2> /dev/null
	if [ -x _testprog ]; then
		COPTIM="-fschedule-insns2 $COPTIM"
		printf "yes\n"
	else
		printf "no\n"
	fi
	rm -f _testprog

	printf "checking whether -fschedule-insns can be used... "
	$CC $COPTIM _testprog.c -o _testprog -fschedule-insns 2> /dev/null
	if [ -x _testprog ]; then
		COPTIM="-fschedule-insns $COPTIM"
		printf "yes\n"
	else
		printf "no\n"
	fi
	rm -f _testprog

#	#  -intrinsics
#	$CC $COPTIM _testprog.c -o _testprog -intrinsics 2> /dev/null
#	if [ -x _testprog ]; then
#		COPTIM="-intrinsics $COPTIM"
#	fi
#	rm -f _testprog

	#  -fast
	printf "checking whether -fast can be used... "
	$CC $COPTIM _testprog.c -o _testprog -fast 2> /dev/null
	if [ -x _testprog ]; then
		COPTIM="-fast $COPTIM"
		printf "yes\n"
	else
		printf "no\n"
	fi
	rm -f _testprog
fi


#  -fpeephole
printf "checking whether -fpeephole can be used... "
$CC $COPTIM -fpeephole _testprog.c -o _testprog > _testprog.stdout 2>&1
cat _testprog.stdout >> _testprog.error
if grep peephole _testprog.error > /dev/null 2>&1; then
	printf "no\n"
else
	if [ -x _testprog ]; then
		COPTIM="-fpeephole $COPTIM"
		printf "yes\n"
	else
		printf "no\n"
	fi
fi
rm -f _testprog _testprog.error _testprog.stdout


#  -fmove-all-movables
printf "checking whether -fmove-all-movables can be used... "
$CC $COPTIM -fmove-all-movables _testprog.c -o _testprog > _testprog.stdout 2>&1
cat _testprog.stdout >> _testprog.error
if grep movables _testprog.error > /dev/null 2>&1; then
	printf "no\n"
else
	if [ -x _testprog ]; then
		COPTIM="-fmove-all-movables $COPTIM"
		printf "yes\n"
	else
		printf "no\n"
	fi
fi
rm -f _testprog _testprog.error _testprog.stdout


#  -fomit-frame-pointer
printf "checking whether -fomit-frame-pointer can be used... "
$CC $COPTIM -fomit-frame-pointer _testprog.c -o _testprog 1> _testprog.stdout 2>&1
cat _testprog.stdout >> _testprog.error
if grep frame _testprog.error > /dev/null 2>&1; then
	printf "no\n"
else
	if [ -x _testprog ]; then
		COPTIM="-fomit-frame-pointer $COPTIM"
		printf "yes\n"
	else
		printf "no\n"
	fi
fi
rm -f _testprog _testprog.error _testprog.stdout


#############################################################################

INCLUDE=-I../include/

echo C compiler: $CC
echo C compiler flags: $COPTIM $CWARNINGS $INCLUDE
echo Linker flags: $OTHERLIBS
echo "CWARNINGS=$CWARNINGS" >> _Makefile.header
echo "COPTIM=$COPTIM" >> _Makefile.header
echo "INCLUDE=$INCLUDE" >> _Makefile.header
echo "CC=$CC" >> _Makefile.header
echo "OTHERLIBS=$OTHERLIBS" >> _Makefile.header
echo "" >> _Makefile.header

printf "Regression test setup:\n"
printf "Checking for a GNU CC cross-compiler for 64-bit MIPS... "
echo 'int f(int x) { return x; }' > _testprog.c

MIPS_CC=''
for MIPS_TRY in mips64-unknown-elf mips-unknown-elf64; do
	printf '#!/bin/sh\n'$MIPS_TRY'-gcc _testprog.c -c\n' > _test.sh
	chmod 755 _test.sh
	./_test.sh > /dev/null 2> /dev/null
	if [ -f _testprog.o ]; then
		MIPS_CC=$MIPS_TRY
		break
	fi
done

if [ z$MIPS_CC = z ]; then
	echo "none"
	echo '("make regtest" will not work)'
else
	echo $MIPS_CC"-gcc"
fi

rm -f _testprog* _test.sh

echo "MIPS_CC="$MIPS_CC"-gcc -g -O3 -fno-builtin -fschedule-insns -mips64 -mabi=64" >> _Makefile.header
echo "MIPS_AS="$MIPS_CC"-as -mabi=64 -mips64" >> _Makefile.header
echo "MIPS_LD="$MIPS_CC"-ld -Ttext 0xa800000000030000 -e main --oformat=elf64-bigmips" >> _Makefile.header
echo "" >> _Makefile.header


for a in . src devices tests; do
	echo "creating $a/Makefile"
	cat _Makefile.header > $a/Makefile
	cat $a/Makefile.skel >> $a/Makefile
done

#  Tail of config.h:
printf "\n#endif  /*  CONFIG_H  */\n" >> config.h

#  Remove temporary Makefile header:
rm -f _Makefile.header

echo Configured. You may now run make to build mips64emul.

