
# EPONYM = IvanHoe-ZOG-MP
# VERSION = 999958l-ZOG-MP
EPONYM = IvanHoe-Beta
VERSION = 999947c
CC = gcc
FLAGS = -Wall
OPTIONS = -g -O3 -fstrict-aliasing -fomit-frame-pointer -fno-exceptions
# OPTIONS = -pg -O1

HAS = -DHAS_POPCNT # -DHAS_PREFETCH
HAS = # -DHAS_PREFETCH # comment out when HAS_POPCNT can avail
DEFINITIONS = -Dx86_64 $(HAS) -DVERSION=\"$(VERSION)\"
DEFINITIONS += -DEPONYM=\"$(EPONYM)\"
DEFINITIONS += -DCON_ROBBO_BUILD # RobboBases usage
# DEFINITIONS += -DNOME_WINDOWS # uncomment for RobboBuild with WINDOWS names

DEFINITIONS += -DYUSUF_MULTICORE # capitalist!
# DEFINITIONS += -DUSER_SPLIT # allow capitalist users to split nodes
# DEFINITIONS += -DMINIMAL # for MINIMAL
# DEFINITIONS += -DWINDOWS # for WINDOWS
# DEFINITIONS += -DDEBUG # validate
DEFINITIONS += -DBENCHMARK
# DEFINITIONS += -DTRACE_COMPILE
# DEFINITIONS += -DBUILD_ZOG_MP_COMPILE # active ZOG MP

LIBS = -lpthread

RobboLito_SOURCES = main.c \
                    arrays.c \
                    SLAB_MEMORY.c \
                    set_position.c \
                    utility.c \
                    material_value.c \
                    pawn_eval.c \
                    evaluation.c \
                    mobility.c \
                    50move.c \
                    MEM_HANDLER.c \
                    Zobrist_init.c \
                    Zobrist_use.c \
                    SEE.c \
                    move_gen.c \
                    make_move.c \
                    un_make_move.c \
                    top_node.c \
                    root_node.c \
                    pv_node.c \
                    cut_node.c \
                    all_node.c \
                    exclude_node.c \
                    control.c \
                    MonteCarlo.c \
                    TopNodeMonteCarlo.c \
                    RootNodeMonteCarlo.c \
                    input.c \
                    search.c \
                    static.c \
                    next_move.c \
                    low_depth.c \
                    qsearch.c \
                    qsearch_pv.c \
                    ok_move.c \
                    Robbo_glue.c \
                    signals.c \
                    magic_mult.c 

IVAN_SOURCES = SMP.c \
               SMP_init.c \
               SMP_search.c \
               top_analysis.c \
               root_analysis.c \
               root_multipv.c \
                benchmark.c \
#               root_zog_node.c \
#               ZOG_init.c \
#               ZOG_MP.c

RobboLito_SOURCES += $(IVAN_SOURCES)

UTILITY_SOURCES =  matval_explain.c \
                   eval_explain.c \
                   pawn_eval_explain.c \
                   validate.c \
                   perft.c

# DEFINITIONS += -DDEBUG -DUTILITIES # perft # eval explain # Fact: EE slows 3%
# RobboLito_SOURCES += $(UTILITY_SOURCES) # comment out

RobboGlueing_SOURCES = Robbo_utilita.c \
                       Robbo_cache.c \
                       Robbo_iniz.c \
                       Robbo_decomp.c \
                       Robbo_iniz_IO.c
RobboUsage_SOURCES = $(RobboGlueing_SOURCES) \
                     RobboTripleValue.c \
                     RobboTripleVerify.c \
                     RobboTripleDisk.c \
                     RobboTriple.c \
                     RobboTriple_iniz.c
RobboUsage_HEADERS = RobboTotalBase.h \
                     RobboTripleBase.h

ROBBO_GLUE = $(RobboUsage_SOURCES:.c=.o)

RobboLito_OBJECTS = $(RobboLito_SOURCES:.c=.o) $(ROBBO_GLUE)
RobboAnalysis_OBJECTS = $(RobboLito_OBJECTS:.o=.ao)

