CC          = @CC@
AR          = @AR@
LIBNAME     = @LIBNAME@
RANLIB      = @RANLIB@
srcdir      = @srcdir@
CC_SHL      = @CC_SHL@
SHLIBNAME   = @SHLIBNAME@

INCLUDE_DIR = -I@MPI_INCLUDE_DIR@ -I${srcdir} -I../include -I${srcdir}/../adio/include -I../adio/include -I${srcdir}/../../../include  -I../../../include 
CFLAGS      = @CPPFLAGS@ @CFLAGS@ $(MPIOPROFILE) $(INCLUDE_DIR)

top_builddir  = @master_topbuild_dir@
LIBTOOL       = @LIBTOOL@
C_COMPILE_SHL = $(CC_SHL) 

@VPATH@

# mpi_sources gives the files that implement the MPI IO routines.  
# This is used for the MPICH2 documentation target, and does not
# include files either not used in MPICH2 or that do not contain
# documentation entries.
mpi_sources = close.c read.c open.c write.c set_view.c \
      iread.c iwrite.c \
      seek.c get_posn.c \
      delete.c read_all.c read_at.c \
      read_atall.c iread_at.c iwrite_at.c write_all.c get_bytoff.c \
      write_at.c write_atall.c get_view.c get_group.c get_amode.c \
      get_extent.c fsync.c get_atom.c set_atom.c \
      set_size.c get_size.c prealloc.c file_f2c.c file_c2f.c \
      set_info.c get_info.c \
      rd_atallb.c rd_atalle.c read_allb.c read_alle.c wr_atallb.c \
      wr_atalle.c write_allb.c write_alle.c \
      get_posn_sh.c iread_sh.c read_sh.c write_sh.c \
      iwrite_sh.c seek_sh.c read_ord.c read_orde.c write_ordb.c \
      read_ordb.c write_ord.c write_orde.c mpiu_greq.c register_datarep.c

# EXTRA objects are ones that need to be included for all but MPICH2
MPIO_OBJECTS = close.o read.o open.o write.o set_view.o \
      iread.o iwrite.o \
      seek.o get_posn.o \
      delete.o read_all.o read_at.o \
      read_atall.o iread_at.o iwrite_at.o write_all.o get_bytoff.o \
      write_at.o write_atall.o get_view.o get_group.o get_amode.o \
      get_extent.o fsync.o get_atom.o set_atom.o \
      set_size.o get_size.o prealloc.o file_f2c.o file_c2f.o \
      set_info.o get_info.o \
      rd_atallb.o rd_atalle.o read_allb.o read_alle.o wr_atallb.o \
      wr_atalle.o write_allb.o write_alle.o \
      get_posn_sh.o iread_sh.o read_sh.o write_sh.o \
      iwrite_sh.o seek_sh.o read_ord.o read_orde.o write_ordb.o \
      read_ordb.o write_ord.o write_orde.o mpiu_greq.o mpich2_fileutil.o \
      register_datarep.o

# MPIO_REQOBJECTS are the routines that provide the MPIO_Wait etc.
# routines (iotest.o, iowait.o iowaitall.o iowaitany.o iotestall.o
# iotestany.o iowaitsome.o and iotestsome.o)
MPIO_REQOBJECTS = @MPIO_REQOBJECTS@

# Either get_errh.o set_errh.o or empty 
MPIO_EXTRA_OBJECTS = @MPIO_EXTRA_OBJECTS@ @MPIO_REQOBJECTS@

# mpich2_fileutil.p is not included because it does not include any
# name-shifted functions
MPIO_TMP_POBJECTS = close.p read.p open.p write.p get_extent.p \
      iread.p iwrite.p \
      seek.p \
      delete.p read_all.p read_at.p \
      read_atall.p iread_at.p iwrite_at.p get_posn.p \
      write_all.p write_at.p write_atall.p get_bytoff.p \
      set_view.p get_view.p get_group.p get_amode.p \
      fsync.p get_atom.p set_atom.p set_size.p get_size.p prealloc.p \
      file_f2c.p file_c2f.p set_info.p get_info.p \
      rd_atallb.p rd_atalle.p read_allb.p read_alle.p wr_atallb.p \
      wr_atalle.p write_allb.p write_alle.p \
      get_posn_sh.p iread_sh.p read_sh.p write_sh.p \
      iwrite_sh.p seek_sh.p read_ord.p read_orde.p write_ordb.p \
      read_ordb.p write_ord.p write_orde.p register_datarep.p

