# Makefile for general rules

# To Change the compile environment to your current system you should set:
#   -1- ROOT variable to the directory where you found this file
#   -2- if needed use a different compiler (CC) if gcc is not available
#   -3- on Solaris system use RANLIB=ranlib which is defined below

# the current directory (in vi ":r!pwd")
ROOT=REPLACE_WITH_PWD

#############################################################################
# Some convenient abbreviations
B = $(ROOT)/bin
I = $(ROOT)/include
L = $(ROOT)/lib

########################################################################
# C compiler; change this only if you are using a different C-compiler

#GNU 
#CC = gcc-mp-5 
CC = gcc
FC = 
#FC = gfortran
# Linux gcc version 4.x
OPTC = -O3 -ffast-math
##to include parallelisation with OpenMP
OPTC += -fopenmp
# for better performing code you can try:
#OPTC = -O3 -fno-trapping-math -ffast-math -funroll-all-loops -mavx -fomit-frame-pointer -mfpmath=sse -ftree-vectorizer-verbose=1
# Linux gcc version 3.x
#OPTC = -O3 -ffast-math -funroll-all-loops -mfpmath=sse

#for double precision use FFTlib and emmod
#OPTC += -DDOUBLE
#OPTF += -fdefault-double-8 -fdefault-real-8

#Cray
#CC=cc
#FC=ftn
#OPTC = -O2
#OPTF = -O2

#Intel 
#CC = icc
#FC = ifort
### Linux
##OPTC = -O3 -qopt-report-phase=vec,openmp
##OPTF = -O3 -qopt-report-phase=vec,openmp
#OPTC = -O3 -xCORE-AVX2 
#OPTF = -O3 -xCORE-AVX2
###to include parallelisation with OpenMP
#OPTC += -qopenmp
## for static linking uncomment the following
#LDFLAGS = -static-intel -qopenmp-link=static

#PGI 
#CC = pgcc
#FC = pgf90
#OPTC = -fast  -Minfo=vect -Mvect=simd:256 -Msafeptr
#OPTC = -fast  -Minfo=vect -Mvect=simd:256 -Msafeptr -Mprof=lines
#OPTF = -fast
#LDFLAGS = -fast -Minfo=vect -Mvect=simd:256 -Msafeptr 

#Apple OSX clang/gcc (10.9) llvm
#CC = clang
#OPTC = -Ofast
#LDFLAGS = -Ofast

#AMD Open64 this will be replaced with the latest AOCC compilers
#CC = opencc
#FC = openf95
#OPTC = -Ofast
#OPTF = -Ofast
#LDFLAGS = -static -Ofast

#############################################################################
# BLAS and LAPACK libraries 
#MKLROOT=/opt/intel/mkl/
#MKLROOT=
ifneq ($(strip $(MKLROOT)),)
    ifeq ($(CC),icc)
        #for intel compilers
        OPTC += -DMKL -I$(MKLROOT)/include -I$(MKLROOT)/include/fftw
        BLAS = -mkl
    else
        #for GNU and other compilers
        #on linux you want to use groups and MKL is in lib/intel64
        MKLLIB=${MKLROOT}/lib/intel64
        OPTC += -DMKL -I$(MKLROOT)/include -I$(MKLROOT)/include/fftw
        BLAS = -Wl,-rpath ${MKLLIB} -Wl,--start-group -L${MKLLIB} -lmkl_gf_lp64 -lmkl_sequential -lmkl_core -Wl,--end-group -lpthread -lm -ldl
endif
else
    BLAS = -llapack -lblas 
endif

#LIBARIES
LIBS= -L$L -lgenfft $(BLAS)
ifeq ($(FC),ifort)
   LIBS += -lifcore
# for static linking with Intel compiler
# LDFLAGS = -static-intel -qopenmp-link=static
endif
ifeq ($(FC),gfortran)
   LIBS += -lgfortran
endif

#MATH library: on some linux distributions the math (cos, sin, ..) must be excluded explicitely
#if that's needed uncomment the following line
#LIBSM= -lm



########################################################################
# standard CFLAGS
CFLAGS = -I$I -I.

#############################################################################
# the archiver
AR = ar

#############################################################################
# ar FLAGS
ARFLAGS = rv

#############################################################################
# ranlib definition
RANLIB = ar -s
# on Sun SOLARIS use:
#RANLIB = ranlib


.SUFFIXES : .o .c .cc .f .a .F90
.c.o	:
	$(CC) -c $(CFLAGS) $(OPTC) $<
.c.a	:
	$(CC) -c $(CFLAGS) $(OPTC) $<
	$(AR) $(ARFLAGS) $@ $*.o
	rm -f $*.o
.o.a	:
	$(AR) $(ARFLAGS) $@ $*.o
	rm -f $*.o
.f.o	:
	$(FC) -c $(FFLAGS) $(OPTF) $<
.F90.o	:
	$(FC) -c $(FFLAGS) $(OPTF) $<
.f.a	:
	$(FC) -c $(FFLAGS) -I$I $<
	$(AR) $(ARFLAGS) $@ $*.o
	rm -f $*.o
.cc.a	:
	$(C++) -c $(C++FLAGS) -I$I $<
	$(AR) $(ARFLAGS) $@ $*.o
	rm -f $*.o
.cc.o	:
	$(C++) -c $(C++FLAGS) $<