#!/bin/sh
# The purpose of this script is to check for all external tools, headers, and
# libraries VBox OSE depends on.

#
# Copyright (C) 2006-2007 Sun Microsystems, Inc.
#
# This file is part of VirtualBox Open Source Edition (OSE), as
# available from http://www.virtualbox.org. This file is free software;
# you can redistribute it and/or modify it under the terms of the GNU
# General Public License as published by the Free Software Foundation,
# in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
# distribution. VirtualBox OSE is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY of any kind.
#

LC_ALL=C
export LC_ALL

# append some extra paths
PATH="$PATH:/opt/gnome/bin"
# Solaris (order of paths important for tr, echo, grep, sed to work)
PATH="/usr/xpg4/bin:/usr/ucb:$PATH:/usr/local/bin:/usr/sfw/bin:/usr/ccs/bin"
ORGPATH=$PATH

#
# Defaults
#
OSE=1
ODIR="`pwd`/"
SETUP_WINE=
TARGET_MACHINE=""
TARGET_CPU=""
WITH_XPCOM=1
WITH_LIBIDL=1
WITH_QT=1
WITH_QT4=1
WITH_SDL=1
WITH_SDL_TTF=1
WITH_X11=1
WITH_ALSA=1
WITH_PULSE=1
WITH_KMODS=1
CC="gcc"
CC_COMPAT=""
CC32=""
CC64=""
CXX="g++"
CXX32=""
CXX64=""
BCC="bcc"
YASM="yasm"
IASL="iasl"
AS86="as86"
XSLTPROC="xsltproc"
GENISOIMAGE="genisoimage"
MKISOFS="mkisofs"
BUILD_LIBXML2=
BUILD_LIBXSLT=
LIBCRYPTO="-lcrypto"
LIBPTHREAD="-lpthread"
LIBX11="-L/usr/X11R6/lib -L/usr/X11R6/lib64 -L/usr/local/lib -lXext -lX11"
INCX11="/usr/local/include"
LIBXCURSOR="-lXcursor"
INCZ=""
LIBZ="-lz"
INCPNG=""
LIBPNG="-lpng"
QT3DIR="/usr/qt/3 /usr/lib/qt3 /usr/lib/qt-3.3 /usr/share/qt3 /usr/lib64/qt-3.3 /usr/X11R6 /usr/lib/qt"
QT4DIR="/usr/lib/qt4 /usr/share/qt4 /usr"
QT4UIC3DIR="/usr/bin"
KBUILDDIR="`cd \`dirname $0\`; pwd`/kBuild"
DEVDIR="`cd \`dirname $0\`; pwd`/tools"
if [ -d "/lib/modules/`uname -r`/build" ]; then
 LINUX="/lib/modules/`uname -r`/build"
else
 LINUX="/usr/src/linux"
fi
KCHMVIEWER="kchmviewer"
LOG="configure.log"
CNF="AutoConfig.kmk"
ENV="env.sh"
BUILD_TYPE="release"
# the restricting tool is ar (mri mode).
INVALID_CHARS="[^A-Za-z0-9/\\$:._-]"

if (cd `dirname $0`; pwd)|grep -q "$INVALID_CHARS"; then
  echo "Error: VBox base path contains invalid characters!"
  exit 1
fi

# darwin /bin/sh has a builtin echo that doesn't grok -n. gotta love it.
if [ "`uname`" = "Darwin" ]; then
  ECHO_N="/bin/echo -n"
else
  ECHO_N="echo -n"
fi


cleanup()
{
  rm -f .tmp_src.cc .tmp_src.c .tmp_out .test_execute.log
}

fail()
{
  if [ -z "$nofatal" -o "x$1" != "x" ]; then
    cleanup
    rm -f $ENV
    exit 1
  fi
}

log()
{
  echo "$1"
  echo "$1" >> $LOG
}

log_success()
{
  if [ -n "$1" ]; then $ECHO_N "$1, "; fi
  echo "OK."
  echo "$1" >> $LOG
  echo >> $LOG
  echo >> $LOG
}

log_failure()
{
  echo
  echo "  ** $1!"
  echo "** $1!" >> $LOG
  echo >> $LOG
}

cnf_append()
{
  printf "%-30s := %s\n" "$1" "$2" >> $CNF
}

strip_l()
{
  echo "$1"|$KBUILD_SED 's|-l\([^ ]\+\)|\1|g; s|-[^l]\([^ ]\+\)||g; s|^ ||; s| *$||g'
}

strip_L()
{
  echo "$1"|$KBUILD_SED 's|-L\([^ ]\+\)|\1|g; s|-[^L]\([^ ]\+\)||g; s|^ ||; s| *$||g'
}

strip_I()
{
  echo "$1"|$KBUILD_SED 's|-I\([^ ]\+\)|\1|g; s|-[^I]\([^ ]\+\)||g; s|^ ||; s| *$||g'
}

prefix_I()
{
  echo "$1"|$KBUILD_SED 's|^\/|-I/|g; s| \/| -I/|g'
}

# Wrapper for ancient /usr/bin/which on darwin that always returns 0
which_wrapper()
{
  if [ -z "$have_ancient_which" ]; then
    if which /bin/___cErTaINly_a_nOn_eXisTing_fIle___ 2> /dev/null > /dev/null; then
      have_ancient_which="yes"
    else
      have_ancient_which="no"
    fi
  fi
  if [ "$have_ancient_which" = "yes" ]; then
    retval=`which $* 2>/dev/null`
    echo "$retval"
    test -n "$retval" -a -e "$retval"
    unset retval
  else
    which $* 2> /dev/null
  fi
}

check_avail()
{
   if [ -z "$1" ]; then
     log_failure "$2 is empty"
     fail $3
     return 1
   elif which_wrapper $1 > /dev/null; then
     return 0
   else
     log_failure "$1 (variable $2) not found"
     fail $3
     return 1
   fi
}

# Prepare a test
test_header()
{
  echo "***** Checking $1 *****" >> $LOG
  $ECHO_N "Checking for $1: "
}

# Compile a test
test_compile()
{
  echo "compiling the following source file:" >> $LOG
  cat .tmp_src.cc >> $LOG
  echo "using the following command line:" >> $LOG
  echo "$CXX -O -Wall -o .tmp_out .tmp_src.cc \"$1\"" >> $LOG
  $CXX -O -Wall -o .tmp_out .tmp_src.cc $1 >> $LOG 2>&1
  if [ $? -ne 0 ]; then
    if [ -z "$4" ]; then
      echo
      echo "  $2 not found at $1 or $3 headers not found"
      echo "  Check the file $LOG for detailed error information."
      fail
    else
      echo "not found."
      echo >> $LOG
      echo >> $LOG
    fi
    return 1
  fi
  return 0
}

# Execute a compiled test binary
test_execute()
{
  echo "executing the binary" >> $LOG
  ./.tmp_out > .test_execute.log
  rc=$?
  cat .test_execute.log | tee -a $LOG
  if [ $rc -ne 0 ]; then
    fail $1
    return 1
  fi
  echo >> $LOG
  echo >> $LOG
  return 0
}

