#!/bin/sh
#
# This is the configuration file creator.
#
# configure,v 1.21 2003/06/27 23:19:01 frenger Exp
#

args="$0 $*"

unset debug
unset prof
unset optimized
unset shared

matlab=0
cblas=1
lapack=1
fftw=1
mkl=0
use_cc=0
it_dir=`pwd`
exceptions=0
ar="ar"

if [ -n "$MATLAB" ]; then
    matlab=1
fi

cpu="`uname -m`"
case "$cpu" in
    sun4*)   cxx_extraflag=-mv8 ;;
    i?86)    cxx_extraflag=-mcpu="$cpu" ;;
esac

os="`uname`"

# check if we are running Darwin, if so, check if we have an altivec
# processor and are using Apples gcc, then enable altivec
applecc=0
if [ "$os"="Darwin" ]; then 
	cxx_extraflag=-D__unix
	applecc="`g++ -dN -E - </dev/null | grep -c __APPLE_CC__`"
	if [ "`sysctl -n hw.optional.altivec`" ]; then
		if [ $applecc=1 ]; then
			cxx_extraflag="-D__unix -faltivec"
		fi
	fi
fi

while [ $# -gt 0 ]; do
    case $1 in
	all)             debug=1 prof=1 shared=1 optimized=1 ;;
	debug)           debug=1 ;;
	no-debug)        unset debug ;;
	prof*)           prof=1 ;;
	no-prof*)        unset prof ;;
	shared)          shared=1 ;;
	no-shared)       unset shared ;;
	opt*)            optimized=1 ;;
	no-opt*)         unset optimized ;;
	exceptions)      exceptions=1 ;;
	matlab)          matlab=1 ;;
	no-matlab)       matlab=0 ;;
	pentium3)        cxx_extraflag="-march=pentium3 -mfpmath=sse -msse" ;;
	pentium4)        cxx_extraflag="-march=pentium4 -mfpmath=sse -msse2" ;;
	sparc)           cxx_extraflag=-mv8 ;;
	CC)              use_cc=1 ;;
	no-extlib)       cblas=0 lapack=0 fftw=0 ;;
	no-cblas)        cblas=0 ;;
	no-lapack)       lapack=0 ;;
	no-fftw)         fftw=0 ;;
	mkl)             mkl=1 ;;
	ranlib)          ar="ranlib" ;;
        help)            cat <<EOF
Available options:
  all             Create optimized, debugging, profiling and shared libraries.
  debug           Create a debugging library.
  no-debug        Do not create a debugging library.
  prof            Create a profiling library.
  no-prof         Do not create a profiling library.
  shared          Create a shared (dynamically linked) library.
  no-shared       Do not create a shared library.
  opt             Create an optimized library.
  no-opt          Do not create an optimized library.
  exceptions      Use throw instead of abort().
  matlab          Enable the use of matlab. (Auto-detected)
  no-matlab       Disable the use of matlab.
  pentium3        Compile for pentium3 including SSE instructions.
  pentium4        Compile for pentium4 including SSE2 instructions.
  sparc           Compile for sparc. (Auto-detected)
  CC              Use the CC compiler instead of g++ (experimental).
  no-extlib       Do not use an external library (i.e. no CBLAS, LAPACK or FFTW support)
  no-cblas        Do not use CBLAS routines for faster math.
  no-lapack       Do not use LAPACK routines for faster math (no matrix decompositions will be avaiable)
  no-fftw         Do not use FFTW routines (no FFTs will be available).
  mkl             Use Intel Math Kernel Library instead of Netlib-CBLAS/LAPACK for faster math
  ranlib          Use ranlib instead of ar (to create table of content for the libfiles)
  help            This text.

Example:
  ./configure debug opt
EOF
			 exit ;;
	*)               echo "Unknown flag ignored ($1)" ;;
    esac
    shift
done

if [ "$optimized$debug$prof$shared" = "" ]; then
    optimized=1
fi

if [ "$optimized" != "" ]; then
    echo "Configuring makefiles for building of an optimized library"
fi

if [ "$debug" != "" ]; then
    echo "Configuring makefiles for building of a debug library"
fi

if [ "$prof" != "" ]; then
    echo "Configuring makefiles for building of a profiling library"
fi

if [ "$shared" != "" ]; then
    echo "Configuring makefiles for building of a shared library"
fi

if [ $use_cc = 1 ]; then
    echo "Configuring makefiles to use the SUN CC compiler."
    compiler="CC"
