# Makefile for SQLite3 ODBC Drivers
# using MinGW WIN64 cross compiler

MINGW =		/opt/mingw64/bin/x86_64-w64-mingw32-
CC =		$(MINGW)gcc
STRIP =		$(MINGW)strip
RC =		$(MINGW)windres
MAKENSIS =	makensis

DRV_VER=	$(shell cat VERSION)

CFLAGS=		-O2 -Wall -DNDEBUG=1 -DDRIVER_VER_INFO=\"$(DRV_VER)\"

ifeq ($(MSVCRT),70)
	CFLAGS += -D__MSVCRT_VERSION=0x0700
	LMSVCRT = -nodefaultlibs -lmingw32 -lgcc_eh -lmoldname -lmingwex -lgcc -lmsvcr70
endif
ifeq ($(MSVCRT),80)
	CFLAGS += -D__MSVCRT_VERSION=0x0800
	LMSVCRT = -nodefaultlibs -lmingw32 -lgcc_eh -lmoldname -lmingwex -lgcc -lmsvcr80
endif
ifeq ($(MSVCRT),90)
	CFLAGS += -D__MSVCRT_VERSION=0x0900
	LMSVCRT = -nodefaultlibs -lmingw32 -lgcc_eh -lmoldname -lmingwex -lgcc -lmsvcr90
endif
ifeq ($(MSVCRT),100)
	CFLAGS += -D__MSVCRT_VERSION=0x0A00
	LMSVCRT = -nodefaultlibs -lmingw32 -lgcc_eh -lmoldname -lmingwex -lgcc -lmsvcr100
endif
ifeq ($(LMSVCRT),)
	LMSVCRT = -lmsvcrt
endif

SQLITE_INC =	sqlite
SQLITE_LIB =	sqlite/libsqlite.a
SQLITE_FLAGS =	-DHAVE_LIBVERSION=1 \
		-DHAVE_ENCDEC=1 \
		-DHAVE_SQLITEATOF=1 \
		-DHAVE_SQLITEMPRINTF=1 \
		-DHAVE_SQLITETRACE=1

SQLITE3_INC =	sqlite3
SQLITE3_SRC =	sqlite3/src
SQLITE3_LIB =	sqlite3/libsqlite3.a
SQLITE3_FLAGS=	-DHAVE_SQLITE3COLUMNTABLENAME=1 \
		-DHAVE_SQLITE3COLUMNDATABASENAME=1 \
		-DHAVE_SQLITE3COLUMNORIGINNAME=1 \
		-DHAVE_SQLITE3LOADEXTENSION=1 \
		-DHAVE_SQLITE3PREPAREV2=1 \
		-DHAVE_SQLITE3VFS=1 \
		-DHAVE_SQLITE3PROFILE=1 \
		-DHAVE_SQLITE3CLOSEV2=1 \
		-DHAVE_SQLITE3STRNICMP=1 \
		-DHAVE_SQLITE3TABLECOLUMNMETADATA=1
SQLITE3_A10N =	sqlite3/sqlite3.c
SQLITE3_A10N_FLAGS = \
		-DWIN32=1 -DNDEBUG=1 -DNO_TCL -DTHREADSAFE=1 \
		-DSQLITE_ENABLE_COLUMN_METADATA=1 \
		-DSQLITE_DLL=1 \
		-DSQLITE_THREADSAFE=1 \
		-DSQLITE_OS_WIN=1 \
		-DSQLITE_ASCII=1 \
		-DSQLITE_SOUNDEX=1

ODBC_FLAGS =	-DHAVE_LONG_LONG=1 -DHAVE_SQLROWOFFSET=1 -DHAVE_SQLLEN=1 \
		-DHAVE_SQLULEN=1 -DHAVE_SQLROWCOUNT=1 -DHAVE_SQLSETPOSIROW=1 \
		-DPTRDIFF_T=ptrdiff_t
ODBC_LIB =	-lodbc

