#------------------------------------------------------------------------------
#	Scid (Shane's Chess Information Database)
#	Copyright (C) 2000-2002, Shane Hudson (sgh@users.sourceforge.net)
#
#	Microsoft Visual C++ makefile for use with nmake.exe v1.62+ (VC++ 5.0+)
#       VC++ 6 and 7
#------------------------------------------------------------------------------
#
#	Usage:
#
#	1) Targets are:
#		release	-- Builds scid.exe and scid.gui (default).
#		all	-- Builds everything.
#		clean	-- removes the contents of $(TMP_DIR).
#		hose	-- removes the contents of $(TMP_DIR) and $(OUT_DIR).
#		rebuild	-- first cleans then builds release.
#
#
#	2) Macros usable on the commandline:
#		LANG=<language>
#			-- This allows you to build the GUI with only english
#			   and one other language
#			<language>
#				portbr		-- Brazilian Portuguese
#				czech		-- Czech
#				deutsch		-- German
#				english		-- English only
#				spanish		-- Spanish
#				francais	-- French
#				hungary		-- Hungarian
#				italian		-- Italian
#				nederlan	-- Dutch
#				norsk		-- Norwegian
#				polish		-- Polish
#				russian		-- Russian
#				serbian		-- Serbian
#				swedish		-- Swedish
#
#		DEBUG=1
#			-- Builds debug versions of scid.exe and scid.gui.
#
#		GLOBAL_OPT=1
#			-- Enables 'Whole Program Optimization' (VC 7.0+)
#
#		TMP_DIR=<path>
#		OUT_DIR=<path>
#			-- Allows the intermediate and output directories to be
#			   changed.
#			   $(OUT_DIR) is assumed to be .\(Release|Debug) based
#			   on if debug is requested or not.
#			   $(TMP_DIR) will be $(OUT_DIR)\Temp by default.
#
#		TCL_DIR=<path>
#			-- Allows the Tcl directory location to be changed. If
#			   this is not set, the makefile will attempt to locate
#			   Tcl in its default install directory.
#			   "C:\Program Files\Tcl"
#
#	3) Examples:
#
#	Basic syntax of calling nmake looks like this:
#	nmake -f makefile.vc [target|macrodef [target|macrodef] [...]]
#
#	Release
#		c:\scid_src>nmake -f makefile.vc release
#
#	Release with English and French only
#		c:\scid_src>nmake -f makefile.vc release LANG=francais
#
#	Release with Tcl in a non-default directory
#		c:\scid_src>nmake -f makefile.vc release TCL_DIR="C:\Temp\Tcl"
#
#	Everything with Tcl and Tk in a non-default directory
#		c:\scid_src>nmake -f makefile.vc all TCL_DIR="E:\Tcl" TK_DIR="E:\Tcl"
#------------------------------------------------------------------------------

!message ===============================================================================

#--------------------------------------------------------------------
# Environment Setup
#--------------------------------------------------------------------

##	Check if MSVCDIR has been set
#
!if "$(MSVCDIR)" == ""
MSG = ^
*** You'll need to run vcvars32.bat from Developer Studio, first, to setup^
    the environment.
!error $(MSG)
!endif

##	Check if in base directory
#
!if !exist("makefile.vc")
MSG = ^
*** You must run this makefile only from the directory it is in.^
    Please `cd` to its location first.
!error $(MSG)
!endif

##	Set compiler and linker
#
CC		= @cl
LD		= @link
RC		= @rc


##	Set Output and Intermediate directory
#
!if "$(OUT_DIR)" == ""
!if "$(DEBUG)" == "1"
OUT_DIR		= .\Debug
!else
OUT_DIR		= .\Release
!endif
!endif
!message *** Output directory		= '$(OUT_DIR)'

!ifndef TMP_DIR
TMP_DIR		= $(OUT_DIR)\Temp
!endif
!message *** Intermediate directory	= '$(TMP_DIR)'

##	TCL_VERSION: Tcl/Tk version
#	This should be "84" for Tcl/Tk 8.4, etc.
#
TCL_VERSION	= 84

