# makefile for the CGNS Library

include make.defs

CGNSVER = @CGNSVERS@

.SUFFIXES :
.SUFFIXES : .c .F90 .$(O) $(EXE)

OBJDIR  = @BUILDDIR@
COPTS	= $(CFLAGS) $(CFGFLAGS) -I. @BUILDOPTS@
FOPTS   = $(FFLAGS) -I. @BUILDOPTS@
D_PRE   = @FC_D_PRE@
CGNSLIB = $(LIBCGNS)
INSTLIB = @CGNSLIB@

# Prepend to "-D" if necessary (xlf compilers)
FOPTS_MOD=$(subst -D,$(D_PRE)-D,$(FOPTS))

#----------

CGNSOBJS=\
	$(OBJDIR)/cgns_error.$(O) \
	$(OBJDIR)/cgns_internals.$(O) \
	$(OBJDIR)/cgns_io.$(O) \
	$(OBJDIR)/cgnslib.$(O)

# ADF/ADFH routines

ADFOBJS=@ADFOBJS@ \
	$(OBJDIR)/ADF_interface.$(O) \
	$(OBJDIR)/ADF_internals.$(O)

# Fortran interface routines

@FGNSOBJS@=$(OBJDIR)/cgns_f.$(O)
@F2COBJS@=$(OBJDIR)/cg_ftoc.$(O) $(OBJDIR)/cgio_ftoc.$(O)

#----------
FORTRAN_TESTS=
ifeq ("$(BUILDFORTRAN)" , "1")
	FORTRAN_TESTS=cd examples/fortran && $(MAKE) test && cd ../../Test_UserGuideCode/Fortran_code && $(MAKE) test
endif

MakeTests = runtests @PTESTS@
all     : $(CGNSLIB) tools @CGNSTOOLS@
cgns    : $(CGNSLIB)
test    : $(MakeTests)
check   : $(MakeTests)
install : install-cgns @INSTALLCGNSTOOLS@

#----------

$(CGNSLIB) : $(OBJDIR) $(CGNSOBJS) $(FGNSOBJS) $(ADFOBJS) $(F2COBJS)
	-@$(RM) $@
	@AR_LIB@ $@ $(CGNSOBJS) $(FGNSOBJS) $(ADFOBJS) $(F2COBJS)
	@RAN_LIB@ $@

$(OBJDIR) :
	-$(MKDIR) $(OBJDIR)

#----------

tools : $(CGNSLIB)
	cd tools && $(MAKE)

cgnstools : $(CGNSLIB)
	cd cgnstools && $(MAKE)

tests : $(CGNSLIB)
	cd tests && $(MAKE)

ptests : $(CGNSLIB)
	cd ptests && $(MAKE)

runtests : $(CGNSLIB)
	cd tests && $(MAKE) test
	cd Test_UserGuideCode/C_code && $(MAKE) test
	$(FORTRAN_TESTS)

runptests : $(CGNSLIB)
	cd ptests && $(MAKE) test

#----------

