# Visual C++ 2.x and 4.0 makefile
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# 
# Copyright (c) 1995-1996 Sun Microsystems, Inc.
# SCCS: @(#) makefile.vc 1.8 98/07/29 13:14:52

# Does not depend on the presence of any environment variables in
# order to compile tcl; all needed information is derived from 
# location of the compiler directories.
#
# NOTE: Be sure to modify the "makefile.vc" file in the toplevel directory
#   for the itcl distribution.  Include the location of your VC++ development
#   tools and the installation directory.

!include "..\..\makefile.vc"


# ROOT    = top of source tree
#
# TMPDIR  = location where .obj files should be stored during build

ROOT		= ..
ITCLDIR     = ..\..\itcl

######################################################################
# Do not modify below this line
######################################################################

ITKNAMEPREFIX  = itk
WISHNAMEPREFIX = itkwish
VERSION	       = 31
DOTVERSION     = 3.1
STUBPREFIX = $(ITKNAMEPREFIX)stub

BINROOT		= .
!IF "$(NODEBUG)" == "1"
TMPDIR		= $(BINROOT)\Release
DBGX		=
!ELSE
TMPDIR		= $(BINROOT)\Debug
DBGX		= d
!ENDIF
OUTDIR		= $(TMPDIR)

ITKDLLNAME	= $(ITKNAMEPREFIX)$(VERSION)$(DBGX).dll
ITKDLL 		= $(OUTDIR)\$(ITKDLLNAME)
ITKLIB 		= $(OUTDIR)\$(ITKNAMEPREFIX)$(VERSION)$(DBGX).lib

ITKWISH 	= $(OUTDIR)\$(WISHNAMEPREFIX)$(VERSION)$(DBGX).exe
DUMPEXTS 	= $(TMPDIR)\dumpexts.exe

ITKSTUBLIBNAME = $(STUBPREFIX)$(VERSION)$(DBGX).lib
ITKSTUBLIB     = $(OUTDIR)\$(ITKSTUBLIBNAME)


LIB_INSTALL_DIR	= $(INSTALLDIR)\lib
BIN_INSTALL_DIR	= $(INSTALLDIR)\bin
SCRIPT_INSTALL_DIR	= $(INSTALLDIR)\lib\itk$(DOTVERSION)
INCLUDE_INSTALL_DIR	= $(INSTALLDIR)\include

ITKWISHOBJS = \
	$(TMPDIR)\winMain.obj

ITKOBJS = \
	$(TMPDIR)\itkStubInit.obj \
	$(TMPDIR)\itk_archetype.obj \
	$(TMPDIR)\itk_cmds.obj \
	$(TMPDIR)\itk_option.obj \
	$(TMPDIR)\itk_util.obj \
	$(TMPDIR)\dllEntryPoint.obj

ITKSTUBOBJS = \
    $(TMPDIR)\itkStubLib.obj

cc32		= $(TOOLS32)\bin\cl.exe
link32		= $(TOOLS32)\bin\link.exe
rc32		= $(TOOLS32_rc)\bin\rc.exe
include32	= -I$(TOOLS32)\include
lib32       = "$(TOOLS32)\bin\lib.exe"

WINDIR      = $(ROOT)\win
GENERICDIR	= $(ROOT)\generic
RCDIR       = $(WINDIR)\rc

TCLBUILDDIR	= $(TCLDIR)\win\$(OUTDIR)
TCLLIB      = $(TCLBUILDDIR)\tcl81$(DBGX).lib
TCLSTUBLIB      = $(TCLBUILDDIR)\tclstub81$(DBGX).lib
TCLDLL      = $(TCLBUILDDIR)\tcl81$(DBGX).dll
TKBUILDDIR	= $(TKDIR)\win\$(OUTDIR)
TKLIB       = $(TKBUILDDIR)\tk81$(DBGX).lib
TKSTUBLIB       = $(TKBUILDDIR)\tkstub81$(DBGX).lib
TKDLL       = $(TKBUILDDIR)\tk81$(DBGX).dll
ITCLBUILDDIR = $(ITCLDIR)\win\$(OUTDIR)
ITCLLIB      = $(ITCLBUILDDIR)\itcl$(VERSION)$(DBGX).lib
ITCLDLL      = $(ITCLBUILDDIR)\itcl$(VERSION)$(DBGX).dll

ITCLSTUBLIBNAME = itclstub$(VERSION)$(DBGX).lib
ITCLSTUBLIB     = $(ITCLBUILDDIR)\$(ITCLSTUBLIBNAME)

