#!/bin/sh
#
# kqemu configure script (c) 2006 Fabrice Bellard
#
# set temporary file name
if test ! -z "$TMPDIR" ; then
    TMPDIR1="${TMPDIR}"
elif test ! -z "$TEMPDIR" ; then
    TMPDIR1="${TEMPDIR}"
else
    TMPDIR1="/tmp"
fi

TMPC="${TMPDIR1}/qemu-conf-${RANDOM}-$$-${RANDOM}.c"
TMPO="${TMPDIR1}/qemu-conf-${RANDOM}-$$-${RANDOM}.o"
TMPE="${TMPDIR1}/qemu-conf-${RANDOM}-$$-${RANDOM}"
TMPS="${TMPDIR1}/qemu-conf-${RANDOM}-$$-${RANDOM}.S"

# default parameters
prefix=""
static="no"
cross_prefix=""
cc="gcc"
host_cc="gcc"
ar="ar"
make="make"
strip="strip"
cpu=`uname -m`
case "$cpu" in
  i386|i486|i586|i686|i86pc|BePC)
    cpu="i386"
  ;;
  armv*b)
    cpu="armv4b"
  ;;
  armv*l)
    cpu="armv4l"
  ;;
  alpha)
    cpu="alpha"
  ;;
  "Power Macintosh"|ppc|ppc64)
    cpu="powerpc"
  ;;
  mips)
    cpu="mips"
  ;;
  s390)
    cpu="s390"
  ;;
  sparc)
    cpu="sparc"
  ;;
  sparc64)
    cpu="sparc64"
  ;;
  ia64)
    cpu="ia64"
  ;;
  m68k)
    cpu="m68k"
  ;;
  x86_64|amd64)
    cpu="x86_64"
  ;;
  *)
    cpu="unknown"
  ;;
esac
gprof="no"
bigendian="no"
mingw32="no"
EXESUF=""
gdbstub="yes"
slirp="yes"
adlib="no"
oss="no"
dsound="no"
coreaudio="no"
alsa="no"
fmod="no"
fmod_lib=""
fmod_inc=""
linux="no"
kqemu="no"
kernel_path=""
cocoa="no"
check_gfx="yes"
check_gcc="yes"

# OS specific
targetos=`uname -s`
case $targetos in
CYGWIN*)
mingw32="yes"
CFLAGS="-O2 -mno-cygwin"
;;
MINGW32*)
mingw32="yes"
;;
FreeBSD)
bsd="yes"
oss="yes"
if [ "$cpu" = "i386" -o "$cpu" = "x86_64" ] ; then
    kqemu="yes"
fi
;;
NetBSD)
bsd="yes"
oss="yes"
;;
OpenBSD)
bsd="yes"
oss="yes"
;;
Darwin)
bsd="yes"
darwin="yes"
;;
*)
oss="yes"
linux="yes"
if [ "$cpu" = "i386" -o "$cpu" = "x86_64" ] ; then
    kqemu="yes"
fi
;;
esac

if [ "$bsd" = "yes" ] ; then
  if [ ! "$darwin" = "yes" ] ; then
    make="gmake"
  fi
fi

# find source path
# XXX: we assume an absolute path is given when launching configure,
# except in './configure' case.
source_path=${0%configure}
source_path=${source_path%/}
source_path_used="yes"
if test -z "$source_path" -o "$source_path" = "." ; then
    source_path=`pwd`
    source_path_used="no"
fi

