#!/bin/bash -e

# --------------------------------------------------------------------------- #
# cmplr.env : Defines the compiler executable and its options for WW3         #
#             from the compiler keyword given in argument                     #
#                                                                             #
#  - supported compiler are SGI-MPT, INTEL, GNU, PORTLAND, CRAY               #
#                                                                             #
#                                                                             #
# use : cmplr.env cmplr                                                       #
#                                                                             #
#  cmplr : keyword based on a value and optional suffix, prefix and extras    #
#                                                                             #
#       value : mpt / intel / gnu / pgi / cray                                #
#       suffix : _debug                                                       #
#       prefix : datarmor_                                                    #
#       extras : so_                                                          #
#                                                                             #
#  - examples : intel / pgi_debug / datarmor_mpt_debug / so_intel             #
#                                                                             #
# remarks :                                                                   #
#                                                                             #
#  - template files comp.tmpl and link.tmpl will be used to create the        #
#    comp and link file based on the following environment variables :        # 
#    $optc, $optl, $comp_seq, $comp_mpi, $optomp, $err_pattern, $warn_pattern #
#                                                                             #
#                                                                             #
#                                                      M. Accensi             #
#                                                      August   2018          #
# --------------------------------------------------------------------------- #


## Set some defaults (can be overriden by individual compiler sections)

# grep pattern for errors/warnings in compiler output:
err_pattern='error'
warn_pattern='warn'

# disable listing done by the compiler
list='no'


###############################
# MPT                         #
###############################

if [ "$cmplr" == "mpt" ] || [ "$cmplr" == "mpt_debug" ] || [ "$cmplr" == "mpt_prof" ] || \
   [ "$cmplr" == "datarmor_mpt" ] || [ "$cmplr" == "datarmor_mpt_debug" ] || [ "$cmplr" == "datarmor_mpt_prof" ] ; then

  # COMPILER - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  # common compiler
  comp_seq='ifort'
  comp_mpi='ifort -lmpi'

  # Cray compiler
  if [ ! -z "$(echo $cmplr | grep wcoss_cray)" ] ; then
    comp_seq='ftn'
    comp_mpi='ftn'
  fi
    
 
  # OPTIONS - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  # common options
  optc='-c -module $path_m -no-fma -ip -g -i4 -real-size 32 -fp-model precise -assume byterecl -convert big_endian -fno-alias -fno-fnalias'
  optl='-o $prog -g'

  # list options
  if [ "$list" == 'yes' ] ; then optc="$optc -list"; fi

  # omp options
  optomp="-openmp"

  # optimized options
  if [ -z "$(echo $cmplr | grep debug)" ] ; then
    optc="$optc -O3"
    optl="$optl -O3"
  fi

  # profiling option
  if [ ! -z "$(echo $cmplr | grep prof)" ] ; then
    optc="$optc -p"
    optl="$optl -p"
  fi

  # debugging options
  if [ ! -z "$(echo $cmplr | grep debug)" ] ; then
    optc="$optc -O0 -debug all -warn all -check all -check noarg_temp_created -fp-stack-check -heap-arrays -traceback -fpe0"
    optl="$optl -O0 -traceback"
  fi

  # system-dependant options
  if [ ! -z "$(echo $cmplr | grep datarmor)" ] ; then
    optc="$optc -xcore-avx2"
    optl="$optl -xcore-avx2"
  else
    optc="$optc -xhost"
    optl="$optl -xhost"
  fi

fi


###############################
# INTEL                       #
###############################