ITK_INCLUDES	= -I$(WINDIR) -I$(GENERICDIR) \
			-I$(TCLDIR)\generic -I$(TKDIR)\generic \
			-I$(TCLDIR)\win -I$(TKDIR)\win \
			-I$(TKDIR)\xlib -I$(ITCLDIR)\generic

ITK_EXE_DEFINES	= -DBUILD_itk $(DEBUGDEFINES)

ITK_EXE_CFLAGS	= $(cdebug) $(cflags) $(cvarsdll) $(include32) \
			$(ITK_INCLUDES) $(ITK_EXE_DEFINES) 

ITK_CFLAGS	= $(ITK_EXE_CFLAGS) -DUSE_TK_STUBS -DUSE_TCL_STUBS \
		         -DUSE_ITCL_STUBS

######################################################################
# Link flags
######################################################################

!IF "$(NODEBUG)" == "1"
ldebug = /RELEASE
!ELSE
ldebug = -debug:full -debugtype:cv
!ENDIF

# declarations common to all linker options
lcommon = /NODEFAULTLIB /RELEASE /NOLOGO

# declarations for use on Intel i386, i486, and Pentium systems
!IF "$(MACHINE)" == "IX86"
DLLENTRY = @12
lflags   = $(lcommon) /MACHINE:$(MACHINE)
!ELSE
lflags   = $(lcommon) /MACHINE:$(MACHINE)
!ENDIF

conlflags = $(lflags) -subsystem:console -entry:mainCRTStartup
guilflags = $(lflags) -subsystem:windows -entry:WinMainCRTStartup
dlllflags = $(lflags) -entry:_DllMainCRTStartup$(DLLENTRY) -dll

!IF "$(MACHINE)" == "PPC"
libc = libc.lib
libcdll = crtdll.lib
!ELSE
libc = libc.lib oldnames.lib
libcdll = msvcrt.lib oldnames.lib
!ENDIF

baselibs   = kernel32.lib $(optlibs) advapi32.lib
winlibs    = $(baselibs) user32.lib gdi32.lib comdlg32.lib winspool.lib
guilibs	   = $(libc) $(winlibs)

guilibsdll = $(libcdll) $(winlibs)

######################################################################
# Compile flags
######################################################################

!IF "$(NODEBUG)" == "1"
!IF "$(MACHINE)" == "ALPHA"
# MSVC on Alpha doesn't understand -Ot
cdebug = -O2i -Gs -GD -DNDEBUG
!ELSE
cdebug = -Oti -Gs -GD -DNDEBUG
!ENDIF
!ELSE
cdebug = -Z7 -Od -WX -DDEBUG
!ENDIF

# declarations common to all compiler options
ccommon = -c -W3 -nologo -Fp$(TMPDIR)\ -YX

!IF "$(MACHINE)" == "IX86"
cflags = $(ccommon) -D_X86_=1
!ELSE
!IF "$(MACHINE)" == "MIPS"
cflags = $(ccommon) -D_MIPS_=1
!ELSE
!IF "$(MACHINE)" == "PPC"
cflags = $(ccommon) -D_PPC_=1
!ELSE
!IF "$(MACHINE)" == "ALPHA"
cflags = $(ccommon) -D_ALPHA_=1
!ENDIF
!ENDIF
!ENDIF
!ENDIF

cvars      = -DWIN32 -D_WIN32
cvarsmt    = $(cvars) -D_MT
cvarsdll   = $(cvarsmt) -D_DLL

!IF "$(NODEBUG)" == "1"
cvarsdll   = $(cvars) -MD
!ELSE
cvarsdll   = $(cvars) -MDd
!ENDIF

CON_CFLAGS	= $(cdebug) $(cflags) $(cvars) $(include32) -DCONSOLE

######################################################################
# Project specific targets
######################################################################

all:    setup $(ITKWISH) $(ITKDLL) $(ITKSTUBLIB)
test:	setup $(ITKDLL) $(ITKSTUBLIB) $(ITKWISH)
	-@copy $(TCLDLL) $(TMPDIR)
	-@copy $(TKDLL) $(TMPDIR)
	-@copy $(ITCLDLL) $(TMPDIR)
#	$(ITKWISH)

setup:
	if not exist $(TMPDIR) mkdir $(TMPDIR)
	if not exist $(OUTDIR) mkdir $(OUTDIR)

