# This makefile cross-compiles the whole LTFAT for Matlab on Windows 32 and 64 bit
#
# It was tested with M cross environment http://mxe.cc/
#
# To cross-compile, one must define paths to Matlab (and other libs) in MATLIBS variable
# and a destination directory OUTDIR. The makefile creates a directory structure mirroring
# the tree structure of LTFAT i.e. it creates subdirectories mex, thirdparty/Playrec and
# thirdparty/PolygonClip
#
# The MATLIBS dir must contain:
#    libfftw3-3.dll,libfft3f-3.dll obtainable from http://www.fftw.org/install/windows.html (select correct 32 or 64 bit)
#   
#   libmex.dll, libmx.dll libmwblas.dll and libmwlapack.dll from a Windows
#    Matlab instalation path MATLAB/bin/{arch}, where arch is win64 or win32.
#
#   matrix.h, mex.h, tmwtypes.h from a Windows Matlab installation path
#   MATLAB/extern/include
#
#   portaudio_x64.dll - I am using a version compiled on Windows, but it should be
#   possible to cross-compile that too. Change the library name accordingly.
#
#   When cross compiling for 32bin windows, specify EXT=mexw32 in addition to other params.
#
#   !!!!!!!!!!!!!!!!!! NO SPACES IN PATHS !!!!!!!!!!!!!!!!!!!!!!!
#
#   Example 1: Cross compiling for 64 bit 
#
#   	make -f Makefile_crossmingw CROSS=x86_64-w64-mingw32- MATLIBS=/home/susnak/Dropbox/win64libs 
#   	   OUTDIR=/home/susnak/Dropbox/ltfat_win64 EXT=mexw64 PORTAUDIOLIB=portaudio_x64.dll
#
#   Example 2: Cross compiling for 32 bit 
#
#   	make -f Makefile_crossmingw CROSS=i686-pc-mingw32- MATLIBS=/home/susnak/Dropbox/win32libs 
#   	   OUTDIR=/home/susnak/Dropbox/ltfat_win32 EXT=mexw32 PORTAUDIOLIB=portaudio_x86.dll
#
#

ifndef MATLIBS
   MATLIBS=/home/susnak/Dropbox/win64libs
endif

ifndef OUTDIR
   OUTDIR=/home/susnak/Dropbox/ltfat_win64
endif

ifndef EXT
	EXT=mexw64
endif

ifndef CROSS
$(error CROSS variable should be set as a prefix to Mingw tools)
endif

CC=$(CROSS)gcc
LD=$(CROSS)ld
AR=$(CROSS)ar
MKDIR_P = mkdir -p
ifndef
PORTAUDIOLIB = portaudio_x64.dll
endif

include filedefs.mk
include ../mex/filedefs.mk
include ostools.mk

DFILES = $(addprefix d,$(files)) $(files_notypechange)
DFILES_BLASLAPACK = $(addprefix d,$(files_blaslapack))
SFILES = $(addprefix s,$(files)) 
SFILES_BLASLAPACK = $(addprefix s,$(files_blaslapack))

MEXBASE = $(MEXBASESAFE) $(MEXBASEMORE)
MEXS    = $(addsuffix .$(EXT),$(MEXBASE))

MEXCOMPFLAGS=-I$(MATLIBS) -DMATLAB_MEX_FILE
MEXLINKFLAGS=-static-libgcc -Wl,--dll -L$(MATLIBS) \
			    -lmex -lmx -lmwlapack -lmwblas 


CFLAGS=-03 -s -std=c99 -I./thirdparty/ -I./ -DMATLABFORTRAN -DDLL_EXPORT_SYMBOLS

all: clean makedirtree backlib $(MEXS) polygonclip playrec copyrest clean 

backlib: CFLAGS=-O3 -s -std=c99 -I./thirdparty/ -I./ -DMATLABFORTRAN -DDLL_EXPORT_SYMBOLS
backlib: backlib_double backlib_single