#
# Check for OS, MACHINE, CPU
#
check_environment()
{
  test_header environment
  OS=`uname -s | sed -e 's/GNU\/Linux/Linux/g' | tr [:upper:] [:lower:]`
  case "$OS" in
    linux)
      ;;
    darwin)
      ;;
    freebsd)
      ;;
    sunos)
      OS='solaris'
      ;;
    *)
      log_failure "Cannot determine OS"
      exit 1
      ;;
  esac
  BUILD_CPU=`uname -m`
  [ "$OS" = "solaris" ] && BUILD_CPU=`isainfo | cut -f 1 -d ' '`
  case "$BUILD_CPU" in
    i[3456789]86|x86|i86pc)
      BUILD_MACHINE='x86'
      LIB='lib'
      ;;
    x86_64|amd64)
      BUILD_MACHINE='amd64'
      BUILD_CPU='k8'
      if [ "$OS" != "solaris" ]; then
        # on AMD64 systems, 64bit libs are usually located in /usr/lib64
        # see http://www.pathname.com/fhs/pub/fhs-2.3.html#LIB64
        LIB='lib64'
      else
        # Solaris doesn't seem to subscribe to fhs, libs are usually in
        # a '64' subdirectory of the standard 'lib' dirs while some 64-bit
        # alternative binaries can be found in 'amd64' subdirs of the 'bin'
        # ones. So, in order to find the right stuff (esp. sdl-config) we'll
        # have to make sure the */bin/amd64 dirs are searched before the */bin
        # ones. (The sed has some sideeffects, but they shouldn't harm us...)
        echo "64-bit Solaris detected, hacking the PATH" >> $LOG
        echo "old PATH: $PATH" >> $LOG
        PATH=`echo ":$PATH:" | sed -e 's,\(:[^:]*/bin\):,\1/amd64:\1:,g' \
                                   -e 's/^:*//' -e 's/:*$//g' -e 's/::*/:/g' `
        export PATH
        echo "new PATH: $PATH" >> $LOG
        LIB='lib/64'
      fi
      ;;
    *)
      log_failure "Cannot determine system"
      exit 1
      ;;
  esac
  [ -z "$TARGET_MACHINE" ] && TARGET_MACHINE=$BUILD_MACHINE
  [ -z "$TARGET_CPU"     ] && TARGET_CPU=$BUILD_CPU
  DEVDIR_BIN="$DEVDIR/$OS.$BUILD_MACHINE/bin"
  log_success "Determined build machine: $OS.$BUILD_MACHINE, target machine: $OS.$TARGET_MACHINE"

  echo "export BUILD_PLATFORM=\"$OS\"" >> $ENV
  echo "export BUILD_PLATFORM_ARCH=\"$BUILD_MACHINE\"" >> $ENV
  echo "export BUILD_TARGET=\"$OS\"" >> $ENV
  echo "export BUILD_TARGET_ARCH=\"$TARGET_MACHINE\"" >> $ENV
  echo "export BUILD_TARGET_CPU=\"$TARGET_CPU\"" >> $ENV
  echo "export BUILD_TYPE=\"$BUILD_TYPE\"" >> $ENV
}

#
# Check for gcc with version >= 3.2.
# We depend on a working gcc, if we fail terminate in every case.
#
check_gcc()
{
  test_header gcc
  if check_avail "$CC" CC really; then
    cc_ver=`$CC -dumpversion` 2>/dev/null
    if [ $? -ne 0 ]; then
      log_failure "cannot execute '$CC -dumpversion'"
      fail really
    fi
    if check_avail "$CXX" CXX really; then
      cxx_ver=`$CXX -dumpversion` 2>/dev/null
      if [ $? -ne 0 ]; then
        log_failure "cannot execute '$CXX -dumpversion'"
        fail really
      fi
      cc_maj=`echo $cc_ver|cut -d. -f1`
      cc_min=`echo $cc_ver|cut -d. -f2`
      if [ "x$cc_ver" != "x$cxx_ver" ]; then
        log_failure "gcc version $cc_ver does not match g++ version $cxx_ver"
        fail really
      elif [ $cc_maj -eq 4 -a $cc_min -eq 0 -a "OS" = "darwin" ]; then
        log_success "found version $cc_ver"
      elif [ $cc_maj -eq 4 -a $cc_min -eq 3 ]; then
        if [ -z "$CC_COMPAT" ]; then
          log ""
          log "  ** There are known problems with gcc version 4.3 when compiling the recompiler"
          log "  ** stuff. You need to specify a compatibility compiler with version < 4.3. Look"
          log "  ** for a package compat-gcc-34 on Fedora systems or something similar on other"
          log "  ** distributions and call configure with parameter --with-gcc-compat=gcc34."
          fail really
        fi
        if check_avail "$CC_COMPAT" CC_COMPAT really; then
          cc_compat_ver=`$CC_COMPAT -dumpversion` 2>/dev/null
          if [ $? -ne 0 ]; then
            log_failure "cannot execute '$CC_COMPAT -dumpversion'"
            fail really
          fi
          cc_compat_maj=`echo $cc_compat_ver|cut -d. -f1`
          cc_compat_min=`echo $cc_compat_ver|cut -d. -f2`
          if [ $cc_compat_maj -lt 3 \
               -o \( $cc_compat_maj -eq 3 -a $cc_compat_min -lt 2 \) \
               -o \( $cc_compat_maj -eq 4 -a $cc_compat_min -lt 1 \) \
               -o \( $cc_compat_maj -eq 4 -a $cc_compat_min -gt 2 \) \
               -o $cc_compat_maj -gt 4 ]; then
            log ""
            log "  ** Version $cc_compat_ver of the compatibility gcc found. Expected gcc 3.x with x>1"
            log "  ** or gcc 4.x with 0<x<3"
            fail really
          fi
          cnf_append "VBOX_RECOMPILER_OP_GCC" "$CC_COMPAT"
        fi
      # gcc-4.0 is allowed for Darwin only
      elif [ $cc_maj -lt 3 \
             -o \( $cc_maj -eq 3 -a $cc_min -lt 2 \) \
             -o \( $cc_maj -eq 4 -a $cc_min -lt 1 -a "$OS" != "darwin" \) \
             -o \( $cc_maj -eq 4 -a $cc_min -gt 3 \) \
             -o $cc_maj -gt 4 ]; then
        log_failure "gcc version $cc_ver found, expected gcc 3.x with x>1 or gcc 4.x with 0<x<4"
        fail really
      else
        log_success "found version $cc_ver"
      fi
      if [ "$BUILD_MACHINE" = "amd64" ]; then
        [ -z "$CC32"  ] && CC32="$CC -m32"
        [ -z "$CXX32" ] && CXX32="$CXX -m32"
      else
        [ -z "$CC32"  ] && CC32="$CC"
        [ -z "$CXX32" ] && CXX32="$CXX"
      fi
      if [ "$BUILD_MACHINE" = "x86" -a "$TARGET_MACHINE" = "amd64" ]; then
        [ -z "$CC64"  ] && CC64="$CC -m64"
        [ -z "$CXX64" ] && CXX64="$CXX -m64"
      fi
      if [ "$CC" != "gcc" ]; then
        cnf_append "TOOL_GCC3_CC"   "$CC"
        cnf_append "TOOL_GCC3_AS"   "$CC"
        cnf_append "TOOL_GCC3_LD"   "$CC"
        cnf_append "TOOL_GXX3_CC"   "$CC"
        cnf_append "TOOL_GXX3_AS"   "$CC"
      fi
      if [ "$CXX" != "g++" ]; then
        cnf_append "TOOL_GCC3_CXX"  "$CXX"
        cnf_append "TOOL_GXX3_CXX"  "$CXX"
        cnf_append "TOOL_GXX3_LD"   "$CXX"
      fi
      if [ "$CC32" != "gcc -m32" ]; then
        cnf_append "TOOL_GCC32_CC"  "$CC32"
        cnf_append "TOOL_GCC32_AS"  "$CC32"
        cnf_append "TOOL_GCC32_LD"  "$CC32"
        cnf_append "TOOL_GXX32_CC"  "$CC32"
        cnf_append "TOOL_GXX32_AS"  "$CC32"
      fi
      if [ "$CXX32" != "g++ -m32" ]; then
        cnf_append "TOOL_GCC32_CXX" "$CXX32"
        cnf_append "TOOL_GXX32_CXX" "$CXX32"
        cnf_append "TOOL_GXX32_LD"  "$CXX32"
      fi
      # this isn't not necessary, there is not such tool.
      if [ -n "$CC64" ]; then
        cnf_append "TOOL_GCC64_CC"  "$CC64"
        cnf_append "TOOL_GCC64_AS"  "$CC64"
        cnf_append "TOOL_GCC64_LD"  "$CC64"
        cnf_append "TOOL_GXX64_CC"  "$CC64"
        cnf_append "TOOL_GXX64_AS"  "$CC64"
      fi
      if [ -n "$CXX64" ]; then
        cnf_append "TOOL_GCC64_CXX" "$CXX64"
        cnf_append "TOOL_GXX64_CXX" "$CXX64"
        cnf_append "TOOL_GXX64_LD"  "$CXX64"
      fi
      # Solaris sports a 32-bit gcc/g++.
      if [ "$OS" = "solaris" -a "$BUILD_MACHINE" = "amd64" ]; then
        [ "$CC" = "gcc" ] && CC="gcc -m64"
        [ "$CXX" = "g++" ] && CXX="g++ -m64"
      fi
    fi
  fi
}