##	SCID_INCLUDES: included files from Tcl/Tk
#	This will try to find your Tcl/Tk installation.
#
!ifndef TCL_DIR
!if exist("C:\Program Files\Tcl\include\tcl.h")
TCL_DIR		= C:\Program Files\Tcl
!else
MSG=^
*** Don't know where Tcl is.  Set the TCL_DIR macro.
!error $(MSG)
!endif
!else
!if exist("$(TCL_DIR)\include\tcl.h")
!elseif exist("$(TCL_DIR)\generic\tcl.h")
MSG =^
*** Tcl source found.  The TCL_DIR macro must point to the installed version.
!error $(MSG)
!else
MSG =^
*** Don't know where Tcl is.  The TCL_DIR macro doesn't appear correct.
!error $(MSG)
!endif
!endif
TCL_INCLUDES	= -I"$(TCL_DIR)\include"
!message *** Tcl directory		= '$(TCL_DIR)'

!ifndef TK_DIR
!if exist("C:\Program Files\Tcl\include\tk.h")
TK_DIR		= C:\Program Files\Tcl
!else
MSG=^
*** Don't know where Tk is.  Set the TK_DIR macro.
!error $(MSG)
!endif
!else
!if exist("$(TK_DIR)\include\tk.h")
!elseif exist("$(TK_DIR)\generic\tk.h")
MSG =^
*** Tk source found.  The TK_DIR macro must point to the installed version.
!error $(MSG)
!else
MSG =^
*** Don't know where Tk is.  The TK_DIR macro doesn't appear correct.
!error $(MSG)
!endif
!endif
TK_INCLUDES	= -I"$(TCL_DIR)\include"
!message *** Tk directory		= '$(TK_DIR)'

SCID_INCLUDES	= $(TCL_INCLUDES) $(TK_INCLUDES)

!if !exist("$(TCL_DIR)\lib\tcl$(TCL_VERSION).lib")
MSG =^
*** The file tcl$(TCL_VERSION).lib appears to be missing or the wrong version.
!error $(MSG)
!endif
TCL_LIB		= "$(TCL_DIR)\lib\tcl$(TCL_VERSION).lib"

!if !exist("$(TCL_DIR)\lib\tk$(TCL_VERSION).lib")
MSG =^
*** The file tk$(TCL_VERSION).lib appears to be missing or the wrong version.
!error $(MSG)
!endif
TK_LIB		= "$(TCL_DIR)\lib\tk$(TCL_VERSION).lib" $(TCL_LIB)

##	SCID_FLAGS: Scid customization flags.
#	Use -DZLIB if your system does not have zlib and you need
#	to include the code in the src/zlib directory.
#	The default is to use the system zlib library.
#
SCID_FLAGS	= -DZLIB

##	SCID_TB: Tablebase support.
#	for no tablebase reading capability.
#		Use:	SCID_TB =
#	for tablebase capability.
#		Use:	SCID_TB = -DSCID_USE_TB
#	for tablebase capability including 4-1
#	(King + 3 pieces vs lone king) tablebases.
#		Use:	SCID_TB = -DSCID_USE_TB -DT41_INCLUDE
#
#	Note: With 4-1 (King + 3 pieces vs lone King) tablebases,
#	older MS compilers need a "/Zm" option to tell them to use
#	more memory when compiling.
#
SCID_TB		= -DSCID_USE_TB -DT41_INCLUDE -Zm101

##	SCID_EXECS: all the executable programs compiled from C++ files.
#	Note: only "scid.exe" is compiled by default.
#
SCID_EXECS = \
	eco2epd.exe \
	pgnscid.exe \
	scid.exe \
	scidlet.exe \
	scidt.exe \
	scmerge.exe \
	tcscid.exe \
	tkscid.exe

##	ZLIB_OBJS: obj. files in the zlib compression library.
#
ZLIB_OBJS = \
	$(TMP_DIR)\adler32.obj \
	$(TMP_DIR)\compress.obj \
	$(TMP_DIR)\crc32.obj \
	$(TMP_DIR)\gzio.obj \
	$(TMP_DIR)\uncompr.obj \
	$(TMP_DIR)\deflate.obj \
	$(TMP_DIR)\trees.obj \
	$(TMP_DIR)\zutil.obj \
	$(TMP_DIR)\inflate.obj \
	$(TMP_DIR)\infblock.obj \
	$(TMP_DIR)\inftrees.obj \
	$(TMP_DIR)\infcodes.obj \
	$(TMP_DIR)\infutil.obj \
	$(TMP_DIR)\inffast.obj

