## Makefile (D. Prendergast July 2008)
##

PREFIX=..
include $(PREFIX)/Common/common-rules.mk

SRC = sigma_main.f90 inread.f90 input.f90 input_outer.f90 genwf_disk.f90 \
      genwf_mpi.f90 mtxel.f90 mtxel_ch.f90 mtxel_sxch.f90 mtxel_vxc.f90 wpeff.f90 \
      write_result.f90 write_result_hp.f90 write_result_dyn.f90 \
      write_result_dyn_hp.f90 epscopy_disk.f90 epscopy_mpi.f90 \
      shiftenergy.f90 shiftenergy_dyn.f90
OBJ = $(SRC:.f90=.o)

# object files from $(COMMON) directory
COMOBJ = misc.o subgrp.o bessel.o gmap.o check_inversion.o sort.o \
         checkconsistency.o checkgriduniformity.o fftw.o fullbz.o \
         fft_parallel.o vcoul_generator.o minibzaverage.o fixwings.o \
         trunc_cell_wire.o trunc_cell_box.o \
         trunc_cell_box_d.o trunc_scell_box_d.o checkbz.o eqpcor.o \
         createpools.o write_program_header.o svninfo.o random.o input_utils.o \
         wfn_rho_vxc_io.o irrbz.o blas.o splines.o eqpcor.o
COMMONOBJS = $(addprefix $(COMMON)/,$(COMOBJ)) $(GLOBALOBJS)

TOOLOBJEXT = offdiag.o sig2wan.o


TOOLOBJ = $(TOOLOBJEXT) $(TOOLOBJINT)

LIBS = $(FFTWLIB) $(LAPACKLIB) $(PERFORMANCE)

default: sigma

all: sigma tools

#tools: offdiag$(FLAVOR).x offdiag_QSGW$(FLAVOR).x sig2wan.x
tools: $(TOOLOBJINT:.o=$(FLAVOR).x) offdiag$(FLAVOR).x sig2wan.x

sigma: sigma$(FLAVOR).x
sigma$(FLAVOR).x: $(COMMONOBJS) $(OBJ)
	$(LINK) $(FOPTS) -o $@ $^ $(LIBS)
	$(INSTALL_CMD)

offdiag$(FLAVOR).x: offdiag.o $(GLOBALOBJS) $(COMMON)/lapack.o
	$(LINK) $(FOPTS) -o $@ $^ $(LAPACKLIB)
	$(INSTALL_CMD)


# sig2wan does not use libraries, but misc uses BLAS so we need to link it
sig2wan.x: sig2wan.o $(GLOBALOBJS) $(COMMON)/misc.o $(COMMON)/blas.o
	$(LINK) $(FOPTS) -o $@ $^ $(LAPACKLIB)
	$(INSTALL_CMD)

# dependencies

$(OBJ) $(TOOLOBJ) : $(GLOBALMODS)
offdiag.o : $(COMMON)/lapack_m.mod

# ifort issued a warning when compiling Sigma/sigma_main.f90 on lawrencium
# << Space exceeded in Data Dependence Test in MAIN__               >>
# << Subdivide routine into smaller ones to avoid optimization loss >>
# set FNOOPTS to -O2 in lawrencium.mk and to $(FOPTS) in other .mk files
sigma_main.o: sigma_main.f90 $(GLOBALMODS) $(COMMON)/sort_m.mod $(COMMON)/misc_m.mod $(COMMON)/fullbz_m.mod \
              $(COMMON)/irrbz_m.mod $(COMMON)/fftw_m.mod $(COMMON)/vcoul_generator_m.mod mtxel_sxch_m.mod \
              $(COMMON)/wfn_rho_vxc_io_m.mod $(COMMON)/gmap_m.mod $(COMMON)/fixwings_m.mod
	$(f90_CPP)
	$(F90_CMD_NOOPT)
	$(RM_P_F_CMD)

input.o genwf_mpi.o genwf_disk.o : $(COMMON)/sort_m.mod
genwf_mpi.o genwf_disk.o : $(COMMON)/gmap_m.mod
input.o mtxel_vxc.o mtxel.o mtxel_ch.o : $(COMMON)/fftw_m.mod
input.o input_outer.o epscopy_disk.o epscopy_mpi.o sig2wan.o : $(COMMON)/misc_m.mod
input.o input_outer.o : $(COMMON)/wfn_rho_vxc_io_m.mod $(COMMON)/input_utils_m.mod $(COMMON)/eqpcor_m.mod
mtxel_sxch.o mtxel_sxch_m.mod : $(COMMON)/misc_m.mod $(COMMON)/global_m.mod
