#!/bin/sh
#
# qemu configure script (c) 2003 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}.exe"

trap "rm -f $TMPC $TMPO $TMPE ; exit" 0 2 3 15

compile_object() {
  $cc $QEMU_CFLAGS -c -o $TMPO $TMPC > /dev/null 2> /dev/null
}

compile_prog() {
  local_cflags="$1"
  local_ldflags="$2"
  $cc $QEMU_CFLAGS $local_cflags -o $TMPE $TMPC $LDFLAGS $local_ldflags > /dev/null 2> /dev/null
}

# default parameters
cpu=""
static="no"
sparc_cpu=""
cross_prefix=""
cc="gcc"
block_drv_whitelist=""
host_cc="gcc"
ar="ar"
make="make"
ld="ld"
helper_cflags=""
libs_softmmu=""

# parse CC options first
for opt do
  optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
  case "$opt" in
  --cross-prefix=*) cross_prefix="$optarg"
  ;;
  --cc=*) cc="$optarg"
  ;;
  --cpu=*) cpu="$optarg"
  ;;
  --extra-cflags=*) QEMU_CFLAGS="$optarg $QEMU_CFLAGS"
  ;;
  --extra-ldflags=*) LDFLAGS="$optarg $LDFLAGS"
  ;;
  --sparc_cpu=*)
    sparc_cpu="$optarg"
    case $sparc_cpu in
    v7|v8|v8plus|v8plusa)
      cpu="sparc"
    ;;
    v9)
      cpu="sparc64"
    ;;
    *)
      echo "undefined SPARC architecture. Exiting";
      exit 1
    ;;
    esac
  ;;
  esac
done
# OS specific
# Using uname is really, really broken.  Once we have the right set of checks
# we can eliminate it's usage altogether

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

# default flags for all hosts
QEMU_CFLAGS="-fno-strict-aliasing $QEMU_CFLAGS -Wno-error=array-bounds"
CFLAGS="-g $CFLAGS"
QEMU_CFLAGS="-Wall -Wundef -Wendif-labels -Wwrite-strings -Wmissing-prototypes $QEMU_CFLAGS"
QEMU_CFLAGS="-Wstrict-prototypes -Wredundant-decls $QEMU_CFLAGS"
QEMU_CFLAGS="-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE $QEMU_CFLAGS"
QEMU_CFLAGS="-U_FORTIFY_SOURCE $QEMU_CFLAGS"
QEMU_CFLAGS="-I. -I\$(SRC_PATH) $QEMU_CFLAGS"
LDFLAGS="-g $LDFLAGS"

gcc_flags="-Wold-style-declaration -Wold-style-definition"
cat > $TMPC << EOF
int main(void) { }
EOF
for flag in $gcc_flags; do
    if compile_prog "$QEMU_CFLAGS" "$flag" ; then
        QEMU_CFLAGS="$flag $QEMU_CFLAGS"
    fi
done

# check that the C compiler works.
cat > $TMPC <<EOF
int main(void) {}
EOF

if compile_object ; then
  : C compiler works ok
else
    echo "ERROR: \"$cc\" either does not exist or does not work"
    exit 1
fi

check_define() {
cat > $TMPC <<EOF
#if !defined($1)
#error Not defined
#endif
int main(void) { return 0; }
EOF
  compile_object
}

if test ! -z "$cpu" ; then
  # command line argument
  :
elif check_define __i386__ ; then
  cpu="i386"
elif check_define __x86_64__ ; then
  cpu="x86_64"
elif check_define __sparc__ ; then
  # We can't check for 64 bit (when gcc is biarch) or V8PLUSA
  # They must be specified using --sparc_cpu
  if check_define __arch64__ ; then
    cpu="sparc64"
  else
    cpu="sparc"
  fi
elif check_define _ARCH_PPC ; then
  if check_define _ARCH_PPC64 ; then
    cpu="ppc64"
  else
    cpu="ppc"
  fi
elif check_define __mips__ ; then
  cpu="mips"
else
  cpu=`uname -m`
fi