#
# Check for the bcc compiler, needed for compiling the BIOS
#
check_bcc()
{
  test_header bcc
  if check_avail "$BCC" BCC; then
    bcc_ver=`$BCC -v 2>&1|grep version|sed 's+^bcc: version \(.*\)+\1+'`
    if [ $? -ne 0 ]; then
      log_failure "not found"
      fail
    else
      echo "compiling the following source file:" >> $LOG
      cat > .tmp_src.c << EOF
int foo(a)
  int a;
{
  return 0;
}
EOF
      cat .tmp_src.c >> $LOG
      bcc_path=`which_wrapper $BCC`
      bcc_dir="`dirname $bcc_path`/"
      echo "using the following command line:" >> $LOG
      echo "$BCC -B $bcc_dir -C-c -3 -S -o .tmp_out .tmp_src.c" >> $LOG
      $BCC -B $bcc_dir -C-c -3 -S -o .tmp_out .tmp_src.c >> $LOG 2>&1
      if [ $? -ne 0 ]; then
        log_failure "not found"
        fail
      else
        log_success "found version $bcc_ver"
        cnf_append "VBOX_BCC" "$bcc_path -B $bcc_dir"
      fi
      unset bcc_path
      unset bcc_dir
    fi
  fi
}

#
# Check for the as86 assembler, needed for compiling the BIOS
#
check_as86()
{
  test_header as86
  if check_avail "$AS86" AS86; then
    as86_ver=`$AS86 -v 2>&1|grep version|sed 's+^as86 version: \(.*\)+\1+'`
    if [ $? -ne 0 ]; then
      log_failure "not found"
      fail
    else
      log_success "found version $as86_ver"
      cnf_append "VBOX_AS86" "`which_wrapper $AS86`"
    fi
  fi
}

#
# Check for yasm, needed to compile assembler files
#
check_yasm()
{
  test_header yasm
  if check_avail "$YASM" YASM; then
    yasm_ver=`$YASM --version|grep "^yasm"|sed 's+^yasm \(.*\)+\1+'`
    if [ $? -ne 0 ]; then
      log_failure "not found"
      fail
    else
      yasm_maj=`echo $yasm_ver|cut -d. -f1`
      yasm_min=`echo $yasm_ver|cut -d. -f2`
      yasm_rev=`echo $yasm_ver|cut -d. -f3`
      yasm_ver_mul=`expr $yasm_maj \* 10000 + $yasm_min \* 100 + $yasm_rev`
      if [ $yasm_ver_mul -lt 501 ]; then
        log_failure "found version $yasm_ver, expected at least 0.5.1"
        fail
      else
        log_success "found version $yasm_ver"
      fi
    fi
  fi
}

#
# Check for the iasl ACPI compiler, needed to compile vbox.dsl
#
check_iasl()
{
  test_header iasl
  if check_avail "$IASL" IASL; then
    iasl_ver=`$IASL|grep version|sed 's+^ASL.*version \([0-9]*\).*+\1+'`
    if [ $? -ne 0 ]; then
      log_failure "not found"
      fail
    else
      log_success "found version $iasl_ver"
      cnf_append "VBOX_IASLCMD" "`which_wrapper $IASL`"
    fi
  fi
}

#
# Check for xsltproc, needed by Main
#
check_xsltproc()
{
  test_header xslt
  if check_avail "$XSLTPROC" XSLTPROC; then
    xsltproc_ver=`$XSLTPROC --version`
    if [ $? -ne 0 ]; then
      log_failure "not found"
      fail
    else
      log_success "found"
      cnf_append "VBOX_XSLTPROC" "`which_wrapper $XSLTPROC`"
    fi
  fi
}

#
# Check for mkisofs, needed to build the CDROM image containing the additions
#
check_mkisofs()
{
  test_header mkisofs
  if which_wrapper $GENISOIMAGE > /dev/null; then
    mkisofs_ver=`$GENISOIMAGE --version`
    if [ $? -ne 0 ]; then
      log_failure "not found"
      fail
    else
      log_success "found $mkisofs_ver"
      cnf_append "VBOX_MKISOFS" "`which_wrapper $GENISOIMAGE`"
    fi
  elif check_avail "$MKISOFS" MKISOFS; then
    mkisofs_ver=`$MKISOFS --version`
    if [ $? -ne 0 ]; then
      log_failure "not found"
      fail
    else
      log_success "found $mkisofs_ver"
      cnf_append "VBOX_MKISOFS" "`which_wrapper $MKISOFS`"
    fi
  fi
}

#
# Check for libxml2, needed by VBoxSettings
# 2.6.24 is known to NOT work, 2.6.26 is known to work (there is no 2.6.25 release)
#
check_libxml2()
{
  if [ -z "$BUILD_LIBXML2" ]; then
    test_header libxml2
    if which_wrapper pkg-config > /dev/null; then
      libxml2_ver=`pkg-config libxml-2.0 --modversion 2>> $LOG`
      if [ $? -ne 0 ]; then
        log_failure "not found"
        fail
      else
        FLGXML2=`pkg-config libxml-2.0 --cflags`
        INCXML2=`strip_I "$FLGXML2"`
        LIBXML2=`pkg-config libxml-2.0 --libs`
        cat > .tmp_src.cc << EOF
#include <cstdio>
#include <libxml/xmlversion.h>
extern "C" int main(void)
{
  printf("found version %s", LIBXML_DOTTED_VERSION);
#if LIBXML_VERSION >= 20626
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 2.6.26 or higher\n");
  return 1;
#endif
}
EOF
        [ -n "$INCXML2" ] && I_INCXML2=`prefix_I "$INCXML2"`
        if test_compile "$LIBXML2 $LIBPTHREAD $I_INCXML2" xml2 xml2; then
          if test_execute; then
            cnf_append "SDK_VBOX_LIBXML2_INCS" "$INCXML2"
            cnf_append "SDK_VBOX_LIBXML2_LIBS" "`strip_l "$LIBXML2"`"
          fi
        fi
      fi
    elif which_wrapper xml2-config; then
      libxml2_ver=`xml2-config --version`
      if [ $? -ne 0 ]; then
        log_failure "not found"
        fail
      else
        log_success "found version $libxml2_ver"
        FLGXML2=`xml2-config --cflags`
        INCXML2=`strip_I "$FLGXML2"`
        LIBXML2=`xml2-config --libs`
        cat > .tmp_src.cc << EOF
#include <cstdio>
#include <libxml/xmlversion.h>
extern "C" int main(void)
{
  printf("found version %s", LIBXML_DOTTED_VERSION);
#if LIBXML_VERSION >= 20626
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 2.6.26 or higher\n");
  return 1;
#endif
}
EOF
        [ -n "$INCXML2" ] && I_INCXML2=`prefix_I "$INCXML2"`
        if test_compile "$LIBXML2 $LIBPTHREAD $I_INCXML2" xml2 xml2; then
          if test_execute; then
            cnf_append "SDK_VBOX_LIBXML2_INCS" "$INCXML2"
            cnf_append "SDK_VBOX_LIBXML2_LIBS" "`strip_l "$LIBXML2"`"
          fi
        fi
      fi
    else
      log_failure "neither pkg-config nor xml2-config found"
      fail
    fi
  fi
}

