#!/bin/sh
EXTRA_ARGS=$@

### Set to ON for parallel compile; otherwise OFF for serial (default)
MPI=OFF

### Switch for Debug or Release build:
#BUILD_TYPE=Release
BUILD_TYPE=Debug

### Define to YES to *disable* all exodus deprecated functions
OMIT_DEPRECATED_CODE="NO"

### The following assumes you are building in a subdirectory of ACCESS Root
### If not, then define "ACCESS" to point to the root of the SEACAS source code.
ACCESS=$(cd ..; pwd)

### If you do not have the X11 developer package on your system
### which provides X11/Xlib.h and the libX11, then change the "ON"
### below to "OFF". It will disable blot and fastq
HAVE_X11=ON

### Set to ON to enable the building of a thread-safe version of the Exodus and IOSS libraries.
THREADSAFE_IO_LIBS=OFF

### The SEACAS code will install in ${INSTALL_PATH}/bin, ${INSTALL_PATH}/lib, and ${INSTALL_PATH}/include.
INSTALL_PATH=${ACCESS}

### TPLs -- 
### Make sure these point to the locations to find the libraries and includes in lib and include
### subdirectories of the specified paths.
### For example, netcdf.h should be in ${NETCDF_PATH}/include
HAVE_NETCDF=ON
HAVE_MATIO=ON
HAVE_CGNS=OFF
HAVE_DATAWAREHOUSE=OFF

NETCDF_PATH=${ACCESS}
MATIO_PATH=${ACCESS}
HDF5_PATH=${ACCESS}
CGNS_PATH=${ACCESS}
DATAWAREHOUSE_PATH=${ACCESS}

### Define to NO to *enable* exodus deprecated functions
OMIT_DEPRECATED_CODE="NO"

### Set to ON to use Kokkos in the Ioss library; otherwise OFF (default)
KOKKOS=OFF

### Set to ON for CUDA compile; otherwise OFF (default) (only used if KOKKOS=ON
CUDA=OFF

### Change this to point to the compilers you want to use
COMPILER="gnu"
#COMPILER="clang"
if [ "$MPI" == "ON" ]
then
  MPI_EXEC=`which mpiexec`
  MPI_BIN=`dirname $MPI_EXEC`

  if [ "$COMPILER" == "gnu" ]
  then
      CXX=mpicxx
      CC=mpicc
      FC=mpif77
  else
      CXX=mpicxx
      CC=mpicc
      FC=mpif77
  fi
else
  if [ "$COMPILER" == "gnu" ]
  then
      CXX=g++
      CC=gcc
      FC=gfortran
  else
      CXX=clang++
      CC=clang
      FC=gfortran-mp-4.9
  fi
fi

if [ "$KOKKOS" == "ON" ]
then
  if [ "$CUDA" == "ON" ]
  then
    export "OMPI_CXX=${SEACAS_SRC_DIR}/packages/kokkos/config/nvcc_wrapper"
    export CUDA_MANAGED_FORCE_DEVICE_ALLOC=1
    KOKKOS_SYMBOLS="-D SEACASProj_ENABLE_Kokkos:BOOL=ON \
                    -D TPL_ENABLE_CUDA:Bool=ON \
                    -D CUDA_TOOLKIT_ROOT_DIR:PATH=${CUDA_PATH} \
                    -D Kokkos_ENABLE_Pthread:BOOL=OFF"
  else
    export OMPI_CXX=`which gcc`
    unset CUDA_MANAGED_FORCE_DEVICE_ALLOC
    KOKKOS_SYMBOLS="-D SEACASProj_ENABLE_Kokkos:BOOL=ON \
                    -D SEACASProj_ENABLE_OpenMP:Bool=ON \
                    -D Kokkos_ENABLE_Pthread:BOOL=OFF"
  fi
else
  KOKKOS_SYMBOLS="-D SEACASProj_ENABLE_Kokkos:BOOL=OFF"
fi

if [ "HAVE_DATAWAREHOUSE" == "ON" ]
then
    DW_SYMBOLS="-DTPL_ENABLE_DATAWAREHOUSE:BOOL=${HAVE_DATAWAREHOUSE} \
                -DDataWarehouse_LIBRARY_DIRS:PATH=${DATAWAREHOUSE_PATH}/lib \
                -DDataWarehouse_INCLUDE_DIRS:PATH=${DATAWAREHOUSE_PATH}/include"
fi
    
rm -f CMakeCache.txt

### To use the clang sanitizers:
sanitizer=address     #: AddressSanitizer, a memory error detector.
#sanitizer=integer     #: Enables checks for undefined or suspicious integer behavior.
#sanitizer=thread      #: ThreadSanitizer, a data race detector.
#sanitizer=memory      #: MemorySanitizer, experimental detector of uninitialized reads.
#sanitizer=undefined   #: Fast and compatible undefined behavior checker. 
#sanitizer=dataflow    #: DataFlowSanitizer, a general data flow analysis.
#sanitizer=cfi         #: control flow integrity checks. Requires -flto.
#sanitizer=safe-stack  #: safe stack protection against stack-based memory corruption errors.
SANITIZER="-fsanitize=${sanitizer} -fno-omit-frame-pointer -fPIC"

