################################################################################
#
# 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
#
################################################################################

include ../Makefile.defs

all: rmxeq mkdep mkxeq
.PHONY: all

# main programs and modules to be compiled

MAIN = time1 time2 time3 time3test

ARCHIVE = archive sarchive

BLOCK = block blk_grid map_u2blk map_sw2blk map_s2blk

DFL = dfl_geometry dfl_subspace ltl_gcr dfl_sap_gcr dfl_modes

DIRAC = Dw_dble Dw Dw_bnd

FLAGS = flags action_parms dfl_parms force_parms hmc_parms lat_parms \
        mdint_parms rat_parms rw_parms sap_parms solver_parms

FORCES = force0 force1 force2 force3 force4 force5 \
         frcfcts genfrc tmcg tmcgm xtensor

LATTICE = bcnds uidx ftidx geometry

LINALG = salg salg_dble valg valg_dble liealg cmatrix_dble cmatrix

LINSOLV = cgne mscg fgcr fgcr4vd

LITTLE = Aw_gen Aw_com Aw_ops Aw_dble Aw ltl_modes

MDFLDS = mdflds fcom

RANDOM = ranlux ranlxs ranlxd gauss

RATFCTS = elliptic zolotarev ratfcts

SAP = sap_com sap_gcr sap blk_solv

SFLDS = sflds scom sdcom Pbnd Pbnd_dble

SU3FCTS = chexp su3prod su3ren cm3x3 random_su3

SW_TERM = pauli pauli_dble swflds sw_term

TCHARGE = ftcom ftensor tcharge ym_action

UFLDS = plaq_sum uflds udcom bstap

UPDATE = chrono mdsteps counters mdint hmc rwtm rwtmeo rwrat

UTILS = endian mutils utils wspace

VFLDS = vflds vinit vcom vdcom

BM = cg_iter cg_iter_dble time_msap time_Awhat

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


# openQCD distribution

OQCD = ../openQCD-1.4-bgopt

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

LOGOPTION =


# search path for modules

MDIR = $(OQCD)/modules

MDIR_BM = ../modules

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


# additional include directories

INCPATH = $(MPIR_HOME)/include $(OQCD)/include/ ../include


# additional libraries

LIBS = m

LIBPATH = $(MPIR_HOME)/lib


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

PGMS= $(MAIN) $(MODULES)

# 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
	$(LD) $< $(addsuffix .o,$(MODULES)) $(CFLAGS) $(LOGOPTION) \
        $(addprefix -L,$(LIBPATH)) $(addprefix -l,$(LIBS)) -o $@

kernel: $(addsuffix .o,$(MODULES)) Makefile time3.o
	$(AR) $(ARFLAGS) ../../kernel_C.a $(addsuffix .o,$(MODULES)) time3.o

# produce executables

mkxeq: $(MAIN)


# remove old executables

rmxeq:
	$(RM) $(MAIN); \
        echo "delete old executables"


# clean directory

clean:
	$(RM) -r *.d *.o *.alog *.clog *.slog $(MAIN)
.PHONY: clean

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