#
# Check for libxslt, needed by VBoxSettings. For now we depend on 1.1.17.
# This library is available on Ubuntu Edgy which fulfils the minimal libxml2
# requirement (2.6.26).
#
check_libxslt()
{
  if [ -z "$BUILD_LIBXSLT" ]; then
    test_header libxslt
    if which_wrapper pkg-config > /dev/null; then
      libxslt_ver=`pkg-config libxslt --modversion 2>> $LOG`
      if [ $? -ne 0 ]; then
        log_failure "not found"
        fail
      else
        FLGXSLT=`pkg-config libxslt --cflags`
        INCXSLT=`strip_I "$FLGXSLT"`
        LIBXSLT=`pkg-config libxslt --libs`
        cat > .tmp_src.cc << EOF
#include <cstdio>
#include <libxslt/xsltconfig.h>
extern "C" int main(void)
{
  printf("found version %s", LIBXSLT_DOTTED_VERSION);
#if LIBXSLT_VERSION >= 10117
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 1.1.17 or higher\n");
  return 1;
#endif
}
EOF
        [ -n "$INCXSLT" ] && I_INCXSLT=`prefix_I "$INCXSLT"`
        if test_compile "$LIBXSLT $LIBPTHREAD $I_INCXSLT" xslt xslt; then
          if test_execute; then
            cnf_append "SDK_VBOX_LIBXSLT_INCS" "$INCXSLT"
            cnf_append "SDK_VBOX_LIBXSLT_LIBS" "`strip_l "$LIBXSLT"`"
          fi
        fi
      fi
    elif which_wrapper xslt-config; then
      libxslt_ver=`xslt-config --version`
      if [ $? -ne 0 ]; then
        log_failure "not found"
        fail
      else
        log_success "found version $libxslt_ver"
        FLGXSLT=`xslt-config --cflags`
        INCXSLT=`strip_I "$FLGXSLT"`
        LIBXSLT=`xslt-config --libs`
        cat > .tmp_src.cc << EOF
#include <cstdio>
#include <libxslt/xsltconfig.h>
extern "C" int main(void)
{
  printf("found version %s", LIBXSLT_DOTTED_VERSION);
#if LIBXSLT_VERSION >= 10117
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 1.1.17 or higher\n");
  return 1;
#endif
}
EOF
        [ -n "$INCXSLT" ] && I_INCXSLT=`prefix_I "$INCXSLT"`
        if test_compile "$LIBXSLT $LIBPTHREAD $I_INCXSLT" xslt xslt; then
          if test_execute; then
            cnf_append "SDK_VBOX_LIBXSLT_INCS" "$INCXSLT"
            cnf_append "SDK_VBOX_LIBXSLT_LIBS" "`strip_l "$LIBXSLT"`"
          fi
        fi
      fi
    else
      log_failure "neither pkg-config nor xslt-config found"
      fail
    fi
  fi
}

#
# Check for libIDL, needed by xpcom
#
check_libidl()
{
  test_header libIDL

  if which_wrapper libIDL-config-2 > /dev/null; then
    libidl_ver=`libIDL-config-2 --version`
    if [ $? -ne 0 ]; then
      log_failure "not found"
      fail
    else
      log_success "found version $libidl_ver"
      cnf_append "VBOX_LIBIDL_CONFIG" \
        "PKG_CONFIG_PATH=`libIDL-config-2 --prefix`/$LIB/pkgconfig `which_wrapper libIDL-config-2`"
    fi
  elif check_avail "libIDL-config" libIDL-config; then
    libidl_ver=`libIDL-config --version`
    if [ $? -ne 0 ]; then
      log_failure "not found"
      fail
    else
      log_success "found version $libidl_ver"
      cnf_append "VBOX_LIBIDL_CONFIG" "`which_wrapper libIDL-config`"
    fi
  fi
}

#
# Check for openssl, needed for RDP
#
check_ssl()
{
  test_header ssl
  cat > .tmp_src.cc << EOF
#include <cstdio>
#include <openssl/opensslv.h>
extern "C" int main(void)
{
  printf("found version %s", OPENSSL_VERSION_TEXT);
#if OPENSSL_VERSION_NUMBER >= 0x0090700
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 0.9.7 or higher\n");
  return 1;
#endif
}
EOF
  if test_compile $LIBCRYPTO libcrypto openssl; then
    if test_execute nofatal; then
      cnf_append "SDK_VBOX_OPENSSL_INCS" ""
      cnf_append "SDK_VBOX_OPENSSL_LIBS" "`strip_l "$LIBCRYPTO"`"
    fi
  fi
}

#
# Check for pthread, needed by VBoxSVC, frontends, ...
#
check_pthread()
{
  test_header pthread
  cat > .tmp_src.cc << EOF
#include <cstdio>
#include <pthread.h>
extern "C" int main(void)
{
  pthread_mutex_t mutex;
  if (pthread_mutex_init(&mutex, NULL)) {
    printf("pthread_mutex_init() failed\n");
    return 1;
  }
  if (pthread_mutex_lock(&mutex)) {
    printf("pthread_mutex_lock() failed\n");
    return 1;
  }
  if (pthread_mutex_unlock(&mutex)) {
    printf("pthread_mutex_unlock() failed\n");
    return 1;
  }
  printf("found, OK.\n");
}
EOF
  if test_compile $LIBPTHREAD pthread pthread; then
    if test_execute; then
      cnf_append "LIB_PTHREAD" "`strip_l "$LIBPTHREAD"`"
    fi
  fi
}

#
# Check for zlib, needed by VBoxSVC, Runtime, ...
#
check_z()
{
  test_header zlib
  cat > .tmp_src.cc << EOF
#include <cstdio>
#include <zlib.h>
extern "C" int main(void)
{
  printf("found version %s", ZLIB_VERSION);
#if ZLIB_VERNUM >= 0x1210
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 1.2.1 or higher\n");
  return 1;
#endif
}
EOF
  [ -n "$INCZ" ] && I_INCZ=`prefix_I "$INCZ"`
  if test_compile "$LIBZ $I_INCZ" zlib zlib; then
    if test_execute; then
      cnf_append "SDK_VBOX_ZLIB_LIBS" "`strip_l "$LIBZ"`"
      cnf_append "SDK_VBOX_ZLIB_INCS" "$INCZ"
    fi
  fi
}

#
# Check for libpng, needed by kchmviewer
#
check_png()
{
  test_header libpng
  cat > .tmp_src.cc << EOF
#include <cstdio>
#include <png.h>
extern "C" int main(void)
{
  printf("found version %s", PNG_LIBPNG_VER_STRING);
#if PNG_LIBPNG_VER >= 10205
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 1.2.5 or higher\n");
  return 1;
#endif
}
EOF
  [ -n "$INCPNG" ] && I_INCPNG=`prefix_I "$INCPNG"`
#  if test_compile "$LIBPNG $I_INCPNG" libpng libpng nofatal; then
  if test_compile "$LIBPNG $I_INCPNG" libpng libpng; then
#    if test_execute nofatal; then
    if test_execute; then
      cnf_append "SDK_VBOX_LIBPNG_LIBS" "`strip_l "$LIBPNG"`"
      cnf_append "SDK_VBOX_LIBPNG_INCS" "$INCPNG"
    fi
  fi
}

#
# Check for pam, needed by VRDPAuth
# Version 79 was introduced in 9/2005, do we support older versions?
# Debian/sarge uses 76
# OpenSUSE comes with 0.99.xxx where they changed the versioning scheme.
#
check_pam()
{
  test_header pam
  cat > .tmp_src.cc << EOF
#include <cstdio>
#include <security/pam_appl.h>
extern "C" int main(void)
{
  printf("found version %d", __LIBPAM_VERSION);
  if (__LIBPAM_VERSION >= 76)
  {
    printf(", OK.\n");
    return 0;
  }
  else
  {
    printf(", expected version 76 or higher\n");
    return 1;
  }
}
EOF
  if test_compile "-lpam" pam pam nofatal; then
    if test_execute nofatal; then
      return 0;
    fi
  fi
  test_header linux_pam
  cat > .tmp_src.cc << EOF
#include <cstdio>
#include <security/pam_appl.h>
extern "C" int main(void)
{
  printf("found version %d.%d", __LINUX_PAM__, __LINUX_PAM_MINOR__);
  if (__LINUX_PAM__ >= 1)
  {
    printf(", OK.\n");
    return 0;
  }
  else
  {
    printf(", expected version 1.0 or higher\n");
    return 1;
  }
}
EOF
  if test_compile "-lpam" pam pam; then
    test_execute
  fi
}


