### Windows Makefile for Scid vs. PC -- using MinGW environment
#
# make W64=1 to build for WIN64 (x64)
# [not] setting it properly will result in bad CFLAGS / failure
#
# Change TCL_DIR to meet your setup. E.g.:
#   make -f Makefile.mingw TCL_DIR=d:\Tcl
#
# If this is a cross-compile, e.g. you are on Linux,
# then specify CROSS to meet your setup. E.g.:
#   make -f Makefile.mingw CROSS=x86_64-w64-mingw32-
#  (do not omit the trailing '-' :  it _is_ necessary)
#

# default cross-toolchain prefixes :
ifeq ($(W64),1)
DEF_CROSS=x86_64-w64-mingw32-
else
#DEF_CROSS=i686-pc-mingw32-
DEF_CROSS=i686-w64-mingw32-
endif

# detect if running under unix by finding 'rm' in $PATH :
ifeq ($(wildcard $(addsuffix /rm,$(subst :, ,$(PATH)))),)
WINMODE=1
else
WINMODE=0
endif

ifeq ($(WINMODE),1)
# native windows setup :
UNLINK = del $(subst /,\,$(1))
CAT = type $(subst /,\,$(1))
TCL_DIR=c:\Tcl
else
# cross-compile setup :
UNLINK = $(RM) $(1)
CAT = cat $(1)
TCL_DIR=/opt/cross_tcl
ifeq ($(CROSS),)
CROSS=$(DEF_CROSS)
endif
endif

# Tcl/Tk version: 8.5 / 8.6 / ...
TCL_VERSION=8.5
TCL_VERSION_NODOT=$(subst .,,$(TCL_VERSION))

### Compiler and linker
CXX=$(CROSS)g++
CC=$(CROSS)gcc
RC=$(CROSS)windres
STRIP=$(CROSS)strip
LINK=$(CXX)


### TB: Using Nalimov tablebases with Scid. Use "TB = -DSCID_USE_TB" for
#      tablebase support, or just "TB = " for no tablebase capability.
#      Use "TB = -DSCID_USE_TB -DT41_INCLUDE" to include use of 4-1
#      (King + 3 pieces vs lone king) tablebases.

TB = -DSCID_USE_TB -DT41_INCLUDE

### SCIDFLAGS: 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.

SCIDFLAGS = -DZLIB

WARNINGS = -Wall

CFLAGS = -O2 $(WARNINGS) $(DEBUG)
CXXFLAGS = $(CFLAGS) $(SCIDFLAGS)
RCFLAGS = --output-format=coff

LDFLAGS = -static-libgcc -static-libstdc++

ifeq ($(W64),1)
CFLAGS += -m64
#CXXFLAGS+= -m64
LDFLAGS+= -m64
RCFLAGS+= --target=pe-x86-64
else
CFLAGS += -m32
#CXXFLAGS+= -m32
LDFLAGS+= -m32
RCFLAGS+= --target=pe-i386
endif

CQL_INCLUDE = -Isrc/cql -Isrc -I$(TCL_DIR)/include

### EXECS: all the evecutable programs compiled from C++ files.
EXECS= pgnscid.exe scidt.exe scid.exe tcscid.exe scmerge.exe eco2epd.exe scidlet.exe

### SCIDOBJS: not all the .o files that make up scid, just the standard
###   files that most of the scid programs use.

SCIDOBJS= src/misc.o src/index.o src/date.o src/namebase.o src/position.o \
      src/game.o src/gfile.o src/matsig.o src/bytebuf.o src/textbuf.o \
      src/myassert.o src/stralloc.o src/mfile.o src/dstring.o src/pgnparse.o \
      src/stored.o src/movelist.o \
      src/polyglot/attack.o src/polyglot/board.o src/polyglot/book.o \
      src/polyglot/book_make.o src/polyglot/book_merge.o src/polyglot/colour.o \
      src/polyglot/fen.o src/polyglot/game.o src/polyglot/hash.o \
      src/polyglot/io.o src/polyglot/line.o src/polyglot/list.o src/polyglot/main.o src/polyglot/move.o \
      src/polyglot/move_do.o src/polyglot/move_gen.o src/polyglot/move_legal.o src/polyglot/option.o \
      src/polyglot/parse.o src/polyglot/pgn.o src/polyglot/piece.o src/polyglot/random.o \
      src/polyglot/san.o src/polyglot/search.o src/polyglot/square.o src/polyglot/util.o

