##### Makefile for Scid for Unix operating systems.

### Compiler: Most Unix systems use g++ for compiling and linking.
#
COMPILE = g++
CC = gcc
LINK = g++
RANLIB = ranlib
PPC = -DTCL_ONLY -DWINCE
MAKEFLAGS = -j 2

### TCL_VERSION: Set this according to the version of Tcl/Tk you have
#   installed that you want Scid to use: 8.0, 8.1, 8.2, 8.3, etc.
#
TCL_VERSION = 8.5

# TCL_INCLUDE, TCL_LIBRARY, TK_LIBRARY: these are the compiler options
#    needed for linking Scid with Tcl/Tk.  The program "./configure"
#    will try to determine them automatically, but if it cannot, you
#    can use the examples below for help in setting these variables.
#
# The settings determined by "./configure" are:
#
TCL_INCLUDE = -I/usr/include
TCL_LIBRARY = -ltcl$(TCL_VERSION) -ldl
TK_LIBRARY  = $(TCL_LIBRARY) -ltk$(TCL_VERSION) 

########################################
### Compiler options:

### 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

### OPTIMIZE: Optimization options for C++ compiler.
#      -O4 is the most optimization for g++. I have found -O2 to do
#      just as well, but someone reported a noticable difference in speed
#      between -O4 and -O2 so the default here is -O4.
#      On some systems, adding "-fno-rtti" and "-fno-exceptions" produces
#      smaller, faster programs since Scid does not use those C++ features.
#
OPTIMIZE = 
# -O4 -fno-rtti -fno-exceptions

### DEBUG: Defining the macro ASSERTIONS will turn on assertions, which
#       helps to track bugs after modifications, but the programs will run 
#       a little faster with assertions turned off.
#
DEBUG = #-DASSERTIONS

### WARNINGS: I always compile with all warnings on (-Wall), and all the
#       files should compile warning-free using g++.
#
WARNINGS = -Wall

### PROFILE: Set this to "-pg" for profiling in g++ and gcc.
#
PROFILE = 

### CPP_FLAGS: Flags for C++ compilation.
#
CPP_FLAGS = $(PROFILE) $(OPTIMIZE) $(WARNINGS) $(DEBUG) $(SCIDFLAGS) $(PPC)

### CFLAGS: Flags for C compilation (only used for compiling zlib).
#
CFLAGS = -O2 -Wall $(PROFILE) $(OPTIMIZE) $(PPC) -DWINCE

############################################################
#
# You should not need to edit anything below this line.
#
############################################################

### EXECS: executable programs compiled from C++ files.
#     Note: scidt and eco2epd are obsolete and not compiled by default.
#
EXECS= tkscid toga toga134 toga141SE glaurung fruit_21 gambitfruit viper01 phalanx scorpio greko scidlet robbolito stockfish

### SCIDOBJS: not all the .o files that make up Scid, just the standard ones 
#     that most of the programs include.
#
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/cutil.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


### 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
       
### ZLIB: Should be "-lz" if your system has zlib, "" otherwise.
#
ZLIB =  

### OBJS: Will be "$(SCIDOBJS)", "$(POLYGLOTOBJS)", and also "$(ZLIBOBJS)" if they are
#      needed on your system.
OBJS= $(SCIDOBJS)


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

### Type "make" or "make all" to make all programs:
#
all: $(EXECS) 
	cat ./pocket/hires.tcl ./pocket/lowres.tcl \
./pocket/validate.tcl ./pocket/misc.tcl ./pocket/htext.tcl ./pocket/ui.tcl ./pocket/engine.tcl ./pocket/import.tcl \
./pocket/backoff.tcl ./pocket/novag.tcl ./pocket/tactics.tcl ./pocket/pgn.tcl ./pocket/fics.tcl ./pocket/edit.tcl ./pocket/book.tcl \
./pocket/search/header.tcl ./pocket/search/material.tcl ./pocket/search/search.tcl ./pocket/search/board.tcl \
 ./pocket/utils/history.tcl ./pocket/utils/date.tcl ./pocket/utils/string.tcl ./pocket/utils/scrolledframe.tcl ./pocket/utils/utils.tcl \
  > ./pocket/core.tcl