if [ "$cmplr" == "intel" ]          || [ "$cmplr" == "intel_debug" ]    || [ "$cmplr" == "intel_prof" ]    || \
   [ "$cmplr" == "so_intel" ]       || [ "$cmplr" == "so_intel_debug" ] || [ "$cmplr" == "so_intel_prof" ] || \
   [ "$cmplr" == "datarmor_intel" ] || [ "$cmplr" == "datarmor_intel_debug" ] || [ "$cmplr" == "datarmor_intel_prof" ] || \
   [ "$cmplr" == "wcoss_phase2" ]   || [ "$cmplr" == "wcoss_cray" ]     || [ "$cmplr" == "wcoss_dell_p3" ]  || [ "$cmplr" == "wcoss2" ] || \
   [ "$cmplr" == "theia" ]          || [ "$cmplr" == "hera" ] || [ "$cmplr" == "orion" ] ; then


  # COMPILER - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  # compiler
  comp_seq='ifort'
  comp_mpi='mpiifort'

  # cray compiler
  if [ ! -z "$(echo $cmplr | grep wcoss_cray)" ] ; then
    comp_seq='ftn'
    comp_mpi='ftn'
  fi

  if [ ! -z "$(echo $cmplr | grep wcoss2)" ] ; then
    comp_seq='ftn'
    comp_mpi='ftn'
  fi


  # OPTIONS - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  # common options
  optc='-c -module $path_m -no-fma -ip -g -i4 -real-size 32 -fp-model precise -assume byterecl -convert big_endian -fno-alias -fno-fnalias -sox'
  optl='-o $prog -g'

  if [ ! -z "$(echo $cmplr | grep datarmor)" ] ; then
    optc="$optc -xcore-avx2"
    optl="$optl -xcore-avx2"
  elif [ ! -z "$(echo $cmplr | grep wcoss_cray)" ] ; then
    optc="$optc -xCORE-AVX2"
    optl="$optl -xCORE-AVX2"
  elif [ ! -z "$(echo $cmplr | grep wcoss_dell_p3)" ] ; then
    optc="$optc -xHOST"
    optl="$optl -xHOST"
  elif [ ! -z "$(echo $cmplr | grep wcoss2)" ] ; then
    optc="$optc"
    optl="$optl"
  else
    optc="$optc -xhost"
    optl="$optl -xhost"
  fi


  # list options
  if [ "$list" == 'yes' ] ; then optc="$optc -list"; fi

  # omp options
  if [ "$cmplr" == "hera" ] || [ "$cmplr" == "orion" ] || [ "$cmplr" == "wcoss_cray" ] || [ "$cmplr" == "wcoss_dell_p3" ] || [ "$cmplr" == "wcoss2" ] ; then
     optomp="-qopenmp"
  else
     optomp="-openmp"
  fi

  # optimized options
  if [ -z "$(echo $cmplr | grep debug)" ] ; then
    optc="$optc -O3"
    optl="$optl -O3"
  fi

  # profiling option
  if [ ! -z "$(echo $cmplr | grep prof)" ] ; then
    optc="$optc -p"
    optl="$optl -p"
  fi

  # debugging options
  if [ ! -z "$(echo $cmplr | grep debug)" ] ; then
    optc="$optc -O0 -debug all -warn all -check all -check noarg_temp_created -fp-stack-check -heap-arrays -traceback -fpe0"
    optl="$optl -O0 -traceback"
  fi

  # system-dependant options
  if [ ! -z "$(echo $cmplr | grep datarmor)" ] ; then
    optc="$optc -xcore-avx2"
    optl="$optl -xcore-avx2"
  elif [ ! -z "$(echo $cmplr | grep wcoss2)" ] ; then
    optc="$optc"
    optl="$optl"
  else
    optc="$optc -xhost"
    optl="$optl -xhost"
  fi
  
  if [ ! -z "$(echo $cmplr | grep so)" ] ; then
    optc="$optc -fPIC"
    optl='-o $prog -g'
  fi

fi

###############################
# GNU                         #
###############################


if [ "$cmplr" == "gnu" ] || [ "$cmplr" == "gnu_debug" ] || [ "$cmplr" == "gnu_prof" ] || \
   [ "$cmplr" == "so_gnu" ] || [ "$cmplr" == "so_gnu_debug" ] || [ "$cmplr" == "so_gnu_prof" ] || \
   [ "$cmplr" == "datarmor_gnu" ] || [ "$cmplr" == "datarmor_gnu_debug" ] || [ "$cmplr" == "datarmor_gnu_prof" ] || \
   [ "$cmplr" == "ukmo_cray_gnu" ] || [ "$cmplr" == "ukmo_cray_gnu_debug" ] || [ "$cmplr" == "ukmo_cray_gnu_prof" ] ; then

  # COMPILER - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  # common compiler
  comp_seq='gfortran'
  comp_mpi='mpif90'

  # Cray compiler
  if [ ! -z "$(echo $cmplr | grep cray)" ] ; then
    comp_seq='ftn'
    comp_mpi='ftn'
  fi


  # OPTIONS - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  # common options
  optc='-c -J$path_m -g -fno-second-underscore -ffree-line-length-none -fconvert=big-endian'
  optl='-o $prog -g'

  # omp options
  optomp='-fopenmp'

  # optimized options
  if [ -z "$(echo $cmplr | grep debug)" ] ; then
    optc="$optc -O3"
    optl="$optl -O3"
  fi

  # profiling option
  if [ ! -z "$(echo $cmplr | grep prof)" ] ; then
    optc="$optc -p"
    optl="$optl -p"
  fi

  # debugging options
  if [ ! -z "$(echo $cmplr | grep debug)" ] ; then
    optc="$optc -O0 -Wall -fcheck=all -ffpe-trap=invalid,zero,overflow -frecursive -fbacktrace"
    optl="$optl -O0 -fbacktrace"
  fi

  # system-dependant options
  if [ ! -z "$(echo $cmplr | grep datarmor)" ] ; then
    optc="$optc -march=core-avx2"
    optl="$optl -march=core-avx2"
  elif [ ! -z "$(echo $cmplr | grep ukmo_cray)" ]; then
    # don't specify -march for Cray; processor specific tuning handled by ftn wrapper script
    optc="$optc -frecord-marker=4"
  else
    optc="$optc -march=native"
    optl="$optl -march=native"
  fi

  if [ ! -z "$(echo $cmplr | grep so)" ] ; then
    optc="$optc -fPIC"
  fi
