#!/bin/sh
# ----------------------------------------------------------------------------
# Generic script for building Trilinos and Sundance on Linux machines with 
# configurations similar to gibbon.math.ttu.edu
#
# Run this script as 
# <kevin@gibbon> ./gibbon-linux-sundance [flags]
# where the possible flags are listed by running
# <kevin@gibbon> ./gibbon-linux-sundance --help
#
# This isn't meant as a completely general configuration script; rather, it
# allows streamlined setting of the several options I most commonly vary in
# my own work. You'll probably want to tweak this script for your own system
# and your own selection of packages. In particular, I've assumed we're 
# always building Sundance and that the directory layout is identical to 
# that on my development machines gibbon and chimera. 
#
# 22 March 2009
# Kevin Long (kevin.long@ttu.edu)
#
#-----------------------------------------------------------------------------

# Decide whether to build the SEACAS mesh IO package.
# Default is yes. 
#
ENABLE_SEACAS=ON
# Decide whether to build Stokhos. Default is yes.
ENABLE_STOKHOS=ON
# Decide whether to build Zoltan mesh partitioning. Default is yes.
ENABLE_ZOLTAN=ON


# ----------------------------------------------------------------------------
# Set the fortran library name. On older Linux systems this will 
# need to be changed to -lg2c. 
#
FORTRAN_LIBS="-lgfortran"


# ----------------------------------------------------------------------------
# Set variables that control the type of build
#
#
# Whether to build PySundance
PY_SUNDANCE=OFF
# Whether to build shared libraries. Default=no.
BUILD_SHARED=ON
# Whether to build with MPI. Default=yes.
ENABLE_MPI=ON
# Whether to build optimized or debug. Default=debug. Set to RELEASE or leave
# blank to enable an optimized build.
BUILD_TYPE=DEBUG
# Whether to enable runtime checking of STL calls. Default=no. 
# Do NOT enable this feature unless you know what you're doing, because if any
# other libraries were built without it the combination of enabled and
# disabled STL checking in a single executable will cause serious problems,
# i.e., segfaults in random places.  
CHECK_STL=OFF
# Whether to enable a paranoid level of compiler warnings. This can cause
# problems with Python. Default: on. 
PARANOID_COMPILER=ON
# Whether to do explicit template instantiation
EXPLICIT_TEMPLATES=ON
# Ordinal type (should normally be int)
ORDINAL_TYPE=int
# ----------------------------------------------------------------------------
# Verbosity of configuration
VERBOSE_CONFIGURE=OFF

EXTRA_C_FLAGS=
EXTRA_CXX_FLAGS=


# ----------------------------------------------------------------------------
# Set variables controlling which third-party libraries are available and
# should be linked in. 
#
# Whether the exodus mesh database libraries are available. Default=yes.
ENABLE_NETCDF=ON

# ----------------------------------------------------------------------------
# Specify directories for the Trilinos source and Trilnos data, and for
# the installation of this build.

# Set the installation path
INSTALL_PREFIX=$PWD

# ---------------------------------------------------------------------------
#
# At this point, all variables have been assigned default values. We now
# parse the command line to see if any values are overridden.
#
# ---------------------------------------------------------------------------
# Argument parsing logic.
TEMP=`getopt -o x --long python,help,mpi,serial,shared,static,dryrun,verbose,check-stl,debug,opt,stohkos,no-stokhos,lib64,explicit,no-explicit,lax,ordinal:,src:,data:,prefix:,extra-cflags:,extra-cxxflags: -- "$@"`

eval set -- "$TEMP"

while true ; do
    case "$1" in 
      --python) PY_SUNDANCE=ON; 
                PARANOID_COMPILER=OFF; 
                PYTHON_INC_STRING="-I${PYTHON_INC_PATH}";
                shift ;;
      --mpi) ENABLE_MPI=ON; shift ;;
      --serial) ENABLE_MPI=OFF; shift ;;
      --dryrun) ECHO=echo; shift;;
      --verbose) VERBOSE_CONFIGURE=ON; shift;;
      --shared) BUILD_SHARED=ON; shift;;
      --static) BUILD_SHARED=OFF; shift;;
      --check-stl) CHECK_STL=ON; shift;;
      --debug) BUILD_TYPE=DEBUG; shift;;
      --opt) BUILD_TYPE=RELEASE; 
             OPT_STR="-finline-functions -funroll-loops"; shift;;
      --lax) PARANOID_COMPILER=OFF; shift;;
      --lib64) LIB_PATH="${COMPILER_ROOT}/lib;${COMPILER_ROOT}/lib64"; shift;;
      --explicit) EXPLICIT_TEMPLATES=ON; shift;;
      --no-explicit) EXPLICIT_TEMPLATES=OFF; shift;;
      --stokhos) ENABLE_STOKHOS=ON; shift;;
      --no-stokhos) ENABLE_STOKHOS=OFF; shift;;
      --src)
            TRILINOS_SRC_DIR="$2"; shift 2;;
      --extra-cxxflags)
            EXTRA_CXX_FLAGS="$2"; shift 2;;
      --extra-cflags)
            EXTRA_C_FLAGS="$2"; shift 2;;
      --extra-fflags)
            EXTRA_F_FLAGS="$2"; shift 2;;
      --data)
            TRILINOS_DATA_DIR="$2"; shift 2;;
      --ordinal)
            ORDINAL_TYPE="$2"; shift 2;;
      --prefix)
            INSTALL_PREFIX="$2"; shift 2;;
      --help)
            echo "Command-line arguments:"
            echo "--help           help"
            echo "--dryrun         print the command that would be used"
            echo "--python         enables PySundance"
            echo "--verbose        enable verbose configuration (default: off)"
            echo "--mpi            enable MPI (default: on)"
            echo "--serial         turn off MPI"
            echo "--shared         build shared libraries (default: off)"
            echo "--static         build static libraries (default: on)"
            echo "--check-stl      turn on STL runtime checking (expert use only)"
            echo "--debug          turn on debugging features (default: on)"
            echo "--opt            turn on optimized compilation (default: off)"
            echo "--lax            turn off paranoid compiler flags (necessary for python wrappers)"
            echo "--lib64          include lib64 in search path (default: off)"
            echo "--explicit       enable explicit template instantiation (default: on)"
            echo "--src <srcdir>   set location of Trilinos sources (default: \${HOME}/Code/Trilinos)"
            echo "--extra-cxxflags <f>   extra C++ flags (default: none)" 
            echo "--extra-cflags <f>     extra C flags (default: none)"
            echo "--extra-fflags <f>     extra Fortran flags (default: none)"
            echo "--ordinal <str>  ordinal type (default: int)"
            echo "--data <dir>     set location of Trilinos data (default: \${HOME}/Code/TrilinosData)"
            echo "--prefix <dir>   set installation directory (default: \${PWD})"
            echo " ";
            exit 0;
            shift
            ;;
      --) shift; break;;
   esac
