####### Makefile to compile sload.exe for loading data supported by libbiosig into Mathematica
###
###  Copyright (C) 2010,2011,2012,2013,2015 Alois Schloegl, IST Austria <alois.schloegl@ist.ac.at>
###  This file is part of the "BioSig for C/C++" repository
###  (biosig4c++) at http://biosig.sf.net/
###
##############################################################


###########################################
# user-defined variables 
###########################################
 
###########################################
## set Mathematica variables

#MLINKDIR = /usr/local/Wolfram/gridMathematicaServer/7.0
#MLINKDIR = /usr/local/Wolfram/Mathematica/8.0
#MLINKDIR = /usr/local/Wolfram/Mathematica/9.0
#MLINKDIR = /usr/local/Wolfram/Mathematica/10.0

ifeq (Darwin,$(shell uname))
MLINKDIR := /Applications/Mathematica.app/
SYS       = MacOSX-x86-64
LDFLAGS  += -demangle -dynamic -arch x86_64 -macosx_version_min 10.10.0
LDFLAGS  += -framework CoreFoundation -framework CoreVideo -framework VideoDecodeAcceleration
else
ifeq ($(wildcard $(MLINKDIR)/Executables/mathematica),)
    MLINKDIR := $(dir $(shell readlink -f $(shell which mathematica) ) )..
endif
SYS       = Linux-x86-64# Set this value with the result of evaluating $SystemID
endif

# automated detection of most recent MLINTERFACE, without starting mathematica
# 10.0 supports 3 and 4, 9.0 and earlier supports only 3,
MLIVER := $(shell ${CC} mliver.c -I${MLINKDIR}/SystemFiles/Links/MathLink/DeveloperKit/${SYS}/CompilerAdditions/ -E -o - | tail -1)

#
# Windows
#MLINKDIR  = /usr/local/Wolfram/gridMathematicaServer/7.0/SystemFiles/Links/MathLink/DeveloperKit
SYSwin       = Windows# Set this value with the result of evaluating $SystemID
MLLIBwin     = ml32i3m# Set this to ML64i3 if using a 64-bit system
SYSwin64     = Windows-x86-64# Set this value with the result of evaluating $SystemID
MLLIBwin64   = ml64i3m# Set this to ML64i3 if using a 64-bit system
###########################################

ifneq ($(wildcard ${MLINKDIR}/SystemFiles/Libraries/${SYS}/libuuid*),)
LIBDIR   += -L${MLINKDIR}/SystemFiles/Libraries/$(SYS)
EXTRALIBS += -luuid
endif
CADDSDIR  = ${MLINKDIR}/SystemFiles/Links/MathLink/DeveloperKit/${SYS}/CompilerAdditions
INCDIR    = -I${CADDSDIR} -I${MLINKDIR}/SystemFiles/IncludeFiles/C/
LIBDIR   += -L${CADDSDIR}

EXTRALIBS += -lm -ldl # Set these with appropriate libs for your system.

ifeq (Darwin,$(shell uname))
MLLIB   = MLi${MLIVER} # ML32i3 # Set this to ML64i3 if using a 64-bit system
SED	= gsed
else
MLLIB   = ML64i${MLIVER} # ML32i3 # Set this to ML64i3 if using a 64-bit system
EXTRALIBS += -lrt
LD 	= ${CC}
SED	= sed
endif


ifneq (${MLIVER},)
CFLAGS += -DMLINTERFACE=${MLIVER}
endif

MPREP     = ${CADDSDIR}/mprep

all : sload.exe ${SYSwin}/sload.exe ${SYSwin64}/sload.exe
mma : sload.exe

.c.o:
	${CC} ${CFLAGS} -c ${INCDIR} "$<"

sloadtm.c: sload.tm
	${MPREP} "$<" -o "$@"
	# enable macro expansion
	${SED} -i 's# __DATE__# ",__DATE__," ",__TIME__,"#' "$@"