backlib_double: $(DFILES) $(DFILES_BLASLAPACK) Makefile_crossmingw
	$(CC) -shared -Wl,--dll -L$(MATLIBS) -lfftw3-3 -lmwlapack -lmwblas $(DFILES) $(DFILES_BLASLAPACK)  \
	-o $(OUTDIR)/mex/ltfat.dll -static-libgcc 

backlib_single: $(SFILES) $(SFILES_BLASLAPACK) Makefile_crossmingw  
	$(CC) -shared -Wl,--dll -L$(MATLIBS) -lfftw3-3 -lfftw3f-3 -lmwlapack -lmwblas -L$(OUTDIR)/mex -lltfat $(SFILES) $(SFILES_BLASLAPACK)  \
	-o $(OUTDIR)/mex/ltfatf.dll -static-libgcc 

$(MEXS): CFLAGS=-O3 -shared -s -Wall -std=c99 -I./thirdparty -I./ -fvisibility=hidden 
$(MEXS): %.$(EXT): ../mex/%.c 
	$(CC) $(CFLAGS) $(MEXCOMPFLAGS) $< -o $(OUTDIR)/mex/$@ -L$(OUTDIR)/mex -lfftw3-3 -lfftw3f-3 -lltfat -lltfatf $(MEXLINKFLAGS)

polygonclip: CFLAGS=-std=c99 -s -O3 -Wall -shared -DMATLAB_MEX_FILE
polygonclip:
	$(CC) $(CFLAGS) -I../thirdparty/PolygonClip -I../thirdparty/GPC -I$(MATLIBS) \
	  	../thirdparty/PolygonClip/PolygonClip.c ../thirdparty/GPC/gpc.c \
		-L$(MATLIBS) -lmex -lmx -static-libgcc \
	  	-o $(OUTDIR)/thirdparty/PolygonClip/PolygonClip.$(EXT)

playrec: CFLAGS=-static-libgcc -std=c99 -O3 -Wall -shared -DMATLAB_MEX_FILE -DHAVE_PORTAUDIO
playrec: 
	$(CC) $(CFLAGS) -I../thirdparty/Playrec -I./thirdparty -I$(MATLIBS) \
	  	../thirdparty/Playrec/mex_dll_core.c  ../thirdparty/Playrec/pa_dll_playrec.c \
	  	../thirdparty/Playrec/ltfatresample.c  \
		-L$(MATLIBS) -l:$(PORTAUDIOLIB) -lmex -lmx -static-libgcc \
	  	-o $(OUTDIR)/thirdparty/Playrec/playrec.$(EXT)


makedirtree:
	$(MKDIR_P) $(OUTDIR)
	$(MKDIR_P) $(OUTDIR)/mex
	$(MKDIR_P) $(OUTDIR)/thirdparty
	$(MKDIR_P) $(OUTDIR)/thirdparty/Playrec
	$(MKDIR_P) $(OUTDIR)/thirdparty/PolygonClip

copyrest:
	$(CP) $(MATLIBS)/libfftw3-3.dll $(OUTDIR)/mex
	$(CP) $(MATLIBS)/libfftw3f-3.dll $(OUTDIR)/mex
	$(CP) $(MATLIBS)/$(PORTAUDIOLIB) $(OUTDIR)/thirdparty/Playrec

s%.o: %.c config.h
	$(CC) $(CFLAGS) $(DIRECTIVES) -DLTFAT_SINGLE  -c $< -o s$*.o

d%.o: %.c config.h
	$(CC) $(CFLAGS) $(DIRECTIVES) -DLTFAT_DOUBLE  -c $< -o d$*.o

%.o: %.c
	$(CC) $(CFLAGS) $(DIRECTIVES) -DLTFAT_DOUBLE  -c $<

clean: 
	$(RM) ../mex/*.$(EXT)
	$(RM) *.o
	$(RM) *.a
	$(RM) ../thirdparty/Playrec/*.o
	$(RM) ../thirdparty/PolygonClip/*.o

.PHONY: all clean makedirtree copyrest