### You can add these below if you want more verbosity...
#-D CMAKE_VERBOSE_MAKEFILE:BOOL=ON \
#-D SEACASProj_VERBOSE_CONFIGURE=ON \

### You can add these below to regenerate the flex and bison files for
### aprepro and aprepro_lib May have to touch aprepro.l aprepro.y
### aprepro.ll and aprepro.yy to have them regenerate 
#-D GENERATE_FLEX_FILES=ON \
#-D GENERATE_BISON_FILES=ON \

### Additional gcc warnings:
if [ "$COMPILER" == "gnu" ]
then
  COMMON_WARNING_FLAGS="\
   -Wduplicated-cond\
   -Wlogical-op\
   -Wnull-dereference\
   -Wshadow\
   -Wformat=2\
  "

  C_WARNING_FLAGS="${COMMON_WARNING_FLAGS} -Wjump-misses-init"

  CXX_WARNING_FLAGS="${COMMON_WARNING_FLAGS}"

  # -Wuseless-cast
  # -Wold-style-cast
  # -Wdouble-promotion
else
  C_WARNING_FLAGS="-Weverything -Wno-missing-prototypes -Wno-sign-conversion -Wno-reserved-id-macro"

  CXX_WARNING_FLAGS="-Weverything -Wno-c++98-compat -Wno-old-style-cast -Wno-sign-conversion -Wno-reserved-id-macro"
fi
###------------------------------------------------------------------------
cmake  \
-D CMAKE_CXX_FLAGS="-Wall -Wunused -pedantic ${CXX_WARNING_FLAGS} ${SANITIZER}" \
-D CMAKE_C_FLAGS="-Wall -Wunused -pedantic ${C_WARNING_FLAGS} ${SANITIZER}" \
-D CMAKE_Fortran_FLAGS="-Wall ${SANITIZER}" \
-D CMAKE_MACOSX_RPATH:BOOL=ON \
-D CMAKE_INSTALL_RPATH:PATH=${INSTALL_PATH}/lib \
-D BUILD_SHARED_LIBS:BOOL=OFF \
-D CMAKE_BUILD_TYPE=${BUILD_TYPE} \
-D SEACASProj_ENABLE_ALL_PACKAGES:BOOL=ON \
-D SEACASProj_ENABLE_ALL_OPTIONAL_PACKAGES:BOOL=ON \
-D SEACASProj_ENABLE_SECONDARY_TESTED_CODE:BOOL=ON \
-D SEACASProj_ENABLE_TESTS=ON \
-D CMAKE_INSTALL_PREFIX:PATH=${INSTALL_PATH} \
-D CMAKE_CXX_COMPILER:FILEPATH=${CXX} \
-D CMAKE_C_COMPILER:FILEPATH=${CC} \
-D CMAKE_Fortran_COMPILER:FILEPATH=${FC} \
-D SEACASProj_SKIP_FORTRANCINTERFACE_VERIFY_TEST:BOOL=ON \
-D SEACASProj_ENABLE_STRONG_C_COMPILE_WARNINGS=ON \
-D SEACASProj_ENABLE_STRONG_CXX_COMPILE_WARNINGS=ON \
-D SEACASProj_HIDE_DEPRECATED_CODE:BOOL=${OMIT_DEPRECATED_CODE} \
\
-D TPL_ENABLE_Netcdf:BOOL=${HAVE_NETCDF} \
-D TPL_ENABLE_Matio:BOOL=${HAVE_MATIO} \
-D TPL_ENABLE_CGNS:BOOL=${HAVE_CGNS} \
-D TPL_ENABLE_MPI:BOOL=${MPI} \
-D TPL_ENABLE_Pamgen:BOOL=OFF \
-D TPL_ENABLE_Pthread:BOOL=${THREADSAFE_IO_LIBS} \
-D SEACASExodus_ENABLE_THREADSAFE:BOOL=${THREADSAFE_IO_LIBS} \
-D SEACASIoss_ENABLE_THREADSAFE:BOOL=${THREADSAFE_IO_LIBS} \
-D TPL_X11_INCLUDE_DIRS:PATH=/opt/local/include \
-D TPL_ENABLE_X11:BOOL=${HAVE_X11} \
\
${KOKKOS_SYMBOLS} \
${DW_SYMBOLS} \
\
-D MPI_BIN_DIR:PATH=${MPI_BIN} \
-D NetCDF_ROOT:PATH=${NETCDF_PATH} \
-D HDF5_ROOT:PATH=${HDF5_PATH} \
-D HDF5_NO_SYSTEM_PATHS=ON \
-D CGNS_ROOT:PATH=${CGNS_PATH} \
-D Matio_LIBRARY_DIRS:PATH=${MATIO_PATH}/lib \
-D TPL_Matio_INCLUDE_DIRS:PATH=${MATIO_PATH}/include \
\
$EXTRA_ARGS \
..