## SCID_XOBJS: all the extra .obj files that make up Scid.
SCID_XOBJS = src/crosstab.o \
      src/engine.o \
      src/filter.o \
      src/optable.o \
      src/pbook.o \
      src/probe.o \
      src/recog.o \
      src/spellchk.o \
      src/tree.o

SCID_TK_OBJS = src/tk_selection.o src/TkDND_OleDND.o

CHARSETCONVOBJS= src/charsetdetector.o src/charsetconverter.o \
      src/universalchardet/CharDistribution.o src/universalchardet/JpCntx.o \
      src/universalchardet/LangBulgarianModel.o src/universalchardet/LangCyrillicModel.o \
      src/universalchardet/LangGreekModel.o src/universalchardet/LangHebrewModel.o \
      src/universalchardet/LangHungarianModel.o src/universalchardet/LangThaiModel.o \
      src/universalchardet/nsBig5Prober.o src/universalchardet/nsCharSetProber.o \
      src/universalchardet/nsEscCharsetProber.o src/universalchardet/nsEscSM.o \
      src/universalchardet/nsEUCJPProber.o src/universalchardet/nsEUCKRProber.o \
      src/universalchardet/nsEUCTWProber.o src/universalchardet/nsGB2312Prober.o \
      src/universalchardet/nsHebrewProber.o src/universalchardet/nsLatin1Prober.o \
      src/universalchardet/nsMBCSGroupProber.o src/universalchardet/nsMBCSSM.o \
      src/universalchardet/nsSBCharSetProber.o src/universalchardet/nsSBCSGroupProber.o \
      src/universalchardet/nsSJISProber.o src/universalchardet/nsUniversalDetector.o \
      src/universalchardet/nsUTF8Prober.o

# Integrated CQL objects.
CQLOBJS_I=  \
	src/cql/i_countsquaresnode.o src/cql/i_cqlassert.o \
	src/cql/i_cqlexit.o src/cql/i_cqlglobals.o src/cql/i_linearize.o \
	src/cql/i_deleteable.o src/cql/i_repeatconstituent.o src/cql/i_sequencenest.o \
	src/cql/i_assignnode.o src/cql/i_attacknode.o src/cql/i_beginvariationnode.o \
	src/cql/i_betweennode.o src/cql/i_chesstypes.o src/cql/i_clone.o \
	src/cql/i_colortransform.o src/cql/i_commentbase.o src/cql/i_composetransform.o \
	src/cql/i_cqlinitialize.o \
	src/cql/i_cqlnodematch.o src/cql/i_cqlnode.o \
	src/cql/i_dihedraltransform.o src/cql/i_directionmatch.o src/cql/i_direction.o \
	src/cql/i_directionparameter.o src/cql/i_echodistancespec.o src/cql/i_echolcaspec.o \
	src/cql/i_echolongestsubstringspec.o src/cql/i_echomatchinfo.o src/cql/i_echonode.o \
	src/cql/i_echoparse.o src/cql/i_echosidetomovespec.o src/cql/i_echospec.o \
	src/cql/i_echosquarespec.o src/cql/i_echosquarespec-transform.o src/cql/i_echotransformspec.o \
	src/cql/i_elonode.o src/cql/i_eventnode.o src/cql/i_existsnode.o \
	src/cql/i_extensionnode.o src/cql/i_filename.o src/cql/i_forallnode.o \
	src/cql/i_futurenode.o src/cql/i_futurestarnode.o src/cql/i_gamesortinfo.o \
	src/cql/i_holderconstituent.o src/cql/i_keywords.o src/cql/i_lexer.o \
	src/cql/i_longestcommonsubstring.o src/cql/i_markboardmap.o src/cql/i_markboard.o \
	src/cql/i_markboardstatic.o src/cql/i_matchcommentnode.o src/cql/i_matchcountnode.o \
	src/cql/i_match.o src/cql/i_movebase.o src/cql/i_movenumbernode.o \
	src/cql/i_nodematch.o src/cql/i_node.o src/cql/i_nodetransform.o \
	src/cql/i_notnode.o src/cql/i_numericvariable.o src/cql/i_onnode.o \
	src/cql/i_optionalconstituent.o src/cql/i_origChanges.o src/cql/i_originnode.o \
	src/cql/i_ornode.o src/cql/i_parser.o \
	src/cql/i_pastnode.o src/cql/i_paststarnode.o src/cql/i_pieceidnode.o \
	src/cql/i_piecelocmatch.o src/cql/i_pieceloc.o src/cql/i_piecelocparse.o \
	src/cql/i_piover4transform.o src/cql/i_playernode.o src/cql/i_plusconstituent.o \
	src/cql/i_positionnode.o src/cql/i_powerdifferencenode.o src/cql/i_powernode.o \
	src/cql/i_raynode.o src/cql/i_seqconstituent.o src/cql/i_seqret.o \
	src/cql/i_sequencebase.o src/cql/i_sequencematch.o src/cql/i_setbase.o \
	src/cql/i_shifttransform.o src/cql/i_sitenode.o src/cql/i_sortvalue.o \
	src/cql/i_squaremask.o src/cql/i_squarevariable.o src/cql/i_starconstituent.o \
	src/cql/i_tagvariable.o src/cql/i_token.o src/cql/i_tokenstream.o \
	src/cql/i_transform_members.o src/cql/i_transformnode.o \
	src/cql/i_transform.o src/cql/i_transform-parse.o \
	src/cql/i_util.o src/cql/i_variable.o src/cql/i_variations.o \
	src/cql/i_vectorconstituent.o src/cql/i_vectornode.o src/cql/i_version.o \
	src/cql/i_yearnode.o

