# CHARMM 33 makefile for OSX compilers (GNU and XLF)
ROOT = rootdir
SRC = $(ROOT)/source
FLX = $(ROOT)/tool/preflx
GMS = $(ROOT)/tool/gmscomp_osx
LIB = $(ROOT)/lib/osx
EXEC = $(ROOT)/exec/osx
CC = gcc -Dnographics -O -Dgnu $(I8DUM2)
# Note that CC on line 8 and GLIB on line 10, sorry
GLIB =  
CPP = /bin/cat
INCLUDE = 
ADDLIB = 
ADDLIB := $(ADDLIB) $(GLIB)
QLIB =
# GNU options for MBO(N)D code:
# note that -fno-automatic -finit-local-zero are dangerous
# options because they mask incorrect behaviour. They are
# here only to simulate SGI compiler behaviour to facilitate
# initial porting from SGI (with -static)
# and eventually should be removed and offending code fixed.
# NOTE: BLAS library is needed by MBO(N) code
# BLAS for Linux/PPro from: http://www.cs.utk.edu/~ghenry/distrib
# BLAS dependency has been removed as of c27b2 and c28a2
ifdef MBOND
MBONDFLG = -fno-automatic -finit-local-zero -O -Wall -Wsurprising -W -DCHARMM
INCLUDE = -I../../source/moldyn
endif

RANLIB = ranlib
.SUFFIXES: .fcm .src .o
VPATH = fcm

# Supported compilers gfortran (default), g95, ifort (Intel), xlf95 (IBM)
FFLAGS = 
ifdef MPI_OSX
   FCOMPILER = mpif90
#   FFLAGS = -Impi
else
    ifdef GNU_GFORTRAN
	FCOMPILER = gfortran
    endif
    ifdef GNU_G95
	FCOMPILER = g95
    endif
    ifdef XLF95_OSX
	FCOMPILER = xlf95
    endif
    ifdef INTEL_IFORT
	FCOMPLER = ifort
    endif
endif


# *** GFORTRAN *** GFORRAN f95 compiler
ifdef GNU_GFORTRAN
    FC = $(FCOMPILER) -DGNU $(FFLAGS)
    LD = $(FCOMPILER) $(FFLAGS)
else

# *** G95 *** Gnu g95 compiler
    ifdef GNU_G95
	FC = $(FCOMPILER) $(FFLAGS)
	LD = $(FCOMPILER) $(FFLAGS)
    endif

# IBM xlf compiler for Mac OSX
    ifdef XLF95_OSX
# Flag qposition=appendold requried to keep xlf95 from rewinding/overwriting 
# files opened for writing with open append. cb3
	FFLAGS = -qposition=appendold -qfixed -qalign=4k -qarch=auto -qcache=auto -qtune=auto -qmaxmem=-1 $(FFLAGS)
	FC = $(FCOMPILER) $(FFLAGS)
	LD = $(FCOMPILER) -O3  $(FFLAGS)
	CC = xlc -Dnographics -O -Dibmrs -Dosx $(I8DUM2) $(FFLAGS)
	CCPP = xlc++
    endif

# INTEL ifort compiler for Mac OSX (32 bit)
    ifdef INTEL_IFORT
	ifdef BIG_ENDIAN
	    ENDIAN := -convert big_endian
	endif
# removed -axW, not supported under Intel ifort 9.0 on osx
	FC = $(FCOMPILER) -tpp7 -132 -w95 -cm -align all $(ENDIAN) $(FFLAGS)
	LD = $(FCOMPILER) -O3 -tpp7
    endif
endif

ifdef GNU_GFORTRAN
    FC0 = $(FC) -c -O0
    FC1 = $(FC) -c -O1
    FC2 = $(FC) -c -O3
    FC3 = $(FC) -c -O3
    FCR = $(FC) -c -g 
    FCD = $(FC) -c -g -O0 
    FCRD = $(FC) -c -g -V -O0 
else

# IBM xlf beta compiler for Mac OSX
    ifdef XLF95_OSX
	FC0 = $(FC) -c -g
	FC1 = $(FC) -c -O1 -g
	FC2 = $(FC) -c -O2 -qarch=auto -qtune=auto -qunroll=yes
	FC3 = $(FC) -c -O4 -qarch=auto -qtune=auto -qunroll=yes
	FCR = $(FC) -c -O3 -qstrict -g
	FCD = $(FC) -c -no -g
	FCRD = $(FC) -c -no -g
    endif
    ifdef GNU_G95
	FC0 = $(FC) -c -O0
	FC1 = $(FC) -c -O1
	FC2 = $(FC) -c -O3
	FC3 = $(FC) -c -O3
	FCR = $(FC) -c -g 
	FCD = $(FC) -c -g -O0 
	FCRD = $(FC) -c -g -V -O0 
    endif
    ifdef  INTEL_IFORT
          FC0 = $(FC) -c -O0
    	  FC1 = $(FC) -c -O1
	  FC2 = $(FC) -c -O3
          FC3 = $(FC) -c -O3
	  FCR = $(FC) -c -u -V
	  FCD = $(FC) -c -g -O0 -u -traceback
	  FCRD = $(FC) -c -g -V -O0 -u -save -zero
    endif

endif

ifdef DEBUG
    FC0 = $(FCD)
    FC1 = $(FCD)
    FC2 = $(FCD)
    FC3 = $(FCD)
    LD  = $(LD) -g
endif

OBJS = \
        $(LIB)/charmm_main.o \
        $(LIB)/help.o \
        $(LIB)/iniall.o \
        $(LIB)/miscom.o \
        $(LIB)/usersb.o
LIBS = \
	$(LIB)/adumb.a \
	$(LIB)/cadint.a \
	$(LIB)/cff.a \
	$(LIB)/correl.a \
	$(LIB)/dimb.a \
	$(LIB)/dynamc.a \
	$(LIB)/energy.a \
	$(LIB)/gamint.a \
	$(LIB)/gukint.a \
	$(LIB)/gener.a \
	$(LIB)/graphics.a \
	$(LIB)/image.a \
	$(LIB)/io.a \
	$(LIB)/machdep.a \
	$(LIB)/manip.a \
	$(LIB)/mbond.a \
	$(LIB)/mc.a \
	$(LIB)/minmiz.a \
	$(LIB)/misc.a \
	$(LIB)/mmff.a \
	$(LIB)/moldyn.a \
	$(LIB)/molvib.a \
	$(LIB)/nbonds.a \
	$(LIB)/pert.a \
	$(LIB)/quantum.a \
	$(LIB)/rxncor.a \
	$(LIB)/shapes.a \
	$(LIB)/solvation.a \
	$(LIB)/util.a \
	$(LIB)/vibran.a

$(EXEC)/charmm : $(LIBS) $(OBJS)
	$(LD) -o charmm.exe $(LIB)/*.o $(LIBS) $(LIBS) $(LIBS) $(ADDLIB) $(QLIB) $(MSGLIB)