case "$cpu" in
  alpha|cris|ia64|m68k|microblaze|ppc|ppc64|sparc64)
    cpu="$cpu"
  ;;
  i386|i486|i586|i686|i86pc|BePC)
    cpu="i386"
  ;;
  x86_64|amd64)
    cpu="x86_64"
  ;;
  armv*b)
    cpu="armv4b"
  ;;
  armv*l)
    cpu="armv4l"
  ;;
  aarch64)
    cpu="aarch64"
  ;;
  parisc|parisc64)
    cpu="hppa"
  ;;
  mips*)
    cpu="mips"
  ;;
  s390)
    cpu="s390"
  ;;
  s390x)
    cpu="s390x"
  ;;
  sparc|sun4[cdmuv])
    cpu="sparc"
  ;;
  *)
    cpu="unknown"
  ;;
esac

# Default value for a variable defining feature "foo".
#  * foo="no"  feature will only be used if --enable-foo arg is given
#  * foo=""    feature will be searched for, and if found, will be used
#              unless --disable-foo is given
#  * foo="yes" this value will only be set by --enable-foo flag.
#              feature will searched for,
#              if not found, configure exits with error
#
# Always add --enable-foo and --disable-foo command line args.
# Distributions want to ensure that several features are compiled in, and it
# is impossible without a --enable-foo that exits if a feature is not found.

sparse="no"

gprof="no"
debug_tcg="no"
debug="no"
strip_opt="yes"
bigendian="no"
mingw32="no"
EXESUF=""
bsd="no"
linux="no"
solaris="no"
profiler="no"
aix="no"
pkgversion=""
zero_malloc=""

# OS specific
if check_define __linux__ ; then
  targetos="Linux"
elif check_define _WIN32 ; then
  targetos='MINGW32'
elif check_define __OpenBSD__ ; then
  targetos='OpenBSD'
elif check_define __sun__ ; then
  targetos='SunOS'
else
  targetos=`uname -s`
fi

case $targetos in
CYGWIN*)
  mingw32="yes"
  QEMU_CFLAGS="-mno-cygwin $QEMU_CFLAGS"
;;
MINGW32*)
  mingw32="yes"
;;
GNU/kFreeBSD)
  bsd="yes"
;;
FreeBSD)
  bsd="yes"
  make="gmake"
;;
DragonFly)
  bsd="yes"
  make="gmake"
;;
NetBSD)
  bsd="yes"
  make="gmake"
;;
OpenBSD)
  bsd="yes"
  make="gmake"
;;
Darwin)
  bsd="yes"
  darwin="yes"
  # on Leopard most of the system is 32-bit, so we have to ask the kernel it if we can
  # run 64-bit userspace code
  if [ "$cpu" = "i386" ] ; then
    is_x86_64=`sysctl -n hw.optional.x86_64`
    [ "$is_x86_64" = "1" ] && cpu=x86_64
  fi
  if [ "$cpu" = "x86_64" ] ; then
    QEMU_CFLAGS="-arch x86_64 $QEMU_CFLAGS"
    LDFLAGS="-arch x86_64 $LDFLAGS"
  else
    QEMU_CFLAGS="-mdynamic-no-pic $QEMU_CFLAGS"
  fi
  LDFLAGS="-framework CoreFoundation -framework IOKit $LDFLAGS"
  libs_softmmu="-F/System/Library/Frameworks -framework Cocoa -framework IOKit $libs_softmmu"
;;
SunOS)
  solaris="yes"
  make="gmake"
  ld="gld"
  needs_libsunmath="no"
  solarisrev=`uname -r | cut -f2 -d.`
  # have to select again, because `uname -m` returns i86pc
  # even on an x86_64 box.
  solariscpu=`isainfo -k`
  if test "${solariscpu}" = "amd64" ; then
    cpu="x86_64"
  fi
  if [ "$cpu" = "i386" -o "$cpu" = "x86_64" ] ; then
    if test "$solarisrev" -le 9 ; then
      if test -f /opt/SUNWspro/prod/lib/libsunmath.so.1; then
        needs_libsunmath="yes"
        QEMU_CFLAGS="-I/opt/SUNWspro/prod/include/cc $QEMU_CFLAGS"
        LDFLAGS="-L/opt/SUNWspro/prod/lib -R/opt/SUNWspro/prod/lib $LDFLAGS"
        LIBS="-lsunmath $LIBS"
      else
        echo "QEMU will not link correctly on Solaris 8/X86 or 9/x86 without"
        echo "libsunmath from the Sun Studio compilers tools, due to a lack of"
        echo "C99 math features in libm.so in Solaris 8/x86 and Solaris 9/x86"
        echo "Studio 11 can be downloaded from www.sun.com."
        exit 1
      fi
    fi
  fi