# Standalone CQL objects.
CQLOBJS_S= src/cql/s_cql.o \
	src/cql/s_countsquaresnode.o src/cql/s_cqlassert.o \
	src/cql/s_cqlexit.o src/cql/s_cqlglobals.o src/cql/s_linearize.o \
	src/cql/s_deleteable.o src/cql/s_repeatconstituent.o src/cql/s_sequencenest.o \
	src/cql/s_assignnode.o src/cql/s_attacknode.o src/cql/s_beginvariationnode.o \
	src/cql/s_betweennode.o src/cql/s_chesstypes.o src/cql/s_clone.o \
	src/cql/s_colortransform.o src/cql/s_commentbase.o src/cql/s_composetransform.o \
	src/cql/s_cqlinitialize.o \
	src/cql/s_cqlnodematch.o src/cql/s_cqlnode.o \
	src/cql/s_dihedraltransform.o src/cql/s_directionmatch.o src/cql/s_direction.o \
	src/cql/s_directionparameter.o src/cql/s_echodistancespec.o src/cql/s_echolcaspec.o \
	src/cql/s_echolongestsubstringspec.o src/cql/s_echomatchinfo.o src/cql/s_echonode.o \
	src/cql/s_echoparse.o src/cql/s_echosidetomovespec.o src/cql/s_echospec.o \
	src/cql/s_echosquarespec.o src/cql/s_echosquarespec-transform.o src/cql/s_echotransformspec.o \
	src/cql/s_elonode.o src/cql/s_eventnode.o src/cql/s_existsnode.o \
	src/cql/s_extensionnode.o src/cql/s_filename.o src/cql/s_forallnode.o \
	src/cql/s_futurenode.o src/cql/s_futurestarnode.o src/cql/s_gamesortinfo.o \
	src/cql/s_holderconstituent.o src/cql/s_keywords.o src/cql/s_lexer.o \
	src/cql/s_longestcommonsubstring.o src/cql/s_markboardmap.o src/cql/s_markboard.o \
	src/cql/s_markboardstatic.o src/cql/s_matchcommentnode.o src/cql/s_matchcountnode.o \
	src/cql/s_match.o src/cql/s_movebase.o src/cql/s_movenumbernode.o \
	src/cql/s_nodematch.o src/cql/s_node.o src/cql/s_nodetransform.o \
	src/cql/s_notnode.o src/cql/s_numericvariable.o src/cql/s_onnode.o \
	src/cql/s_optionalconstituent.o src/cql/s_origChanges.o src/cql/s_originnode.o \
	src/cql/s_ornode.o src/cql/s_parser.o \
	src/cql/s_pastnode.o src/cql/s_paststarnode.o src/cql/s_pieceidnode.o \
	src/cql/s_piecelocmatch.o src/cql/s_pieceloc.o src/cql/s_piecelocparse.o \
	src/cql/s_piover4transform.o src/cql/s_playernode.o src/cql/s_plusconstituent.o \
	src/cql/s_positionnode.o src/cql/s_powerdifferencenode.o src/cql/s_powernode.o \
	src/cql/s_raynode.o src/cql/s_seqconstituent.o src/cql/s_seqret.o \
	src/cql/s_sequencebase.o src/cql/s_sequencematch.o src/cql/s_setbase.o \
	src/cql/s_shifttransform.o src/cql/s_sitenode.o src/cql/s_sortvalue.o \
	src/cql/s_squaremask.o src/cql/s_squarevariable.o src/cql/s_starconstituent.o \
	src/cql/s_tagvariable.o src/cql/s_token.o src/cql/s_tokenstream.o \
	src/cql/s_transform_members.o src/cql/s_transformnode.o \
	src/cql/s_transform.o src/cql/s_transform-parse.o \
	src/cql/s_util.o src/cql/s_variable.o src/cql/s_variations.o \
	src/cql/s_vectorconstituent.o src/cql/s_vectornode.o src/cql/s_version.o \
	src/cql/s_yearnode.o \
	src/bytebuf.o src/crosstab.o src/date.o src/dstring.o \
	src/filter.o src/game.o src/gfile.o src/index.o src/matsig.o \
	src/mfile.o src/misc.o src/movelist.o src/myassert.o src/namebase.o \
	src/optable.o src/pgnparse.o src/pbook.o src/position.o src/probe.o \
	src/spellchk.o src/stored.o src/stralloc.o src/textbuf.o src/tree.o


