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 \
       sys2blacs_handle_.o free_blacs_system_handle_.o \
       blacs2sys_handle_.o

Clong = Cblacs_gridinit_.C Cblacs_gridmap_.C Cblacs_freebuff_.C \
        Cblacs_gridexit_.C Cblacs_gridinfo_.C Cblacs_barrier_.C \
        Csys2blacs_handle_.C Cfree_blacs_system_handle_.C \
        Cblacs2sys_handle_.C

#  --------------------
#  The support routines
#  --------------------
supp = 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 = BI_HypBS.o BI_HypBR.o BI_IdringBS.o BI_IdringBR.o \
           BI_MpathBS.o BI_MpathBR.o BI_SringBS.o BI_SringBR.o \
           BI_TreeBS.o BI_TreeBR.o \
           BI_Ssend.o BI_Rsend.o BI_Srecv.o BI_Asend.o BI_Arecv.o \
           BI_TreeComb.o BI_BeComb.o BI_MringComb.o \
           BI_ArgCheck.o BI_TransDist.o BI_GetBuff.o BI_UpdateBuffs.o \
           BI_EmergencyBuff.o BI_BlacsErr.o BI_BlacsWarn.o BI_BlacsAbort.o \
           BI_BuffIsFree.o BI_imvcopy.o BI_smvcopy.o BI_dmvcopy.o \
           BI_ivmcopy.o BI_svmcopy.o BI_dvmcopy.o \
           BI_Pack.o BI_Unpack.o BI_GetMpiGeType.o BI_GetMpiTrType.o \
           BI_ivvsum.o BI_svvsum.o BI_dvvsum.o BI_cvvsum.o BI_zvvsum.o \
           BI_ivvamx.o BI_svvamx.o BI_dvvamx.o BI_cvvamx.o BI_zvvamx.o \
           BI_ivvamx2.o BI_svvamx2.o BI_dvvamx2.o BI_cvvamx2.o BI_zvvamx2.o \
           BI_ivvamn.o BI_svvamn.o BI_dvvamn.o BI_cvvamn.o BI_zvvamn.o \
           BI_ivvamn2.o BI_svvamn2.o BI_dvvamn2.o BI_cvvamn2.o BI_zvvamn2.o \
           BI_iMPI_amx.o BI_sMPI_amx.o BI_dMPI_amx.o BI_cMPI_amx.o \
           BI_zMPI_amx.o BI_iMPI_amx2.o BI_sMPI_amx2.o BI_dMPI_amx2.o \
           BI_cMPI_amx2.o BI_zMPI_amx2.o BI_iMPI_amn.o BI_sMPI_amn.o \
           BI_dMPI_amn.o BI_cMPI_amn.o BI_zMPI_amn.o BI_iMPI_amn2.o \
           BI_sMPI_amn2.o BI_dMPI_amn2.o BI_cMPI_amn2.o BI_zMPI_amn2.o \
           BI_cMPI_sum.o BI_zMPI_sum.o BI_ContxtNum.o BI_GlobalVars.o \
           BI_MPI_F77_to_c_trans_comm.o BI_MPI_C_to_f77_trans_comm.o \
           BI_TransUserComm.o bi_f77_mpi_attr_get.o bi_f77_mpi_op_create.o \
           bi_f77_mpi_initialized.o bi_f77_mpi_test.o bi_f77_mpi_testall.o \
           bi_f77_get_constants.o

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

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

$(BLACSFINIT) :
	$(CC) -o Cblacs_pinfo.o -c $(CCFLAGS) $(BLACSDEFS) -DCallFromC -DMainInF77 \
                 blacs_pinfo_.c
	$(CC) -c $(CCFLAGS) $(BLACSDEFS) -DMainInF77 blacs_pinfo_.c
	$(ARCH) $(ARCHFLAGS) $(BLACSFINIT) blacs_pinfo_.o Cblacs_pinfo.o
	$(RANLIB) $(BLACSFINIT)

$(BLACSCINIT) :
	$(CC) -o Cblacs_pinfo.o -c $(CCFLAGS) $(BLACSDEFS) -DCallFromC -DMainInC \
                 blacs_pinfo_.c
	$(CC) -c $(CCFLAGS) $(BLACSDEFS) -DMainInC blacs_pinfo_.c
	$(ARCH) $(ARCHFLAGS) $(BLACSCINIT) blacs_pinfo_.o Cblacs_pinfo.o
	$(RANLIB) $(BLACSCINIT)