else
    echo "Configuring makefiles to use the GNU g++ compiler."
    compiler="g++"
fi

if [ $mkl = 1 ]; then
    echo "Using the Intel MKL libraries."
    lapack=1;
    cblas=1;
fi

if [ $cblas = 1 ]; then
    echo "Configuring IT++ to use CBLAS functionality."
else
    echo "Configuring IT++ not to use CBLAS functionality."
fi

if [ $lapack = 1 ]; then
    echo "Configuring IT++ to use LAPACK functionality."
else
    echo "Configuring IT++ not to use LAPACK functionality."
fi

if [ $fftw = 1 ]; then
    echo "Configuring IT++ to use FFTW functionality."
else
    echo "Configuring IT++ not to use FFTW functionality."
fi

if [ $exceptions = 1 ]; then
    echo "Using C++ exceptions for error handling by default."
else
    echo "NOT using C++ exceptions for error handling by default."
fi

libtargets="${optimized:+\$(OPT_LIB) }${debug:+\$(DEBUG_LIB) }${prof:+\$(PROF_LIB) }${shared:+\$(SHARED_LIB)}"

if [ $mkl = 1 ]; then

    # ----------------------------------------------------------------------------
    #    Try to detect MKL in it++/lib
    # ----------------------------------------------------------------------------

    echo ""
    echo "Trying to detect if MKL libraries are present in the folder $it_dir/lib..."
    echo ""

    mkl_processor_specific_lib=""

    # Searching for the processor specific MKL library: libmkl_*.a or .so
    if [ -f "$it_dir/lib/libmkl_def.so" ]; then 
	mkl_processor_specific_lib="-lmkl_def"
	echo "Using the MKL library file libmkl_def.so (for Pentium, Pentium Pro, and Pentium II processors)"
    else
	if [ -f "$it_dir/lib/libmkl_def.a" ]; then 
	    mkl_processor_specific_lib="-lmkl_def"
	    echo "Using the MKL library file libmkl_def.a (for Pentium, Pentium Pro, and Pentium II processors)"
	fi
    fi
    if [ -f "$it_dir/lib/libmkl_p3.so" ]; then 
	mkl_processor_specific_lib="-lmkl_p3"
	echo "Using the MKL library file libmkl_p3.so (for Pentium III processors)"
    else
	if [ -f "$it_dir/lib/libmkl_p3.a" ]; then 
	    mkl_processor_specific_lib="-lmkl_p3"
	    echo "Using the MKL library file libmkl_p3.a (for Pentium III processors)"
	fi
    fi
    if [ -f "$it_dir/lib/libmkl_p4.so" ]; then 
	mkl_processor_specific_lib="-lmkl_p4"
	echo "Using the MKL library file libmkl_p4.so (for Pentium 4 processors)"
    else
	if [ -f "$it_dir/lib/libmkl_p4.a" ]; then 
	    mkl_processor_specific_lib="-lmkl_p4"
	    echo "Using the MKL library file libmkl_p4.a (for Pentium 4 processors)" 
	fi  
    fi

    if [ "$mkl_processor_specific_lib" = "" ]; then
	echo "WARNING"
	echo "Could not find any processor specific MKL library in the folder $it_dir/lib." 
	echo "Please change directory to $it_dir/lib"
	echo "and use ONE of the commands below to create a symbolic link:"
	echo ""
	echo "* Pentium, Pentium Pro, Pentium II (Choose .a for static and .so for dynamic linking):"
	echo "  > ln -s <Path to your MKL-lib folder>/libmkl_def.a ./libmkl_def.a"
	echo "  > ln -s <Path to your MKL-lib folder>/libmkl_def.so ./libmkl_def.so"
	echo ""
	echo "* Pentium III (Choose .a for static and .so for dynamic linking):"
	echo "  > ln -s <Path to your MKL-lib folder>/libmkl_p3.a ./libmkl_p3.a"
	echo "  > ln -s <Path to your MKL-lib folder>/libmkl_p3.so ./libmkl_p3.so"
	echo ""
	echo "* Pentium 4 (Choose .a for static and .so for dynamic linking):"
	echo "  > ln -s <Path to your MKL-lib folder>/libmkl_p4.a ./libmkl_p4.a"
	echo "  > ln -s <Path to your MKL-lib folder>/libmkl_p4.so ./libmkl_p4.so"
	echo ""
	echo "If you choose to ignore this warning, the it-config script in the $itpp_dir/bin folder"
	echo "may not work properly."
	echo ""
    fi

    #Searching for the other MKL libraries: libmkl_lapack.a and libguide.a
    if [ -f "$it_dir/lib/libmkl_lapack.a" -a -f "$it_dir/lib/libguide.a" ]; then
	ext_libs="-lmkl_lapack $mkl_processor_specific_lib -lguide -lpthread -lit++external -lg2c"
	echo "The following additional MKL libraries were found in $it_dir/lib:"
	echo "libmkl_lapack.a and libguide.a"
	echo "The following libraries are added to the it-config script in the $it_dir/bin folder:"
	echo "-lmkl_lapack $mkl_processor_specific_lib -lguide -lpthread -g2c"
    else 
	echo "WARNING:"
	echo "All required MKL libraries can not be found in the folder $it_dir/lib."
	echo "To fix this,  you can make symbolic links by issuing the following commands"
	echo "in the $it_dir/lib folder:"
	echo ""
	echo "> ln -s <Path to your MKL-lib folder>/libmkl_<your_cpu_type>.a ./libmkl_<your_cpu_type>.a"
	echo "> ln -s <Path to your MKL-lib folder>/libmkl_lapack.a ./libmkl_lapack.a"
	echo "> ln -s <Path to your MKL-lib folder>/libguide.a ./libguide.a"
	echo ""
	echo "If you choose to ignore this warning, the it-config script in the $itpp_dir/bin folder"
	echo "may not work properly. For more information, please read the sections about installation"
	echo "and linking in the IT++ getting started manual."
	echo ""
	echo "Note: Use the .so library files instead of the .a library files for dynamic linking."
    fi