### ZLIBOBJS: object files in the zlib compression library.

ZLIBOBJS= src/zlib/adler32.o src/zlib/compress.o src/zlib/crc32.o \
      src/zlib/gzio.o src/zlib/uncompr.o src/zlib/deflate.o src/zlib/trees.o \
      src/zlib/zutil.o src/zlib/inflate.o src/zlib/infblock.o \
      src/zlib/inftrees.o src/zlib/infcodes.o src/zlib/infutil.o \
      src/zlib/inffast.o

### OBJS: all standard object files for scid.

OBJS= $(SCIDOBJS) $(ZLIBOBJS)

## LANGUAGES are now source from scidShareDir/lang

### TCLS: all the .tcl files that make up "scid".
TCLS= \
  tcl/start.tcl \
  tcl/config.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/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/book.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/comp.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/tools/correspondence.tcl \
    tcl/lang/english.tcl tcl/dnd/tkdnd.tcl tcl/dnd/tkdnd_windows.tcl \
  tcl/tools/fics.tcl tcl/tools/uci.tcl tcl/end.tcl tcl/tools/tacgame.tcl tcl/tools/sergame.tcl tcl/tools/calvar.tcl tcl/tools/tactics.tcl tcl/tools/novag.tcl tcl/misc/flags.tcl tcl/tools/inputengine.tcl


########################################

all: $(EXECS) scid.gui

scid.gui: $(TCLS)
	-$(call UNLINK,scid.gui)
	$(call CAT,$(TCLS)) > scid.gui

scmerge.exe: src/scmerge.o $(OBJS)
	$(LINK) $(LDFLAGS) -o scmerge.exe src/scmerge.o $(OBJS) $(CHARSETCONVOBJS) -L$(TCL_DIR)/lib -ltcl$(TCL_VERSION_NODOT)

pgnscid.exe: src/pgnscid.o $(OBJS) $(CHARSETCONVOBJS)
	$(LINK) $(LDFLAGS) -o pgnscid.exe src/pgnscid.o $(OBJS) $(CHARSETCONVOBJS) -L$(TCL_DIR)/lib -ltcl$(TCL_VERSION_NODOT)

scidt.exe: src/scidt.o $(OBJS) $(CHARSETCONVOBJS)
	$(LINK) $(LDFLAGS) -o scidt.exe src/scidt.o $(OBJS) $(CHARSETCONVOBJS) -L$(TCL_DIR)/lib -ltcl$(TCL_VERSION_NODOT)

scidlet.exe: src/scidlet.o src/engine.o src/recog.o $(OBJS)
	$(LINK) $(LDFLAGS) -o scidlet.exe src/scidlet.o src/engine.o src/recog.o \
          src/misc.o src/position.o src/movelist.o src/dstring.o src/myassert.o

