#  Wilson static code
#  MIMD version 6

#  Generic make-include file for wilson_static codes

#  This template file defines rules and macros common to all architectures
#  It is intended to be an include file for other Makefiles.
#  Don't use it by itself!
#
#
# "make su3_static_mr" uses minimal residual for light props
# "make su3_static_cg" uses conjugate gradient for light props


MYINCLUDEDIR = .
INCLUDEDIR = ../include
#Where the complex and su3 libraries are
LIBDIR = ../libraries

GLOBAL_HEADERS = ${INCLUDEDIR} ../libraries/include

MY_HEADERS = \
  lattice.h \
  params.h \
  defines.h \
  w_static_includes.h \
  debug_static.h \
  ${ADDHEADERS}

HEADERS = ${GLOBAL_HEADERS} ${MY_HEADERS} ${INLINE_HEADERS} ${SCIDAC_HEADERS}

# include file defining site structure, etc., for compiling generic code
DEBUGDEF =  -DDEBUGDEF=\<debug_static.h\>

# All required object files
# Basic set from current directory
MY_OBJECTS =         \
	bj_to_weyl.o \
	bparam_smear_routines.o \
	calc_matrix.o \
	calc_smear_meson.o \
	control_mr.o \
	create_smear_source.o \
	cs_wv.o \
	debug_static.o \
	fft_su3_matrix.o \
	gauge_info.o \
	hopping.o \
        ksprop_info.o      \
	light_meson.o \
	loadup_stripquark.o \
	m_wvec_matdagger.o \
	mb_gamma_r_wvec.o \
	output_m.o \
	output_mb2.o \
	setup_mr.o \
	setup_smearing.o \
	static_prop.o \
	util_vary.o \
	w_meson_hop.o \
	wall_sink_h.o \
	wilson_info.o  \
	write_smear_meson.o \
	write_vary_matrix.o

# Basic set from generic directory
G_OBJECTS =          \
	check_unitarity.o  \
	d_plaq4.o \
	gaugefix2.o \
        io_detect.o        \
	io_helpers.o \
	io_lat4.o  \
        io_lat_utils.o     \
        make_lattice.o \
	nersc_cksum.o \
        remap_stdio_from_args.o \
	restrict_fourier.o \
	reunitarize2.o

ifeq ($(strip ${HAVEQIO}),true)
  G_OBJECTS += io_scidac.o io_scidac_types.o io_scidac_w.o
endif

# Basic set from generic_clover directory
G_CLOVER_OBJECTS =   \
  f_mu_nu.o          \
  io_source_cmplx_fm.o \
  make_clov2.o

# Set from generic_ks directory
G_KS_OBJECTS =      \
  io_helpers_ks.o   \
  io_prop_ks.o      \
  io_prop_ks_fm.o   \
  ks_source.o       \
  ks_utilities.o

ifeq ($(strip ${HAVEQIO}),true)
  G_KS_OBJECTS += io_scidac_ks.o
endif

# Basic set from generic_wilson directory
G_WILSON_OBJECTS =   \
        canopy2weyl_rot.o  \
	dirac_utilities.o  \
        dslash_w3.o \
        gammas.o \
	w_source_h.o \
        io_helpers_w.o     \
        io_prop_w.o        \
        io_prop_w_fm.o     \
        io_source_w_fm.o   \
        staggered2naive.o  \
	wilson_invert.o

# Selection of the conjugate gradient algorithm
# The following inverters are not supported in QOP
# so we use the plain MILC version:
CONGRAD_CL = \
      cl_solver_utilities.o \
      d_cgilu_cl.o     \
      d_hopilu_cl.o    \
      d_mrilu_cl.o
# The bicgilu inverter is supported, so select it
ifeq ($(strip ${HAVEQOP}),true)
# Interface to access QOP
  CONGRAD_CL += d_bicgilu_cl_qop.o \
     d_bicgilu_cl_qop_D.o d_bicgilu_cl_qop_F.o
  CONGRAD_CL += ${WILSONINVERTQOP}
else
  ifeq ($(strip ${HAVEQDP}),true)
# We don't have a working standalone QDP version of the bicgilu inverter
# so we use the plain MILC code version
    CONGRAD_CL += d_bicgilu_cl.o
  else
# Plain MILC inverter
    CONGRAD_CL += d_bicgilu_cl.o
  endif
endif

CL_OBJECTS = ${FLINKS_WILSON} ${CONGRAD_CL}

# Objects depending on architecture and software package 

