##### Makefile for Scid for Pocket PC

COMPILE = arm-cegcc-g++
CC = arm-cegcc-gcc
STRIP = arm-cegcc-strip
LINK = arm-cegcc-g++
RANLIB = arm-cegcc-ranlib
OPT = -O4
MAKEFLAGS = -j 2

CABWIZTOOLS = ../cabwiz/Tools/

# use -DPOCKET to target pocket hardware (no Linux emul)
PPC = -DUSE_TCL_STUBS -DTCL_ONLY -DWINCE -DPOCKET -fsigned-char

# BINDIR: where the Scid programs are copied for "make install".
#
BINDIR = /usr/local/bin

# SHAREDIR: where scid.eco is copied for "make install".
#
SHAREDIR = /usr/local/share/scid

### 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 = -I../etcl/tcl8.5/generic/
TCL_LIBRARY = -L/usr/lib -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 = $(OPT) -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) $(TCL_INCLUDE)

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

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

### EXECS: executable programs compiled from C++ files.
EXECS= tkscid toga toga134 toga141 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) 

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

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

toga141:
	cd pocket/toga141SE/src && $(MAKE) -f Makefile.arm
	
fruit_21:
	cd pocket/fruit_21/src && $(MAKE) -f Makefile.arm

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

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

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

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

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

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

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

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

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

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

### copy all necessary runtime files
dist: all
	rm -rf ./pocket/scid_pocket/*
	mkdir -p ./pocket/scid_pocket/
	cp ./pocket/scidppc.tcl ./pocket/core.tcl ./pocket/scid_pocket/
	cp ./pocket/toga/src/toga.exe tkscid.dll ./pocket/scid_pocket/
	cp ./pocket/toga134/src/toga134.exe ./pocket/scid_pocket/
	cp ./pocket/toga141SE/src/toga141SE.exe ./pocket/scid_pocket/
	cp ./pocket/glaurung/src/glaurung.exe ./pocket/scid_pocket/
	cp ./pocket/glaurung/src/kpk.bin ./pocket/scid_pocket/
	cp ./pocket/fruit_21/src/fruit_21.exe ./pocket/scid_pocket/
	cp ./pocket/gambitfruit/src/gambitfruit.exe ./pocket/scid_pocket/
	cp ./pocket/viper01/src/viper01.exe ./pocket/scid_pocket/
	cp ./pocket/phalanx/src/phalanx.exe ./pocket/scid_pocket/
	cp ./pocket/scorpio/src/scorpio.exe ./pocket/scid_pocket/
	cp -r ./pocket/scorpio/personality ./pocket/scid_pocket/
	cp ./pocket/greko/src/greko.exe ./pocket/scid_pocket/
	cp ./pocket/scidlet/src/scidlet.exe ./pocket/scid_pocket/
	cp ./pocket/robbolito/src/robbolito.exe ./pocket/scid_pocket/
	cp ./pocket/stockfish/src/stockfish.exe ./pocket/scid_pocket/
	cp -r ./pocket/books ./pocket/scid_pocket
	cp -r ./pocket/sounds ./pocket/scid_pocket
	cp -r ./pocket/msgs ./pocket/scid_pocket
	upx ./pocket/scid_pocket/*.exe
	upx ./pocket/scid_pocket/*.dll
	
### build CAB file for Pocket PC
cab: dist
	cp -r pocket/scid_pocket $(CABWIZTOOLS)
	cd $(CABWIZTOOLS) && wine Cabwiz.exe scidpocket.inf /cpu Arm
	cp $(CABWIZTOOLS)/scidpocket.Arm.CAB /media/disk/

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.arm
	$(LINK) -shared -pipe -o tkscid.dll 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)  -L../etcl/tcl8.5/unix/ -ltcl_armstub8.5
	$(STRIP) tkscid.dll
	
### 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) $(PROFILE) $(OPTIMIZE) $(DEBUG) $(SCIDFLAGS) $(TCL_INCLUDE) $(TB) $(PPC) -o src/probe.o -c src/probe.cpp

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

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

### End of Makefile