scid.exe: src/scid.o $(OBJS) $(CQLOBJS_I) $(CHARSETCONVOBJS) $(SCID_XOBJS) $(SCID_TK_OBJS) scid.res
	$(LINK) $(LDFLAGS) -o scid.exe src/scid.o $(OBJS) $(CQLOBJS_I) $(CHARSETCONVOBJS) $(SCID_XOBJS) $(SCID_TK_OBJS) scid.res \
	    -mwindows -lole32 -luuid -L$(TCL_DIR)/lib -ltk$(TCL_VERSION_NODOT) -ltcl$(TCL_VERSION_NODOT)

tcscid.exe: src/tcscid.o $(OBJS) $(CQLOBJS_I) $(SCID_XOBJS) $(CHARSETCONVOBJS)
	$(LINK) $(LDFLAGS) -o tcscid.exe src/tcscid.o $(OBJS) $(CQLOBJS_I) $(CHARSETCONVOBJS) $(SCID_XOBJS) \
	    -ltcl$(TCL_VERSION_NODOT) -L$(TCL_DIR)/lib -ltcl$(TCL_VERSION_NODOT)

scql.exe: $(CQLOBJS_S) $(CHARSETCONVOBJS) $(ZLIBOBJS)
	$(LINK) $(LDFLAGS) -o scql.exe $(CQLOBJS_S) $(CHARSETCONVOBJS) $(ZLIBOBJS) -L$(TCL_DIR)/lib -ltcl$(TCL_VERSION_NODOT)

eco2epd.exe: src/eco2epd.o src/pbook.o $(OBJS) $(CHARSETCONVOBJS)
	$(LINK) $(LDFLAGS) -o eco2epd.exe src/eco2epd.o src/pbook.o $(OBJS) $(CHARSETCONVOBJS) -L$(TCL_DIR)/lib -ltcl$(TCL_VERSION_NODOT)

scid.res: scid.rc
	$(RC) $(RCFLAGS) -o scid.res scid.rc

clean:
	-$(call UNLINK,src/*.o)
	-$(call UNLINK,src/zlib/*.o)
	-$(call UNLINK,src/polyglot/*.o)
	-$(call UNLINK,src/universalchardet/*.o)
	-$(call UNLINK,src/cql/*.o)
	-$(call UNLINK,scid.res)
	-$(call UNLINK,scid.gui)
	-$(call UNLINK,$(EXECS))

strip:
	$(STRIP) $(EXECS)


src/tcscid.o: src/tkscid.cpp
	$(CXX) $(CXXFLAGS) -std=c++11 -I$(TCL_DIR)/include -DTCL_ONLY -c src/tkscid.cpp \
	  -o src/tcscid.o

src/tkscid.o: src/tkscid.cpp
	$(CXX) $(CXXFLAGS) -std=c++11 -I$(TCL_DIR)/include -c src/tkscid.cpp \
	  -o src/tkscid.o

src/scid.o: src/tkscid.cpp
	$(CXX) $(CXXFLAGS) -std=c++11 -I$(TCL_DIR)/include -DSOURCE_TCL_FILE="\"scid.gui\"" \
	  -c src/tkscid.cpp -o src/scid.o

src/probe.o: src/probe.cpp src/egtb/tbindex.cpp src/egtb/tbdecode.c
	$(CXX) $(CXXFLAGS) -Wno-missing-braces $(TB) -c src/probe.cpp -o src/probe.o

%.o: %.cpp
	$(CXX) $(CXXFLAGS) -I$(TCL_DIR)/include -c $< -o $@

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

src/TkDND_OleDND.o: src/tkdnd/win/TkDND_OleDND.cpp
	$(CXX) $(CXXFLAGS) -I$(TCL_DIR)/include -Isrc/tkdnd/win -c $< -o $@

src/cql/s_%.o: src/cql/%.cpp
	$(CXX) $(CXXFLAGS) -std=c++11 -Wall -Wno-write-strings -Wno-char-subscripts  -Wno-parentheses -Wno-sign-compare -fdiagnostics-show-option -DSTANDALONE $(CQL_INCLUDE) -o $@ -c $<

src/cql/i_%.o: src/cql/%.cpp
	$(CXX) $(CXXFLAGS) -std=c++11 -Wall -Wno-write-strings -Wno-char-subscripts  -Wno-parentheses -Wno-sign-compare -fdiagnostics-show-option -DINTEGRATED $(CQL_INCLUDE) -o $@ -c $<
