#!/bin/bash

# Build options for gfortran compiler (GNU) on Linux amd64 platform albedo (@AWI)
#
#  Tested with gcc-gfortran v12.1.0 loading the following modules
#
# module purge
# module load gcc
# module load openmpi/4.1.3
# module load netcdf-fortran/4.5.4-openmpi4.1.3-gcc12.1.0
# albedo0::> module list
# Currently Loaded Modulefiles:
#  1) gcc/12.1.0   2) openmpi/4.1.3
#  3) netcdf-fortran/4.5.4-openmpi4.1.3-oneapi2022.1.0
#

if test "x$MPI" = xtrue ; then
  CC=mpicc
  FC=mpif77
  F90C=mpif90
else
  CC=gcc
  FC=gfortran
  F90C=gfortran
fi

DEFINES='-DWORDLENGTH=4 -DNML_TERMINATOR'
EXTENDED_SRC_FLAG='-ffixed-line-length-132'
F90FIXEDFORMAT='-ffixed-form'
GET_FC_VERSION="--version"
OMPFLAG='-fopenmp'

NOOPTFLAGS='-O0'
NOOPTFILES=''

CFLAGS='-O0'

#- for setting specific options, check compiler version:
fcVers=`$CC -dumpversion | head -n 1 | sed 's/^[^0-9]* //;s/\..*$//'`
if ! [[ $fcVers =~ ^[0-9]+$ ]] ; then
  echo "    un-recognized gompiler-version '$fcVers' ; ignored (-> set to 0)" ; fcVers=0 ;
else echo "    get gompiler-version: '$fcVers'" ; fi

if [ $fcVers -ge 10 ] ; then
  FFLAGS="$FFLAGS -fallow-argument-mismatch"
fi
#- Requires gfortran from 2006 onwards for -fconvert=big-endian
FFLAGS="$FFLAGS -fconvert=big-endian -fimplicit-none"
#- 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 '-fdefault-real-8' for fizhi pkg:
#FFLAGS="$FFLAGS -fdefault-real-8 -fdefault-double-8"
#- speeds up compilation at the cost of more memory overhead
FFLAGS="$FFLAGS -pipe"

if test "x$IEEE" = x ; then     #- with optimisation:
    #- full optimisation
    FOPTIM='-march=core-avx2 -mtune=core-avx2 -funroll-loops'
    # -O3 is generally not recommended, so we use -O2 with -ftree-vectorize
    # as the default
    #FOPTIM="$FOPTIM -O3"
    #- can use -O2 (safe optimisation) to avoid Pb with some gcc version of -O3:
    FOPTIM="$FOPTIM -O2 -ftree-vectorize"
    NOOPTFILES="$NOOPTFILES ini_masks_etc.F"
else
   # these may also be useful, but require specific gfortran versions:
   # -Wnonstd-intrinsics        for gfortran <= 4.3
   # -Wintrinsics-std           for gfortran >= 4.4
   # -Wno-tabs                  for gfortran >= 4.3
   # -Wno-unused-dummy-argument for gfortran >= 4.6
   #FFLAGS="$FFLAGS -Waliasing -Wampersand -Wsurprising -Wline-truncation"
   #- or simply:
    FFLAGS="$FFLAGS -Wall"
    if [ $fcVers -ge 10 ] ; then
      FFLAGS="$FFLAGS -Wno-unused-dummy-argument"
    fi
   #- to get plenty of warnings: -Wall -Wextra (older form: -Wall -W) or:
   #FFLAGS="$FFLAGS -Wconversion -Wimplicit-interface -Wunused-labels"
  if test "x$DEVEL" = x ; then  #- no optimisation + IEEE :
    FOPTIM='-O0'
  else                          #- development/check options:
    FOPTIM='-O0 -g -fbounds-check'
    FOPTIM="$FOPTIM -ffpe-trap=invalid,zero,overflow -finit-real=inf"
  fi
fi

F90FLAGS=$FFLAGS
F90OPTIM=$FOPTIM

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

if [[ -n $( nf-config --fflags )  && ($? == 0) ]]; then
    INCLUDES=$(nf-config --fflags)
#    LIBS=$(nf-config --flibs)
    LIBS="-L$(nf-config --prefix)/lib -lnetcdff"
fi

if [ -n "$MPI_HOME" -a -z "$MPI_INC_DIR" ]; then
    MPI_INC_DIR="$MPI_HOME/include"
fi

if [ "x$MPI" = xtrue ] ; then
   if [ -z "$MPI_INC_DIR" ] ; then
      # MPI env variables are not set, trying pkg-config insteal
      if [[ -n $( pkg-config --cflags-only-I ompi ) && ($? == 0) ]] ; then
         MPI_INC_DIR=$(pkg-config --cflags-only-I ompi | awk '{ print $1 }' | sed -e "s/-I//" )
      else
         echo MPI_HOME is not set and pkg-config not available, aborting
         exit 1
      fi
   fi
   if [ -n "$MPI_INC_DIR" ] ; then
      # only fill this if we can find MPI, otherwise triggers netcdf error
      INCLUDES+=" -I$MPI_INC_DIR"
      INCLUDEDIRS+=" $MPI_INC_DIR"
      #- used for parallel (MPI) DIVA
      MPIINCLUDEDIR="$MPI_INC_DIR"
   else
      echo could not set MPI_INC_DIR, aborting
      exit 1
   fi
fi