##	SCID_OBJS: not all the .obj files that make up Scid, just the standard
#	files that most of the Scid programs use.
#
SCID_OBJS = \
	$(ZLIB_OBJS) \
	$(TMP_DIR)\bytebuf.obj \
	$(TMP_DIR)\date.obj \
	$(TMP_DIR)\dstring.obj \
	$(TMP_DIR)\game.obj \
	$(TMP_DIR)\gfile.obj \
	$(TMP_DIR)\index.obj \
	$(TMP_DIR)\matsig.obj \
	$(TMP_DIR)\mfile.obj \
	$(TMP_DIR)\misc.obj \
	$(TMP_DIR)\movelist.obj \
	$(TMP_DIR)\myassert.obj \
	$(TMP_DIR)\namebase.obj \
	$(TMP_DIR)\pgnparse.obj \
	$(TMP_DIR)\position.obj \
	$(TMP_DIR)\stored.obj \
	$(TMP_DIR)\stralloc.obj \
	$(TMP_DIR)\textbuf.obj \
	$(TMP_DIR)\scid.res

##	SCID_XOBJS: all the extra .obj files that make up Scid.
#
SCID_XOBJS = \
	$(SCID_OBJS) \
	$(TMP_DIR)\crosstab.obj \
	$(TMP_DIR)\engine.obj \
	$(TMP_DIR)\filter.obj \
	$(TMP_DIR)\optable.obj \
	$(TMP_DIR)\pbook.obj \
	$(TMP_DIR)\probe.obj \
	$(TMP_DIR)\recog.obj \
	$(TMP_DIR)\spellchk.obj \
	$(TMP_DIR)\tree.obj

##	SCID_LANG: all the extra language files for Scid.
#
!if "$(LANG)" == "english"
SCID_LANG	= tcl\lang\$(LANG).tcl
!message *** Language file		= '$(LANG)'
!elseif exist("tcl\lang\$(LANG).tcl")
SCID_LANG	= tcl\lang\english.tcl tcl\lang\$(LANG).tcl
!message *** Language file		= '$(LANG)'
!else
SCID_LANG = \
	tcl\lang\english.tcl \
	tcl\lang\portbr.tcl \
	tcl\lang\czech.tcl \
	tcl\lang\deutsch.tcl \
	tcl\lang\spanish.tcl \
	tcl\lang\francais.tcl \
	tcl\lang\hungary.tcl \
	tcl\lang\italian.tcl \
	tcl\lang\nederlan.tcl \
	tcl\lang\norsk.tcl \
	tcl\lang\polish.tcl \
	tcl\lang\russian.tcl \
	tcl\lang\serbian.tcl \
	tcl\lang\swedish.tcl
!if "$(LANG)" == ""
!message *** Language file		= all
!else
!message
!message *** Language file not found, defaulting to 'all'.
!message
!message *** Language file		= all
!endif
!endif

##	SCID_TCLS: Setup the .tcl files for scid
#
SCID_TCLS = \
  tcl\start.tcl \
  tcl\bitmaps.tcl \
  tcl\language.tcl \
  tcl\utils.tcl \
    tcl\utils\date.tcl tcl\utils\font.tcl tcl\utils\graph.tcl tcl\utils\history.tcl \
    tcl\utils\pane.tcl tcl\utils\sound.tcl tcl\utils\string.tcl tcl\utils\tooltip.tcl \
    tcl\utils\validate.tcl tcl\utils\win.tcl \
  tcl\misc\misc.tcl tcl\htext.tcl \
  tcl\contrib\combobox.tcl \
  tcl\file.tcl \
    tcl\file\finder.tcl tcl\file\bookmark.tcl tcl\file\recent.tcl tcl\file\epd.tcl \
    tcl\file\spellchk.tcl tcl\file\maint.tcl \
  tcl\edit.tcl \
  tcl\game.tcl \
    tcl\game\browser.tcl \
  tcl\windows.tcl \
    tcl\windows\gamelist.tcl tcl\windows\pgn.tcl \
    tcl\windows\comment.tcl tcl\windows\eco.tcl \
    tcl\windows\stats.tcl tcl\windows\tree.tcl tcl\windows\crosstab.tcl \
    tcl\windows\pfinder.tcl tcl\windows\tourney.tcl tcl\windows\switcher.tcl \
  tcl\search\search.tcl \
    tcl\search\board.tcl tcl\search\header.tcl tcl\search\material.tcl \
  tcl\contrib\ezsmtp\ezsmtp.tcl \
    tcl\tools\email.tcl \
    tcl\tools\import.tcl \
    tcl\tools\optable.tcl tcl\tools\preport.tcl tcl\tools\pinfo.tcl \
    tcl\tools\analysis.tcl tcl\tools\wbdetect.tcl \
    tcl\tools\reper.tcl tcl\tools\graphs.tcl tcl\tools\tablebase.tcl tcl\tools\ptracker.tcl \
  tcl\help\help.tcl tcl\help\tips.tcl \
  tcl\menus.tcl tcl\board.tcl tcl\move.tcl tcl\main.tcl \
    tcl\lang\english.tcl $(SCID_LANG) \
  tcl\end.tcl


