################################################################################
#
# Makefile to compile and link C programs with MPI subroutines
#
# Version valid for Linux machines with MPICH
#
# "make" compiles and links the specified main programs and modules,
# using the specified libraries (if any), and produces the executables
#
# "make clean" removes all files generated by "make"
#
# Dependencies on included files are automatically taken care of
#
################################################################################

all: rmxeq mkdep mkxeq
.PHONY: all


# main programs and modules to be compiled

MAIN = check1 check2 check3 check4 check5 time1

FLAGS = flags lat_parms sap_parms dfl_parms

LATTICE = bcnds ftidx uidx geometry

LINALG = salg salg_dble valg valg_dble liealg cmatrix_dble cmatrix

LINSOLV = fgcr

RANDOM = ranlux ranlxs ranlxd gauss

UFLDS = plaq_sum shift uflds udcom

SU3FCTS = chexp su3prod su3ren cm3x3 random_su3

UTILS = endian mutils utils wspace

SFLDS = sflds scom sdcom Pbnd Pbnd_dble

TCHARGE = ftcom ftensor

SW_TERM = pauli pauli_dble swflds sw_term

DIRAC = Dw_dble Dw Dw_bnd

BLOCK = block blk_grid map_u2blk map_sw2blk map_s2blk

SAP = blk_solv sap_com sap sap_gcr

ARCHIVE = archive

DFL = dfl_geometry dfl_subspace

VFLDS = vflds vinit vcom vdcom

LITTLE = Aw_gen Aw_com Aw_ops Aw_dble Aw ltl_modes

MODULES = $(FLAGS) $(LATTICE) $(LINALG) $(LINSOLV) $(RANDOM) $(UFLDS) \
          $(SU3FCTS) $(UTILS) $(SFLDS) $(TCHARGE) $(SW_TERM) $(DIRAC) \
          $(BLOCK) $(SAP) $(ARCHIVE) $(DFL) $(VFLDS) $(LITTLE)


# Logging option (-mpilog or -mpitrace or -mpianim)

LOGOPTION =


# search path for modules

MDIR = ../../modules

VPATH = .:$(MDIR)/flags:$(MDIR)/lattice:$(MDIR)/linalg:$(MDIR)/linsolv:\
          $(MDIR)/random:$(MDIR)/uflds:$(MDIR)/su3fcts:$(MDIR)/utils:\
          $(MDIR)/sflds:$(MDIR)/tcharge:$(MDIR)/sw_term:$(MDIR)/dirac:\
          $(MDIR)/block:$(MDIR)/sap:$(MDIR)/archive:$(MDIR)/dfl:\
          $(MDIR)/vflds:$(MDIR)/little


# additional include directories

INCPATH = $(MPI_INCLUDE) ../../include


# additional libraries

LIBS = m

LIBPATH = $(MPI_HOME)/lib


# scheduling and optimization options

CFLAGS = -std=c89 -pedantic -fstrict-aliasing \
         -Wall -Wno-long-long -Wstrict-prototypes -Werror \
         -O -mno-avx -Dx64 -DPM


############################## do not change ###################################

SHELL=/bin/bash
CC=$(MPI_HOME)/bin/mpicc
CLINKER=$(CC)

PGMS= $(MAIN) $(MODULES)

-include $(addsuffix .d,$(PGMS))


# rule to make dependencies

$(addsuffix .d,$(PGMS)): %.d: %.c Makefile
	@ $(GCC) -ansi $< -MM $(addprefix -I,$(INCPATH)) -o $@


# rule to compile source programs

$(addsuffix .o,$(PGMS)): %.o: %.c Makefile
	$(CC) $< -c $(CFLAGS) $(LOGOPTION) $(addprefix -I,$(INCPATH))


# rule to link object files

$(MAIN): %: %.o $(addsuffix .o,$(MODULES)) Makefile
	$(CLINKER) $< $(addsuffix .o,$(MODULES)) $(CFLAGS) $(LOGOPTION) \
        $(addprefix -L,$(LIBPATH)) $(addprefix -l,$(LIBS)) -o $@


# produce executables

mkxeq: $(MAIN)


# remove old executables

rmxeq:
	@ -rm -f $(MAIN); \
        echo "delete old executables"


# make dependencies

mkdep:  $(addsuffix .d,$(PGMS))
	@ echo "generate tables of dependencies"


# clean directory

clean:
	@ -rm -rf *.d *.o *.alog *.clog *.slog $(MAIN)
.PHONY: clean

################################################################################