# needed for CMSG_ macros in sys/socket.h
  QEMU_CFLAGS="-D_XOPEN_SOURCE=600 $QEMU_CFLAGS"
# needed for TIOCWIN* defines in termios.h
  QEMU_CFLAGS="-D__EXTENSIONS__ $QEMU_CFLAGS"
  QEMU_CFLAGS="-std=gnu99 $QEMU_CFLAGS"
  LIBS="-lsocket -lnsl -lresolv $LIBS"
;;
AIX)
  aix="yes"
  make="gmake"
;;
*)
  linux="yes"
;;
esac

if test "$mingw32" = "yes" ; then
  EXESUF=".exe"
  QEMU_CFLAGS="-DWIN32_LEAN_AND_MEAN -DWINVER=0x501 $QEMU_CFLAGS"
  LIBS="-lwinmm -lws2_32 -liphlpapi $LIBS"
fi

# find source path
source_path=`dirname "$0"`
source_path_used="no"
workdir=`pwd`
if [ -z "$source_path" ]; then
    source_path=$workdir
else
    source_path=`cd "$source_path"; pwd`
fi
[ -f "$workdir/exec.c" ] || source_path_used="yes"

werror=""

for opt do
  optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
  case "$opt" in
  --help|-h) show_help=yes
  ;;
  --source-path=*) source_path="$optarg"
  source_path_used="yes"
  ;;
  --cross-prefix=*)
  ;;
  --cc=*)
  ;;
  --host-cc=*) host_cc="$optarg"
  ;;
  --make=*) make="$optarg"
  ;;
  --extra-cflags=*)
  ;;
  --extra-ldflags=*)
  ;;
  --cpu=*)
  ;;
  --enable-gprof) gprof="yes"
  ;;
  --static) static="yes"
  ;;
  --block-drv-whitelist=*) block_drv_whitelist=`echo "$optarg" | sed -e 's/,/ /g'`
  ;;
  --enable-debug-tcg) debug_tcg="yes"
  ;;
  --disable-debug-tcg) debug_tcg="no"
  ;;
  --enable-debug)
      # Enable debugging options that aren't excessively noisy
      debug_tcg="yes"
      debug="yes"
      strip_opt="no"
  ;;
  --enable-sparse) sparse="yes"
  ;;
  --disable-sparse) sparse="no"
  ;;
  --disable-strip) strip_opt="no"
  ;;
  --enable-profiler) profiler="yes"
  ;;
  --sparc_cpu=*)
  ;;
  --enable-werror) werror="yes"
  ;;
  --disable-werror) werror="no"
  ;;
  --with-pkgversion=*) pkgversion=" ($optarg)"
  ;;
  *) echo "ERROR: unknown option $opt"; show_help="yes"
  ;;
  esac
done

#
# If cpu ~= sparc and  sparc_cpu hasn't been defined, plug in the right
# QEMU_CFLAGS/LDFLAGS (assume sparc_v8plus for 32-bit and sparc_v9 for 64-bit)
#
case "$cpu" in
    sparc) case $sparc_cpu in
           v7|v8)
             QEMU_CFLAGS="-mcpu=${sparc_cpu} -D__sparc_${sparc_cpu}__ $QEMU_CFLAGS"
           ;;
           v8plus|v8plusa)
             QEMU_CFLAGS="-mcpu=ultrasparc -D__sparc_${sparc_cpu}__ $QEMU_CFLAGS"
           ;;
           *) # sparc_cpu not defined in the command line
             QEMU_CFLAGS="-mcpu=ultrasparc -D__sparc_v8plus__ $QEMU_CFLAGS"
           esac
           LDFLAGS="-m32 $LDFLAGS"
           QEMU_CFLAGS="-m32 -ffixed-g2 -ffixed-g3 $QEMU_CFLAGS"
           if test "$solaris" = "no" ; then
             QEMU_CFLAGS="-ffixed-g1 -ffixed-g6 $QEMU_CFLAGS"
             helper_cflags="-ffixed-i0"
           fi
           ;;
    sparc64)
           QEMU_CFLAGS="-m64 -mcpu=ultrasparc -D__sparc_v9__ $QEMU_CFLAGS"
           LDFLAGS="-m64 $LDFLAGS"
           QEMU_CFLAGS="-ffixed-g5 -ffixed-g6 -ffixed-g7 $QEMU_CFLAGS"
           if test "$solaris" != "no" ; then
             QEMU_CFLAGS="-ffixed-g1 $QEMU_CFLAGS"
           fi
           ;;
    s390)
           QEMU_CFLAGS="-march=z900 $QEMU_CFLAGS"
           ;;
    i386)
           QEMU_CFLAGS="-m32 $QEMU_CFLAGS"
           LDFLAGS="-m32 $LDFLAGS"
           helper_cflags="-fomit-frame-pointer"
           ;;
    x86_64)
           QEMU_CFLAGS="-m64 $QEMU_CFLAGS"
           LDFLAGS="-m64 $LDFLAGS"
           ;;
