#------------------------------------------------------------- -*- makefile -*-
#
# Makefile for TBDC MySQL interface
#
# Basic build, test and install
#   nmake /s /nologo /f makefile.vc INSTALLDIR=c:\path\to\installdir TCLDIR=c:\path\to\tcl\source
#   nmake /s /nologo /f makefile.vc INSTALLDIR=c:\path\to\installdir TCLDIR=c:\path\to\tcl\source test
#   nmake /s /nologo /f makefile.vc INSTALLDIR=c:\path\to\installdir TCLDIR=c:\path\to\tcl\source install
#
# For other build options (debug, static etc.)
# See TIP 477 (https://core.tcl.tk/tips/doc/trunk/tip/477.md) for
# detailed documentation.
# 
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
#------------------------------------------------------------------------------

PROJECT = tdbcmysql
# Tcl 8.6 etc. compile with /DUNICODE. TDBC pre-nmake reform compiled
# without -DUNICODE. Keep that behaviour for now. TBD
USE_WIDECHAR_API  = 0

!include "rules-ext.vc"

# nmakehelp -V <file> <tag> will search the file for tag, skips until a
#	number and returns all character until a character not in [0-9.ab]
#	is read.
!if [echo REM = This file is generated from Makefile.vc > versions.vc]
!endif
!if [echo TCL_VERSION_REQ = \>> versions.vc] \
   && [nmakehlp -V ..\configure.ac TCL_VERSION_REQ >> versions.vc]
!endif
!include "versions.vc"

PRJ_OBJS = \
	$(TMP_DIR)\tdbcmysql.obj \
	$(TMP_DIR)\mysqlStubInit.obj

PRJ_STUBOBJS = \
	$(TMP_DIR)\mysqlStubInit.obj

PRJHEADERS = \
	$(GENERICDIR)\fakemysql.h \
	$(GENERICDIR)\mysqlStubs.h

### tdbc
!ifndef TDBC_DOTVERSION
TDBC_DOTVERSION		= $(DOTVERSION)
!endif
TDBC_VERSION		= $(TDBC_DOTVERSION:.=)
# tdbc source folder is "../tdbc" in source distribution and
# "../tdbc1.0.0" in tcl8.6.0 bundled distribution
TDBC_DIR		= $(ROOT)\..\tdbc
!if !exist($(TDBC_DIR))
TDBC_DIR		= $(ROOT)\..\tdbc$(TDBC_DOTVERSION)
!endif
TDBC_GENERIC_DIR	= $(TDBC_DIR)\generic
### tdbc stub lib
TDBCSTUBLIBNAME		= tdbcstub$(TDBC_VERSION).lib
TDBCSTUBLIB 		= "$(_TCLDIR)\lib\$(TDBCSTUBLIBNAME)"
!if !exist($(TDBCSTUBLIB))
TDBCSTUBLIB 		= $(TDBC_DIR)\win\$(BUILDDIRTOP)\$(TDBCSTUBLIBNAME)
!endif
TDBC_LIB_FILE		= tdbc$(TDBC_VERSION).dll
TDBC_BIN_DIR = $(TDBC_DIR)/win/$(BUILDDIRTOP)

PRJ_INCLUDES	= -I"$(TDBC_GENERIC_DIR)"

!if !$(STATIC_BUILD)
PRJ_LIBS  = $(TDBCSTUBLIB)
!endif

# Disable standard targets because we have custom test target
DISABLE_STANDARD_TARGETS = 1
!include "$(_RULESDIR)\targets.vc"

setup: default-setup
install: default-install default-install-stubs default-install-docs-n
shell: default-shell
clean: default-clean
realclean: hose
hose: default-hose
distclean: realclean default-distclean

$(PRJ_OBJS): $(PRJHEADERS)

# The TIP 477 generation of pkgIndex.tcl from pkgIndex.tcl.in does not include
# all replacements below so define our own it.
pkgindex:   $(OUT_DIR)\pkgIndex.tcl
$(OUT_DIR)\pkgIndex.tcl: $(ROOT)\pkgIndex.tcl.in
	@nmakehlp -s << $** > $@
@PACKAGE_NAME@        $(PROJECT)
@PACKAGE_VERSION@     $(DOTVERSION)
@TCL_VERSION_REQ@     $(TCL_VERSION_REQ)
@PKG_LIB_FILE@        $(PRJLIBNAME)
<<

test: setup $(PROJECT)
        @set TCL_LIBRARY=$(TCL_LIBRARY:\=/)
        @set TCLLIBPATH=$(OUT_DIR_PATH:\=/)
	@set TDBC_LIBRARY=$(LIBDIR:\=/)
	@$(CPY) $(LIBDIR)\*.tcl $(OUT_DIR)
!if $(TCLINSTALL)
        @set PATH=$(_TCLDIR)\bin;$(PATH)
	$(DEBUGGER) $(TCLSH) "$(ROOT:\=/)/tests/all.tcl" $(TESTFLAGS)
!else
        @set PATH=$(_TCLDIR)\win\$(BUILDDIRTOP);$(PATH)
	$(DEBUGGER) $(TCLSH) "$(ROOT:\=/)/tests/all.tcl" $(TESTFLAGS) \
		-load "package ifneeded tdbc::mysql $(DOTVERSION) \
			{source {$(LIBDIR:\=/)/tdbcmysql.tcl};load {$(PRJLIB:\=/)} $(PROJECT)};\
		package ifneeded tdbc $(TDBC_DOTVERSION) \
			{source {$(TDBC_BIN_DIR:\=/)/tdbc.tcl};load {$(TDBC_BIN_DIR:\=/)/$(TDBC_LIB_FILE)} tdbc}"
!endif