all:		sqliteodbc.dll sqlite3odbc$(SEEEXT).dll \
		sqlite3_mod_blobtoxy.dll \
		sqlite3_mod_impexp.dll \
		sqlite3_mod_csvtable.dll \
		sqlite3_mod_zipfile.dll \
		sqlite.exe \
		inst.exe instq.exe uninst.exe uninstq.exe \
		adddsn.exe remdsn.exe \
		addsysdsn.exe remsysdsn.exe $(SQLITE3_EXE)


all_no2:	sqlite3odbc$(SEEEXT).dll \
		sqlite3_mod_blobtoxy.dll \
		sqlite3_mod_impexp.dll \
		sqlite3_mod_csvtable.dll \
		sqlite3_mod_zipfile.dll \
		inst.exe instq.exe uninst.exe uninstq.exe \
		adddsn.exe remdsn.exe \
		addsysdsn.exe remsysdsn.exe $(SQLITE3_EXE)

sqliteodbc.o:	sqliteodbc.c sqliteodbc.h resource.h
		$(CC) $(CFLAGS) -mdll -c -I$(SQLITE_INC) $(ODBC_FLAGS) \
		    $(SQLITE_FLAGS) $(ADD_CFLAGS) sqliteodbc.c

sqliteodbcu.o:	sqliteodbc.c sqliteodbc.h resource.h
		$(CC) $(CFLAGS) -mdll -c -I$(SQLITE_INC) $(ODBC_FLAGS) \
		    $(SQLITE_FLAGS) $(ADD_CFLAGS) -o sqliteodbcu.o sqliteodbc.c

sqliteodbc.dll:		sqliteodbc.o sqliteodbcres.o
		$(CC) $(CFLAGS) -shared -Wl,--kill-at \
		    -Wl,--out-implib,libsqliteodbc.a -Wl,--strip-all \
		    -o sqliteodbc.dll \
		    sqliteodbc.o sqliteodbcres.o \
		    $(LMSVCRT) $(SQLITE_LIB) $(LMSVCRT) \
		    -lodbc32 -lodbccp32 -lgdi32 -lcomdlg32 \
		    -ladvapi32 -lshell32 -luser32 -lkernel32

sqlite.exe:		sqliteodbc.dll sqlite/src/minshell.c sqlite/sqlite.h \
			sqliteres.o
		$(CC) $(CFLAGS) $(SQLITE_FLAGS) -o sqlite.exe \
		    sqlite/src/minshell.c sqliteres.o -L. -lsqliteodbc \
		    $(SQLITE_LIB)
		$(STRIP) sqlite.exe

sqliteodbcu.dll:	sqliteodbcu.o sqliteodbcures.o
		$(CC) $(CFLAGS) -shared -Wl,--kill-at \
		    -Wl,--out-implib,libsqliteodbcu.a -Wl,--strip-all \
		    -o sqliteodbcu.dll \
		    sqliteodbcu.o sqliteodbcures.o \
		    $(LMSVCRT) $(SQLITE_LIB) $(LMSVCRT) \
		    -lodbc32 -lodbccp32 -lgdi32 -lcomdlg32 \
		    -ladvapi32 -lshell32 -luser32 -lkernel32

sqliteu.exe:		sqliteodbc.dll sqlite/src/minshell.c sqlite/sqlite.h \
			sqliteres.o
		$(CC) $(CFLAGS) $(SQLITE_FLAGS) -o sqliteu.exe \
		    sqlite/src/minshell.c sqliteres.o -L. -lsqliteodbcu \
		    $(SQLITE_LIB)
		$(STRIP) sqliteu.exe

sqliteodbcres.o:	sqliteodbc.rc resource.h
		$(RC) -o sqliteodbcres.o -I$(SQLITE_INC) sqliteodbc.rc

sqliteodbcures.o:	sqliteodbc.rc resource.h
		$(RC) -o sqliteodbcures.o -I$(SQLITE_INC) sqliteodbc.rc

resource.h:	resource.h.in
		VERS=`cat VERSION` ;\
		VERS_C=`echo $$VERS | sed -e 's/\([0-9]\+\)[.]\([0-9]\+\).*/\1,\2/g'` ;\
		sed -e 's/--VERS_C--/'$$VERS_C'/g' < resource.h.in | \
		sed -e 's/--VERS--/'$$VERS'/g' > resource.h