esac

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 "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] for dyngen etc."
echo "  --extra-cflags=CFLAGS    append extra C compiler flags QEMU_CFLAGS"
echo "  --extra-ldflags=LDFLAGS  append extra linker flags LDFLAGS"
echo "  --make=MAKE              use specified make [$make]"
echo "  --static                 enable static build [$static]"
echo "  --enable-debug-tcg       enable TCG debugging"
echo "  --disable-debug-tcg      disable TCG debugging (default)"
echo "  --enable-debug           enable common debug build options"
echo "  --enable-sparse          enable sparse checker"
echo "  --disable-sparse         disable sparse checker (default)"
echo "  --disable-strip          disable stripping binaries"
echo "  --disable-werror         disable compilation abort on warning"
echo "  --block-drv-whitelist=L  set block driver whitelist"
echo "                           (affects only QEMU, not qemu-img)"
echo "  --sparc_cpu=V            Build qemu for Sparc architecture v7, v8, v8plus, v8plusa, v9"
echo ""
echo "NOTE: The object files are built at the place where configure is launched"
exit 1
fi

#
# Solaris specific configure tool chain decisions
#
if test "$solaris" = "yes" ; then
  sol_ar=`which ar 2> /dev/null | /usr/bin/grep -v "no ar in"`
  if test -z "$sol_ar" ; then
    echo "Error: No path includes ar"
    if test -f /usr/ccs/bin/ar ; then
      echo "Add /usr/ccs/bin to your path and rerun configure"
    fi
    exit 1
  fi
fi


feature_not_found() {
  feature=$1

  echo "ERROR"
  echo "ERROR: User requested feature $feature"
  echo "ERROR: configure was not able to find it"
  echo "ERROR"
  exit 1;
}

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 compile_prog "" "" ; then
$TMPE && bigendian="yes"
else
echo big/little test failed
fi

else

# if cross compiling, cannot launch a program, so make a static guess
case "$cpu" in
  armv4b|hppa|m68k|mips|mips64|ppc|ppc64|s390|s390x|sparc|sparc64)
    bigendian=yes
  ;;
esac

fi

# host long bits test
hostlongbits="32"
case "$cpu" in
  x86_64|alpha|ia64|sparc64|ppc64|s390x)
    hostlongbits=64
  ;;
esac


##########################################
# zlib check

cat > $TMPC << EOF
#include <zlib.h>
int main(void) { zlibVersion(); return 0; }
EOF
if compile_prog "" "-lz" ; then
    :
else
    echo
    echo "Error: zlib check failed"
    echo "Make sure to have the zlib libs and headers installed."
    echo
    exit 1
fi

##########################################
# Sparse probe
if test "$sparse" != "no" ; then
  if test -x "$(which cgcc 2>/dev/null)"; then
    sparse=yes
  else
    if test "$sparse" = "yes" ; then
      feature_not_found "sparse"
    fi
    sparse=no
  fi
fi

##########################################
# fnmatch() probe, used for ACL routines
fnmatch="no"
cat > $TMPC << EOF
#include <fnmatch.h>
int main(void)
{
    fnmatch("foo", "foo", 0);
    return 0;
}
EOF
if compile_prog "" "" ; then
   fnmatch="yes"
fi

##########################################
# pthread probe
PTHREADLIBS_LIST="-lpthread -lpthreadGC2"

