dlvl = ../..
include $(dlvl)/Bmake.inc

#  --------------------------
#  The communication routines
#  --------------------------
comm = igesd2d_.o sgesd2d_.o dgesd2d_.o cgesd2d_.o zgesd2d_.o \
       itrsd2d_.o strsd2d_.o dtrsd2d_.o ctrsd2d_.o ztrsd2d_.o \
       igerv2d_.o sgerv2d_.o dgerv2d_.o cgerv2d_.o zgerv2d_.o \
       itrrv2d_.o strrv2d_.o dtrrv2d_.o ctrrv2d_.o ztrrv2d_.o \
       igebs2d_.o sgebs2d_.o dgebs2d_.o cgebs2d_.o zgebs2d_.o \
       igebr2d_.o sgebr2d_.o dgebr2d_.o cgebr2d_.o zgebr2d_.o \
       itrbs2d_.o strbs2d_.o dtrbs2d_.o ctrbs2d_.o ztrbs2d_.o \
       itrbr2d_.o strbr2d_.o dtrbr2d_.o ctrbr2d_.o ztrbr2d_.o \
       igsum2d_.o sgsum2d_.o dgsum2d_.o cgsum2d_.o zgsum2d_.o \
       igamx2d_.o sgamx2d_.o dgamx2d_.o cgamx2d_.o zgamx2d_.o \
       igamn2d_.o sgamn2d_.o dgamn2d_.o cgamn2d_.o zgamn2d_.o

#  -----------------------------------------------------------------------------
#  These names are too long for the archiver to distinguish based on the suffix.
#  I'm going to use logical links to get around this problem.  It's hokey, but
#  it lets us use the suffix rule and keep the makefile portable.
#  -----------------------------------------------------------------------------
long = blacs_gridinit_.o blacs_gridmap_.o blacs_freebuff_.o \
       blacs_gridexit_.o blacs_gridinfo_.o blacs_barrier_.o

Clong = Cblacs_gridinit_.C Cblacs_gridmap_.C Cblacs_freebuff_.C \
        Cblacs_gridexit_.C Cblacs_gridinfo_.C Cblacs_barrier_.C

#  --------------------
#  The support routines
#  --------------------
supp = blacs_pinfo_.o blacs_setup_.o blacs_set_.o blacs_get_.o \
       blacs_abort_.o blacs_exit_.o blacs_pnum_.o blacs_pcoord_.o \
       ksendid_.o krecvid_.o kbsid_.o kbrid_.o \
       dcputime00_.o dwalltime00_.o

#  ----------------------------
#  The fortran and C interfaces
#  ----------------------------
Fintobj   = $(comm) $(supp) $(long)
Cintobj   = $(comm:.o=.C) $(supp:.o=.C) $(Clong)

#  ---------------------
#  The internal routines
#  ---------------------
internal = GlobalVars.o \
           Shyp_bs.o Shyp_br.o Sidring_bs.o Sidring_br.o \
           Smpath_bs.o Smpath_br.o Ssring_bs.o Ssring_br.o \
           Stree_bs.o Stree_br.o  tree_comb.o BE_comb.o \
           Ssend2d00.o Ssend2dID.o Srecv2d00.o Srecv2dID.o \
           Asend2d00.o Asend2dID.o Arecv2d00.o Arecv2dID.o \
           ArgCheck00.o getbuff.o GetCombBuff.o TransDist.o \
           BlacsErr.o BlacsWarn.o BlacsAbort00.o \
           mvcopy4.o mvcopy8.o vmcopy4.o vmcopy8.o \
           tvcopy4.o tvcopy8.o vtcopy4.o vtcopy8.o tvcopy16.o vtcopy16.o \
           ivvsum.o svvsum.o dvvsum.o cvvsum.o zvvsum.o \
           ivvamx.o svvamx.o dvvamx.o cvvamx.o zvvamx.o \
           ivvamx2.o svvamx2.o dvvamx2.o cvvamx2.o zvvamx2.o \
           ivvamn.o svvamn.o dvvamn.o cvvamn.o zvvamn.o \
           ivvamn2.o svvamn2.o dvvamn2.o cvvamn2.o zvvamn2.o \
           BuffIsFree.o RepTreeComb.o