sqlite3a10n.o:	$(SQLITE3_A10N)
		$(CC) $(CFLAGS) -c -I$(SQLITE3_INC) $(SQLITE3_A10N_FLAGS) \
		    $(ADD_CFLAGS) -o sqlite3a10n.o $(SQLITE3_A10N)

sqlite3odbc.o:	sqlite3odbc.c sqlite3odbc.h resource3.h
		$(CC) $(CFLAGS) -c -I$(SQLITE3_INC) $(ODBC_FLAGS) \
		    $(SQLITE3_FLAGS) $(ADD_CFLAGS) sqlite3odbc.c

sqlite3odbcnw.o:	sqlite3odbc.c sqlite3odbc.h resource3.h
		$(CC) $(CFLAGS) -c -I$(SQLITE3_INC) $(ODBC_FLAGS) \
		    $(SQLITE3_FLAGS) $(ADD_CFLAGS) -DWITHOUT_WINTERFACE=1 \
		    -o sqlite3odbcnw.o sqlite3odbc.c

sqlite3odbc$(SEEEXT).dll:	sqlite3odbc.o sqlite3odbcres.o \
				$(SQLITE3_A10N_O)
		$(CC) $(CFLAGS) -shared -Wl,--kill-at \
		    -Wl,--out-implib,libsqlite3odbc.a -Wl,--strip-all \
		    -o sqlite3odbc$(SEEEXT).dll \
		    sqlite3odbc.o sqlite3odbcres.o $(SQLITE3_A10N_O) \
		    $(SQLITE3_DLL) $(LMSVCRT) \
		    -lodbc32 -lodbccp32 -lgdi32 -lcomdlg32 \
		    -ladvapi32 -lshell32 -luser32 -lkernel32
		$(STRIP) sqlite3odbc$(SEEEXT).dll

sqlite3odbc$(SEEEXT)nw.dll:	sqlite3odbcnw.o sqlite3odbcres.o \
				$(SQLITE3_A10N_O)
		$(CC) $(CFLAGS) -shared -Wl,--kill-at \
		    -Wl,--out-implib,libsqlite3odbcnw.a -Wl,--strip-all \
		    -o sqlite3odbc$(SEEEXT)nw.dll \
		    sqlite3odbcnw.o sqlite3odbcres.o $(SQLITE3_A10N_O) \
		    $(SQLITE3_DLL) $(LMSVCRT) \
		    -lodbc32 -lodbccp32 -lgdi32 -lcomdlg32 \
		    -ladvapi32 -lshell32 -luser32 -lkernel32
		$(STRIP) sqlite3odbc$(SEEEXT)nw.dll

sqlite3.exe:		sqlite3odbc$(SEEEXT).dll sqlite3/src/minshell.c \
			sqliteres.o
		$(CC) $(CFLAGS) -DUNICODE $(SQLITE3_FLAGS) -Isqlite3 \
		    -municode -o sqlite3.exe \
		    sqlite3/src/minshell.c sqliteres.o \
		    -L. -lsqlite3odbc$(SEEEXT) -lcomdlg32
		$(STRIP) sqlite3.exe

sqlite3odbcres.o:	sqlite3odbc.rc resource3.h
		$(RC) -o sqlite3odbcres.o -I$(SQLITE3_INC) sqlite3odbc.rc

resource3.h:	resource.h.in
		VERS=`cat VERSION` ;\
		VERS_C=`echo $$VERS | sed -e 's/\([0-9]\+\)[.]\([0-9]\+\).*/\1,\2/g'` ;\
		sed -e 's/--VERS_C--/'$$VERS_C'/g' < resource.h.in | \
		sed -e 's/--VERS--/'$$VERS'/g' > resource3.h

sqliteres.rc:
		@echo "ico ICON sqlite.ico" > sqliteres.rc

sqliteres.o:	sqliteres.rc
		$(RC) -o sqliteres.o sqliteres.rc

instres.o:	inst.rc
		$(RC) -o instres.o -I$(SQLITE3_INC) inst.rc

inst.exe:	inst.c instres.o
		$(CC) $(CFLAGS) $(ADD_CFLAGS) -mwindows -o inst.exe \
		    inst.c instres.o -lodbc32 -lodbccp32 -lkernel32 \
		    -luser32
		$(STRIP) inst.exe