else

    # ----------------------------------------------------------------------------
    #    Try to detect ATLAS and LAPACK in it++/lib
    # ----------------------------------------------------------------------------

    if [ $cblas = 1 ]; then
	if [ $lapack = 1 ]; then
	  if [ $applecc = 0 ]; then
	    echo ""
	    echo "Trying to detect if ATLAS and LAPACK libraries are present in the folder $it_dir/lib..."
	    echo ""
	    if [ -f "$it_dir/lib/libit++external.a" ]; then
		ext_libs="-lit++external -lg2c"
		echo "Found libit++external.a in $it_dir/lib:"
		echo "This library (and -lg2c) is added to the it-config script in the $it_dir/bin folder."
	    else
		echo "WARNING:"
		echo "The ATLAS, LAPACK and FFTW libraries can not be found in the folder $it_dir/lib."
		echo "Note that the it-config script in the $it_dir/bin folder may not function properly."
		echo "";
		echo "If you choose to ignore this warning, the it-config script in the $itpp_dir/bin folder"
		echo "may not work properly. For more information, please read the sections about installation"
		echo "and linking in the IT++ getting started manual."
	    fi
	  else
		echo "You appear to be using Apple's gcc compiler, and to get BLAS and LAPACK support,"
		echo "the vecLib framework will be used."
		ext_libs="-framework vecLib -lit++external"
	  fi
	fi
    fi
fi
echo ""

if [ $cblas = 0 ]; then
    cblas_flags="-DNO_CBLAS"
fi

if [ $lapack = 0 ]; then
    lapack_flags="-DNO_LAPACK"
fi

if [ $fftw = 0 ]; then
    lapack_flags="-DNO_FFTW"
fi

it_version="`cat version`"

# ----------------------------------------------------------------------------
#    Create MakeConfig
# ----------------------------------------------------------------------------

echo Creating MakeConfig
cat <<EOF > MakeConfig
#
# This file was automatically generated by configure using
# $args
#

.SUFFIXES: .h .c .hpp .cpp .o .a

IT_VERSION  = $it_version
OPT_LIB     = \$(LIB_PATH)\$(LIB_BASE).a
DEBUG_LIB   = \$(LIB_PATH)\$(LIB_BASE)_d.a
PROF_LIB    = \$(LIB_PATH)\$(LIB_BASE)_p.a
SHARED_LIB  = \$(LIB_PATH)\$(LIB_BASE)_s.so
LIBTARGETS  = $libtargets
HAVE_MATLAB = $matlab
HAVE_CBLAS  = $cblas
HAVE_LAPACK = $lapack
HAVE_FFTW   = $fftw
OS          = $os
IT_DIR      = $it_dir
#CXX        = $compiler

PROF_FLAGS  = -pg \$(OPTIMIZED_FLAGS)
DEBUG_FLAGS = -g -DASSERT_LEVEL=2
CXXFLAGS    = -Wall -pipe $cxx_extraflag