# Either get_errh.p set_errh.p or empty 
MPIO_EXTRA_TMP_POBJECTS = @MPIO_EXTRA_TMP_POBJECTS@ @MPIO_REQ_TMP_POBJECTS@

# _mpich2_fileutil.o is not included because it does not include any 
# name-shifted functions
MPIO_REAL_POBJECTS = _close.o _read.o _open.o _get_extent.o \
      _write.o _set_view.o _seek.o _read_at.o _prealloc.o \
      _iread.o _iwrite.o \
      _get_posn.o \
      _delete.o _read_all.o _get_bytoff.o \
      _read_atall.o _iread_at.o _iwrite_at.o _get_group.o _get_amode.o \
      _write_all.o _write_at.o _write_atall.o _get_view.o \
      _fsync.o _get_atom.o _set_atom.o _set_size.o _get_size.o \
      _file_f2c.o _file_c2f.o \
      _set_info.o _get_info.o \
      _rd_atallb.o _rd_atalle.o _read_allb.o _read_alle.o _wr_atallb.o \
      _wr_atalle.o _write_allb.o _write_alle.o \
      _get_posn_sh.o _iread_sh.o _read_sh.o _write_sh.o \
      _iwrite_sh.o _seek_sh.o _read_ord.o _read_orde.o _write_ordb.o \
      _read_ordb.o _write_ord.o _write_orde.o _register_datarep.o

# Either _get_errh.o _set_errh.o or empty 
MPIO_EXTRA_REAL_POBJECTS = @MPIO_EXTRA_REAL_POBJECTS@ @MPIO_REQ_REAL_POBJECTS@

all: $(LIBNAME)
	@if [ "@ENABLE_SHLIB@" != "none" ] ; then \
	    $(MAKE) $(SHLIBNAME).la ;\
	fi

.SUFFIXES: $(SUFFIXES) .p .sp .lo .man .html .latex .txt 

.c.o:
	$(CC) $(CFLAGS) -c $<
.c.lo:
	$(C_COMPILE_SHL) $(CFLAGS) -c $< -o _s$*.o
	@mv -f _s$*.o $*.lo

.c.p:
	$(CC) $(CFLAGS) -c $< -o _$*.o

#	@cp $(srcdir)/$*.c _$*.c
#	@rm -f _$*.c
# Send the output to a .o file because some compilers require all object
# files to have an extension of .o
.c.sp:
	$(C_COMPILE_SHL) $(CFLAGS) -c $< -o _sp$*.o
	@mv -f _sp$*.o _$*.lo

profile: $(MPIO_REAL_POBJECTS) $(MPIO_EXTRA_TMP_POBJECTS)
	$(AR) $(LIBNAME) $(MPIO_REAL_POBJECTS) $(MPIO_EXTRA_REAL_POBJECTS)
	$(RANLIB) $(LIBNAME) 
	@if [ "@ENABLE_SHLIB@" != "none" ] ; then \
	    $(MAKE) P$(SHLIBNAME).la ;\
	fi

coverage:
	-@for file in  ${MPIO_OBJECTS:.o=.c} ; do \
		gcov -b -f $$file ; done

$(LIBNAME): $(MPIO_OBJECTS) $(MPIO_EXTRA_OBJECTS)
	$(AR) $(LIBNAME) $(MPIO_OBJECTS) $(MPIO_EXTRA_OBJECTS)
	$(RANLIB) $(LIBNAME)

MPIO_LOOBJECTS = $(MPIO_OBJECTS:.o=.lo)
MPIO_EXTRA_LOOBJECTS = $(MPIO_EXTRA_OBJECTS:.o=.lo)

$(SHLIBNAME).la: $(MPIO_LOOBJECTS) $(MPIO_EXTRA_LOOBJECTS)
	$(AR) $(SHLIBNAME).la $(MPIO_LOOBJECTS) $(MPIO_EXTRA_LOOBJECTS)

# --------------------------------------------------------------------------
# We use P$(SHLBNAME) simply to distinguish the regular lib build from the
# profiling library build
MPIO_LOPOBJECTS = $(MPIO_REAL_POBJECTS:.o=.lo)
MPIO_EXTRA_LOPOBJECTS = $(MPIO_EXTRA_REAL_POBJECTS:.o=.lo)
MPIO_TMP_LOPOBJECTS = ${MPIO_TMP_POBJECTS:.p=.sp}
MPIO_EXTRA_LOPOBJECTS = ${MPIO_EXTRA_TMP_OBJECTS:.p=.sp}
P$(SHLIBNAME).la: $(MPIO_TMP_LOPOBJECTS) $(MPIO_EXTRA_TMP_LOPOBJECTS)
	$(AR) $(SHLIBNAME).la $(MPIO_LOPOBJECTS) $(MPIO_EXTRA_LOPOBJECTS)
