#!/bin/sh
#
#  cpuinfo configure script (C) 2006-2007 Gwenole Beauchesne
#
PACKAGE=cpuinfo

# set temporary file name
if test ! -z "$TMPDIR" ; then
    TMPDIR1="${TMPDIR}"
elif test ! -z "$TEMPDIR" ; then
    TMPDIR1="${TEMPDIR}"
else
    TMPDIR1="/tmp"
fi

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

# default parameters
prefix="/usr"
libdir=""
cc="cc"
build_static="no"
build_shared="no"
build_perl="no"
build_python="no"
install_sdk="no"
install_stripped="no"
target_os=`uname -s | tr '[A-Z]' '[a-z]'`
target_cpu=`uname -m`
case "$target_cpu" in
i386|i486|i586|i686|i86pc|BePC)
    target_cpu="i386"
    ;;
ia64)
    target_cpu="ia64"
    ;;
"Power Macintosh"|ppc)
    target_cpu="ppc"
    ;;
ppc64)
    target_cpu="ppc64"
    ;;
sparc)
    target_cpu="sparc"
    ;;
sparc64)
    target_cpu="sparc64"
    ;;
x86_64|amd64)
    target_cpu="x86_64"
    ;;
mips|IP27|IP35)
    target_cpu="mips"
    ;;
mips64)
    target_cpu="mips64"
    ;;
*)
    target_cpu="unknown"
    ;;
esac
case "$target_os" in
cygwin*|mingw*)
    target_os="windows"
    ;;
irix*)
    target_os="irix"
    ;;
hp-ux*)
    target_os="hpux"
    ;;
esac

# 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
--prefix=*)
    prefix=`echo $opt | cut -d '=' -f 2`
    ;;
--libdir=*)
    libdir=`echo $opt | cut -d '=' -f 2`
    ;;
--target-os=*)
    target_os=`echo $opt | cut -d '=' -f 2 | tr '[A-Z]' '[a-z]'`
    ;;
--target-cpu=*)
    target_cpu=`echo $opt | cut -d '=' -f 2 | sed -e 's/^i.86$/i386/'`
    ;;
--enable-static)
    build_static="yes"
    ;;
--disable-static)
    build_static="no"
    ;;
--enable-shared)
    build_shared="yes"
    ;;
--disable-shared)
    build_shared="no"
    ;;
--enable-perl*)
    build_perl="yes"
    build_perl_arg="`echo $opt | cut -d '=' -s -f 2`"
    if [ -n $build_perl_arg ]; then
	PERL_INSTALLDIRS="INSTALLDIRS=$build_perl_arg"
    fi
    ;;
--disable-perl)
    build_perl="no"
    ;;
--enable-python)
    build_python="yes"
    ;;
--disable-python)
    build_python="no"
    ;;
--install-sdk)
    install_sdk="yes"
    ;;
--install-stripped)
    install_stripped="yes"
    ;;
--with-cc=*)
    cc=`echo $opt | cut -d '=' -f 2`
    ;;
esac
done

if test "$install_sdk" = "yes"; then
    build_static="yes"
fi

# print configure help
if test x"$1" = x"-h" -o x"$1" = x"--help" ; 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 "  --libdir=LIBDIR             use LIBDIR for native libraries [$libdir]"
echo "  --enable-static             build static libraries [$build_static]"
echo "  --enable-shared             build shared libraries [$build_shared]"
echo "  --enable-perl               build Perl bindings [$build_perl]"
echo "  --enable-python             build Python bindings [$build_python]"
echo "  --install-sdk               install headers and libraries [$install_sdk]"
echo ""
echo "Advanced options (experts only):"
echo "  --source-path=PATH          path of source code [$source_path]"
echo "  --with-cc=CC                use C compiler CC [$cc]"
echo "  --target-os=OS              cross-compile for target OS [$target_os]"
echo "  --target-cpu=CPU            cross-compile for target CPU [$target_cpu]"
echo "  --install-stripped          strip libraries and binaries [$install_stripped]"
echo ""
echo "NOTE: The object files are built at the place where configure is launched"
exit 1
fi

# check for libdir name
if test -z "$libdir"; then
    case $target_os in
    linux)
	# test if the compiler is 64bit
        echo 'int i;' > $TMPC
	cpuinfo_64bit_output=no
	if $cc -o $TMPO -c $TMPC; then
	    case `/usr/bin/file $TMPO` in
	    *"ELF 64"*)
		cpuinfo_64bit_output=yes
		;;
	    esac
	fi
	rm -f $TMPC $TMPO
	;;
    esac
    case $target_cpu:$cpuinfo_64bit_output in
    ppc64:yes | s390x:yes | sparc64:yes | x86_64:yes)
	libdir="$prefix/lib64"
	;;
    *)
	libdir="$prefix/lib"
	;;
    esac