RobboLito_HEADERS = RobboLito.h \
                    IH-win-linux.h \
                    IH-hash.h \
                    IH-ZOG-MP.h \
                    IH-SMP.h \
                    IH-board.h \
                    IH-move.h \
                    SLAB_MEMORY.h \
                    MonteCarlo.h \
                    arrays.h \
                    bits.h \
                    functions.h \
                    control.h \
                    make_unmake.h \
                    white.h \
                    black.h \
                    undef.h \
                    common.h \
#                    ZOG_NODE.h

WINDOWS_HEADERS = win32bits.h \
                  win64bits.h
RobboLito_HEADERS += $(WINDOWS_HEADERS)

INCLUDE = init_gen.i \
          history.i \
          null_move.i \
          material_value.i

ALTRO = Makefile \
        YakovChart.bi \
        YakovChart.ne \
        evaluation.v \
        pawn_eval.v \
        .depend \
        ROBBO_BASE_BRIEF \
        ROBBO_TRIPLE_INFO \
        UCI_OPTIONS \
        triple3.sh \
        triple4.sh \
        triple5.sh \
        tripleZ.sh \
        triple33.sh \
        triple42.sh \
        triple51.sh \
        triple6Z.sh \
        maximal.6 \
        maximal.6Z \
        CHANGE_LOG \
        COMRADES_HASH \
        LARGE_PAGES

RobboTotalBuild_SOURCES = Robbo_gen.c \
                          Robbo_conto.c \
                          Robbo_utilita.c \
                          Robbo_cache.c \
                          Robbo_iniz.c \
                          Robbo_iniz_IO.c \
                          Robbo_mfa.c \
                          RobboTotalBuild.c \
                          Robbo_comp.c \
                          Robbo_decomp.c \
                          Robbo_popolare.c \
                          Robbo_mobilita.c \
                          Robbo_extra.c \
                          Robbo_suffix.c

RobboTotalBuild_HEADERS = Robbo_gen_mossa.h \
                          RobboTotalBase.h

RobboBuild_HACK =  arrays.o magic_mult.o SLAB_MEMORY.o MEM_HANDLER.o
RobboTotalBuild_OBJECTS = $(RobboTotalBuild_SOURCES:.c=.o) $(RobboBuild_HACK)

RobboTripleBuild_SOURCES = $(RobboGlueing_SOURCES) \
                           RobboTripleBuild.c \
                           RobboTripleHuffmann.c \
                           Robbo_mobilita.c \
                           Robbo_mfa.c

RobboTripleBuild_SOURCES -= RobboTripleValue.c
RobboTripleBuild_HEADERS = $(RobboUsage_HEADERS) RobboTripleBase.h Robbo_gen_mossa.h
RobboTripleBuild_OBJECTS = $(RobboTripleBuild_SOURCES:.c=.o) $(RobboBuild_HACK)

RobboExplorer_SOURCES = $(RobboGlueing_SOURCES) \
                        RobboExplorer.c \
                        RobboExplorer_glue.c \
                        RobboExplorer_extra.c \
                        RobboTripleValue.c \
                        RobboTripleDisk.c \
                        RobboTriple.c \
                        RobboTriple_iniz.c \
                        mobility.c \
                        Robbo_mfa.c \
                        move_gen.c \
                        set_position.c \
                        utility.c
RobboExplorer_OBJECTS = $(RobboExplorer_SOURCES:.c=.o) $(RobboBuild_HACK)

UTILITY_OBJECTS = $(UTILITY_SOURCES:.c=.o)

RobboTriples = RobboTripleBuild.c \
               RobboTripleHuffmann.c \
               RobboTriple_iniz.c \
               RobboTripleValue.c \
               RobboTripleVerify.c \
               RobboTriple.c \
               RobboTripleDisk.c

RobboExplorer_ADDITIONAL = RobboExplorer.c \
                           RobboExplorer_glue.c \
                           RobboExplorer_extra.c

SOURCES = $(RobboLito_SOURCES) \
          $(RobboTotalBuild_SOURCES) \
          $(RobboTriples) \
          $(UTILITY_SOURCES) \
          $(RobboExplorer_ADDITIONAL)
HEADERS = $(RobboLito_HEADERS) $(RobboTotalBuild_HEADERS) RobboTripleBase.h

ALTRO += md5.33 md5.42 md5.6Z
TUTTO = $(SOURCES) $(HEADERS) $(INCLUDE) $(ALTRO) 