#
# Check for the SDL library, needed by VBoxSDL and VirtualBox
# We depend at least on version 1.2.7
#
check_sdl()
{
  test_header SDL
  if [ "$OS" = "darwin" ]; then
    if [ -f "/System/Library/Frameworks/SDL.framework/SDL" ]; then
      PATH_SDK_LIBSDL="/System/Library/Framework/SDL.framework"
    elif [ -f "/Library/Frameworks/SDL.framework/SDL" ]; then
      PATH_SDK_LIBSDL="/Library/Frameworks/SDL.framework"
    fi
    if [ -n "$PATH_SDK_LIBSDL" ]; then
      foundsdl=1
      INCSDL="$PATH_SDK_LIBSDL/Headers"
      FLDSDL="-framework SDL"
    else
      log_failure "SDL framework not found"
      fail
    fi
  else
    if which_wrapper sdl-config > /dev/null; then
      FLGSDL=`sdl-config --cflags`
      INCSDL=`strip_I "$FLGSDL"`
      LIBSDL=`sdl-config --libs`
      LIBSDLMAIN="-lSDLmain"
      FLDSDL=
      foundsdl=1
    fi
  fi
  [ "$OS" = "linux" -o "$OS" = "darwin" -o "$OS" = "solaris" ] && LIBSDLMAIN=""
  if [ -n "$foundsdl" ]; then
    cat > .tmp_src.cc << EOF
#include <cstdio>
#include <SDL.h>
#include <SDL_main.h>
#undef main
extern "C" int main(int argc, char** argv)
{
  printf("found version %d.%d.%d",
         SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL);
#if SDL_VERSION_ATLEAST(1,2,7)
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 1.2.7 or higher\n");
  return 1;
#endif
}
EOF
    [ -n "$INCSDL" ] && I_INCSDL=`prefix_I "$INCSDL"`
    if test_compile "$LIBSDL $LIBSDLMAIN $I_INCSDL $FLDSDL" SDL SDL; then
      if test_execute; then
        cnf_append "LIB_SDK_LIBSDL_SDL"     "`strip_l "$LIBSDL"`"
        cnf_append "SDK_LIBSDL_LIBPATH"     "`strip_L "$LIBSDL"`"
        cnf_append "LIB_SDK_LIBSDL_SDLMAIN" "`strip_l "$LIBSDLMAIN"`"
        [ -n "$INCSDL" ] && cnf_append "SDK_LIBSDL_INCS"    "$INCSDL"
        [ -n "$FLDSDL" ] && cnf_append "SDK_LIBSDL_LDFLAGS" "$FLDSDL"
      fi
    fi
  else
    log_failure "not found"
    fail
  fi
}

#
# Check for the SDL_ttf library, needed by VBoxSDL (secure label)
#
check_sdl_ttf()
{
  test_header SDL_ttf
  cat > .tmp_src.cc << EOF
#include <cstdio>
#include <SDL_ttf.h>
#ifndef SDL_TTF_MAJOR_VERSION
#define SDL_TTF_MAJOR_VERSION TTF_MAJOR_VERSION
#define SDL_TTF_MINOR_VERSION TTF_MINOR_VERSION
#define SDL_TTF_PATCHLEVEL    TTF_PATCHLEVEL
#endif
extern "C" int main(void)
{
  printf("found version %d.%d.%d",
         SDL_TTF_MAJOR_VERSION, SDL_TTF_MINOR_VERSION, SDL_TTF_PATCHLEVEL);
#if 10000*SDL_TTF_MAJOR_VERSION + 100*SDL_TTF_MINOR_VERSION + SDL_TTF_PATCHLEVEL >= 20006
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 2.0.6 or higher\n");
  return 1;
#endif
}
EOF
  if test_compile "-lSDL_ttf $I_INCSDL" SDL_ttf SDL_ttf nofatal; then
    if ! test_execute nofatal; then
      cnf_append "VBOX_WITH_SECURELABEL" ""
    fi
  else
    cnf_append "VBOX_WITH_SECURELABEL" ""
  fi
}

#
# Check for libasound, needed by the ALSA audio backend
#
check_alsa()
{
  test_header ALSA
  cat > .tmp_src.cc << EOF
#include <cstdio>
#include <alsa/asoundlib.h>
extern "C" int main(void)
{
  printf("found version %d.%d.%d",
         SND_LIB_MAJOR, SND_LIB_MINOR, SND_LIB_SUBMINOR);
#if 10000*SND_LIB_MAJOR + 100*SND_LIB_MINOR + SND_LIB_SUBMINOR >= 10006
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 1.0.6 or higher\n");
  return 1;
#endif
}
EOF
  if test_compile "-lasound" asound asound; then
    test_execute
  fi
}

#
# Check for PulseAudio
#
check_pulse()
{
  test_header "PulseAudio"
  cat > .tmp_src.cc << EOF
#include <cstdio>
#include <pulse/version.h>
extern "C" int main(void)
{
  printf("found version %s API version %d", pa_get_headers_version(), PA_API_VERSION);
#if PA_API_VERSION >= 9
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 0.9.0 (API version 9) or higher\n");
  return 1;
#endif
}
EOF
  if test_compile "-lpulse" pulse pulse; then
    test_execute
  fi
}

#
# Check for the Xcursor library, needed by VBoxSDL and VBoxBFE
#
check_xcursor()
{
  test_header Xcursor
  cat > .tmp_src.cc << EOF
#include <cstdio>
#include <X11/Xlib.h>
#include <X11/Xcursor/Xcursor.h>
extern "C" int main(void)
{
  XcursorImage *cursor = XcursorImageCreate (10, 10);
  XcursorImageDestroy(cursor);
  return 0;
}
EOF
  [ -n "$INCX11" ] && I_INCX11=`prefix_I "$INCX11"`
  if test_compile "$LIBX11 $LIBXCURSOR $I_INCX11" Xcursor Xcursor; then
    log_success "found"
    cnf_append "VBOX_XCURSOR_LIBS" "`strip_l "$LIBXCURSOR"`"
  fi
}

#
# Check for the X libraries (Xext, X11)
#
check_x()
{
  test_header "X libraries"
  cat > .tmp_src.cc << EOF
#include <cstdio>
#include <X11/Xlib.h>
extern "C" int main(void)
{
  Display *dpy;
  int scrn_num;
  Screen *scrn;
  Window win;

  dpy      = XOpenDisplay(NULL);
  scrn_num = DefaultScreen(dpy);
  scrn     = ScreenOfDisplay(dpy, scrn_num);
  win      = XCreateWindow(dpy, RootWindowOfScreen(scrn), 0, 0, 100, 100,
                           0, 16, InputOutput, CopyFromParent, 0, NULL);
  XDestroyWindow(dpy, win);
}
EOF
  [ -n "$INCX11" ] && I_INCX11=`prefix_I "$INCX11"`
  if test_compile "$LIBX11 $I_INCX11" Xlibs Xlibs; then
    log_success "found"
  fi
}

#
# Check for the Qt3 library, needed by VirtualBox
#
check_qt3()
{
  test_header Qt3
  cat > .tmp_src.cc << EOF
#include <cstdio>
#include <qglobal.h>
extern "C" int main(void)
{
  printf("found version %s", QT_VERSION_STR);
#if QT_VERSION >= 0x030305
  printf(", OK.\n");
  return 0;
#elif QT_VERSION >= 0x030300
  printf("\n  ** WARNING: QT < 3.3.5 has known problems!\n");
#else
  printf(", expected version 3.3.0 or higher\n");
  return 1;
#endif
}
EOF
  found_qt=0
  libs="lib"
  [ "$LIB" = "lib64" ] && libs="$libs lib64"
  for q in $QT3DIR; do
    for l in $libs; do
      echo "compiling the following source file:" >> $LOG
      cat .tmp_src.cc >> $LOG
      echo "using the following command line:" >> $LOG
      echo "$CXX -O -Wall -o .tmp_out .tmp_src.cc -I$q/include -L$q/$l -lqt-mt $LIBPTHREAD" >> $LOG
      $CXX -O -Wall -o .tmp_out .tmp_src.cc -I$q/include -L$q/$l -lqt-mt $LIBPTHREAD >> $LOG 2>&1
      if [ $? -eq 0 ]; then
        if test_execute; then
          cnf_append "QTDIR" "`cd $q ; pwd`"
          found_qt=1
          break
        fi
      fi
    done
    if [ $found_qt -eq 1 ]; then
      break
    fi
  done
  if [ $found_qt -ne 1 ]; then
    echo
    echo "  Qt3 not found at \"$QT3DIR\" or Qt3 headers not found"
    echo "  Check the file $LOG for detailed error information."
    fail
    return 1
  fi
  test_header "Qt3 devtools"
  if check_avail "$q/bin/moc" QT3DIR/bin; then
    moc_ver=`$q/bin/moc 2>&1 -v|sed 's+^.*(Qt \(.*\))+\1+'`
    if [ $? -ne 0 ]; then
      log_failure "not found"
      fail
    else
      log_success "found version $moc_ver"
    fi
  fi
}

