#/****************************************************************************
#**  SIONLIB     http://www.fz-juelich.de/jsc/sionlib                       **
#*****************************************************************************
#**  Copyright (c) 2008-2009                                                **
#**  Forschungszentrum Juelich, Juelich Supercomputing Centre               **
#**                                                                         **
#**  See the file COPYRIGHT in the package base directory for details       **
#****************************************************************************/
# Makefile for sionlib
# Version 1.0
# Wolfgang Frings 
#
# Version 1.1
# Ventsislav Petkov
# 20.08.2008

### Jugene: 
#	BE: _BGP
#	FE: _LINUX
#		#include ./Makefile_BGP_FEgcc.defs
#		include ./Makefile_BGP_FE.defs


ARCH=_LINUX

LIBNAME = sion
LIBSERNAME = sionser

RANLIB          = echo
ARCMD           = ar
ARCHFLAGS       = cr

include ./Makefile$(ARCH).defs

PINSTDIR := $(realpath $(INSTDIR))

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

DEBUG_CFLAGS 	= -g -DSION_DEBUG -O0 
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

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 sion_internal.o printts.o sion_mpi.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 = partest siondump sionsplit siondefrag fpartest

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

serial: $(llibser) siondump sionsplit siondefrag

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

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

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

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

sion_fortran_serial.o: sion_fortran.c sion_fortran.h
	$(CC)  -c $(CFLAGS) -o $@ $<

sion_mpi.o: sion_mpi.c sion.h sion_fd.c sion_fd.h
	$(CC_MPI)  -c $(CFLAGS) $(MPI_FLAGS)  $<

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
	mkdir -p $(INSTDIR)
	AINSTDIR=`(cd $(INSTDIR) && pwd)`; \
	sed -e "s!@ARCH@!$(ARCH)!" \
		-e "s!@INSTDIR@!$$AINSTDIR!" \
		-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 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 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:
	@echo Clean $(HOST) 
	-rm -f $(lobj) $(lobjser) $(llib) $(llibser) $(utils) $(utils:%=%.o) mpi_io.o single_file_io.o sionconfig