done

# ---------------------------------------------------------------------------
#
# Now run cmake!!
#
# ---------------------------------------------------------------------------

${ECHO} cmake \
-D CMAKE_BUILD_TYPE:STRING=${BUILD_TYPE} \
-D BUILD_SHARED_LIBS:BOOL=${BUILD_SHARED} \
-D CMAKE_SYSTEM_LIBRARY_PATH:FILEPATH="${LIB_PATH}" \
-D CMAKE_SYSTEM_INCLUDE_PATH:FILEPATH="${INC_PATH}" \
-D CMAKE_CXX_FLAGS:STRING="${PYTHON_INC_STRING} ${OPT_STR} ${EXTRA_CXX_FLAGS}" \
-D CMAKE_C_FLAGS:STRING="${PYTHON_INC_STRING} ${EXTRA_C_FLAGS}" \
-D CMAKE_Fortran_FLAGS:STRING="${EXTRA_F_FLAGS}" \
-D TPL_ENABLE_MPI:BOOL=${ENABLE_MPI} \
-D TPL_ENABLE_Netcdf:BOOL=${ENABLE_NETCDF} \
-D TPL_HDF5_LIBRARIES:STRING="/usr/lib/libhdf5.so" \
-D Trilinos_EXTRA_LINK_FLAGS:STRING=${FORTRAN_LIBS} \
-D Trilinos_ENABLE_SHADOW_WARNINGS:BOOL=OFF \
-D Trilinos_ENABLE_ALL_OPTIONAL_PACKAGES:BOOL=OFF \
-D Trilinos_ENABLE_SECONDARY_STABLE_CODE:BOOL=ON \
-D Trilinos_ENABLE_STRONG_CXX_COMPILE_WARNINGS:BOOL=${PARANOID_COMPILER} \
-D Trilinos_ENABLE_CHECKED_STL:BOOL=${CHECK_STL} \
-D Trilinos_ENABLE_TESTS:BOOL=OFF \
-D Trilinos_ENABLE_EXAMPLES:BOOL=OFF \
-D Trilinos_ENABLE_EXPLICIT_INSTANTIATION:BOOL=${EXPLICIT_TEMPLATES} \
-D Trilinos_ENABLE_Sundance:BOOL=ON \
-D Trilinos_ENABLE_SEACAS:BOOL=${ENABLE_SEACAS} \
-D Trilinos_ENABLE_Sacado:BOOL=${ENABLE_SACADO} \
-D Trilinos_ENABLE_Stokhos:BOOL=${ENABLE_STOKHOS} \
-D Trilinos_ENABLE_Zoltan:BOOL=${ENABLE_ZOLTAN} \
-D Sundance_ENABLE_TESTS:BOOL=ON \
-D Sundance_ENABLE_EXAMPLES:BOOL=ON \
-D Stokhos_ENABLE_TESTS:BOOL=OFF \
-D Zoltan_ENABLE_TESTS:BOOL=OFF \
-D SEACAS_ENABLE_TESTS:BOOL=OFF \
-D EpetraExt_USING_HDF5:BOOL=OFF \
-D Trilinos_ENABLE_SEACASNemslice:BOOL=ON \
-D Trilinos_ENABLE_SEACASEpu:BOOL=OFF \
-D Stokhos_ENABLE_EXAMPLES:BOOL=OFF \
-D Sundance_ENABLE_Python:BOOL=${PY_SUNDANCE} \
-D Sundance_ENABLE_SEACAS:BOOL=${ENABLE_SEACAS} \
-D Teuchos_ORDINAL_TYPE:STRING=${ORDINAL_TYPE} \
-D Trilinos_DATA_DIR:FILEPATH="${TRILINOS_DATA_DIR}" \
-D NOX_ENABLE_LOCA:BOOL=OFF \
-D CMAKE_INSTALL_PREFIX:PATH=${INSTALL_PREFIX} \
${TRILINOS_SRC_DIR}