#
# Check for the Qt4 library, needed by VirtualBox4
#
# Currently not fatal.
#
check_qt4()
{
  test_header Qt4
  if [ "$OS" = "darwin" ]; then
    if [ -f "/System/Library/Frameworks/QtCore.framework/QtCore" ]; then
      PATH_SDK_QT4="/System/Library/Framework/QtCore.framework"
    elif [ -f "/Library/Frameworks/QtCore.framework/QtCore" ]; then
      PATH_SDK_QT4="/Library/Frameworks/QtCore.framework"
    fi
    if [ -n "$PATH_SDK_QT4" ]; then
      foundqt4=1
      INCQT4="$PATH_SDK_QT4/Headers"
      LIBQT4=
      FLDQT4="-framework QtCore"
    else
      log_failure "Qt4 framework not found"
      fail
    fi
  else
    if which_wrapper pkg-config > /dev/null; then
      qt4_ver=`pkg-config QtCore --modversion 2>> $LOG`
      if [ $? -ne 0 ]; then
        log_failure "not found"
  #      fail
      else
        FLGQT4=`pkg-config QtCore --cflags`
        INCQT4=`strip_I "$FLGQT4"`
        LIBQT4=`pkg-config QtCore --libs`
        foundqt4=1
      fi
    else
      log_failure "pkg-config not found"
# not yet
#     fail
    fi
  fi
  if [ -n "$foundqt4" ]; then
    cat > .tmp_src.cc << EOF
#include <cstdio>
#include <QtGlobal>
extern "C" int main(void)
{
  printf("found version %s", QT_VERSION_STR);
#if QT_VERSION >= 0x040200
  printf(", OK.\n");
  return 0;
  printf(", expected version 4.2.0 or higher\n");
  return 1;
#endif
}
EOF
    [ -n "$INCQT4" ] && I_INCQT4=`prefix_I "$INCQT4"`
    if test_compile "$LIBQT4 $LIBPTHREAD $I_INCQT4 $FLDQT4" qt4 qt4 nofatal; then
      if test_execute; then
#        cnf_append "SDK_VBOX_LIBQT4_INCS" "$INCQT4"
#        cnf_append "SDK_VBOX_LIBQT4_LIBS" "`strip_l "$LIBQT4"`"
        if [ "$OS" = "darwin" ]; then
          cnf_append "VBOX_WITH_QT4GUI" "1"
        else
          test_header "Qt4 devtools"
          for q in $QT4DIR; do
            if which_wrapper "$q/bin/moc" > /dev/null; then
              moc_ver=`$q/bin/moc -v 2>&1|sed 's+^.*(Qt \(.*\))+\1+'`
              if [ $? -ne 0 ]; then
                log_failure "not found"
                fail
              else
                log_success "found version $moc_ver"
                cnf_append "VBOX_PATH_QT4" "$q"
                test_header "Qt4 uic3"
                for r in $q/bin $QT4UIC3DIR; do
                  if which_wrapper "$r/uic3" > /dev/null; then
                    uic3_ver=`$r/uic3 -version 2>&1|sed 's+^.*version \(.*\)+\1+'`
                    if [ $? -ne 0 ]; then
                      log_failure "not found"
                      fail
                    else
                      log_success "found version $uic3_ver"
                      cnf_append "VBOX_UIC3" "$r/uic3"
                      return
                    fi
                  fi
                done
                log_failure "not found"
                fail
              fi
            fi
          done
        fi
      fi
    else
      log_failure "not found"
      fail
    fi
  fi
}


#
# Check whether static libstdc++ is installed
#
check_staticlibstdcxx()
{
  test_header "static stc++ library"
  libstdcxx=`$CXX -print-file-name=libstdc++.a`
  cat > .tmp_src.cc << EOF
#include <string>

extern "C" int main(void)
{
  std::string s = "test";
  return 0;
}
EOF
  if test_compile "$libstdcxx" libstdc++ libstdc++; then
    log_success "found"
  fi
}

#
# Check for Linux sources
#
check_linux()
{
  test_header "Linux kernel sources"
  cat > .tmp_src.c << EOF
#include <linux/version.h>
int printf(const char *format, ...);
int main(void)
{
  printf("found version %d.%d.%d",  LINUX_VERSION_CODE / 65536,
                                   (LINUX_VERSION_CODE % 65536) / 256,
                                    LINUX_VERSION_CODE          % 256);
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,0)
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 2.4.0 or higher\n");
  return 1;
#endif
}
EOF
  echo "compiling the following source file:" >> $LOG
  cat .tmp_src.c >> $LOG
  echo "using the following command line:" >> $LOG
  echo "$CC -O -Wall -o .tmp_out .tmp_src.c -nostdinc -I$LINUX/include" >> $LOG
  $CC -O -Wall -o .tmp_out .tmp_src.c -nostdinc -I$LINUX/include >> $LOG 2>&1
  if [ $? -ne 0 ]; then
    echo
    echo "  Linux kernel headers not found at $LINUX"
    echo "  Check the file $LOG for detailed error information."
    fail
  else
    if test_execute; then
      cnf_append "VBOX_LINUX_SRC" "`cd $LINUX ; pwd`"
    fi
  fi
}

#
# Check for kchmviewer, needed to display the online help
#
check_kchmviewer()
{
  test_header kchmviewer
  if check_avail "$KCHMVIEWER" KCHMVIEWER; then
    kchmviewer_ver=`$KCHMVIEWER --version|grep "^KchmViewer:"|sed 's+^KchmViewer: \(.*\)+\1+'`
    if [ $? -ne 0 ]; then
      log_failure "not found"
      fail
    else
      log_success "found version $kchmviewer_ver"
    fi
  fi
}

#
# Check for the kBuild tools, we don't support GNU make
#
check_kbuild()
{
  test_header kBuild
  if which_wrapper "$KBUILDDIR/bin/$OS.$BUILD_MACHINE/kmk" > /dev/null; then
    KBUILDDIR_BIN="$KBUILDDIR/bin/$OS.$BUILD_MACHINE"
    echo "export PATH_KBUILD=\"`cd $KBUILDDIR ; pwd`\"" >> $ENV
    echo "export PATH_DEVTOOLS=\"$DEVDIR\"" >> $ENV
    echo "path_kbuild_bin=\"\$PATH_KBUILD/bin/\$BUILD_TARGET.\$BUILD_PLATFORM_ARCH\"" >> $ENV
    echo "export PATH_KBUILD_BIN=\"\$path_kbuild_bin\"" >> $ENV
    echo "path_dev_bin=\"\$PATH_DEVTOOLS/\$BUILD_TARGET.\$BUILD_PLATFORM_ARCH\"/bin" >> $ENV
    if [ "$OS" = "solaris" ]; then
      # Because of sh being non-default shell in Solaris we need to export PATH again when
      # sourcing env.sh. Simply exporting from ./configure does not export PATH correctly.
      echo "PATH=\"$ORGPATH\"" >> $ENV
      echo "echo \"\$PATH\" | /usr/sfw/bin/ggrep -q \"\$path_kbuild_bin\" || PATH=\"\$path_kbuild_bin:\$PATH\"" >> $ENV
      echo "echo \"\$PATH\" | /usr/sfw/bin/ggrep -q \"\$path_dev_bin\" || PATH=\"\$path_dev_bin:\$PATH\"" >> $ENV
    else
      echo "echo \"\$PATH\" | grep -q \"\$path_kbuild_bin\" || PATH=\"\$path_kbuild_bin:\$PATH\"" >> $ENV
      echo "echo \"\$PATH\" | grep -q \"\$path_dev_bin\" || PATH=\"\$path_dev_bin:\$PATH\"" >> $ENV
    fi
    echo "export PATH" >> $ENV
    echo "unset path_kbuild_bin path_dev_bin" >> $ENV
    KBUILD_SED="$KBUILDDIR_BIN/kmk_sed"
  elif check_avail "kmk" KBUILDDIR really; then
    # check for installed kBuild
    KBUILD_SED="`which_wrapper kmk_sed`"
  else
    fail
  fi
  log_success "found"
}


