# Use MinGW Make to process this file. Y must provide your sytem system specific MATLABROOT
# variable on the command line e.g.:
#
# make -f Makefile_mingw64 MATLABROOT="C:\Program Files\MATLAB\R2011b" ARCH=win64
#
# REMARK: When compiling dll, all dependencies need to be resolved
# This is not the case when compiled as a static lib.

ifndef MATLABROOT
  $(warning MATLABROOT variable is undefined. Using default MATLABROOT="C:\Program Files\MATLAB\R2011b" )
  MATLABROOT=C:\Program Files\MATLAB\R2011b
endif

ifndef ARCH
  $(warning ARCH variable is undefined. Using default ARCH=win64 )
  ARCH=win64
endif

ifndef DFFTW
   DFFTW=-lfftw3-3
endif

ifndef SFFTW
   SFFTW=-lfftw3f-3
endif

MATLABLINKFLAGS=-L"$(MATLABROOT)\bin\$(ARCH)" $(MATLABLIBS) -lmwlapack -lmwblas


CFLAGS+=-g -std=c99 -I./thirdparty -I./

# the following adds parameters to CFLAGS
include comptarget.mk

DIRECTIVES=-DMATLABFORTRAN -DDLL_EXPORT_SYMBOLS 

include 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))

all: shared

shared: double_shared single_shared

shared_noblaslapack: double_shared_noblaslapack double_shared_noblaslapack

static: double_static single_static

double_shared: $(DFILES) $(DFILES_BLASLAPACK) Makefile_mingw
	$(CC) -shared -Wl,--dll $(DFILES) $(DFILES_BLASLAPACK)  \
	-o ../lib/ltfat.dll -static-libgcc -L../mex $(DFFTW) $(MATLABLINKFLAGS) \
    -Wl,--out-implib,../lib/libltfat_dll.a
	$(RM) *.o *.a
	$(CP) ..\lib\ltfat.dll ..\mex\
  
single_shared: $(SFILES) $(SFILES_BLASLAPACK) Makefile_mingw  
	$(CC) -shared -Wl,--dll $(SFILES) $(SFILES_BLASLAPACK)  \
	-o ../lib/ltfatf.dll -static-libgcc -L../mex $(DFFTW) $(SFFTW) -lltfat $(MATLABLINKFLAGS) \
    -Wl,--out-implib,../lib/libltfatf_dll.a
	$(RM) *.o *.a
	$(CP) ..\lib\ltfatf.dll ..\mex\
	
double_shared_noblaslapack: $(DFILES) Makefile_mingw
	$(CC) -shared -Wl,--dll $(DFILES) \
	-o ../lib/ltfat.dll -static-libgcc -L../mex $(DFFTW) \
    -Wl,--out-implib,../lib/libltfat_dll.a
	$(RM) *.o *.a
	$(CP) ..\lib\ltfat.dll ..\mex\
  
single_shared_noblaslapack: $(SFILES) Makefile_mingw  
	$(CC) -shared -Wl,--dll $(SFILES) \
	-o ../lib/ltfatf.dll -static-libgcc -L../mex $(DFFTW) $(SFFTW) -lltfat \
    -Wl,--out-implib,../lib/libltfatf_dll.a
	$(RM) *.o *.a
	$(CP) ..\lib\ltfatf.dll ..\mex\
	
double_static: $(DFILES) $(DFILES_BLASLAPACK) Makefile_mingw
	ar rvu libltfat.a $(DFILES) $(DFILES_BLASLAPACK)
	ranlib libltfat.a
	$(CP) libltfat.a ..\lib
	$(RM) *.o *.a	
	
single_static: $(SFILES) $(SFILES_BLASLAPACK) Makefile_mingw
	ar rvu libltfatf.a $(SFILES) $(SFILES_BLASLAPACK)
	ranlib libltfatf.a
	$(CP) libltfatf.a ..\lib
	$(RM) *.o *.a	

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) ..\lib\ltfat.dll
	$(RM) ..\lib\ltfatf.dll
	$(RM) ..\lib\libltfat.a
	$(RM) ..\lib\libltfatf.a
	$(RM) ..\lib\libltfat_dll.a
	$(RM) ..\lib\libltfatf_dll.a
	$(RM) ..\mex\ltfat.dll
	$(RM) ..\mex\ltfatf.dll
	$(RM) *.o *.a