ifeq (\$(HAVE_MATLAB),1)
  MATLAB_PATH = $matlab_path
endif

ifeq (\$(HAVE_CBLAS),0)
  CXXFLAGS += -DNO_CBLAS
endif

ifeq (\$(HAVE_LAPACK),0)
  CXXFLAGS += -DNO_LAPACK
endif

ifeq (\$(HAVE_FFTW),0)
  CXXFLAGS += -DNO_FFTW
endif

ifeq (\$(CXX),CC)
  SHARED_FLAGS    = -PIC -O3 -DNDEBUG -DASSERT_LEVEL=1
  OPTIMIZED_FLAGS = -fast -cg92 -DNDEBUG -DASSERT_LEVEL=1
else
  SHARED_FLAGS    = -fPIC -O3 -DNDEBUG -DASSERT_LEVEL=1
  OPTIMIZED_FLAGS = -O3 -DNDEBUG -DASSERT_LEVEL=1
endif

DEPEND      = makedepend
LD          = ld
LDFLAGS     =
AR          = $ar

include $it_dir/MakeRules
EOF

# ----------------------------------------------------------------------------
#    Create ./bin/it-config
# ----------------------------------------------------------------------------

eof="EOF"

echo Creating ./bin/it-config
cat <<EOF > ./bin/it-config
#!/bin/sh

#
# This file was automatically generated by configure using
# $args
#

itpp_libs_opt="-L/usr/local/lib -lit++ $ext_libs"
itpp_libs_debug="-L/usr/local/lib -lit++_d $ext_libs"
itpp_libs_prof="-L/usr/local/lib -lit++_p $ext_libs"
itpp_libs_shared="-L/usr/local/lib -lit++ $ext_libs"

itpp_flags_opt="-I/usr/local/include -O3 -DNDEBUG -DASSERT_LEVEL=1 $cblas_flags $lapack_flags $cxx_extraflag"
itpp_flags_debug="-I/usr/local/include -g -DASSERT_LEVEL=2 $cblas_flags $lapack_flags $cxx_extraflag"
itpp_flags_prof="-I/usr/local/include -pg -O3 -DNDEBUG -DASSERT_LEVEL=1 $cblas_flags $lapack_flags $cxx_extraflag"
itpp_flags_shared="-I/usr/local/include -fpic -O3 -DNDEBUG -DASSERT_LEVEL=1 $cblas_flags $lapack_flags $cxx_extraflag"

flags=\$itpp_flags_opt
libs=\$itpp_libs_opt

usage()
{
	cat <<EOF
Usage: it-config [OPTIONS]

Options:
	[--libs]     Display the libraries required for linking with IT++.
	[--flags]    Display flags that should be used to compile IT++ programs.
	[--opt]      Modifies the --libs or the --flags option to enable optimization settings.
	[--debug]    Modifies the --libs or the --flags option to enable debug settings.
	[--prof]     Modifies the --libs or the --flags option to enable profiling settings.
	[--shared]   Modifies the --libs or the --flags option to enable shared settings.
	[--version]  Display the version number of IT++.

Examples:
        Display the libraries that should be used when using a debugger:
        > it-config --libs --debug

        Display the compiler flags that should be used when using a profiler (e.g. gprof):
        > it-config --flags --prof

$eof
	exit $1
}

if test \$# -eq 0; then
	usage 1 1>&2
fi

while test \$# -gt 0; do
  case "\$1" in
  -*=*) optarg=\`echo "\$1" | sed 's/[-_a-zA-Z0-9]*=//'\` ;;
  *) optarg= ;;
  esac

  case \$1 in
    --version)
      echo $it_version
      ;;
    --flags)
      echo_flags=yes
      ;;
    --libs)
      echo_libs=yes
      ;;
    --opt)
      flags=\$itpp_flags_opt
      libs=\$itpp_libs_opt
      ;;
    --debug)
      flags=\$itpp_flags_debug
      libs=\$itpp_libs_debug
      ;;
    --prof)
      flags=\$itpp_flags_prof
      libs=\$itpp_libs_prof
      ;;
    --shared)
      flags=\$itpp_flags_shared
      libs=\$itpp_libs_shared
      ;;
    *)
      usage 1 1>&2
      ;;
  esac
  shift
done

if test "\$echo_flags" = "yes"; then
    echo \$flags
fi

if test "\$echo_libs" = "yes"; then
	echo \$libs
fi      

EOF

chmod ugo+x ./bin/it-config
# ----------------------------------------------------------------------------


syntax highlighted by Code2HTML, v. 0.9.1