#
# Check for compiler.h
# Some Linux distributions include "compiler.h" in their libc linux
# headers package, some don't.  Most don't need it, building might (!)
# not succeed on openSUSE without it.
#
# See http://www.mail-archive.com/qemu-devel%40nongnu.org/msg07980.html
#
check_compiler_h()
{
  test_header compiler.h
  if ! test -f "/usr/include/linux/compiler.h"; then
    cnf_append "VBOX_WITHOUT_LINUX_COMPILER_H" "1"
    log_success "compiler.h not found"
  else
    log_success "compiler.h found"
  fi
}


#
# Check if we are able to build 32-bit applications (needed for the guest additions)
#
check_32bit()
{
  test_header "32-bit support"
  cat > .tmp_src.c << EOF
#include <stdint.h>
int main(void)
{
  return 0;
}
EOF
  echo "compiling the following source file:" >> $LOG
  cat .tmp_src.c >> $LOG
  echo "using the following command line:" >> $LOG
  echo "$CC -m32 -O -Wall -o .tmp_out .tmp_src.c" >> $LOG
  $CC -m32 -O -Wall -o .tmp_out .tmp_src.c >> $LOG 2>&1
  if [ $? -ne 0 ]; then
    echo
    echo "  Cannot compile 32-bit applications (missing headers and/or libraries)!"
    echo "  Check the file $LOG for detailed error information."
    fail
  fi
  log_success ""
}

#
# Setup wine
#
setup_wine()
{
  test_header "Wine support"
  if ! which_wrapper wine > /dev/null; then
    echo "  wine binary not found"
    fail
  fi
  if ! which_wrapper wineprefixcreate > /dev/null; then
    echo "  wineprefixcreate not found"
    fail
  fi
  export WINEPREFIX="${ODIR}wine.$BUILD_MACHINE"
  echo "export WINEPREFIX=\"${ODIR}wine.$BUILD_MACHINE\"" >> $ENV
  rm -rf $WINEPREFIX
  mkdir -p $WINEPREFIX
  touch $WINEPREFIX/.no_prelaunch_window_flag
  if ! wineprefixcreate -q > /dev/null 2>&1; then
    echo "  wineprefixcreate failed"
    fail
  fi
  tmp=.tmp.wine.reg
  rm -f $tmp
  echo 'REGEDIT4' > $tmp
  echo '' >> $tmp
  echo '[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment]' >> $tmp
  echo "\"PATH\"=\"c:\\\\\\\\windows\\\\\\\\system32;c:\\\\\\\\windows;z:$DEVDIR/win.x86/vcc/v8/bin/Microsoft.VC80.CRT;z:$DEVDIR/win.x86/HTML_Help_Workshop/v1.3\"" >> $tmp
  echo '' >> $tmp
  echo '[HKEY_CURRENT_USER\Software\Wine\AppDefaults\hhc.exe\DllOverrides]' >> $tmp
  echo '"itss"="native"' >> $tmp
  echo '' >> $tmp
  echo '[HKEY_CURRENT_USER\Software\Wine\AppDefaults\hhw.exe\DllOverrides]' >> $tmp
  echo '"itss"="native"' >> $tmp
  echo '' >> $tmp
  if ! wine regedit $tmp > /dev/null 2>&1; then
    rm -f $tmp
    echo "  failed to load registry changes (path)."
    fail
  fi
  rm -f $tmp
  log_success "found"
}


#
# Determines the Darwin version.
# @todo This should really check the Xcode/SDK version.
#
check_darwinversion()
{
  test_header "Darwin version"
  darwin_ver=`uname -r`
  case "$darwin_ver" in
    9\.*)
      darwin_ver="10.5"
      #cnf_append "VBOX_TARGET_MAC_OS_X_VERSION_10_5" "1"
      ;;
    8\.*)
      darwin_ver="10.4"
      ;;
    *)
      echo "  failed to determin darwin version. (uname -r: $darwin_ver)"
      fail
      darwin_ver="unknown"
      ;;
  esac
  log_success "found version $darwin_ver"
}


#
# Checks that i386-elf-gcc-3.4.6, i386-elf-gcc-3.4.3, i386-elf-gcc-3.4 or i386-elf-gcc
# is around to prevent confusion when the build fails in src/recompiler.
# Note. Keep the which order in sync with the $(which ) in src/recompiler/Makefile.kmk.
#
check_i386elfgcc()
{
  test_header "i386-elf-gcc"
  i386_elf_gcc=`which_wrapper i386-elf-gcc-3.4.6`
  test -z "$i386_elf_gcc" && i386_elf_gcc=`which_wrapper i386-elf-gcc-3.4.3`
  test -z "$i386_elf_gcc" && i386_elf_gcc=`which_wrapper i386-elf-gcc-3.4`
  test -z "$i386_elf_gcc" && i386_elf_gcc=`which_wrapper i386-elf-gcc`
  if test -z "$i386_elf_gcc"; then
    echo "  failed to find i386-elf-gcc"
    fail
  fi
  log_success "found $i386_elf_gcc"
}


#
# Show help
#
show_help()
{
  cat << EOF
Usage: ./configure [OPTIONS]...

Configuration:
  -h, --help               display this help and exit
  --nofatal                don't abort on errors
  --disable-xpcom          disable XPCOM and related stuff
  --disable-sdl-ttf        disable SDL_ttf detection
  --disable-alsa           disable the ALSA sound backend
  --disable-pulse          disable the PulseAudio backend
  --disable-kmods          don't build Linux kernel modules (host and guest)
  --build-libxml2          build libxml2 from sources
  --build-libxslt          build libxslt from sources (not OSE!)
  --setup-wine             setup a Wine directory and register the hhc hack

Paths:
  --with-gcc=PATH          location of the gcc compiler [$CC]
  --with-g++=PATH          location of the g++ compiler [$CXX]
  --with-kbuild=DIR        kbuild directory [$KBUILDDIR]
  --with-iasl=PATH         location of the iasl compiler [$IASL]
  --with-linux=DIR         Linux kernel source directory [$LINUX]
  --with-mkisofs=PATH      location of mkisofs [$MKISOFS]
  --with-qt-dir=DIR        directory for Qt3 headers/libraries [$QT3DIR]
  --with-qt4-dir=DIR       directory for Qt4 headers/libraries [$QT4DIR]

Build type:
  -d, --build-debug        build with debugging symbols and assertions
  --build-profile          build with profiling support
  --build-headless         build headless (without any X11 frontend)
EOF
  exit 0
}


#
# The body.
#