pthread=no
cat > $TMPC << EOF
#include <pthread.h>
int main(void) { pthread_create(0,0,0,0); return 0; }
EOF
for pthread_lib in $PTHREADLIBS_LIST; do
  if compile_prog "" "$pthread_lib" ; then
    pthread=yes
    LIBS="$pthread_lib $LIBS"
    break
  fi
done

if test "$mingw32" != yes -a "$pthread" = no; then
  echo
  echo "Error: pthread check failed"
  echo "Make sure to have the pthread libs and headers installed."
  echo
  exit 1
fi

##########################################
# iovec probe
cat > $TMPC <<EOF
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
int main(void) { struct iovec iov; return 0; }
EOF
iovec=no
if compile_prog "" "" ; then
  iovec=yes
fi

##########################################
# preadv probe
cat > $TMPC <<EOF
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
int main(void) { preadv; }
EOF
preadv=no
if compile_prog "" "" ; then
  preadv=yes
fi

# check if pipe2 is there
pipe2=no
cat > $TMPC << EOF
#define _GNU_SOURCE
#include <unistd.h>
#include <fcntl.h>

int main(void)
{
    int pipefd[2];
    pipe2(pipefd, O_CLOEXEC);
    return 0;
}
EOF
if compile_prog "" "" ; then
  pipe2=yes
fi

# check if accept4 is there
accept4=no
cat > $TMPC << EOF
#define _GNU_SOURCE
#include <sys/socket.h>
#include <stddef.h>

int main(void)
{
    accept4(0, NULL, NULL, SOCK_CLOEXEC);
    return 0;
}
EOF
if compile_prog "" "" ; then
  accept4=yes
fi

# Search for bswap_32 function
byteswap_h=no
cat > $TMPC << EOF
#include <byteswap.h>
int main(void) { return bswap_32(0); }
EOF
if compile_prog "" "" ; then
  byteswap_h=yes
fi

# Search for bswap_32 function
bswap_h=no
cat > $TMPC << EOF
#include <sys/endian.h>
#include <sys/types.h>
#include <machine/bswap.h>
int main(void) { return bswap32(0); }
EOF
if compile_prog "" "" ; then
  bswap_h=yes
fi

##########################################
# Do we need librt
cat > $TMPC <<EOF
#include <signal.h>
#include <time.h>
int main(void) { clockid_t id; return clock_gettime(id, NULL); }
EOF

if compile_prog "" "" ; then
  :
elif compile_prog "" "-lrt" ; then
  LIBS="-lrt $LIBS"
fi

if test "$darwin" != "yes" -a "$mingw32" != "yes" -a "$solaris" != yes -a \
        "$aix" != "yes" ; then
    libs_softmmu="-lutil $libs_softmmu"
fi

##########################################
# check if the compiler defines offsetof

need_offsetof=yes
cat > $TMPC << EOF
#include <stddef.h>
int main(void) { struct s { int f; }; return offsetof(struct s, f); }
EOF
if compile_prog "" "" ; then
    need_offsetof=no
fi

##########################################
# check if the compiler understands attribute warn_unused_result
#
# This could be smarter, but gcc -Werror does not error out even when warning
# about attribute warn_unused_result

gcc_attribute_warn_unused_result=no
cat > $TMPC << EOF
#if defined(__GNUC__) && (__GNUC__ < 4) && defined(__GNUC_MINOR__) && (__GNUC__ < 4)
#error gcc 3.3 or older
#endif
int main(void) { return 0;}
EOF
if compile_prog "" ""; then
    gcc_attribute_warn_unused_result=yes
fi

##########################################
# check if we have fdatasync

fdatasync=no
cat > $TMPC << EOF
#include <unistd.h>
int main(void) { return fdatasync(0); }
EOF
if compile_prog "" "" ; then
    fdatasync=yes
fi

# End of CC checks
# After here, no more $cc or $ld runs

if test "$debug" = "no" ; then
  CFLAGS="-O2 $CFLAGS"
fi

# Consult white-list to determine whether to enable werror
# by default.  Only enable by default for git builds
z_version=`cut -f3 -d. $source_path/VERSION`

if test -z "$werror" ; then
    if test "$z_version" = "50" -a \
        "$linux" = "yes" ; then
        werror="yes"
    else
        werror="no"
    fi
fi