fi

# check for designated initializers support
cat > $TMPC << EOF
static const int a[6] = { [4] = 4, [2] = 2 };
int main(void) {
  return !(a[0] == 0 && a[2] == 2 && a[4] == 4 && a[5] == 0);
}
EOF
has_designated_initializers=no
if $cc $TMPC -o $TMPE >/dev/null 2>&1; then
    if $TMPE; then
	has_designated_initializers=yes
    fi
fi
rm -f $TMPC $TMPE

# check for __attribute__((visibility())) support
cat > $TMPC << EOF
int foo __attribute__((visibility("hidden"))) = 1;
int bar __attribute__((visibility("protected"))) = 1;
EOF
has_visibility_attribute=no
if $cc -Werror -S $TMPC -o $TMPS >/dev/null 2>&1; then
    if grep '\.hidden.*foo' $TMPS >/dev/null; then
	if grep '\.protected.*bar' $TMPS >/dev/null; then
	    has_visibility_attribute=yes
	fi
    fi
fi
rm -f $TMPC $TMPS

# check for sigaction() support
cat > $TMPC << EOF
#include <signal.h>
static void sigill_handler(int sig) { }

int main(void) {
  struct sigaction old_sigill_sa, sigill_sa;
  sigemptyset(&sigill_sa.sa_mask);
  sigill_sa.sa_flags = 0;
  sigill_sa.sa_handler = sigill_handler;
  if (sigaction(SIGILL, &sigill_sa, &old_sigill_sa) != 0)
    return 1;
  return 0;
}
EOF
has_sigaction=no
if $cc $TMPC -o $TMPE >/dev/null 2>&1; then
    if $TMPE; then
	has_sigaction=yes
    fi
fi
rm -f $TMPC $TMPE

# check for compiler type
cat > $TMPC << EOF
#include <stdio.h>
#if defined __INTEL_COMPILER
#define COMPILER_NAME "icc"
#elif defined __GNUC__
#define COMPILER_NAME "gcc"
#elif defined __xlC__
#define COMPILER_NAME "xlc"
#elif defined __sgi
#define COMPILER_NAME "mipspro"
#else
#define COMPILER_NAME "unknown"
#endif
int main(void)
{
    printf("%s\n", COMPILER_NAME);
    return 0;
}
EOF
compiler="unknown"
if $cc $TMPC -o $TMPE >/dev/null 2>&1; then
    compiler=`$TMPE`
fi
rm -f $TMPC $TMPE

# check for compiler flag
check_cc_option() {
    echo "int i;" > $TMPC
    if $cc $* -c $TMPC -o $TMPO > /dev/null 2>&1; then
	rm -f $TMPC $TMPO
	return 0
    fi
    rm -f $TMPC
    return 1
}

# check for C++ compiler
is_cxx_compiler() {
    cat > $TMPC << EOF
#ifndef __cplusplus
#error "This is not a C++ compiler"
#endif
class A { };
EOF
    if $cc -c $TMPC -o $TMPO > /dev/null 2>&1; then
	rm -f $TMPC $TMPO
	return 0
    fi
    rm -f $TMPC
    return 1
}

# check for C99 support
if ! is_cxx_compiler; then
    if test "$compiler" = "gcc"; then
	if check_cc_option -std=gnu99; then
	    cc="$cc -std=gnu99"
	fi
    elif test "$compiler" = "icc"; then
	if check_cc_option -c99; then
	    cc="$cc -c99"
	fi
    elif test "$compiler" = "xlc"; then
	if check_cc_option -qlanglvl=extc99; then
	    cc="$cc -qlanglvl=extc99"
	fi
    elif test "$compiler" = "mipspro"; then
	if check_cc_option -c99; then
	    cc="$cc -c99"
	fi
    fi
fi
cat > $TMPC << EOF
/* Only check for features we want */
int main(void)
{
    int n = 0, v = 0;
    for (int i = 0; i < 4; i++) {
      --n;
      int j = n * i;
      v += j;
    }
    return 0;
}
EOF
if ! $cc $TMPC -o $TMPE > /dev/null 2>&1; then
    if is_cxx_compiler; then
	echo "ERROR: Unsupported C++ compiler"
    else
	echo "ERROR: Unsupported C compiler, try with a C++ compiler?"
    fi
    exit 1
fi
rm -f $TMPC $TMPE

# check for CFLAGS
if test "$compiler" = "gcc"; then
    if test -z "$CFLAGS"; then
	CFLAGS="-O2 -g"
	if check_cc_option -mtune=generic $CFLAGS; then
	    CFLAGS="$CFLAGS -mtune=generic"
	fi
    fi
    if check_cc_option -Wall; then
	CFLAGS="$CFLAGS -Wall"
    fi