instq.exe:	inst.exe
		cp -p inst.exe instq.exe

uninst.exe:	inst.exe
		cp -p inst.exe uninst.exe

uninstq.exe:	inst.exe
		cp -p inst.exe uninstq.exe

adddsnres.o:	adddsn.rc
		$(RC) -o adddsnres.o -I$(SQLITE3_INC) adddsn.rc

adddsn.exe:	adddsn.c adddsnres.o
		$(CC) $(CFLAGS) $(ADD_CFLAGS) -mwindows -o adddsn.exe \
		    adddsn.c adddsnres.o -lodbc32 -lodbccp32 -lkernel32 \
		    -luser32
		$(STRIP) adddsn.exe

remdsn.exe:	adddsn.exe
		cp -p adddsn.exe remdsn.exe

addsysdsn.exe:	adddsn.exe
		cp -p adddsn.exe addsysdsn.exe

remsysdsn.exe:	adddsn.exe
		cp -p adddsn.exe remsysdsn.exe

blobtoxyres.o:	blobtoxy.rc resource3.h
		$(RC) -o blobtoxyres.o -I$(SQLITE3_INC) blobtoxy.rc

blobtoxy.o:	blobtoxy.c
		$(CC) $(CFLAGS) -mdll -c -I$(SQLITE3_INC) -I$(SQLITE3_SRC) \
		    blobtoxy.c

sqlite3_mod_blobtoxy.dll:	blobtoxy.o blobtoxyres.o
		$(CC) $(CFLAGS) -shared -Wl,--kill-at \
		    -Wl,--strip-all -o sqlite3_mod_blobtoxy.dll \
		    blobtoxy.o blobtoxyres.o $(LMSVCRT) \
		    -lgdi32 -lcomdlg32 \
		    -ladvapi32 -lshell32 -luser32 -lkernel32

impexp.o:	impexp.c
		$(CC) $(CFLAGS) -mdll -c -I$(SQLITE3_INC) -I$(SQLITE3_SRC) \
		    impexp.c

sqlite3_mod_impexp.dll:	impexp.o
		$(CC) $(CFLAGS) -shared -Wl,--kill-at \
		    -Wl,--strip-all -o sqlite3_mod_impexp.dll \
		    impexp.o $(LMSVCRT) \
		    -lgdi32 -lcomdlg32 \
		    -ladvapi32 -lshell32 -luser32 -lkernel32

csvtable.o:	csvtable.c
		$(CC) $(CFLAGS) -mdll -c -I$(SQLITE3_INC) -I$(SQLITE3_SRC) \
		    csvtable.c

sqlite3_mod_csvtable.dll:	csvtable.o
		$(CC) $(CFLAGS) -shared -Wl,--kill-at \
		    -Wl,--strip-all -o sqlite3_mod_csvtable.dll \
		    csvtable.o $(LMSVCRT) \
		    -lgdi32 -lcomdlg32 \
		    -ladvapi32 -lshell32 -luser32 -lkernel32

zipfile.o:	zipfile.c
		$(CC) $(CFLAGS) -mdll -c -I$(SQLITE3_INC) -I$(SQLITE3_SRC) \
		    -Izlib zipfile.c

sqlite3_mod_zipfile.dll:	zipfile.o
		$(CC) $(CFLAGS) -shared -Wl,--kill-at \
		    -Wl,--strip-all -o sqlite3_mod_zipfile.dll \
		    zipfile.o -Lzlib -lz $(LMSVCRT) \
		    -lgdi32 -lcomdlg32 \
		    -ladvapi32 -lshell32 -luser32 -lkernel32

clean:
		rm -f *.o sqliteodbc*.dll sqlite3odbc$(SEEEXT).dll \
		    sqlite3_mod_blobtoxy.dll \
		    sqlite3_mod_impexp.dll \
		    sqlite3_mod_csvtable.dll \
		    sqlite3_mod_zipfile.dll \
		    *inst.exe *dsn.exe sqlite*.exe sqliteres.rc *~ \
		    core core.*
		rm -f resource.h resource3.h

