#------------------------------------------------------------- -*- makefile -*-
#
# Makefile for [Incr Tk] interface
#
# Basic build, test and install
#   nmake /f makefile.vc INSTALLDIR=c:\path\to\installdir
#   nmake /f makefile.vc INSTALLDIR=c:\path\to\installdir test
#   nmake /f makefile.vc INSTALLDIR=c:\path\to\installdir install
#
# For other build options (debug, static etc.),
# See TIP 477 (https://core.tcl-lang.org/tips/doc/main/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 = itk
# Tcl 8.6 etc. compile with /DUNICODE. ITK pre-nmake reform compiled
# without -DUNICODE. Keep that behaviour for now.
USE_WIDECHAR_API  = 0
NEED_TK = 1

PRJ_DEFINES	= $(PRJ_DEFINES) -Dinline=__inline
PRJ_DEFINES = $(PRJ_DEFINES)
PRJ_DEFINES = $(PRJ_DEFINES) -I$(TMP_DIR)

!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)\itkArchBase.obj \
	$(TMP_DIR)\itkArchetype.obj \
	$(TMP_DIR)\itkBase.obj \
	$(TMP_DIR)\itkCmd.obj \
	$(TMP_DIR)\itkHelpers.obj \
	$(TMP_DIR)\itkOption.obj \
	$(TMP_DIR)\itkUtil.obj \
	$(TMP_DIR)\itkStubInit.obj


# Uncomment the following line if you want to build and install stubs
PRJ_STUBOBJS = $(TMP_DIR)\itkStubLib.obj

PRJ_HEADERS = \
	$(GENERICDIR)\itkInt.h \
	$(GENERICDIR)\itk.h

# Locate ITCL headers and libraries.
# In principle, we don't need the ITCL source directory, and an installed
# ITCL should suffice. But that needs additional machinery not implemented
# yet.
!if [echo ITCL_DIR = \> nmakehlp.out] \
   || [nmakehlp -L generic\itcl.h >> nmakehlp.out]
!error Could not locate ITCL source directory.
!endif
# Locate TK headers and libraries.
# In principle, we don't need the TK source directory, and an installed
# TK should suffice. But that needs additional machinery not implemented
# yet.
!if [echo TK_DIR = \> nmakehlp.out] \
   || [nmakehlp -L generic\tk.h >> nmakehlp.out]
!error Could not locate TK source directory.
!endif
!include nmakehlp.out

# Get the ITCL version from its configure.ac
!if [echo ITCL_DOTVERSION = \> versions.vc] \
   && [nmakehlp -V "$(ITCL_DIR)\configure.ac" itcl >> versions.vc]
!error Could not retrieve ITCL version.
!endif
# Get the TK version from its configure.ac
!if [echo TK_DOTVERSION = \> versions.vc] \
   && [nmakehlp -V "$(TK_DIR)\configure.ac" tk >> versions.vc]
!error Could not retrieve TK version.
!endif
!include versions.vc

ITCL_VERSION     = $(ITCL_DOTVERSION:.=)
ITCL_GENERIC_DIR = $(ITCL_DIR)\generic
!if $(TCL_MAJOR_VERSION) == 8 || "$(TCL_BUILD_FOR)" == "8"
ITCLSTUBLIBNAME	 = itclstub$(ITCL_VERSION).lib
ITCL_LIB_FILE	 = itcl$(ITCL_VERSION).dll
!else
ITCLSTUBLIBNAME	 = itclstub.lib
ITCL_LIB_FILE	 = tcl9itcl$(ITCL_VERSION).dll
!endif
ITCLSTUBLIB	 = $(ITCL_DIR)\win\$(BUILDDIRTOP)\$(ITCLSTUBLIBNAME)
ITCL_BIN_DIR     = $(ITCL_DIR)/win/$(BUILDDIRTOP)

TK_VERSION     = $(TK_DOTVERSION:.=)
TK_GENERIC_DIR = $(TK_DIR)\generic
TK_XLIB_DIR = $(TK_DIR)\xlib
!if $(TCL_MAJOR_VERSION) == 8 || "$(TCL_BUILD_FOR)" == "8"
TKSTUBLIBNAME	 = tkstub$(TK_VERSION).lib
!else
TKSTUBLIBNAME	 = tkstub.lib
!endif
TKSTUBLIB	 = $(TK_DIR)\win\$(BUILDDIRTOP)\$(TKSTUBLIBNAME)
TK_BIN_DIR     = $(TK_DIR)/win/$(BUILDDIRTOP)

PRJ_INCLUDES	= -I"$(ITCL_GENERIC_DIR)" -I"$(TK_GENERIC_DIR)" -I"$(TK_XLIB_DIR)"

!if !$(STATIC_BUILD)
PRJ_LIBS  = $(ITCLSTUBLIB) $(TKSTUBLIB)
!endif

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

# Additional default actions for install
install: default-install-docs-n

# The TIP 477 generation of pkgIndex.tcl from pkgIndex.tcl.in does not include
# all replacements below so define our own target.
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)
@PKG_LIB_FILE8@       $(PRJLIBNAME8)
@PKG_LIB_FILE9@       $(PRJLIBNAME9)
<<

$(ROOT)\manifest.uuid:
   copy $(WIN_DIR)\gitmanifest.in $(ROOT)\manifest.uuid
   git rev-parse HEAD >>$(ROOT)\manifest.uuid

$(TMP_DIR)\itkUuid.h:	$(ROOT)\manifest.uuid
	copy $(WIN_DIR)\itkUuid.h.in+$(ROOT)\manifest.uuid $(TMP_DIR)\itkUuid.h

test: setup $(PROJECT)
	@echo testing itk
	@set ITK_LIBRARY=$(LIBDIR:\=/)
	@$(CPY) $(LIBDIR)\*.tcl $(OUT_DIR)
!if $(TKINSTALL)
	@set PATH=$(_TKDIR)\bin;$(PATH)
	$(DEBUGGER) $(WISH) "$(ROOT:\=/)/tests/all.tcl" $(TESTFLAGS)
!else
	@set PATH=$(_TKDIR)\win\$(BUILDDIRTOP);$(PATH)
	$(DEBUGGER) $(WISH) "$(ROOT:\=/)/tests/all.tcl" $(TESTFLAGS) \
		-load "package ifneeded itk $(DOTVERSION) \
			{source {$(LIBDIR:\=/)/itk.tcl};load {$(PRJLIB:\=/)} [string totitle $(PROJECT)]};\
		package ifneeded itcl $(ITCL_DOTVERSION) \
			{source {$(ITCL_BIN_DIR:\=/)/itcl.tcl};load {$(ITCL_BIN_DIR:\=/)/$(ITCL_LIB_FILE)} Itcl}"
!endif


# Explicit dependency rules
$(GENERICDIR)\itkBase.c : $(TMP_DIR)\itkUuid.h
