#
# The follwing targets are available:
#
#	make (does a make debug)
#	make clean (does a make debugclean)
#	make debug
#	make rellease
#	make debugclean
#	make releaseclean
#
####### Directory options

# we default to debug!!
TARGETDIR      	= ./Debug
OBJECTSDIR     	= ./Debug

ifdef DEBUG
	TARGETDIR      	= ./Debug
	OBJECTSDIR     	= ./Debug
endif
ifdef RELEASE
	TARGETDIR      	= ./Release
	OBJECTSDIR     	= ./Release
endif


####### Files
SOURCES			=	bind.c \
					columninfo.c \
					connection.c \
					convert.c \
					descriptor.c \
					dlg_specific.c \
					dlg_wingui.c \
					drvconn.c \
					environ.c \
					execute.c \
					info.c \
					info30.c \
					lobj.c \
					misc.c \
					multibyte.c \
					odbcapi.c \
					odbcapi30.c \
					odbcapi30w.c \
					odbcapiw.c \
					options.c \
					parse.c \
					pgapi30.c \
					pgtypes.c \
					psqlodbc.c \
					qresult.c \
					results.c \
					setup.c \
					socket.c \
					statement.c \
					tuple.c \
					tuplelist.c \
					win_md5.c \
					win_unicode.c

OBJECTS     	= $(patsubst %.c, $(OBJECTSDIR)/%.o, $(SOURCES))
RESOBJECT		= $(OBJECTSDIR)/psqlodbc_res.o
TARGET      	= $(TARGETDIR)/psqlodbc.dll

####### C/C++ Compiler options

CXX         	= gcc
# we default to debug!!!
CXXFLAGS    	= -c -O0 -g3 -Wall -fmessage-length=0 -mthreads -D _DEBUG -D WIN32 -D MD5_ODBC -D FRONTEND -D _WINDOWS -D _NBCS -D _USRDLL -D PSQLODBC_EXPORTS -D ODBCVER=0x0300 -D DRIVER_CURSOR_IMPLEMENT -D WIN_MULTITHREAD_SUPPORT
ifdef DEBUG
CXXFLAGS	= -c -O0 -g3 -Wall -fmessage-length=0 -mthreads -D _DEBUG -D WIN32 -D MD5_ODBC -D FRONTEND -D _WINDOWS -D _NBCS -D _USRDLL -D PSQLODBC_EXPORTS -D ODBCVER=0x0300 -D DRIVER_CURSOR_IMPLEMENT -D WIN_MULTITHREAD_SUPPORT
endif
ifdef RELEASE
CXXFLAGS    = -c -O3 -Wall -fmessage-length=0 -mthreads -D NDEBUG -D WIN32 -D MD5_ODBC -D FRONTEND -D _WINDOWS -D _NBCS -D _USRDLL -D PSQLODBC_EXPORTS -D ODBCVER=0x0300 -D DRIVER_CURSOR_IMPLEMENT -D WIN_MULTITHREAD_SUPPORT
endif
INCPATH     	=

####### Resource compiler

RC			= windres
RCFLAGS		= -v -l 0x809 -D NDEBUG -D MULTIBYTE -D _WIN32 -D WIN32

####### Linker options
#LINK        	= gcc
#LFLAGS      	= -mwindows -mthreads -shared -Wl,--enable-stdcall-fixup  psqlodbc_win32.def
#LIBS        	= -lwsock32 -lodbccp32 -lodbc32

LINK        = dllwrap
LFLAGS      = -mthreads -mwindows -Wl,--enable-stdcall-fixup --def psqlodbc_win32.def
LIBS        = -lwsock32 -lodbccp32 -lodbc32

####### Pattern matching rules

$(OBJECTSDIR)/%.o : %.c
	$(CXX) $(CXXFLAGS) $(INCPATH) $< -o $@

####### Targets

# we have to declare the targets "debug" and "release" phony: otherwise the directories would
# be the targets! see the manual of make: http://www.gnu.org/software/make/manual/html_mono/make.html#SEC41
.PHONY: release debug

all: compiledirs $(TARGET)
	 cp $(TARGET) /c/winnt/system32

debug:
	@echo "Going into debug mode"
	make -f Makefile.mingw -e DEBUG=1 all

debugclean:
	@echo "Going into debug mode"
	make -f Makefile.mingw -e DEBUG=1 clean

release:
	@echo "Going into release mode"
	make -f Makefile.mingw -e RELEASE=1 all

releaseclean:
	@echo "Going into release mode"
	make -f Makefile.mingw -e RELEASE=1 clean

$(TARGET) : .depend $(OBJECTS) $(RESOBJECT)
	$(LINK) $(LFLAGS) -o$(TARGET) $(OBJECTS) $(RESOBJECT) $(LIBS)

$(RESOBJECT) : psqlodbc.rc
	$(RC) $(RCFLAGS) $(INCPATH) -i $< -o $@

.depend:
	-rm -f .depend
	$(CXX) $(CXXFLAGS) $(INCPATH) -MM *.c | sed 's/^[^ ]/$$(OBJECTSDIR)\/&/' >> .depend

compiledirs:
	-mkdir $(TARGETDIR)
	-mkdir $(OBJECTSDIR)


clean:
	-rm .depend
	-rm $(OBJECTS)
	-rm $(RESOBJECT)
	-rm $(TARGET)

ifeq (.depend, $(wildcard .depend))
	include .depend
endif