for opt do
  case "$opt" in
  --help|-h) show_help=yes
  ;;
  --prefix=*) prefix=`echo $opt | cut -d '=' -f 2`
  ;;
  --source-path=*) source_path=`echo $opt | cut -d '=' -f 2`
  ;;
  --cross-prefix=*) cross_prefix=`echo $opt | cut -d '=' -f 2`
  ;;
  --cc=*) cc=`echo $opt | cut -d '=' -f 2`
  ;;
  --host-cc=*) host_cc=`echo $opt | cut -d '=' -f 2`
  ;;
  --make=*) make=`echo $opt | cut -d '=' -f 2`
  ;;
  --extra-cflags=*) CFLAGS="${opt#--extra-cflags=}"
  ;;
  --extra-ldflags=*) LDFLAGS="${opt#--extra-ldflags=}"
  ;;
  --cpu=*) cpu=`echo $opt | cut -d '=' -f 2`
  ;;
  --enable-mingw32) mingw32="yes" ; cross_prefix="i386-mingw32-"
  ;;
  --kernel-path=*) kernel_path=${opt#--kernel-path=}
  ;;
  esac
done

# Checking for CFLAGS
if test -z "$CFLAGS"; then
    CFLAGS="-O2"
fi

cc="${cross_prefix}${cc}"
ar="${cross_prefix}${ar}"
strip="${cross_prefix}${strip}"

if test "$mingw32" = "yes" ; then
    linux="no"
    EXESUF=".exe"
    gdbstub="no"
    oss="no"
    if [ "$cpu" = "i386" ] ; then
        kqemu="yes"
    fi
fi

if test -z "$cross_prefix" ; then

# ---
# big/little endian test
cat > $TMPC << EOF
#include <inttypes.h>
int main(int argc, char ** argv){
        volatile uint32_t i=0x01234567;
        return (*((uint8_t*)(&i))) == 0x67;
}
EOF

if $cc -o $TMPE $TMPC 2>/dev/null ; then
$TMPE && bigendian="yes"
else
echo big/little test failed
fi

else

# if cross compiling, cannot launch a program, so make a static guess
if test "$cpu" = "powerpc" -o "$cpu" = "mips" -o "$cpu" = "s390" -o "$cpu" = "sparc" -o "$cpu" = "sparc64" -o "$cpu" = "m68k" -o "$cpu" = "armv4b"; then
    bigendian="yes"
fi

fi

# host long bits test
hostlongbits="32"
if test "$cpu" = "sparc64" -o "$cpu" = "ia64" -o "$cpu" = "x86_64" -o "$cpu" = "alpha"; then
    hostlongbits="64"
fi

# check gcc options support
cat > $TMPC <<EOF
int main(void) {
}
EOF

if test x"$show_help" = x"yes" ; then
cat << EOF

Usage: configure [options]
Options: [defaults in brackets after descriptions]

EOF
echo "Standard options:"
echo "  --help                   print this message"
echo "  --prefix=PREFIX          install in PREFIX [$prefix]"
echo "  --kernel-path=PATH       set the kernel path (configure probes it)"
echo ""
echo "Advanced options (experts only):"
echo "  --source-path=PATH       path of source code [$source_path]"
echo "  --cross-prefix=PREFIX    use PREFIX for compile tools [$cross_prefix]"
echo "  --cc=CC                  use C compiler CC [$cc]"
echo "  --host-cc=CC             use C compiler CC [$host_cc]"
echo "  --make=MAKE              use specified make [$make]"
echo ""
exit 1
fi

# Linux specific kqemu configuration
if test $kqemu = "yes" -a $linux = "yes" ; then
# find the kernel path
if test -z "$kernel_path" ; then
kernel_version=`uname -r`
kernel_path="/lib/modules/$kernel_version/build"
if test '!' -d "$kernel_path/include" ; then
    kernel_path="/usr/src/linux"
    if test '!' -d "$kernel_path/include" ; then
        echo "Could not find kernel includes in /lib/modules or /usr/src/linux - cannot build the kqemu module"
        kqemu="no"
    fi
fi
fi

if test $kqemu = "yes" ; then

# test that the kernel config is present
if test '!' -f "$kernel_path/Makefile" ; then
    echo "No Makefile file present in $kernel_path - kqemu cannot be built"
    kqemu="no"
fi

# find build system (2.6 or legacy)
kbuild26="yes"
if grep -q "PATCHLEVEL = 4" $kernel_path/Makefile ; then
kbuild26="no"
fi

fi # kqemu

fi # kqemu and linux


echo "Source path       $source_path"
echo "C compiler        $cc"
echo "Host C compiler   $host_cc"
echo "make              $make"
echo "host CPU          $cpu"
if test $kqemu = "yes" -a $linux = "yes" ; then
echo ""
echo "kernel sources    $kernel_path"
echo -n "kbuild type       "
if test $kbuild26 = "yes"; then
echo "2.6"
else
echo "2.4"
fi
fi

config_mak="config-host.mak"

#echo "Creating $config_mak and $config_h"

echo "# Automatically generated by configure - do not modify" > $config_mak

echo "prefix=$prefix" >> $config_mak
echo "bindir=$bindir" >> $config_mak
echo "mandir=$mandir" >> $config_mak
echo "datadir=$datadir" >> $config_mak
echo "docdir=$docdir" >> $config_mak
echo "MAKE=$make" >> $config_mak
echo "CC=$cc" >> $config_mak
echo "HOST_CC=$host_cc" >> $config_mak
echo "AR=$ar" >> $config_mak
echo "STRIP=$strip -s -R .comment -R .note" >> $config_mak
echo "CFLAGS=$CFLAGS" >> $config_mak
echo "LDFLAGS=$LDFLAGS" >> $config_mak
echo "EXESUF=$EXESUF" >> $config_mak
if test "$cpu" = "i386" ; then
  echo "ARCH=i386" >> $config_mak
elif test "$cpu" = "x86_64" ; then
  echo "ARCH=x86_64" >> $config_mak
else
  echo "Unsupported CPU"
  exit 1
fi
if test "$bigendian" = "yes" ; then
  echo "WORDS_BIGENDIAN=yes" >> $config_mak
fi
if test "$mingw32" = "yes" ; then
  echo "CONFIG_WIN32=yes" >> $config_mak
fi
if test "$darwin" = "yes" ; then
  echo "CONFIG_DARWIN=yes" >> $config_mak
  echo "#define CONFIG_DARWIN 1" >> $config_h
fi
if test $linux = "yes" ; then
  echo "KERNEL_PATH=$kernel_path" >> $config_mak
  if test $kbuild26 = "yes" ; then
    echo "CONFIG_KBUILD26=yes" >> $config_mak
  fi
fi
echo "SRC_PATH=$source_path" >> $config_mak

rm -f $TMPO $TMPC $TMPE $TMPS
