ARCH=_LINUX
#ARCH=_AIX
#ARCH=_BGL

include ./Makefile_BGP_FE.defs

### Debug or Release ###
VERSION := DEBUG  # "debug" or "release"

DEBUG_CFLAGS 	= -g -qfullpath -DSION_DEBUG
RELEASE_CFLAGS 	= -O3
PROFILE_CFLAGS 	= -g -pg -DSION_DEBUG 

ifeq "$(strip $(VERSION))" "PROFILE"
CFLAGS 		+= $(PROFILE_CFLAGS)
CPPFLAGS 	+= $(PROFILE_CFLAGS)
FFLAGS 		+= $(PROFILE_CFLAGS)
else
ifeq "$(strip $(VERSION))" "DEBUG"
CFLAGS 		+= $(DEBUG_CFLAGS)
CPPFLAGS 	+= $(DEBUG_CFLAGS)
#FFLAGS		+= $(DEBUG_CFLAGS)
else
ifeq "$(strip $(VERSION))" "RELEASE"
CFLAGS 		+= $(RELEASE_CFLAGS)
CPPFLAGS	+= $(RELEASE_CFLAGS)
FFLAGS 		+= $(RELEASE_CFLAGS)
endif
endif
endif
####

# Kernels 
# ------

#MPI DATATYPES  
#DP_TYPE                = MPI_DOUBLE_PRECISION
DP_TYPE                 = MPI_REAL8
INTEGER_TYPE            = MPI_INTEGER
DC_TYPE                 = MPI_DOUBLE_COMPLEX
# Length of Double Precision in Bytes
IDPLEN          = 8

#
RANLIB          = echo
ARCMD                   = ar
ARCHFLAGS               = cr

#
SHELL           =     /bin/sh

.SUFFIXES: .f .c .f90 .F .c .h. .com .o

## Defines a list of all target files ##
#llib = ./lib$(LIBNAME).a
llibser = ./lib$(LIBSERNAME).a

LSRC = 	sion.c \
		sion_fd.c \
		sion_debug.c \
		printts.c \
		sion_mpi.c \
		sion_internal.c \
		sion_internal_par.c \
		sion_fortran.c \
		sion_convert.c
		
lobj = $(LSRC:%.c=%.o)

#lobj = \
#sion.o sion_fd.o sion_debug.o  printts.o sion_mpi.o sion_internal.o sion_fortran.o

lobjser = \
sion_serial.o sion_fd.o sion_debug.o sion_internal.o printts.o sion_fortran_serial.o sion_convert.o

utils = siondump sionsplit siondefrag #partest fpartest

all: $(llib) $(llibser) $(utils)

%.o: %.c
	$(CC)  -c $(CFLAGS)  $<
%.o: %.f
	$(F90) -c $(FFLAGS)  $<

sion.o: sion.c sion.h
	$(CC)  -c $(CFLAGS) $<

sion_fortran.o: sion_fortran.c sion_fortran.h
	$(CC_MPI)  -c $(CFLAGS) $(MPI_FLAGS) -D_FORTRANNOCAPS  $<
	
sion_fortran_serial.o: sion_fortran.c sion_fortran.h
	$(CC)  -c $(CFLAGS) -D_FORTRANNOCAPS -o $@ $<
	
sion_mpi.o: sion_mpi.c sion.h sion_fd.c sion_fd.h
	$(CC_MPI)  -c $(CFLAGS) $(MPI_FLAGS)  $<

sion_serial.o: sion.c sion.h
	$(CC)  -c $(CFLAGS) -o $@ $<

sion_internal_par.o: sion_internal_par.c sion.h sion_fd.c sion_fd.h
	$(CC_MPI)  -c $(CFLAGS) $(MPI_FLAGS)  $<
		
###### UTILS #########
partest.o: partest.c sion.h
	@echo
	@echo Building the utils
	$(CC_MPI)  -c $(CFLAGS) $(MPI_FLAGS) $<

mpi_io.o: mpi_io.c partest.h
	$(CC_MPI)  -c $(CFLAGS) $(MPI_FLAGS)  $<

single_file_io.o: single_file_io.c partest.h
	$(CC_MPI)  -c $(CFLAGS) $(MPI_FLAGS)  $<

partest: partest.o mpi_io.o single_file_io.o $(llib) sion.h
	$(CCMPI_LOADER)  -o $@ $(CFLAGS) $< mpi_io.o single_file_io.o -L. -l$(LIBNAME) 

fpartest.o: fpartest.F90 sion_fortran.c sion_fortran.h
	$(F90_MPI) -c $(FFLAGS) $<
	
fpartest: fpartest.o $(llib) sion.h
	$(F90MPI_LOADER)  -o $@ $(FFLAGS) $< -L. -l$(LIBNAME)
	
siondump.o: siondump.c sion.h sion.c sion_internal.c
	$(CC)  -c $(CFLAGS)  $<

siondump: siondump.o $(llibser) sion.h
	$(CCLOADER)  -o $@ $(CFLAGS) $< -L. -l$(LIBSERNAME)

sionsplit.o: sionsplit.c sion.h sion.c sion_internal.c
	$(CC)  -c $(CFLAGS)  $<

sionsplit: sionsplit.o $(llibser) sion.h
	$(CCLOADER)  -o $@ $(CFLAGS) $< -L. -l$(LIBSERNAME)
ifeq ($(BUILD_TYPE), RELEASE)
	strip $@ 
endif

siondefrag.o: siondefrag.c sion.h sion.c sion_internal.c
	$(CC)  -c $(CFLAGS)  $<

siondefrag: siondefrag.o $(llibser) sion.h
	$(CCLOADER)  -o $@ $(CFLAGS) $< -L. -l$(LIBSERNAME)
ifeq ($(BUILD_TYPE), RELEASE)
	strip $@
endif

######  The Libs  ######
$(llib): $(lobj) 
	@echo 
	@echo Creating SION MPI version
	$(ARCMD) $(ARCHFLAGS) $@ $(lobj) 
	$(RANLIB) $@

$(llibser): $(lobjser) 
	@echo
	@echo Creating SION Serial version
	$(ARCMD) $(ARCHFLAGS) $@ $(lobjser) 
	$(RANLIB) $@

###### Dependencies ######
sion.o: sion.c sion.h

###### DOC ######
doc:
	cd ..; doxygen doxy

###### sionconfig ######
sionconfig: sionconfig.in
	sed -e "s!@ARCH@!$(ARCH)!" \
		-e "s!@INSTDIR@!$(INSTDIR)!" \
		-e "s!@LIBNAME@!$(LIBNAME)!" \
		-e "s!@LIBSERNAME@!$(LIBSERNAME)!" \
		sionconfig.in > sionconfig

###### INSTALL ######
install: all sionconfig
	mkdir -p $(INSTDIR)/bin	$(INSTDIR)/lib $(INSTDIR)/include
	@echo Installing FE Utilities
	cp -p $(utils) $(INSTDIR)/bin
	@echo Installing the library
	cp -p $(llib) $(llibser) $(INSTDIR)/lib
	@echo Installing header files	
	cp -p sion.h $(INSTDIR)/include
	cp -p sionconfig $(INSTDIR)/bin
	chmod a+rx $(INSTDIR)/*/
	chmod a+r $(INSTDIR)/*/*
	chmod a+rx $(INSTDIR)/bin/*


######  Cleanup  ######
.PHONY: clean
clean:
	-rm -f $(lobj) $(lobjser) $(llib) $(llibser) $(utils) $(utils:%=%.o) mpi_io.o single_file_io.o sionconfig