# --------------------------------------------------------------------------
clean:
	@rm -f *.o *.lo *.gcno *.gcda *.bb *.bbg
	@rm -f ${srcdir}/*.gcno ${srcdir}/*.gcda 
	@rm -f ${srcdir}/*.bb ${srcdir}/*.bbg

# MPICH2 documentation targets
master_top_srcdir = @master_top_srcdir@
DOCTEXT = @DOCTEXT@
.c.man:
	$(DOCTEXT) -mpath ../../../../man/man3 -ext 3 -heading MPI \
		-quotefmt ${master_top_srcdir}/maint/docnotes $<
.c.latex:
	$(DOCTEXT) -latex -mpath ../../../../doc/refman  -heading MPI \
		-quotefmt ${master_top_srcdir}/maint/docnotes $<
.c.html:
	$(DOCTEXT) -html -mpath ../../../../www/www3  -heading MPI \
		-quotefmt ${master_top_srcdir}/maint/docnotes $<
mandoc: ${mpi_sources:.c=.man}
latexdoc: ${mpi_sources:.c=.latex}
htmldoc: ${mpi_sources:.c=.html}

tags: TAGS
SOURCES = ${mpi_sources}
HEADERS = 
TAGS:${HEADERS} ${SOURCES}
	here=`cd ../../../.. && pwd` ; cd ${srcdir} && etags -o $$here/TAGS --append ${HEADERS} ${SOURCES}

# Rules for the profiling objects
_close.o: close.c
	$(CC) $(CFLAGS) -c $(srcdir)/close.c -o _close.o
_read.o: read.c
	$(CC) $(CFLAGS) -c $(srcdir)/read.c -o _read.o
_open.o: open.c
	$(CC) $(CFLAGS) -c $(srcdir)/open.c -o _open.o
_get_extent.o: get_extent.c
	$(CC) $(CFLAGS) -c $(srcdir)/get_extent.c -o _get_extent.o
_write.o: write.c
	$(CC) $(CFLAGS) -c $(srcdir)/write.c -o _write.o
_set_view.o: set_view.c
	$(CC) $(CFLAGS) -c $(srcdir)/set_view.c -o _set_view.o
_seek.o: seek.c
	$(CC) $(CFLAGS) -c $(srcdir)/seek.c -o _seek.o
_read_at.o: read_at.c
	$(CC) $(CFLAGS) -c $(srcdir)/read_at.c -o _read_at.o
_prealloc.o: prealloc.c
	$(CC) $(CFLAGS) -c $(srcdir)/prealloc.c -o _prealloc.o
_iread.o: iread.c
	$(CC) $(CFLAGS) -c $(srcdir)/iread.c -o _iread.o
_iwrite.o: iwrite.c
	$(CC) $(CFLAGS) -c $(srcdir)/iwrite.c -o _iwrite.o
_get_posn.o: get_posn.c
	$(CC) $(CFLAGS) -c $(srcdir)/get_posn.c -o _get_posn.o
_delete.o: delete.c
	$(CC) $(CFLAGS) -c $(srcdir)/delete.c -o _delete.o
_read_all.o: read_all.c
	$(CC) $(CFLAGS) -c $(srcdir)/read_all.c -o _read_all.o
_get_bytoff.o: get_bytoff.c
	$(CC) $(CFLAGS) -c $(srcdir)/get_bytoff.c -o _get_bytoff.o
_read_atall.o: read_atall.c
	$(CC) $(CFLAGS) -c $(srcdir)/read_atall.c -o _read_atall.o
_iread_at.o: iread_at.c
	$(CC) $(CFLAGS) -c $(srcdir)/iread_at.c -o _iread_at.o
_iwrite_at.o: iwrite_at.c
	$(CC) $(CFLAGS) -c $(srcdir)/iwrite_at.c -o _iwrite_at.o
_get_group.o: get_group.c
	$(CC) $(CFLAGS) -c $(srcdir)/get_group.c -o _get_group.o
_get_amode.o: get_amode.c
	$(CC) $(CFLAGS) -c $(srcdir)/get_amode.c -o _get_amode.o
_write_all.o: write_all.c
	$(CC) $(CFLAGS) -c $(srcdir)/write_all.c -o _write_all.o
_write_at.o: write_at.c
	$(CC) $(CFLAGS) -c $(srcdir)/write_at.c -o _write_at.o
_write_atall.o: write_atall.c
	$(CC) $(CFLAGS) -c $(srcdir)/write_atall.c -o _write_atall.o
_get_view.o: get_view.c
	$(CC) $(CFLAGS) -c $(srcdir)/get_view.c -o _get_view.o
_fsync.o: fsync.c
	$(CC) $(CFLAGS) -c $(srcdir)/fsync.c -o _fsync.o
_get_atom.o: get_atom.c
	$(CC) $(CFLAGS) -c $(srcdir)/get_atom.c -o _get_atom.o
_set_atom.o: set_atom.c
	$(CC) $(CFLAGS) -c $(srcdir)/set_atom.c -o _set_atom.o
_set_size.o: set_size.c
	$(CC) $(CFLAGS) -c $(srcdir)/set_size.c -o _set_size.o
_get_size.o: get_size.c
	$(CC) $(CFLAGS) -c $(srcdir)/get_size.c -o _get_size.o
_file_f2c.o: file_f2c.c
	$(CC) $(CFLAGS) -c $(srcdir)/file_f2c.c -o _file_f2c.o
_file_c2f.o: file_c2f.c
	$(CC) $(CFLAGS) -c $(srcdir)/file_c2f.c -o _file_c2f.o
_set_info.o: set_info.c
	$(CC) $(CFLAGS) -c $(srcdir)/set_info.c -o _set_info.o
_get_info.o: get_info.c
	$(CC) $(CFLAGS) -c $(srcdir)/get_info.c -o _get_info.o
_rd_atallb.o: rd_atallb.c
	$(CC) $(CFLAGS) -c $(srcdir)/rd_atallb.c -o _rd_atallb.o
_rd_atalle.o: rd_atalle.c
	$(CC) $(CFLAGS) -c $(srcdir)/rd_atalle.c -o _rd_atalle.o
_read_allb.o: read_allb.c
	$(CC) $(CFLAGS) -c $(srcdir)/read_allb.c -o _read_allb.o
_read_alle.o: read_alle.c
	$(CC) $(CFLAGS) -c $(srcdir)/read_alle.c -o _read_alle.o
_wr_atallb.o: wr_atallb.c
	$(CC) $(CFLAGS) -c $(srcdir)/wr_atallb.c -o _wr_atallb.o
_wr_atalle.o: wr_atalle.c
	$(CC) $(CFLAGS) -c $(srcdir)/wr_atalle.c -o _wr_atalle.o
_write_allb.o: write_allb.c
	$(CC) $(CFLAGS) -c $(srcdir)/write_allb.c -o _write_allb.o
_write_alle.o: write_alle.c
	$(CC) $(CFLAGS) -c $(srcdir)/write_alle.c -o _write_alle.o
_get_posn_sh.o: get_posn_sh.c
	$(CC) $(CFLAGS) -c $(srcdir)/get_posn_sh.c -o _get_posn_sh.o
_iread_sh.o: iread_sh.c
	$(CC) $(CFLAGS) -c $(srcdir)/iread_sh.c -o _iread_sh.o
_read_sh.o: read_sh.c
	$(CC) $(CFLAGS) -c $(srcdir)/read_sh.c -o _read_sh.o
_write_sh.o: write_sh.c
	$(CC) $(CFLAGS) -c $(srcdir)/write_sh.c -o _write_sh.o
_iwrite_sh.o: iwrite_sh.c
	$(CC) $(CFLAGS) -c $(srcdir)/iwrite_sh.c -o _iwrite_sh.o
_seek_sh.o: seek_sh.c
	$(CC) $(CFLAGS) -c $(srcdir)/seek_sh.c -o _seek_sh.o
_read_ord.o: read_ord.c
	$(CC) $(CFLAGS) -c $(srcdir)/read_ord.c -o _read_ord.o
_read_orde.o: read_orde.c
	$(CC) $(CFLAGS) -c $(srcdir)/read_orde.c -o _read_orde.o
_write_ordb.o: write_ordb.c
	$(CC) $(CFLAGS) -c $(srcdir)/write_ordb.c -o _write_ordb.o
_read_ordb.o: read_ordb.c
	$(CC) $(CFLAGS) -c $(srcdir)/read_ordb.c -o _read_ordb.o
_write_ord.o: write_ord.c
	$(CC) $(CFLAGS) -c $(srcdir)/write_ord.c -o _write_ord.o
_write_orde.o: write_orde.c
	$(CC) $(CFLAGS) -c $(srcdir)/write_orde.c -o _write_orde.o
_register_datarep.o: register_datarep.c
	$(CC) $(CFLAGS) -c $(srcdir)/register_datarep.c -o _register_datarep.o