# Disable zero malloc errors for official releases unless explicitly told to
# enable/disable
if test -z "$zero_malloc" ; then
    if test "$z_version" = "50" ; then
        zero_malloc="no"
    else
        zero_malloc="yes"
    fi
fi

if test "$werror" = "yes" ; then
    QEMU_CFLAGS="-Werror $QEMU_CFLAGS"
fi

if test "$solaris" = "no" ; then
    if $ld --version 2>/dev/null | grep "GNU ld" >/dev/null 2>/dev/null ; then
        LDFLAGS="-Wl,--warn-common $LDFLAGS"
    fi
fi

echo "Source path       $source_path"
echo "C compiler        $cc"
echo "Host C compiler   $host_cc"
echo "CFLAGS            $CFLAGS"
echo "QEMU_CFLAGS       $QEMU_CFLAGS"
echo "LDFLAGS           $LDFLAGS"
echo "make              $make"
echo "host CPU          $cpu"
echo "host big endian   $bigendian"
echo "target list       arm-softmmu"
echo "tcg debug enabled $debug_tcg"
echo "gprof enabled     $gprof"
echo "sparse enabled    $sparse"
echo "strip binaries    $strip_opt"
echo "profiler          $profiler"
echo "static build      $static"
echo "-Werror enabled   $werror"
echo "mingw32 support   $mingw32"
echo "Block whitelist   $block_drv_whitelist"
if test -n "$sparc_cpu"; then
    echo "Target Sparc Arch $sparc_cpu"
fi
echo "preadv support    $preadv"
echo "fdatasync         $fdatasync"

config_host_mak="config-host.mak"
config_host_ld="config-host.ld"

echo "# Automatically generated by configure - do not modify" > $config_host_mak
printf "# Configured with:" >> $config_host_mak
printf " '%s'" "$0" "$@" >> $config_host_mak
echo >> $config_host_mak

case "$cpu" in
  i386|x86_64|alpha|cris|hppa|ia64|m68k|microblaze|mips|mips64|ppc|ppc64|s390|s390x|sparc|sparc64)
    ARCH=$cpu
  ;;
  armv4b|armv4l|aarch64)
    ARCH=arm
  ;;
  *)
    echo "Unsupported CPU = $cpu"
    exit 1
  ;;
esac
echo "ARCH=$ARCH" >> $config_host_mak
if test "$debug_tcg" = "yes" ; then
  echo "CONFIG_DEBUG_TCG=y" >> $config_host_mak
fi
if test "$debug" = "yes" ; then
  echo "CONFIG_DEBUG_EXEC=y" >> $config_host_mak
fi
if test "$strip_opt" = "yes" ; then
  echo "STRIP_OPT=-s" >> $config_host_mak
fi
if test "$bigendian" = "yes" ; then
  echo "HOST_WORDS_BIGENDIAN=y" >> $config_host_mak
fi
echo "HOST_LONG_BITS=$hostlongbits" >> $config_host_mak
if test "$mingw32" = "yes" ; then
  echo "CONFIG_WIN32=y" >> $config_host_mak
else
  echo "CONFIG_POSIX=y" >> $config_host_mak
fi

if test "$linux" = "yes" ; then
  echo "CONFIG_LINUX=y" >> $config_host_mak
fi

if test "$darwin" = "yes" ; then
  echo "CONFIG_DARWIN=y" >> $config_host_mak
fi

if test "$aix" = "yes" ; then
  echo "CONFIG_AIX=y" >> $config_host_mak
fi

if test "$solaris" = "yes" ; then
  echo "CONFIG_SOLARIS=y" >> $config_host_mak
  echo "CONFIG_SOLARIS_VERSION=$solarisrev" >> $config_host_mak
  if test "$needs_libsunmath" = "yes" ; then
    echo "CONFIG_NEEDS_LIBSUNMATH=y" >> $config_host_mak
  fi
fi
if test "$static" = "yes" ; then
  echo "CONFIG_STATIC=y" >> $config_host_mak
  LDFLAGS="-static $LDFLAGS"
fi
if test $profiler = "yes" ; then
  echo "CONFIG_PROFILER=y" >> $config_host_mak
fi
echo "CONFIG_BDRV_WHITELIST=$block_drv_whitelist" >> $config_host_mak
if test "$fnmatch" = "yes" ; then
  echo "CONFIG_FNMATCH=y" >> $config_host_mak