CP = /bin/cp
MKDIR = /bin/mkdir
RM = /bin/rm
SED = /bin/sed
TAR = /bin/tar
ALTRO_DIR = $(EPONYM)-$(VERSION)

ROBBO_EXPLORATIVE = RobboExplorative/READ_ME \
                    RobboExplorative/MERIFONT.TTF \
                    RobboExplorative/MANIFEST \
                    RobboExplorative/Makefile \
                    RobboExplorative/*.jar \
                    RobboExplorative/*.java

COMRADES_GUI = ComradesGUI/READ_ME \
               ComradesGUI/MERIFONT.TTF \
               ComradesGUI/LEIPFONT.TTF \
               ComradesGUI/KINGFONT.TTF \
               ComradesGUI/MANIFEST \
               ComradesGUI/Makefile \
               ComradesGUI/*.jar \
               ComradesGUI/*.java

CFLAGS += $(OPTIONS) $(FLAGS) $(DEFINITIONS)

all: GamePlay Analysis \
     RobboTripleBuild RobboTotalBuild RobboExplorer \
     .depend Makefile
GamePlay: $(RobboLito_OBJECTS) $(RobboLito_HEADERS) Makefile
	$(CC) $(CFLAGS) -o $(EPONYM) $(RobboLito_OBJECTS) $(LIBS)
Analysis: $(RobboAnalysis_OBJECTS) $(RobboLito_HEADERS) Makefile
	$(CC) $(CFLAGS) -o $(EPONYM)-Analysis $(RobboAnalysis_OBJECTS) $(LIBS)
%.o: %.c $(RobboLito_HEADERS) Makefile
	$(CC) $(CFLAGS) -DMODE_GAME_PLAY -o $@ -c $<
%.ao: %.c $(RobboLito_HEADERS) Makefile
	$(CC) $(CFLAGS) -DMODE_ANALYSIS -o $@ -c $<
RobboTripleBuild: $(RobboTripleBuild_OBJECTS) $(RobboTripleBuild_HEADERS) Makefile
	$(CC) $(CFLAGS) -o RobboTripleBuild $(RobboTripleBuild_OBJECTS) -lpthread
RobboTotalBuild: $(RobboTotalBuild_OBJECTS) $(RobboTotalBuild_HEADERS) Makefile
	$(CC) $(CFLAGS) -o RobboTotalBuild $(RobboTotalBuild_OBJECTS) -lpthread
RobboExplorer: $(RobboExplorer_OBJECTS) $(RobboLito_HEADERS) $(RobboExplorer_SOURCES) Makefile
	$(CC) $(CFLAGS) -o RobboExplorer $(RobboExplorer_OBJECTS)
RobboExplorative::
	cd RobboExplorative && make clean && make && cd ..
ComradesGUI::
	cd ComradesGUI && make clean && make && cd ..
archive:
	$(RM) -rf $(ALTRO_DIR) # ensure
	$(MKDIR) $(ALTRO_DIR)
	$(CP) $(TUTTO) $(ALTRO_DIR)
	$(MKDIR) $(ALTRO_DIR)/RobboExplorative
	$(CP) $(ROBBO_EXPLORATIVE) $(ALTRO_DIR)/RobboExplorative
	$(MKDIR) $(ALTRO_DIR)/ComradesGUI
	$(CP) $(COMRADES_GUI) $(ALTRO_DIR)/ComradesGUI
	$(TAR) -cf $(EPONYM)-Backup-$(VERSION).tar $(ALTRO_DIR)/* $(ALTRO_DIR)/.depend
	$(RM) -rf $(ALTRO_DIR)
.depend: Makefile $(HEADERS)
	$(CC) -MM $(DEFINITIONS) $(SOURCES) | $(SED) 's/:/: Makefile/g' > $@
clean:
	$(RM) -f $(RobboLito_OBJECTS) $(EPONYM) \
                 $(RobboAnalysis_OBJECTS) $(EPONYM)-Analysis \
                 $(RobboTotalBuild_OBJECTS) RobboTotalBuild \
                 $(RobboTripleBuild_OBJECTS) RobboTripleBuild \
                 .depend
include .depend