lib : all
f77lib : flib
F77lib : flib
Clib : clib

#  ---------------------------------------
#  Make both C and fortran interface BLACS
#  ---------------------------------------
all : INTERN $(Fintobj) $(Cintobj)
	$(ARCH) $(ARCHFLAGS) $(BLACSLIB) $(Fintobj) $(Cintobj)
	$(RANLIB) $(BLACSLIB)

#  -------------------------------------
#  Make only the fortran interface BLACS
#  -------------------------------------
flib : $(Fintobj)
	( cd INTERNAL ; rm -f $(internal) )
	$(MAKE) INTERN
	$(ARCH) $(ARCHFLAGS) $(BLACSLIB) $(Fintobj)
	$(RANLIB) $(BLACSLIB)

#  -------------------------------
#  Make only the C interface BLACS
#  -------------------------------
clib : $(Cintobj)
	( cd INTERNAL ; rm -f $(internal) )
	$(MAKE) INTERN
	$(ARCH) $(ARCHFLAGS) $(BLACSLIB) $(Cintobj)
	$(RANLIB) $(BLACSLIB)

#  ------------------
#  Make the internals
#  ------------------
INTERN :
	( cd INTERNAL ; $(MAKE) -f ../Makefile I_int "dlvl=$(BTOPdir)" )

I_int : Bdef.h Bconfig.h $(internal)
	$(ARCH) $(ARCHFLAGS) $(BLACSLIB) $(internal)

Bdef.h : ../Bdef.h
	rm -f Bdef.h
	ln -s ../Bdef.h Bdef.h

Bconfig.h : ../Bconfig.h
	rm -f Bconfig.h
	ln -s ../Bconfig.h Bconfig.h

#  -----------------------
#  Delete the object files
#  -----------------------
clean :
	rm -f $(Cintobj) $(Fintobj) $(long:.o=.C)
	( cd INTERNAL ; rm -f $(internal) )

#  -------------------------------------
#  Delete the library, object and source
#  -------------------------------------
killib :
	$(MAKE) clean
	rm -f $(Fintobj:.o=.c) Bdef.h Bconfig.h
	( cd INTERNAL ; rm -f $(internal:.o=.c) Bdef.h Bconfig.h )

#  -------------------------------------------------------------------------
#  Establish how to make logical links to the long-name C interface routines
#  that are distinct in first 13 characters from their Fortran interface 
#  equivalents.
#  -------------------------------------------------------------------------
Cblacs_gridinit_.C : blacs_gridinit_.C
	ln -s blacs_gridinit_.C Cblacs_gridinit_.C
Cblacs_gridinfo_.C : blacs_gridinfo_.C
	ln -s blacs_gridinfo_.C Cblacs_gridinfo_.C
Cblacs_gridexit_.C : blacs_gridexit_.C
	ln -s blacs_gridexit_.C Cblacs_gridexit_.C
Cblacs_gridmap_.C : blacs_gridmap_.C
	ln -s blacs_gridmap_.C Cblacs_gridmap_.C
Cblacs_freebuff_.C : blacs_freebuff_.C
	ln -s blacs_freebuff_.C Cblacs_freebuff_.C
Cblacs_barrier_.C : blacs_barrier_.C
	ln -s blacs_barrier_.C Cblacs_barrier_.C

#  ------------------------------------------------------------------------
#  We move C .o files to .C so that we can use the portable suffix rule for
#  compilation, and still have them coexist with the fortran interface
#  .o files.
#  ------------------------------------------------------------------------
.SUFFIXES: .o .C
.c.C:
	$(CC) -o C$*.o -c $(CCFLAGS) $(BLACSDEFS) -DCallFromC $<
	mv C$*.o $*.C
.c.o:
	$(CC) -c $(CCFLAGS) $(BLACSDEFS) $<

