# This Makefile controls the spinapi build process on Windows. 
# This assumes the standard mingw distribution is installed. 
# In addition, it requires a few tools from the Microsoft (lib) to generate the appropriate import libraries. 
# The .dll and gcc import library can be generated without these tools.

# Name of the library
SPINAPI = spinapi

#windows only
VER_STRING_API=UserCompiled
VER_STRING_PTS=UserCompiled
DEFINES= -ggdb -Wall -DDLL_EXPORTS -DWINDOWS -Iddk_headers -I. -IFTD2XX -DVER_STRING_API="\"$(VER_STRING_API)\"" -DVER_STRING_PTS="\"$(VER_STRING_PTS)\""
LIBS=-lkernel32 -lsetupapi FTD2XX\i386\ftd2xx.lib


CC = gcc.exe
OBJS=spinapi.o util.o caps.o if.o usb.o driver-usb-windows.o
DLLTOOL=dlltool.exe

DRIVERNAME = $(SPINAPI)
DLLNAME = $(DRIVERNAME).dll
DEFNAME = $(DRIVERNAME).def
LIBANAME = lib$(DRIVERNAME).a

all: dll

linux-direct:
	@$(MAKE) -f Makefile.linux

# Rule to make object files from C files
%.o:%.c
	$(CC) $(CFLAGS) $(DEFINES) -c $< 
	
windows:$(OBJS) libdriver-windows.a

dll: windows
#make dll, def and .a for mingw
	-dllwrap --dlltool-name=$(DLLTOOL) --output-lib=$(LIBANAME) --output-def $(DEFNAME) --dllname=$(DLLNAME) --driver-name=$(CC) $(OBJS) $(LIBS) libdriver-windows.a
	-copy spinapi.dll ..\dll\spinapi.dll
	-copy libspinapi.a  ..\dll\libspinapi.a
	-copy spinapi.h ..\dll\spinapi.h
	-copy FTD2XX\i386\ftd2xx.dll ..\dll\ftd2xx.dll
	-copy FTD2XX\i386\ftd2xx.lib ..\dll\ftd2xx.lib
	-copy FTD2XX\ftd2xx.h ..\dll\ftd2xx.h
#generate lib for msvc
	-lib /machine:i386 /def:$(DEFNAME)
#copy lib files to dll folder down one level
	-copy spinapi.lib ..\dll\spinapi.lib
#copy the registers header to the dll folder for include.
	-copy registers.h ..\dll\registers.h
	
indent: 
	-indent *.c
	-indent *.h
	-del *.c~
	-del *.h~
	
clean:
	-del *.o
	-del *.bak
	-del *.dll
	-del spinapi.lib
	-del libspinapi.a
	-del libspinapi.exp
	-del spinapi.def

# This target makes a standalone usb program which can be used to test and benchmark the usb communication code
usb: $(OBJS) usbtest.o libdriver-windows.a
	$(CC) $(OBJS) usbtest.o fftw\FFTW2dll.a $(LIBS) -lwinmm libdriver-windows.a -ousbtest.exe		
