
MAKEOPT = --no-print-directory

# There are ___ kinds of F90 flags defined:
# F90COMMONFLAGS: These flags are specified for each kind (Debug, Release) of compilation
# F90DEBUGFLAGS: These flags are only used for creating Debug artefacts
# F90RELEASEFLAGS: These flags are only used for creating Release artefacts

# For standalone compiling set the compiler
ifneq ($(COMPILER),gfortran)
ifneq ($(COMPILER),flang)
ifortErr = $(shell which ifort >/dev/null 2>&1; echo $$?)
else
ifortErr = 1
endif
else
ifortErr = 1
endif

ifeq "$(ifortErr)" "0"

ifortVer_major = $(shell ifort -v 2>&1 | cut -d " " -f 3 | cut -d. -f 1)
compiler_ver = $(shell ifort -v 2>&1)
ifeq ($(shell test $(ifortVer_major) -lt 14; echo $$?),0)
$(error You need ifort 14 or higher [preferably 18.0.1+], or use gfortran 6+)
endif

#Intel compiler
F90C     ?= ifort

mpierr= $(shell which mpiifort >/dev/null 2>&1; echo $$?)
ifeq "$(mpierr)" "0"
MPIF90C  ?= mpiifort -fc=ifort
endif

F90DEBUGFLAGS ?= -g -traceback
F90RELEASEFLAGS ?= -fast
ifeq ($(shell test $(ifortVer_major) -gt 15; echo $$?),0)
F90COMMONFLAGS ?= -fpp -W0 -WB -qopenmp
else
F90COMMONFLAGS ?= -fpp -W0 -WB -openmp
endif
ifneq ($(shell uname -s),Darwin)
F90COMMONFLAGS += -fpic
endif

MODLINK=-I
# Intel has a special archiver for libraries.
AREXE ?= xiar
ifneq "$(ifortVer_major)" "14"
# Check whether FORUTILS_SRC_DIR is set to prevent adding gen-dep multiple times.
ifdef FORUTILS_SRC_DIR
F90COMMONFLAGS += -gen-dep=$*.d
endif
endif

else

gfortErr = $(shell which gfortran >/dev/null 2>&1; echo $$?)
ifeq ($(COMPILER),flang)
gfortErr = 1
endif

ifeq "$(gfortErr)" "0"

major_version = $(shell gfortran -dumpversion 2>&1 | cut -d " " -f 3 | cut -d. -f 1)
ifneq ($(shell test $(major_version) -gt 5; echo $$?),0)
$(error gfortran version 6.3 or higher (or ifort 14+) is required)
endif
compiler_ver = $(shell gfortran -dumpversion 2>&1)

F90C ?= gfortran
F90COMMONFLAGS ?= -cpp -ffree-line-length-none -fmax-errors=4 -MMD -fopenmp -fPIC
F90DEBUGFLAGS ?= -g -O0 -fbacktrace
F90RELEASEFLAGS ?= -O3 -ffast-math
MODLINK=-J

else

#LLVM flang (experimental): used if explicitly requested with COMPILER=flang,
#or as a last-resort fallback when neither ifort nor gfortran is found.
FLANGCC := $(shell command -v flang-new 2>/dev/null || command -v flang 2>/dev/null || \
	ls /usr/bin/flang-new-[0-9]* /usr/bin/flang-[0-9]* /usr/lib/llvm-*/bin/flang 2>/dev/null | sort -V | tail -n1)
ifeq ($(FLANGCC),)
$(error No supported Fortran compiler found (checked ifort, gfortran, flang). Install one, or set COMPILER/F90C)
endif
compiler_ver = flang-$(shell $(FLANGCC) --version 2>&1 | head -n1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -n1)

F90C ?= $(FLANGCC)
# flang does not support -MMD/-M dependency generation, so incremental
# rebuilds after editing a module fall back to full staleness via compiler.ver.
F90COMMONFLAGS ?= -cpp -ffree-form -fopenmp -fPIC
F90DEBUGFLAGS ?= -g -O0
F90RELEASEFLAGS ?= -O3
MODLINK=-module-dir

endif
endif


ifneq ($(NERSC_HOST),)
#special-case setting for NERSC machines
MPIF90C = ftn
endif
MPIF90C ?= mpif90
#Check if MPI compiler found
MPIF90_EXE = $(word 1, $(MPIF90C))
mpierr= $(shell which $(MPIF90_EXE) >/dev/null 2>&1; echo $$?)
ifneq "$(mpierr)" "0"
MPIF90C =
endif

# When no library archiver is set yet, use ar.
AREXE ?= ar