### To remove Scid files placed in the BINDIR and SHAREDIR directories,
#   type "make distclean".
#
distclean:
	cd $(BINDIR) && rm -f $(EXECS) $(SCRIPTS)

### To remove object and executable files: type "make clean".
#
clean:
	rm -f src/*.o src/zlib/*.o src/polyglot/*.o $(EXECS) scid $(SCRIPTS) tkscid.so

### copy all necessary runtime files
dist: all

### To make the executable files smaller: type "make strip".
#

strip:
	strip $(EXECS)

toga:
	cd pocket/toga/src && $(MAKE) -f Makefile

toga134:
	cd pocket/toga134/src && $(MAKE) -f Makefile

toga141SE:
	cd pocket/toga141SE/src && $(MAKE) -f Makefile

fruit_21:
	cd pocket/fruit_21/src && $(MAKE) -f Makefile

gambitfruit:
	cd pocket/gambitfruit/src && $(MAKE) -f Makefile

glaurung:
	cd pocket/glaurung/src && $(MAKE) -f Makefile

viper01:
	cd pocket/viper01/src && $(MAKE) -f Makefile

phalanx:
	cd pocket/phalanx/src && $(MAKE) -f Makefile

scorpio:
	cd pocket/scorpio/src && $(MAKE) -f Makefile

greko:
	cd pocket/greko/src && $(MAKE) -f Makefile

scidlet:
	cd pocket/scidlet/src && $(MAKE) -f Makefile

robbolito:
	cd pocket/robbolito/src && $(MAKE) -f Makefile

stockfish:
	cd pocket/stockfish/src && $(MAKE) -f Makefile

tkscid: src/tkscid.o $(OBJS) src/tree.o src/filter.o src/pbook.o src/crosstab.o \
          src/spellchk.o src/probe.o src/optable.o src/engine.o src/recog.o

	cd src/zlib && $(MAKE) -f Makefile.pocket_x86
	$(LINK) -shared -pipe -o tkscid.so src/tkscid.o $(OBJS) $(ZLIBOBJS) src/tree.o src/filter.o src/pbook.o src/crosstab.o src/spellchk.o src/probe.o src/optable.o src/engine.o src/recog.o $(ZLIB) -lstdc++ -L../etcl/tcl8.5/unix/ -ltcl_x86stub8.5

#-L../../etcl/tcl8.5/unix/ -ltcl_x86stub8.5
#-L../../../prog/etcl/tcl8.4.14/ -ltclstub_x868.4

### Rules to create .o files from .cpp files:
#
src/tcscid.o: src/tkscid.cpp
	$(COMPILE) $(CPP_FLAGS) $(TCL_INCLUDE) -DTCL_ONLY -o src/tcscid.o -c src/tkscid.cpp

src/tkscid.o: src/tkscid.cpp
	$(COMPILE) $(CPP_FLAGS) $(TCL_INCLUDE) -o src/tkscid.o -c src/tkscid.cpp

### The endgame tablebase code in the egtb/ subdirectory (not written by me)
#   gives many warnings, so compile probe.cpp with warnings turned off:
#
src/probe.o: src/probe.cpp src/egtb/tbindex.cpp src/egtb/tbdecode.c
	$(COMPILE) $(CFLAGS) $(DEBUG) $(SCIDFLAGS) $(TB) -o src/probe.o -c src/probe.cpp

src/cutil.o: src/cutil.c
	$(CC) $(CFLAGS) $(DEBUG) $(SCIDFLAGS) $(TB) $(PPC) -c -o src/cutil.o src/cutil.c
	
### Generic rule for all other .cpp files:
#
%.o: %.cpp
	$(COMPILE) $(CPP_FLAGS) -o $@ -c $<

### Rule for compiling zlib source files:
#
src/zlib/%.o: src/zlib/%.c
	$(CC) $(CFLAGS) -o $@ -c $<

### End of Makefile