# scan command line options
for option in $*; do
  case "$option" in
    --help|-help|-h)
      show_help
      ;;
    --nofatal)
      nofatal=1
      ;;
    --env-only)
      ENV_ONLY=1
      ;;
    --with-gcc=*)
      CC=`echo $option | cut -d'=' -f2`
      ;;
    --with-g++=*)
      CXX=`echo $option | cut -d'=' -f2`
      ;;
    --with-gcc-compat=*)
      CC_COMPAT=`echo $option | cut -d'=' -f2`
      ;;
    --with-kbuild=*)
      KBUILDDIR=`echo $option | cut -d'=' -f2`
      if echo $KBUILDDIR|grep -q "$INVALID_CHARS"; then
        echo "Error: KBUILDDIR contains invalid characters!"
        exit 1
      fi
      ;;
    --with-qt-dir=*)
      QT3DIR=`echo $option | cut -d'=' -f2`
      ;;
    --with-qt4-dir=*)
      QT4DIR=`echo $option | cut -d'=' -f2`
      ;;
    --with-iasl=*)
      IASL=`echo $option | cut -d'=' -f2`
      ;;
    --with-linux=*)
      LINUX=`echo $option | cut -d'=' -f2`
      ;;
    --with-mkisofs=*)
      MKISOFS=`echo $option | cut -d'=' -f2`
      ;;
    --disable-xpcom)
      WITH_XPCOM=0
      ;;
    --disable-sdl-ttf)
      WITH_SDL_TTF=0
      ;;
    --disable-qt)
      WITH_QT=0
      WITH_QT4=0
      ;;
    --disable-qt4)
      WITH_QT4=0
      ;;
    --disable-alsa)
      WITH_ALSA=0
      ;;
    --disable-pulse)
      WITH_PULSE=0
      ;;
    --disable-kmods)
      WITH_KMODS=0
      ;;
    --build-debug|-d)
      BUILD_TYPE=debug
      ;;
    --build-profile)
      BUILD_TYPE=profile
      ;;
    --build-libxml2)
      BUILD_LIBXML2=1
      ;;
    --build-libxslt)
      BUILD_LIBXSLT=1
      ;;
    --build-headless)
      HEADLESS=1
      WITH_SDL=0
      WITH_SDL_TTF=0
      WITH_X11=0
      WITH_QT=0
      WITH_QT4=0
      ;;
    --ose)
      OSE=2
      ;;
    --odir=*)
      ODIR="`echo $option | cut -d'=' -f2`/"
      ;;
    --setup-wine)
      SETUP_WINE=1
      ;;
    *)
      echo
      echo "Unrecognized option \"$option\""
      echo
      show_help
      ;;
  esac
done

LOG="$ODIR$LOG"
ENV="$ODIR$ENV"
CNF="$ODIR$CNF"

# initialize output files
cat > $LOG << EOF
# Log file generated by
#
#   '$0 $*'
#

EOF
cat > $CNF << EOF
# -*- Makefile -*-
#
# automatically generated by
#
#   '$0 $*'
#
# It will be completely overwritten if configure is executed again.
#

EOF
cat > $ENV << EOF
#!/bin/bash
#
# automatically generated by
#
#   '$0 $*'
#
# It will be completely overwritten if configure is executed again.
# Make sure you source this file once before you start to build VBox.
#

EOF

# test if we are OSE
if [ $OSE -eq 1 -a -d "`cd \`dirname $0\`; pwd`/src/VBox/Devices/USB" ]; then
  echo "Found USB devices, assuming VBOX_OSE = FALSE" >> $LOG
  echo >> $LOG
  OSE=0
fi

if [ "$BUILD_TYPE" = "debug" ]; then
  echo "Creating DEBUG build!" >> $LOG
elif [ "$BUILD_TYPE" = "profile" ]; then
  echo "Creating PROFILE build!" >> $LOG
fi

# first determine our environment
check_environment
check_kbuild

[ -n "$ENV_ONLY" ] && exit 0

# append the tools directory to the default search path
echo "$PATH" | grep -q "$DEVDIR_BIN" || PATH="$PATH:$DEVDIR_BIN"
export PATH

# some things are not available in for OSE
if [ $OSE -ge 1 ]; then
  cnf_append "VBOX_OSE" "1"
  cnf_append "VBOX_WITH_TESTSUITE" ""
  cnf_append "VBOX_WITH_WIN32_ADDITIONS" ""

  if [ "$OS" = "linux" ]; then
    cnf_append "VBOX_WITH_LINUX_ADDITIONS" "1"
  else
    cnf_append "VBOX_WITH_LINUX_ADDITIONS" ""
  fi
  echo >> $CNF
fi

# headless
if [ -n "$HEADLESS" ]; then
  cnf_append "VBOX_HEADLESS" "1"
fi

if [ "$OS" = "darwin" ]; then
  # On Darwin we want to build against Qt4 only. WITH_QT4 is enabled by
  # default so disable Qt3. --disable-qt disables both Qt3 and Qt4 GUI now,
  # --disable-qt4 disables only the Qt4 GUI (which is not appropriate for
  # Darwin as we disable the Qt3 here anyway.
  # (Qt3 builds for Intel Macs are usually not threaded or for X11. And they
  #  don't contain our patches, which means the result isn't really usable.)
  WITH_QT=0
  # No Qt4 for OSE yet.
  [ $OSE -ge 1 ] && WITH_QT4=0
  BUILD_LIBXSLT=1
  BUILD_LIBXML2=1
fi

# emit disable directives corresponding to any --disable-xxx options.
[ $WITH_XPCOM -eq 0 ]   && cnf_append "VBOX_WITH_MAIN" ""
[ $WITH_QT -eq 0    ]   && cnf_append "VBOX_WITH_QTGUI" ""
[ $WITH_QT4 -eq 0   ]   && cnf_append "VBOX_WITH_QT4GUI" ""
[ $WITH_SDL_TTF -eq 0 ] && cnf_append "VBOX_WITH_SECURELABEL" ""

# the tools
check_gcc
[ "$OS" != "darwin"  ] && check_as86
[ "$OS" != "darwin"  ] && check_bcc
[ "$OS" != "darwin"  ] && check_iasl
# don't check for yasm for the time beeing as 0.40 and 0.50 both have known bugs
# [ "$OS" != "darwin" ] && check_yasm
[ "$OS" != "darwin"  ] && check_xsltproc
[ $OSE -eq 0 -a "$OS" != "darwin" ] && check_mkisofs

# the libraries
[ "$OS" != "darwin"  ] && check_pthread
[ $WITH_XPCOM  -eq 1 ] && check_libxml2
[ $WITH_XPCOM  -eq 1 ] && check_libxslt
[ $WITH_LIBIDL -eq 1 ] && check_libidl
# build openssl on Darwin in every case
[ "$OS" != "darwin" -a $OSE -eq 0 ] && check_ssl
[ "$OS" != "darwin"  ] && check_z
[ "$OS" != "darwin"  ] && check_png
[ $OSE -eq 0 -a "$OS" = "linux" ] && check_pam
[ $WITH_SDL -eq 1 ]    && check_sdl
[ $WITH_SDL_TTF -eq 1 -a $OSE -eq 0 ] && check_sdl_ttf
[ $WITH_X11 -eq 1    ] && check_x
[ $WITH_X11 -eq 1    ] && check_xcursor
[ $WITH_QT -eq 1     ] && check_qt3
[ $WITH_QT4 -eq 1    ] && check_qt4

# Linux-specific
if [ "$OS" = "linux" ]; then
  check_staticlibstdcxx
  if [ $WITH_KMODS -eq 1 ]; then
    check_linux
  else
    cnf_append "VBOX_LINUX_SRC" ""
    cnf_append "VBOX_WITH_VBOXDRV" ""
    cnf_append "VBOX_WITH_ADDITION_DRIVERS" ""
  fi
  if [ $WITH_ALSA -eq 1 ]; then
    check_alsa
  else
    cnf_append "VBOX_WITH_ALSA" ""
  fi
  if [ $WITH_PULSE -eq 1 ]; then
    check_pulse
  else
    cnf_append "VBOX_WITH_PULSE" ""
  fi
  check_compiler_h
  [ "$BUILD_MACHINE" = "amd64" ] && check_32bit
fi

[ -n "$SETUP_WINE" ] && setup_wine

# Darwin-specific
if [ "$OS" = "darwin" ]; then
  check_darwinversion
  check_i386elfgcc
fi

# success!
echo
echo "Successfully generated '$CNF' and '$ENV'."
echo "Source '$ENV' once before you start to build VBox:"
echo ""
echo "  source $ENV"
echo "  kmk"
echo ""
if [ "$OS" = "linux" ]; then
    echo "To compile the kernel module, do:"
    echo ""
    echo "  cd ./out/$OS.$TARGET_MACHINE/$BUILD_TYPE/bin/src"
    echo "  make"
    echo ""
fi
echo "Enjoy!"
cleanup