fi
qemu_version=`head $source_path/VERSION`
echo "VERSION=$qemu_version" >>$config_host_mak
echo "PKGVERSION=$pkgversion" >>$config_host_mak
echo "SRC_PATH=$source_path" >> $config_host_mak
if test "$pipe2" = "yes" ; then
  echo "CONFIG_PIPE2=y" >> $config_host_mak
fi
if test "$accept4" = "yes" ; then
  echo "CONFIG_ACCEPT4=y" >> $config_host_mak
fi
if test "$byteswap_h" = "yes" ; then
  echo "CONFIG_BYTESWAP_H=y" >> $config_host_mak
fi
if test "$bswap_h" = "yes" ; then
  echo "CONFIG_MACHINE_BSWAP_H=y" >> $config_host_mak
fi
if test "$iovec" = "yes" ; then
  echo "CONFIG_IOVEC=y" >> $config_host_mak
fi
if test "$preadv" = "yes" ; then
  echo "CONFIG_PREADV=y" >> $config_host_mak
fi
if test "$need_offsetof" = "yes" ; then
  echo "CONFIG_NEED_OFFSETOF=y" >> $config_host_mak
fi
if test "$gcc_attribute_warn_unused_result" = "yes" ; then
  echo "CONFIG_GCC_ATTRIBUTE_WARN_UNUSED_RESULT=y" >> $config_host_mak
fi
if test "$fdatasync" = "yes" ; then
  echo "CONFIG_FDATASYNC=y" >> $config_host_mak
fi

# XXX: suppress that
if [ "$bsd" = "yes" ] ; then
  echo "CONFIG_BSD=y" >> $config_host_mak
fi

if test "$zero_malloc" = "yes" ; then
  echo "CONFIG_ZERO_MALLOC=y" >> $config_host_mak
fi

echo "MAKE=$make" >> $config_host_mak
echo "CC=$cc" >> $config_host_mak
echo "HOST_CC=$host_cc" >> $config_host_mak
if test "$sparse" = "yes" ; then
  echo "CC      := REAL_CC=\"\$(CC)\" cgcc"       >> $config_host_mak
  echo "HOST_CC := REAL_CC=\"\$(HOST_CC)\" cgcc"  >> $config_host_mak
  echo "QEMU_CFLAGS  += -Wbitwise -Wno-transparent-union -Wno-old-initializer -Wno-non-pointer-null" >> $config_host_mak
fi
echo "AR=$ar" >> $config_host_mak
echo "LD=$ld" >> $config_host_mak
echo "CFLAGS=$CFLAGS" >> $config_host_mak
echo "QEMU_CFLAGS=$QEMU_CFLAGS" >> $config_host_mak
echo "HELPER_CFLAGS=$helper_cflags" >> $config_host_mak
echo "LDFLAGS=$LDFLAGS" >> $config_host_mak
echo "ARLIBS_BEGIN=$arlibs_begin" >> $config_host_mak
echo "ARLIBS_END=$arlibs_end" >> $config_host_mak
echo "LIBS+=$LIBS" >> $config_host_mak
echo "EXESUF=$EXESUF" >> $config_host_mak

# generate list of library paths for linker script

$ld --verbose -v 2> /dev/null | grep SEARCH_DIR > ${config_host_ld}

if test -f ${config_host_ld}~ ; then
  if cmp -s $config_host_ld ${config_host_ld}~ ; then
    mv ${config_host_ld}~ $config_host_ld
  else
    rm ${config_host_ld}~
  fi
fi

config_target_mak=arm-softmmu/config-target.mak

mkdir -p arm-softmmu
mkdir -p arm-softmmu/fpu
mkdir -p arm-softmmu/tcg

#
# don't use ln -sf as not all "ln -sf" over write the file/link
#
rm -f arm-softmmu/Makefile
ln -s $source_path/Makefile-small.target arm-softmmu/Makefile


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

gdb_xml_files="arm-core.xml arm-vfp.xml arm-vfp3.xml arm-neon.xml"
target_phys_bits=32
echo "TARGET_ARCH=arm" >> $config_target_mak
echo "TARGET_ARM=y" >> $config_target_mak
echo "TARGET_BASE_ARCH=arm" >> $config_target_mak
if [ $target_phys_bits -lt $hostlongbits ] ; then
  target_phys_bits=$hostlongbits