#--------------------------------------------------------------------
# Compile flags
#--------------------------------------------------------------------

##	OPTIMIZE: Optimization
#	-O2	:Maximize Speed
#	-Op	:Improve Float Consistency
#	-GL	:Whole Program Optimization
#	-QI0f	:Enable Pentium 0x0f Fix
#
!if "$(DEBUG)" == "1"
OPTIMIZE	= -ZI -Fd$(TMP_DIR)\ -Od
!message *** Build 'Debug' Version	= yes
!else
OPTIMIZE	= -O2 -Op -QI0f -G6
!message *** Build 'Release' Version	= yes
!if "$(GLOBAL_OPT)" == "1"
OPTIMIZE	= $(OPTIMIZE) -GL
!message *** Whole Program Optimization	= yes
!endif
!endif

##	WARNINGS: I always compile with all warnings on (-Wall)
#	Note: (-W2) is the default for MS compiler.
#
!if "$(DEBUG)" == "1"
WARNINGS	= -W3
!else
WARNINGS	= -W2
!endif

##	PROFILE: Set profile for compiling
#	-DNDEBUG		:Turn off debug code
#	-DWIN32			:Target Windows 32bit
#	-DWIN32_LEAN_AND_MEAN	:Speeds building times by excluding some less
#				 common APIs
#
!if "$(DEBUG)" == "1"
PROFILE		= -D_DEBUG -DDEBUG
!else
PROFILE		= -DNDEBUG
!endif
PROFILE		= $(PROFILE) -DWIN32 -DWIN32_LEAN_AND_MEAN

##	CFLAGS: C++ compiler flags
#	-c	:Compile Without linking
#
CFLAGS		= $(PROFILE) $(OPTIMIZE) $(WARNINGS) $(SCID_FLAGS) \
			$(SCID_INCLUDES) -c -nologo


#--------------------------------------------------------------------
# Link flags
#--------------------------------------------------------------------

##	LDFLAGS: C++ linker flags
#	-LTCG	:Link-time Code Generation
#
!if "$(DEBUG)" == "1"
LDFLAGS		= -debug:full
!else
LDFLAGS		= -opt:icf,3 -opt:nowin98
!if "$(GLOBAL_OPT)" == "1"
LDFLAGS		= $(LDFLAGS) -ltcg:status
!endif
!endif
LDFLAGS		= $(LDFLAGS) -nologo


!message ===============================================================================
!message


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

release:	setup scid.exe scid.gui
all:		setup $(SCID_EXECS) scid.gui

setup:
	@if not exist $(OUT_DIR)\nul \
		@echo *** Creating directory '$(OUT_DIR)'...
	@if not exist $(OUT_DIR)\nul mkdir $(OUT_DIR)
	@if not exist $(TMP_DIR)\nul \
		@echo *** Creating directory '$(TMP_DIR)'...
	@if not exist $(TMP_DIR)\nul mkdir $(TMP_DIR)

scid.gui: $(SCID_TCLS)
	@type $(SCID_TCLS) > $(OUT_DIR)\scid.gui

scmerge.exe: $(TMP_DIR)\scmerge.obj $(SCID_OBJS)
	@echo *** scmerge.exe
	$(LD) $(LDFLAGS) $(TMP_DIR)\scmerge.obj $(SCID_OBJS) \
		/out:$(OUT_DIR)\scmerge.exe

pgnscid.exe: $(TMP_DIR)\pgnscid.obj $(SCID_OBJS)
	@echo *** pgnscid.exe
	$(LD) $(LDFLAGS) $(TMP_DIR)\pgnscid.obj $(SCID_OBJS) \
		/out:$(OUT_DIR)\pgnscid.exe

scidt.exe: $(TMP_DIR)\scidt.obj $(SCID_OBJS)
	@echo *** scidt.exe
	$(LD) $(LDFLAGS) $(TMP_DIR)\scidt.obj $(SCID_OBJS) \
		/out:$(OUT_DIR)\scidt.exe

scidlet.exe: $(TMP_DIR)\scidlet.obj $(SCID_XOBJS)
	@echo *** scidlet.exe
	$(LD) $(LDFLAGS) $(TMP_DIR)\scidlet.obj $(SCID_XOBJS) \
		/out:$(OUT_DIR)\scidlet.exe