fi

# Print configuration summary
echo "Install prefix            $prefix"
echo "Library prefix            $libdir"
echo "Source path               $source_path"
echo "C compiler                $cc"
echo "Build static libraries    $build_static"
echo "Build shared libraries    $build_shared"
echo "Build perl bindings       $build_perl"
echo "Build python bindings     $build_python"
echo "Install SDK               $install_sdk"
echo "Install stripped          $install_stripped"
echo "Target OS                 $target_os"
echo "Target CPU                $target_cpu"

config_mak="config.mak"
echo "# Automatically generated by configure - do not modify" > $config_mak

config_h="config.h"
echo "/* Automatically generated by configure - do not modify */" > $config_h

libcpuinfo_pc="libcpuinfo.pc"

echo "COMPILER=$compiler" >> $config_mak
echo "CC=$cc" >> $config_mak
echo "CFLAGS=$CFLAGS" >> $config_mak
echo "COMPILER=$compiler" >> $config_mak
if test "$target_os" = "linux"; then
    echo "OS=linux" >> $config_mak
    echo "#define TARGET_LINUX 1" >> $config_h
    echo "#define TARGET_OS \"linux\"" >> $config_h
elif test "$target_os" = "dragonfly"; then
    echo "OS=dragonfly" >> $config_mak
    echo "#define TARGET_DRAGONFLY 1" >> $config_h
    echo "#define TARGET_OS \"dragonfly\"" >> $config_h
elif test "$target_os" = "freebsd"; then
    echo "OS=freebsd" >> $config_mak
    echo "#define TARGET_FREEBSD 1" >> $config_h
    echo "#define TARGET_OS \"freebsd\"" >> $config_h
elif test "$target_os" = "netbsd"; then
    echo "OS=netbsd" >> $config_mak
    echo "#define TARGET_NETBSD 1" >> $config_h
    echo "#define TARGET_OS \"netbsd\"" >> $config_h
elif test "$target_os" = "darwin"; then
    echo "OS=darwin" >> $config_mak
    echo "#define TARGET_DARWIN 1" >> $config_h
    echo "#define TARGET_OS \"darwin\"" >> $config_h
elif test "$target_os" = "windows"; then
    echo "OS=windows" >> $config_mak
    echo "#define TARGET_WINDOWS 1" >> $config_h
    echo "#define TARGET_OS \"windows\"" >> $config_h
elif test "$target_os" = "irix"; then
    echo "OS=irix" >> $config_mak
    echo "#define TARGET_IRIX 1" >> $config_h
    echo "#define TARGET_OS \"irix\"" >> $config_h
elif test "$target_os" = "hpux"; then
    echo "OS=hpux" >> $config_mak
    echo "#define TARGET_HPUX 1" >> $config_h
    echo "#define TARGET_OS \"hpux\"" >> $config_h
else
    echo "ERROR: Unsupported OS"
    exit 1
fi
if test "$target_cpu" = "i386" ; then
    echo "ARCH=i386" >> $config_mak
    echo "CPUINFO_ARCH=x86" >> $config_mak
    echo "#define TARGET_I386 1" >> $config_h
    echo "#define TARGET_ARCH \"i386\"" >> $config_h
elif test "$target_cpu" = "x86_64" ; then
    echo "ARCH=x86_64" >> $config_mak
    echo "CPUINFO_ARCH=x86" >> $config_mak
    echo "#define TARGET_X86_64 1" >> $config_h
    echo "#define TARGET_ARCH \"x86_64\"" >> $config_h
elif test "$target_cpu" = "ppc" ; then
    echo "ARCH=ppc" >> $config_mak
    echo "CPUINFO_ARCH=ppc" >> $config_mak
    echo "#define TARGET_PPC 1" >> $config_h
    echo "#define TARGET_ARCH \"ppc\"" >> $config_h
elif test "$target_cpu" = "ppc64" ; then
    echo "ARCH=ppc64" >> $config_mak
    echo "CPUINFO_ARCH=ppc" >> $config_mak
    echo "#define TARGET_PPC64 1" >> $config_h
    echo "#define TARGET_ARCH \"ppc64\"" >> $config_h
elif test "$target_cpu" = "mips" ; then
    echo "ARCH=mips" >> $config_mak
    echo "CPUINFO_ARCH=mips" >> $config_mak
    echo "#define TARGET_MIPS 1" >> $config_h
    echo "#define TARGET_ARCH \"mips\"" >> $config_h