# Selection of layout
ifeq ($(strip ${QCDOC}),true)
  LAYOUT = layout_qcdoc.o # Required
else
  ifeq ($(strip ${HAVEQDP}),true)
    LAYOUT = layout_qdp.o # Required
  else
    LAYOUT = layout_hyper_prime.o # Choices possible here
  endif
endif

# EDRAM allocation for QCDOC
ifeq ($(strip ${QCDOC}),true)
  QCDOC_OBJECTS = qcdoc_alloc.o
endif

ADD_OBJECTS = ${MACHINE_DEP_IO} ${COMMPKG} ${QCDOC_OBJECTS} ${QDP_OBJECTS} \
   ${SCIDAC_OBJECTS}

# Generic QOP objects
ifeq ($(strip ${HAVEQOP}),true)
  ADD_OBJECTS += ${GENERICQOP}
endif

# To these we add project-specific extra objects and machine-dependent objects
OBJECTS = ${MY_OBJECTS} ${G_OBJECTS} ${G_KS_OBJECTS} ${G_WILSON_OBJECTS} \
  ${G_CLOVER_OBJECTS} ${LAYOUT} ${CL_OBJECTS} ${ADD_OBJECTS} ${EXTRA_OBJECTS}

#Libraries for complex numbers and su3 functions
P = $(strip ${PRECISION})
QCDLIB = ${LIBDIR}/su3.${P}.a ${LIBDIR}/complex.${P}.a 

LIBRARIES = ${QCDLIB} ${SCIDAC_LIBRARIES}

# Default rule for C compilation
.c.o: ; ${CC} -c ${CFLAGS}  $<

# To force a full remake when changing targets
LASTMAKE = .lastmake.${MAKEFILE}.${P}.${MYTARGET}

${LASTMAKE}: ${ALL_MAKES}
	-/bin/rm -f .lastmake.*
	${MAKE} -f ${MAKEFILE} clean
	touch ${LASTMAKE}

# All make files used in this compilation
ALL_MAKES = \
  Make_template \
  ${MAKEFILE} \
  ../Make_template_qop \
  ../Make_template_scidac \
  ../generic/Make_template \
  ../generic_ks/Make_template \
  ../generic_wilson/Make_template \
  ../generic_clover/Make_template \

# To force a remake when changing any make file, header, or target
${OBJECTS} : ${HEADERS} ${LASTMAKE} ${ALL_MAKES}

ifeq ($(strip ${QCDOC}),true)
  DQCDOC = -DQCDOC
endif

ifeq ($(strip ${HAVEQMP}),true)
  DHAVE_QMP = -DHAVE_QMP
endif

ifeq ($(strip ${HAVEQIO}),true)
  DHAVE_QIO = -DHAVE_QIO
endif

ifeq ($(strip ${HAVEQDP}),true)
  DHAVE_QDP = -DHAVE_QDP
endif

ifeq ($(strip ${HAVEQOP}),true)
  DHAVE_QOP = -DHAVE_QOP
endif

DARCH = ${DQCDOC} ${DHAVE_QMP} ${DHAVE_QIO} ${DHAVE_QDP} ${DHAVE_QOP}

########################################################
# Note: we prefer double precison version of inverters {for global sums}

# Optional DEFINES for timing:
# -DCGTIME for cg timing and iterations
# -DMRTIME for cg timing and iterations

su3_static_cg::
	${MAKE} -f ${MAKEFILE} target "MYTARGET= $@" \
	"DEFINES= -DCL_CG=CG"

su3_static_mr::
	${MAKE} -f ${MAKEFILE} target "MYTARGET= $@" \
	"DEFINES= -DCL_CG=MR"

clean:
	-/bin/rm -f *.o

# Rules for generic routines
include ../generic/Make_template

# Rules for generic_wilson routines
include ../generic_ks/Make_template

# Rules for generic_wilson routines
include ../generic_wilson/Make_template

# Rules for generic_clover routines
include ../generic_clover/Make_template

libmake:
	@echo Building libraries with ${MAKELIBRARIES}
	cd ${LIBDIR} ;\
	${MAKE} -f ${MAKELIBRARIES} "APP_CC=${CC}" "PRECISION=${PRECISION}" all

localmake: ${OBJECTS} ${LIBRARIES}
	${LD} -o ${MYTARGET} ${LDFLAGS} \
	${OBJECTS} ${QCDLIB} ${ILIB} -lm
	touch localmake

target: libmake localmake 