fi
echo "TARGET_PHYS_ADDR_BITS=$target_phys_bits" >> $config_target_mak
echo "CONFIG_SOFTMMU=y" >> $config_target_mak
echo "LIBS+=$libs_softmmu" >> $config_target_mak
echo "HWDIR=../libhw$target_phys_bits" >> $config_target_mak
echo "subdir-arm-softmmu: subdir-libhw$target_phys_bits" >> $config_host_mak
list=""
if test ! -z "$gdb_xml_files" ; then
  for x in $gdb_xml_files; do
    list="$list $source_path/gdb-xml/$x"
  done
  echo "TARGET_XML_FILES=$list" >> $config_target_mak
fi

echo "CONFIG_SOFTFLOAT=y" >> $config_target_mak
#echo "CONFIG_NOSOFTFLOAT=y" >> $config_target_mak

# generate QEMU_CFLAGS/LDFLAGS for targets

cflags=""
ldflags=""

if test "$ARCH" = "sparc64" ; then
  cflags="-I\$(SRC_PATH)/tcg/sparc $cflags"
elif test "$ARCH" = "s390x" ; then
  cflags="-I\$(SRC_PATH)/tcg/s390 $cflags"
else
  cflags="-I\$(SRC_PATH)/tcg/\$(ARCH) $cflags"
fi
cflags="-I\$(SRC_PATH)/tcg $cflags"
cflags="-I\$(SRC_PATH)/fpu $cflags"

for i in $ARCH arm ; do
  case "$i" in
  alpha)
    echo "CONFIG_ALPHA_DIS=y"  >> $config_target_mak
  ;;
  arm)
    echo "CONFIG_ARM_DIS=y"  >> $config_target_mak
  ;;
  cris)
    echo "CONFIG_CRIS_DIS=y"  >> $config_target_mak
  ;;
  hppa)
    echo "CONFIG_HPPA_DIS=y"  >> $config_target_mak
  ;;
  i386|x86_64)
    echo "CONFIG_I386_DIS=y"  >> $config_target_mak
  ;;
  m68k)
    echo "CONFIG_M68K_DIS=y"  >> $config_target_mak
  ;;
  microblaze)
    echo "CONFIG_MICROBLAZE_DIS=y"  >> $config_target_mak
  ;;
  mips*)
    echo "CONFIG_MIPS_DIS=y"  >> $config_target_mak
  ;;
  ppc*)
    echo "CONFIG_PPC_DIS=y"  >> $config_target_mak
  ;;
  s390*)
    echo "CONFIG_S390_DIS=y"  >> $config_target_mak
  ;;
  sh4)
    echo "CONFIG_SH4_DIS=y"  >> $config_target_mak
  ;;
  sparc*)
    echo "CONFIG_SPARC_DIS=y"  >> $config_target_mak
  ;;
  esac
done

case "$ARCH" in
alpha)
  # Ensure there's only a single GP
  cflags="-msmall-data $cflags"
;;
ia64)
  cflags="-mno-sdata $cflags"
;;
esac

if test "$gprof" = "yes" ; then
  echo "TARGET_GPROF=yes" >> $config_target_mak
  ldflags="-p $ldflags"
  echo "GPROF_CFLAGS=-p" >> $config_target_mak
fi

linker_script="-Wl,-T../config-host.ld -Wl,-T,\$(SRC_PATH)/\$(ARCH).ld"
case "$ARCH" in
ia64)
  ldflags="-Wl,-G0 $linker_script -static $ldflags"
  ;;
esac

echo "LDFLAGS+=$ldflags" >> $config_target_mak
echo "QEMU_CFLAGS+=$cflags" >> $config_target_mak

# build tree in object directory if source path is different from current one
if test "$source_path_used" = "yes" ; then
    FILES="Makefile-small"
    # remove the link and recreate it, as not all "ln -sf" overwrite the link
    for f in $FILES ; do
        rm -f $f
        ln -s $source_path/$f $f
    done
fi

for hwlib in 32 64; do
  d=libhw$hwlib
  mkdir -p $d
  rm -f $d/Makefile
  ln -s $source_path/Makefile-small.hw $d/Makefile
  echo "QEMU_CFLAGS+=-DTARGET_PHYS_ADDR_BITS=$hwlib" >> $d/config.mak
done
