#!/bin/bash
#
#
# Build options for Open64 Compiler on Linux x86-64
#  first version tested on Fedore Core 4 x86-64 Opteron (Linux batsi)
#  updated and tested on acesgrid (Fedora Core 15), using:
#     module load open64       (Version 4.2.4)
# Also tested with MPI on acesgrid, using:
#     module load open64       (Version 4.2.4)
#  and  with mpich1:
#     module load mvapich
#   or  with mpich2:
#     module load mvapich2
#   or  with openmpi:  <== not yet tested
#     module load openmpi

#-------
# run with OpenMP: needs to set environment var. OMP_NUM_THREADS
#    and generally, needs to increase the thread stack-size:
#   -  sh,bash:
#     > export OMP_NUM_THREADS=2
#     > export OMP_SLAVE_STACK_SIZE=400m
#   - csh,tcsh:
#     > setenv OMP_NUM_THREADS 2
#     > setenv OMP_SLAVE_STACK_SIZE 400m
#-------

if test "x$MPI" = xtrue ; then
  CC=mpicc
  FC=mpif90
  F90C=mpif90
  LINK=mpif90
else
  CC=opencc
  FC=openf90
  F90C=openf90
  LINK=openf90
fi

DEFINES='-DWORDLENGTH=4'
EXTENDED_SRC_FLAG='-extend-source'
F90FIXEDFORMAT='-fixedform -x f95'
GET_FC_VERSION="--version"
OMPFLAG='-openmp'
#OMPFLAG='-openmp -OPT:early_mp=ON'

NOOPTFLAGS='-O0 -g'
NOOPTFILES=''

CFLAGS='-O0 -m64'
FFLAGS="$FFLAGS -m64 -convert big_endian"

#- for big setups, compile & link with "-fPIC" or set memory-model to "medium":
#CFLAGS="$CFLAGS -fPIC"
#FFLAGS="$FFLAGS -fPIC"
#-  with FC 19, need to use this without -fPIC (which cancels -mcmodel option):
 CFLAGS="$CFLAGS -mcmodel=medium"
 FFLAGS="$FFLAGS -mcmodel=medium"
#- might want to use '-r8' for fizhi pkg:
#FFLAGS="$FFLAGS -r8"

if test "x$IEEE" = x ; then     #- with optimisation:
   #- full optimisation
   # ("-Ofast" eqv "-O3 -ipa -OPT:Ofast -fno-math-errno -ffast-math" and "-OPT:Ofast"
   #  eqv to "-OPT:roundoff=2 -OPT:Olimit=0 -OPT:div_split=ON -OPT:alias=typed")
   #FOPTIM='-Ofast -OPT:roundoff=1:IEEE_arithmetic=3 -LNO:fusion=2'
    FOPTIM='-O3 -OPT:Ofast:roundoff=1:IEEE_arithmetic=3 -ffast-math -LNO:fusion=2'
   #- high optimisation (-O3), might find Pb with few S/R; fully safe: switch to -O2:
   #FOPTIM='-O3 -OPT:roundoff=1:IEEE_arithmetic=3 -LNO:fusion=2'
   #- safer: switch to -O2 with IEEE_arithmetic=2 (to fix some restart Pb):
   #FOPTIM='-O2 -OPT:roundoff=1:IEEE_arithmetic=2 -ffast-math -LNO:fusion=2'
else
  if test "x$DEVEL" = x ; then  #- no optimisation + IEEE :
    FOPTIM='-O0 -OPT:roundoff=0:IEEE_arithmetic=1 -fmath-errno -fno-fast-math'
  else                          #- development/check options:
    FFLAGS="$FFLAGS -ffortran-bounds-check -trapuv -g"
    FOPTIM='-O0 -OPT:roundoff=0:IEEE_arithmetic=1 -fmath-errno -fno-fast-math'
  fi
fi

F90FLAGS=$FFLAGS
F90OPTIM=$FOPTIM
GSLLIB='-lgsl -lgslcblas'

INCLUDEDIRS=''
INCLUDES=''
LIBS=''

if [ "x$NETCDF_ROOT" != x ] ; then
    INCLUDEDIR="${NETCDF_ROOT}/include"
    INCLUDES="-I${NETCDF_ROOT}/include"
    LIBDIR="${NETCDF_ROOT}/lib"
    LIBS="-L${NETCDF_ROOT}/lib"
elif [ "x$NETCDF_HOME" != x ]; then
    INCLUDEDIR="${NETCDF_HOME}/include"
    INCLUDES="-I${NETCDF_HOME}/include"
    LIBDIR="${NETCDF_HOME}/lib"
    LIBS="-L${NETCDF_HOME}/lib"
elif [ "x$NETCDF_INC" != x -a "x$NETCDF_LIB" != x ]; then
    NETCDF_INC=`echo $NETCDF_INC | sed 's/-I//g'`
    NETCDF_LIB=`echo $NETCDF_LIB | sed 's/-L//g'`
    INCLUDEDIR="${NETCDF_INC}"
    INCLUDES="-I${NETCDF_INC}"
    LIBDIR="${NETCDF_LIB}"
    LIBS="-L${NETCDF_LIB}"
elif [ "x$NETCDF_INCDIR" != x -a "x$NETCDF_LIBDIR" != x ]; then
    INCLUDEDIR="${NETCDF_INCDIR}"
    INCLUDES="-I${NETCDF_INCDIR}"
    LIBDIR="${NETCDF_LIBDIR}"
    LIBS="-L${NETCDF_LIBDIR}"
elif test -d /usr/include/netcdf-3 ; then
    INCLUDES='-I/usr/include/netcdf-3'
    LIBS='-L/usr/lib64/netcdf-3 -lnetcdf_g77'
elif test -d /usr/include/netcdf ; then
    INCLUDES='-I/usr/include/netcdf'
elif test -d /usr/local/netcdf ; then
    INCLUDES='-I/usr/local/netcdf/include'
    LIBS='-L/usr/local/netcdf/lib'
elif test -d /usr/local/include/netcdf.inc ; then
    INCLUDES='-I/usr/local/include'
    LIBS='-L/usr/local/lib64'
fi

if [ -n "$MPI_INC_DIR" -a "x$MPI" = xtrue ] ; then
    INCLUDES="$INCLUDES -I$MPI_INC_DIR"
    INCLUDEDIRS="$INCLUDEDIRS $MPI_INC_DIR"
    #- used for parallel (MPI) DIVA
    MPIINCLUDEDIR="$MPI_INC_DIR"
   #MPI_HEADER_FILES='mpif.h mpiof.h'
fi