scid.exe: $(TMP_DIR)\scid.obj $(SCID_XOBJS)
	@echo *** scid.exe
	$(LD) $(LDFLAGS) $(TMP_DIR)\scid.obj $(SCID_XOBJS) $(TK_LIB) \
		/out:$(OUT_DIR)\scid.exe \
		/subsystem:windows /entry:mainCRTStartup

tcscid.exe: $(TMP_DIR)\tcscid.obj $(SCID_XOBJS)
	@echo *** tcscid.exe
	$(LD) $(LDFLAGS) $(TMP_DIR)\tcscid.obj $(SCID_XOBJS) $(TCL_LIB) \
		/out:$(OUT_DIR)\tcscid.exe

tkscid.exe: $(TMP_DIR)\tkscid.obj $(SCID_XOBJS)
	@echo *** tkscid.exe
	$(LD) $(LDFLAGS) $(TMP_DIR)\tkscid.obj $(SCID_XOBJS) $(TK_LIB) \
		/out:$(OUT_DIR)\tkscid.exe

eco2epd.exe: $(TMP_DIR)\eco2epd.obj $(TMP_DIR)\pbook.obj $(SCID_OBJS)
	@echo *** eco2epd.exe
	$(LD) $(LDFLAGS) $(TMP_DIR)\eco2epd.obj $(TMP_DIR)\pbook.obj \
		$(SCID_OBJS) /out:$(OUT_DIR)\eco2epd.exe


#--------------------------------------------------------------------
# Special case object file targets
#--------------------------------------------------------------------

$(TMP_DIR)\tcscid.obj: src\tkscid.cpp
	$(CC) $(CFLAGS) -DTCL_ONLY /Fo$@ $?

$(TMP_DIR)\tkscid.obj: src\tkscid.cpp
	$(CC) $(CFLAGS) /Fo$@ $?

$(TMP_DIR)\scid.obj: src\tkscid.cpp
	$(CC) $(CFLAGS) -DSOURCE_TCL_FILE="\"scid.gui\"" /Fo$@ $?

$(TMP_DIR)\probe.obj: src\probe.cpp src\egtb\tbindex.cpp src\egtb\tbdecode.c
	$(CC) $(CFLAGS) $(SCID_TB) src\probe.cpp /Fo$@


#--------------------------------------------------------------------
# Implicit rules
#--------------------------------------------------------------------

{src}.cpp{$(TMP_DIR)}.obj:
	$(CC) $(CFLAGS) -Fo$(TMP_DIR)\ $<

{src\egtb}.cpp{$(TMP_DIR)}.obj:
	$(CC) $(CFLAGS) -Fo$(TMP_DIR)\ $<

{src\egtb}.c{$(TMP_DIR)}.obj:
	$(CC) $(CFLAGS) -Fo$(TMP_DIR)\ $<

{src\zlib}.c{$(TMP_DIR)}.obj:
	$(CC) $(CFLAGS) -Fo$(TMP_DIR)\ $<
	
.rc{$(TMP_DIR)}.res:
	$(RC) -r -Fo$(TMP_DIR)\$(@B).res $<


#--------------------------------------------------------------------
# Clean up
#--------------------------------------------------------------------

!if "$(OS)" == "Windows_NT"
RMDIR		= rmdir /S /Q
!else
RMDIR		= deltree /Y
!endif

##	clean:
#	This will remove .obj files.
#
clean:
	@echo Cleaning...
	@echo.
	@if exist $(TMP_DIR)\nul @echo *** Removing directory '$(TMP_DIR)'...
	@if exist $(TMP_DIR)\nul $(RMDIR) $(TMP_DIR)
	@echo.
	@echo Finished
	@echo ===============================================================================

##	hose:
#	This will remove .exe and .obj files.
#
hose:
	@echo Hosing...
	@echo.
	@if exist $(OUT_DIR)\nul @echo *** Removing directory '$(OUT_DIR)'...
	@if exist $(OUT_DIR)\nul $(RMDIR) $(OUT_DIR)
	@echo.
	@echo Finished
	@echo ===============================================================================

##	rebuild:
#	This will clean and then rebuild .obj and .exe files.
#
rebuild:
	@echo Cleaning...
	@echo.
	@$(MAKE) -fmakefile.vc -$(MAKEFLAGS) clean -nologo > nul
	@echo Building...
	@echo.
	@$(MAKE) -fmakefile.vc -$(MAKEFLAGS) -nologo
	@echo.
	@echo Finished
	@echo ===============================================================================