fi

###############################
# PGI                         #
###############################

if [ "$cmplr" == "pgi" ] || [ "$cmplr" == "pgi_debug" ] || [ "$cmplr" == "pgi_prof" ] || \
   [ "$cmplr" == "datarmor_pgi" ] || [ "$cmplr" == "datarmor_pgi_debug" ] || [ "$cmplr" == "datarmor_pgi_debug" ] ; then

  # COMPILER - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  # common compiler
  comp_seq='pgf90'
  comp_mpi='mpif90'

  # Cray compiler
  if [ ! -z "$(echo $cmplr | grep wcoss_cray)" ] ; then
    comp_seq='ftn'
    comp_mpi='ftn'
  fi


  # OPTIONS - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  # common options
  optc='-c -module $path_m -g -i4 -r4 -Kieee -byteswapio'
  optl='-o $prog -g'

  # list options
  if [ "$list" == 'yes' ] ; then optc="$optc -Mlist"; fi

  # omp options
  optomp="-mp"

  # optimized options
  if [ -z "$(echo $cmplr | grep debug)" ] ; then
    optc="$optc -O3"
    optl="$optl -O3"
  fi

  # profiling option
  if [ ! -z "$(echo $cmplr | grep prof)" ] ; then
    optc="$optc -p"
    optl="$optl -p"
  fi

  # debugging options
  if [ ! -z "$(echo $cmplr | grep debug)" ] ; then
    optc="$optc -O0 -Mbounds -Mchkfpstk -Mchkstk -Mdalign -Mdclchk -Mdepchk -Miomutex -Ktrap=fp -Mrecursive -traceback"
    optl="$optl -O0 -traceback"
  fi

  # system-dependant options
  if [ ! -z "$(echo $cmplr | grep datarmor)" ] ; then
    optc="$(echo $optc | sed 's/O3/O2/') -Mlist"
    optl="$optl"
  else
    optc="$optc"
    optl="$optl"
  fi
fi


###############################
# Crayftn (CCE)
###############################

if [ "$cmplr" == "ukmo_cray" ] || [ "$cmplr" == "ukmo_cray_debug" ] || \
   [ "$cmplr" == "ukmo_cray_regtest" ]; then

  # COMPILER - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  # common compiler
  comp_seq='ftn'
  comp_mpi='ftn'

  # cray compiler output needs more specific err/warn search patterns:
  err_pattern='crayftn: error'
  warn_pattern='crayftn: warn'
  # OPTIONS - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  # common options
  optc='-c -J$path_m -sdefault32 -eg'
  optl='-o $prog'

  # list options
  if [ "$list" == 'yes' ] ; then optc="$optc -hlist=a"; fi

  # omp options
  optomp="-h omp"

  # optimized options
  if [ -z "$(echo $cmplr | grep debug)" ] ; then
    optc="$optc -O3"
    optl="$optl -O3"
  fi

  # debugging options
  if [ ! -z "$(echo $cmplr | grep debug)" ] ; then
    optc="$optc -O0 -g -Rbcps"
    optl="$optl -O0 -g"
  fi

  # regtest options:
  if [ ! -z "$(echo $cmplr | grep regtest)" ] ; then
    # -O1 best balance between compile time and runtime for regtests
    optc="$optc -O1"
    optl="$optl -O1"
  fi

  # system-dependant options
  # N/A
fi