clean :
	-$(RM) $(OBJDIR)/*.$(O)
	-cd tools && $(MAKE) clean
	-cd tests && $(MAKE) clean
	-cd examples/fortran && $(MAKE) clean
	-cd Test_UserGuideCode/C_code && $(MAKE) clean
	-cd Test_UserGuideCode/Fortran_code && $(MAKE) clean
	-cd ptests && $(MAKE) clean
	-cd cgnstools && $(MAKE) clean

allclean : distclean

distclean :
	-cd tools && $(MAKE) allclean
	-$(RM) tools/Makefile
	-cd tests && $(MAKE) allclean
	-cd examples/fortran && $(MAKE) clean
	-cd Test_UserGuideCode/C_code && $(MAKE) clean
	-cd Test_UserGuideCode/Fortran_code && $(MAKE) clean
	-$(RM) tests/Makefile
	-cd ptests && $(MAKE) allclean
	-$(RM) ptests/Makefile
	-cd cgnstools && $(MAKE) allclean
	-$(RM) $(CGNSLIB)
	-$(RM) $(OBJDIR)/*.$(O)
	-$(RMDIR) $(OBJDIR)
	-$(RM) cgnsconfig.h cgnstypes.h cgnstypes_f.h cgnstypes_f03.h 
	-$(RM) config.log config.status
	-$(RM) make.defs cgnsBuild.defs Makefile
	-$(RM) *~ *.bak *.mod

install-all : install

install-cgns : $(CGNSLIB) $(INCLUDEDIR) $(LIBDIR) @INSTALLPCGNS@ @INSTALLADF@
	$(INSTALL_DATA) cgnstypes.h $(INCLUDEDIR)/cgnstypes.h
	$(INSTALL_DATA) cgnstypes_f.h $(INCLUDEDIR)/cgnstypes_f.h
	$(INSTALL_DATA) cgnstypes_f03.h $(INCLUDEDIR)/cgnstypes_f03.h
	$(INSTALL_DATA) cgnslib.h $(INCLUDEDIR)/cgnslib.h
	$(INSTALL_DATA) cgnswin_f.h $(INCLUDEDIR)/cgnswin_f.h
	$(INSTALL_DATA) cgns_io.h $(INCLUDEDIR)/cgns_io.h
	$(INSTALL_DATA) cgnsconfig.h $(INCLUDEDIR)/cgnsconfig.h
	$(INSTALL_DATA) cgnsBuild.defs $(INCLUDEDIR)/cgnsBuild.defs
	$(INSTALL_DATA) $(CGNSLIB) $(LIBDIR)/$(INSTLIB)
	@if test -f cgns.mod; then \
	$(INSTALL_DATA) cgns.mod $(INCLUDEDIR)/cgns.mod; \
	  fi

install-tools :
	-cd tools && $(MAKE) install

install-cgnstools : install-tools
	-cd cgnstools && $(MAKE) install

install-adf : $(INCLUDEDIR)/adf
	$(INSTALL_DATA) adf/ADF.h $(INCLUDEDIR)/adf/ADF.h

install-adfh : $(INCLUDEDIR)/adfh
	$(INSTALL_DATA) adfh/ADFH.h $(INCLUDEDIR)/adfh/ADFH.h

install-pcgns : $(INCLUDEDIR)
	$(INSTALL_DATA) pcgnslib.h $(INCLUDEDIR)/pcgnslib.h

$(INCLUDEDIR) :
	@if [ ! -d $(INCLUDEDIR) ] ; then \
	    echo "Making directory $(INCLUDEDIR)"; \
	    mkdir -p $(INCLUDEDIR); \
	    chmod 755 $(INCLUDEDIR); \
	  fi;

$(LIBDIR) :
	@if [ ! -d $(LIBDIR) ] ; then \
	    echo "Making directory $(LIBDIR)"; \
	    mkdir -p $(LIBDIR); \
	    chmod 755 $(LIBDIR); \
	  fi;

$(INCLUDEDIR)/adf : $(INCLUDEDIR)
	@if [ ! -d $(INCLUDEDIR)/adf ] ; then \
	    echo "Making directory $(INCLUDEDIR)/adf"; \
	    mkdir -p $(INCLUDEDIR)/adf; \
	    chmod 755 $(INCLUDEDIR)/adf; \
	  fi;

$(INCLUDEDIR)/adfh : $(INCLUDEDIR)
	@if [ ! -d $(INCLUDEDIR)/adfh ] ; then \
	    echo "Making directory $(INCLUDEDIR)/adfh"; \
	    mkdir -p $(INCLUDEDIR)/adfh; \
	    chmod 755 $(INCLUDEDIR)/adfh; \
	  fi;

uninstall :
	-cd tools && $(MAKE) uninstall
	-cd cgnstools && $(MAKE) uninstall
	-$(RM) $(INCLUDEDIR)/cgnstypes.h
	-$(RM) $(INCLUDEDIR)/cgnstypes_f.h
	-$(RM) $(INCLUDEDIR)/cgnslib.h
	-$(RM) $(INCLUDEDIR)/cgnswin_f.h
	-$(RM) $(INCLUDEDIR)/cgns_io.h
	-$(RM) $(INCLUDEDIR)/cgnsconfig.h
	-$(RM) $(INCLUDEDIR)/cgnsBuild.defs
	@if [ -f $(INCLUDEDIR)/cgns.mod ] ; then \
	   $(RM) $(INCLUDEDIR)/cgns.mod \
	  fi;
	-$(RM) $(INCLUDEDIR)/cgns.mod
	-$(RM) $(LIBDIR)/$(INSTLIB)
	@if [ -f $(INCLUDEDIR)/adf/ADF.h ] ; then \
	   $(RM) $(INCLUDEDIR)/adf/ADF.h; \
	   $(RMDIR) $(INCLUDEDIR)/adf; \
	 fi; \
	 if [ -f $(INCLUDEDIR)/adfh/ADFH.h ] ; then \
	   $(RM) $(INCLUDEDIR)/adfh/ADFH.h; \
	   $(RMDIR) $(INCLUDEDIR)/adfh; \
	 fi; \
	 if [ -f $(INCLUDEDIR)/pcgnslib.h ] ; then \
	   $(RM) $(INCLUDEDIR)/pcgnslib.h; \
	 fi;

#---------- mid-level library

$(OBJDIR)/cgns_error.$(O) : cgns_error.c cgnslib.h cgns_header.h cgns_io.h
	$(CC) $(COPTS) $(COOUT)$@ -c cgns_error.c

$(OBJDIR)/cgns_internals.$(O) : cgns_internals.c cgnslib.h cgns_header.h \
	cgns_io.h
	$(CC) $(COPTS) $(HDF5INC) $(COOUT)$@ -c cgns_internals.c

$(OBJDIR)/cgnslib.$(O) : cgnslib.c cgnslib.h cgns_header.h cgns_io.h
	$(CC) $(COPTS) $(HDF5INC) $(MPIINC) $(COOUT)$@ -c cgnslib.c

$(OBJDIR)/cgns_io.$(O) : cgns_io.c cgnslib.h cgns_io.h \
	adf/ADF.h @ADFINCS@
	$(CC) $(COPTS) $(COOUT)$@ -c cgns_io.c

$(OBJDIR)/cg_ftoc.$(O) : cg_ftoc.c fortran_macros.h cgnslib.h \
	cgns_header.h cgns_io.h
	$(CC) $(COPTS) $(HDF5INC) $(F2CFLAGS) $(COOUT)$@ -c cg_ftoc.c

$(OBJDIR)/cgio_ftoc.$(O) : cgio_ftoc.c fortran_macros.h cgns_io.h
	$(CC) $(COPTS) $(HDF5INC) $(F2CFLAGS) $(COOUT)$@ -c cgio_ftoc.c

$(OBJDIR)/cg_malloc.$(O) : cg_malloc.c cg_malloc.h
	$(CC) $(COPTS) $(COOUT)$@ -c cg_malloc.c

$(OBJDIR)/cgns_f.$(O) : cgns_f.F90 cgnstypes_f.h
	$(F77) $(FOPTS_MOD) $(COOUT)$@ -c cgns_f.F90

cgnslib.h : cgnstypes.h
cgns_header.h : cgnstypes.h
cgns_io.h : cgnstypes.h

#---------- ADF

$(OBJDIR)/ADF_interface.$(O) : adf/ADF_interface.c \
	adf/ADF.h adf/ADF_internals.h
	$(CC) $(COPTS) -Iadf $(COOUT)$@ -c adf/ADF_interface.c

$(OBJDIR)/ADF_internals.$(O) : adf/ADF_internals.c \
	adf/ADF.h adf/ADF_internals.h
	$(CC) $(COPTS) -Iadf $(COOUT)$@ -c adf/ADF_internals.c

adf/ADF.h : cgnstypes.h
adf/ADF_internals.h : cgnstypes.h

#---------- HDF5

$(OBJDIR)/ADFH.$(O) : adfh/ADFH.c adfh/ADFH.h
	$(CC) $(COPTS) -Iadfh $(HDF5INC) $(MPIINC) $(COOUT)$@ -c adfh/ADFH.c

adfh/ADFH.h : cgnstypes.h

#---------- PCGNS

$(OBJDIR)/pcgnslib.$(O) : pcgnslib.c pcgnslib.h
	$(CC) $(COPTS) $(HDF5INC) $(MPIINC) $(COOUT)$@ -c pcgnslib.c

pcgnslib.h : cgnslib.h