$(ITKLIB) : $(ITKDLL) $(ITKSTUBLIB)
$(ITKDLL) : $(ITKOBJS) $(TMPDIR)\itk.res $(TMPDIR)\itkvc.def
	set LIB=$(TOOLS32)\lib
       $(link32) $(ldebug) $(dlllflags) -def:$(TMPDIR)\itkvc.def \
		-out:$@ $(TMPDIR)\itk.res $(TCLSTUBLIB)  \
                $(TKSTUBLIB) $(guilibsdll) @<<
			$(ITKOBJS)
<<


$(ITKSTUBLIB): $(ITKSTUBOBJS)
    $(lib32) -nologo -out:$@ $(ITKSTUBOBJS)

$(ITKWISH): $(ITKWISHOBJS) $(ITKLIB) $(TMPDIR)\itkwish.res
	set LIB=$(TOOLS32)\lib
	$(link32) $(ldebug) $(guilflags) $(TMPDIR)\itkwish.res -out:$@ \
		$(guilibsdll) $(TCLLIB) $(TKLIB) $(ITCLLIB) $(ITKLIB) \
		$(ITKWISHOBJS) 

$(TMPDIR)\itkvc.def: $(DUMPEXTS) $(ITKOBJS)
	$(DUMPEXTS) -o $@ $(ITKDLLNAME) @<<
		$(ITKOBJS)
<<

$(DUMPEXTS): $(TCLDIR)\win\winDumpExts.c
	$(cc32) $(CON_CFLAGS) -Fo$(TMPDIR)\ $?
	set LIB=$(TOOLS32)\lib
	$(link32) $(ldebug) $(conlflags) $(guilibs) -out:$@ \
		$(TMPDIR)\winDumpExts.obj 

install: all
	if not exist $(INSTALLDIR) mkdir $(INSTALLDIR)
	if not exist $(BIN_INSTALL_DIR) mkdir $(BIN_INSTALL_DIR)
	if not exist $(LIB_INSTALL_DIR) mkdir $(LIB_INSTALL_DIR)
	if not exist $(SCRIPT_INSTALL_DIR) mkdir $(SCRIPT_INSTALL_DIR)
	if not exist $(INCLUDE_INSTALL_DIR) mkdir $(INCLUDE_INSTALL_DIR)
	copy $(ITKWISH) "$(BIN_INSTALL_DIR)"
	copy $(ITKDLL) "$(BIN_INSTALL_DIR)"
	copy $(ROOT)\generic\itk.h "$(INCLUDE_INSTALL_DIR)"
	copy $(ROOT)\library\*.* "$(SCRIPT_INSTALL_DIR)"
	copy $(ROOT)\win\*.tcl "$(SCRIPT_INSTALL_DIR)"

#
# Regenerate the stubs files.
#
genstubs:
    $(TCLSH) $(TCLDIR)\tools\genStubs.tcl $(GENERICDIR) \
        $(GENERICDIR)\itk.decls

#
# Special case object file targets
#

$(TMPDIR)\winMain.obj: $(ROOT)\win\winMain.c
	$(cc32) $(ITK_EXE_CFLAGS) -Fo$@ $?

# The following object is part of the stub library and should not
# be built as DLL objects but none of the symbols should be exported

$(TMPDIR)\itkStubLib.obj : $(GENERICDIR)\itkStubLib.c
    $(cc32) -DSTATIC_BUILD $(ITK_CFLAGS) -Fo$@ $?

#
# Implicit rules
#

{$(GENERICDIR)}.c{$(TMPDIR)}.obj:
	$(cc32) -DDLL_BUILD $(ITK_CFLAGS) -Fo$(TMPDIR)\ $<

{$(WINDIR)}.c{$(TMPDIR)}.obj:
	$(cc32) -DDLL_BUILD $(ITK_CFLAGS) -Fo$(TMPDIR)\ $<

{$(ROOT)\unix}.c{$(TMPDIR)}.obj:
	$(cc32) -DDLL_BUILD $(ITK_CFLAGS) -Fo$(TMPDIR)\ $<

{$(RCDIR)}.rc{$(TMPDIR)}.res:
	$(rc32) -fo $@ -r -i $(GENERICDIR) -i $(TCLDIR)\generic -i $(ITCLDIR)\generic $<

clean:
	-@del $(OUTDIR)\*.exp 
	-@del $(OUTDIR)\*.lib 
	-@del $(OUTDIR)\*.dll 
	-@del $(OUTDIR)\*.exe
	-@del $(OUTDIR)\*.pdb
	-@del $(TMPDIR)\*.pch
	-@del $(TMPDIR)\*.obj
	-@del $(TMPDIR)\*.res
	-@del $(TMPDIR)\*.def
	-@del $(TMPDIR)\*.exe
	-@rmdir $(OUTDIR)
	-@rmdir $(TMPDIR)