elif test "$target_cpu" = "mips64" ; then
    echo "ARCH=mips64" >> $config_mak
    echo "CPUINFO_ARCH=mips" >> $config_mak
    echo "#define TARGET_MIPS64 1" >> $config_h
    echo "#define TARGET_ARCH \"mips64\"" >> $config_h
elif test "$target_cpu" = "ia64" ; then
    echo "ARCH=ia64" >> $config_mak
    echo "CPUINFO_ARCH=ia64" >> $config_mak
    echo "#define TARGET_IA64 1" >> $config_h
    echo "#define TARGET_ARCH \"ia64\"" >> $config_h
else
    echo "ERROR: Unsupported CPU"
    exit 1
fi

echo "SRC_PATH=$source_path" >> $config_mak
echo "prefix=$prefix" >> $config_mak
echo "bindir=$prefix/bin" >> $config_mak
echo "libdir=$libdir" >> $config_mak
echo "includedir=$prefix/include" >> $config_mak

if test "$install_stripped" = "yes"; then
    echo "INSTALL_STRIPPED=-s" >> $config_mak
fi
echo "build_static=$build_static" >> $config_mak
echo "build_shared=$build_shared" >> $config_mak
echo "build_perl=$build_perl" >> $config_mak
echo "PERL_INSTALLDIRS=$PERL_INSTALLDIRS" >> $config_mak
echo "build_python=$build_python" >> $config_mak
echo "install_sdk=$install_sdk" >> $config_mak

VERSION=`sed < $source_path/$PACKAGE.spec -n '/^\%define version[	]*/s///p'`
RELEASE=`sed < $source_path/$PACKAGE.spec -n '/^\%define release[	]*/s///p'`
SVNDATE=`sed < $source_path/$PACKAGE.spec -n '/^\%define svndate[ 	]*/s///p'`
if test -z "$SVNDATE"; then
    SVNDATE=`date '+%Y%m%d'`
fi
SNAPSHOT=0
if echo "$RELEASE" | grep -q ^0; then
    SNAPSHOT=1
fi
echo "VERSION=$VERSION" >> $config_mak
echo "SVNDATE=$SVNDATE" >> $config_mak
echo "SNAPSHOT=$SNAPSHOT" >> $config_mak
echo "#define CPUINFO_SNAPSHOT $SNAPSHOT" >> $config_h
if test "$SNAPSHOT" = "1"; then
    echo "#define CPUINFO_VERSION \"$VERSION-Pre ($SVNDATE)\"" >> $config_h
else
    echo "#define CPUINFO_VERSION \"$VERSION\"" >> $config_h
fi

if test "$has_designated_initializers" = "yes"; then
    echo "#define HAVE_DESIGNATED_INITIALIZERS 1" >> $config_h
else
    echo "#undef HAVE_DESIGNATED_INITIALIZERS" >> $config_h
fi
if test "$has_visibility_attribute" = "yes"; then
    echo "#define HAVE_VISIBILITY_ATTRIBUTE 1" >> $config_h
else
    echo "#undef HAVE_VISIBILITY_ATTRIBUTE" >> $config_h
fi
if test "$has_sigaction" = "yes"; then
    echo "#define HAVE_SIGACTION 1" >> $config_h
else
    echo "#undef HAVE_SIGACTION" >> $config_h
fi

cat > $libcpuinfo_pc << EOF
prefix=$prefix
exec_prefix=$prefix
libdir=$libdir
includedir=$prefix/include

Name: libcpuinfo
Description: CPU identification library
Version: $VERSION
Cflags: -I\${includedir}
Libs: -L\${libdir} -lcpuinfo
EOF

# check for headers defining fixed-size integers
for header in stdint.h inttypes.h sys/types.h; do
    cat > $TMPC << EOF
#include <$header>
int main(void) { return !(sizeof(uint32_t) == 4); }
EOF
    if $cc $TMPC -o $TMPE > /dev/null 2>&1; then
	header_def=`echo "$header" | tr '[:lower:]./-' '[:upper:]___'`
	echo "#define HAVE_$header_def 1" >> $config_h
    fi
    rm -f $TMPC $TMPE
done

# check for sys/personality.h
header=sys/personality.h
cat > $TMPC << EOF
#include <$header>
int main(void) { return PER_LINUX_32BIT; }
EOF
if $cc $TMPC -o $TMPE > /dev/null 2>&1; then
    header_def=`echo "$header" | tr '[:lower:]./-' '[:upper:]___'`
    echo "#define HAVE_$header_def 1" >> $config_h
fi
rm -f $TMPC $TMPE

# build tree in object directory if source path is different from current one
if test "$source_path_used" = "yes" ; then
    case $source_path in
    ..*)
	sp1=..
	;;
    esac
    ln -sf $source_path/Makefile Makefile
fi