sload.exe: sload.o sloadtm.o
	# static linking
	#${LD} ${LDFLAGS} sloadtm.o sload.o ../libbiosig2.a $(shell pkg-config --libs libbiosig) -lstdc++ ${LIBDIR} -l${MLLIB} ${EXTRALIBS} -o "$@"
	# dynamic linking
	${LD} ${LDFLAGS} sloadtm.o sload.o $(shell pkg-config --libs libbiosig) -lbiosig2 -lstdc++ ${LIBDIR} -l${MLLIB} ${EXTRALIBS} -o "$@"
	mkdir -p ${SYS}
	cp "$@" ${SYS}/
	# copy to user directory if available
	#-cp -r ${SYS} ~/K/Software/MathematicaTools/
	#-cp "$@" ~/K/Software/MathematicaTools/${SYS}/sload-$(shell date +%Y%m%d)


###########################################
#   Win32 - cross-compilation 
###########################################

CCw32        = $(CROSS)-gcc
CADDSDIRwin  = ${MLINKDIR}/SystemFiles/Links/MathLink/DeveloperKit/${SYSwin}/CompilerAdditions/mldev32
INCDIRwin    = -I${CADDSDIRwin}/include  -I${MLINKDIR}../SystemFiles/IncludeFiles/C/
LIBDIRwin    = ${CADDSDIRwin}/lib

EXTRALIBSwin = -lgdi32 -lm -lstdc++ -lws2_32 $(LDLIBS) # Set these with appropriate libs for your system.

MPREPwin     = ${CADDSDIRwin}/bin/mprep.exe

sloadtm.win32.c : sload.tm
	wine ${MPREPwin} "$<" -o "$@"
	sed -i 's# __DATE__# ",__DATE__," ",__TIME__,"#' "$@"

sload.obj : sload.c
	${CCw32} -c ${INCDIRwin} "$<" -o "$@"
sloadtm.obj : sloadtm.win32.c
	${CCw32} -c ${INCDIRwin} "$<" -o "$@"

win32mma : ${SYSwin}/sload.exe

${SYSwin}/sload.exe : sload.obj sloadtm.obj
	mkdir -p ${SYSwin}
	${CCw32} sload.obj sloadtm.obj -static-libgcc -static-libstdc++ -lbiosig2 -lssp -L${LIBDIRwin} -l${MLLIBwin} ${EXTRALIBSwin} -o "$@"
	# copy to users directory if available
	#-cp -r ${SYSwin} ~/K/Software/MathematicaTools/
	#-cp "$@" ~/K/Software/MathematicaTools/${SYSwin}/sload-$(shell date +%Y%m%d).exe


###########################################
#   Win64 - cross-compilation 
###########################################

CCw64         = $(CROSS64)-gcc
CADDSDIRwin64 = ${MLINKDIR}/SystemFiles/Links/MathLink/DeveloperKit/${SYSwin64}/CompilerAdditions/mldev64
INCDIRwin64   = -I${CADDSDIRwin64}/include -I${MLINKDIR}../SystemFiles/IncludeFiles/C/
LIBDIRwin64   = ${CADDSDIRwin64}/lib

MPREPwin64    = ${CADDSDIRwin64}/bin/mprep.exe

sloadtm.win64.c : sload.tm
	wine ${MPREPwin} "$<" -o "$@"   ## use 32bit mprep.exe because wine can not handle the 64bit mprep.exe
	sed -i 's# __DATE__# ",__DATE__," ",__TIME__,"#' "$@"

sload.w64.obj : sload.c
	${CCw64} -c ${INCDIRwin64} "$<" -o "$@"
sloadtm.w64.obj : sloadtm.win64.c
	${CCw64} -c ${INCDIRwin64} "$<" -o "$@"

win64mma : ${SYSwin64}/sload.exe

${SYSwin64}/sload.exe : sload.w64.obj sloadtm.w64.obj
	mkdir -p ${SYSwin64}
	${CCw64} sload.w64.obj sloadtm.w64.obj -static-libgcc -static-libstdc++  -lbiosig2 -lssp -L${LIBDIRwin64} -l${MLLIBwin64} ${EXTRALIBSwin} -o "$@"
	# copy to users directory if available
	#-cp -r ${SYSwin64} ~/K/Software/MathematicaTools/
	#-cp "$@" ~/K/Software/MathematicaTools/${SYSwin64}/sload-$(shell date +%Y%m%d).exe

	
###########################################
clean:
	-$(RM) *.o
	-$(RM) *.obj
	-$(RM) *tm.c
	-$(RM) ${SYSwin64}/sload.exe ${SYSwin32}/sload.exe
	-$(RM) ${SYS}/sload
	