#  ------------------
#  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)
	rm -f blacs_pinfo_.o Cblacs_pinfo.o
	( cd INTERNAL ; rm -f bi_f77_get_constants.o bi_f77_mpi_attr_get.o \
                              bi_f77_mpi_initialized.o bi_f77_mpi_op_create.o \
                              bi_f77_mpi_test.o bi_f77_mpi_testall.o \
                              Bdef.h Bconfig.h mpif.h )
	( cd INTERNAL ; rm -f $(internal) )

#  -------------------------------------
#  Delete the library, object and source
#  -------------------------------------
killib :
	$(MAKE) clean
	rm -f $(Fintobj:.o=.c) Bdef.h Bconfig.h
	rm -f blacs_pinfo_.c
	( cd INTERNAL ; rm -f $(internal:.o=.c) Bdef.h Bconfig.h )
	( cd INTERNAL ; rm -f bi_f77_get_constants.f bi_f77_mpi_attr_get.f \
                              bi_f77_mpi_initialized.f bi_f77_mpi_op_create.f \
                              bi_f77_mpi_test.f bi_f77_mpi_testall.f mpif.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
	rm -f Cblacs_gridinit_.C
	ln -s blacs_gridinit_.C Cblacs_gridinit_.C
Cblacs_gridinfo_.C : blacs_gridinfo_.C
	rm -f Cblacs_gridinfo_.C
	ln -s blacs_gridinfo_.C Cblacs_gridinfo_.C
Cblacs_gridexit_.C : blacs_gridexit_.C
	rm -f Cblacs_gridexit_.C
	ln -s blacs_gridexit_.C Cblacs_gridexit_.C
Cblacs_gridmap_.C : blacs_gridmap_.C
	rm -f Cblacs_gridmap_.C
	ln -s blacs_gridmap_.C Cblacs_gridmap_.C
Cblacs_freebuff_.C : blacs_freebuff_.C
	rm -f Cblacs_freebuff_.C
	ln -s blacs_freebuff_.C Cblacs_freebuff_.C
Cblacs_barrier_.C : blacs_barrier_.C
	rm -f Cblacs_barrier_.C
	ln -s blacs_barrier_.C Cblacs_barrier_.C
Csys2blacs_handle_.C : sys2blacs_handle_.C
	rm -f Csys2blacs_handle_.C
	ln -s sys2blacs_handle_.C Csys2blacs_handle_.C
Cblacs2sys_handle_.C : blacs2sys_handle_.C
	rm -f Cblacs2sys_handle_.C
	ln -s blacs2sys_handle_.C Cblacs2sys_handle_.C
Cfree_blacs_system_handle_.C : free_blacs_system_handle_.C
	rm -f Cfree_blacs_system_handle_.C
	ln -s free_blacs_system_handle_.C Cfree_blacs_system_handle_.C

#  -------------------------------------
#  Compile the (ouch!) fortran internals
#  -------------------------------------
bi_f77_get_constants.o : mpif.h bi_f77_get_constants.f
	$(F77) -c $(F77FLAGS) $*.f
bi_f77_mpi_attr_get.o : mpif.h bi_f77_mpi_attr_get.f
	$(F77) -c $(F77FLAGS) $*.f
bi_f77_mpi_initialized.o : mpif.h bi_f77_mpi_initialized.f
	$(F77) -c $(F77FLAGS) $*.f
bi_f77_mpi_op_create.o : mpif.h bi_f77_mpi_op_create.f
	$(F77) -c $(F77FLAGS) $*.f
bi_f77_mpi_test.o : mpif.h bi_f77_mpi_test.f
	$(F77) -c $(F77FLAGS) $*.f
bi_f77_mpi_testall.o : mpif.h bi_f77_mpi_testall.f
	$(F77) -c $(F77FLAGS) $*.f

mpif.h : $(MPIINCdir)/mpif.h
	rm -f mpif.h
	ln -s $(MPIINCdir)/mpif.h mpif.h

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