#  Pure gauge SU3
#  MIMD version 7
#
#  Generic make-include file for pure_gauge 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_rmd" used the refreshed molecular dynamics algorithm
#	"make su3_hmc" uses hybrid Monte Carlo algorithm.
#	"make su3_ora" uses overrelaxed/quasi heat bath algorithm. 
#

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 \
  pure_gauge_includes.h \
  ${ADDHEADERS}

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

MY_OBJECTS =         \
  control.o          \
  setup.o            \
  gauge_info.o

# Basic set from generic directory
G_OBJECTS =          \
  check_unitarity.o  \
  d_plaq4.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 \
  reunitarize2.o     \
  ranstuff.o

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

# Basic set from generic_pg directory
G_PG_OBJECTS =

# 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

# Selection of the gauge force algorithm
ifeq ($(strip ${HAVEQOP}),true)
# Interface to access QOP
  SYMZK1FORCE = gauge_force_symzk1_qop.o
  SYMZK1FORCE += ${SYMZK1FORCEQOP}
else
  ifeq ($(strip ${HAVEQDP}),true)
    SYMZK1FORCE = gauge_force_symzk1_qdp.o
  else
    SYMZK1FORCE = gauge_force_imp.o
  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

OBJECTS = ${MY_OBJECTS} ${G_OBJECTS} ${G_PG_OBJECTS} ${LAYOUT} \
  ${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}  $*.c 

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

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


ALL_MAKES = \
   Make_template \
   ${MAKEFILE} \
   ../Make_template_qop \
   ../Make_template_scidac \
   ../generic/Make_template \
   ../generic_pg/Make_template \
   ../generic_ks/Make_template \
   ../generic_wilson/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}

##### Targets:

su3_rmd::
	${MAKE} -f ${MAKEFILE} target "MYTARGET= $@" \
	"DEFINES= -DRMD_ALGORITHM" \
	"EXTRA_OBJECTS= update.o  update_u.o update_h.o ranmom.o"

su3_hmc::
	${MAKE} -f ${MAKEFILE} target "MYTARGET= $@" \
	"DEFINES= -DHMC_ALGORITHM" \
	"EXTRA_OBJECTS= update.o  update_u.o update_h.o ranmom.o d_action.o"

su3_ora::
	${MAKE} -f ${MAKEFILE} target "MYTARGET= $@" \
	"DEFINES= -DORA_ALGORITHM" \
	"EXTRA_OBJECTS= update_ora.o dsdu_qhb.o relax.o monte.o gaugefix2.o"

su3_ora_glue::
	${MAKE} -f ${MAKEFILE} target "MYTARGET= $@" \
	"DEFINES= -DORA_ALGORITHM -DGBALL" \
	"EXTRA_OBJECTS= update_ora.o dsdu_qhb.o relax.o monte.o gaugefix2.o glueball_op.o path_product.o" \
	"ADDHEADERS= "glueball_op.h"

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


# Rules for generic routines
include ../generic/Make_template
include ../generic_pg/Make_template
include ../generic_ks/Make_template
include ../generic_wilson/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 
