# This makefile is intended for building with GCC on Windows.

PROJ = winboard

OBJS = \
    backend.o \
    book.o \
    engineoutput.o \
    evalgraph.o \
    gamelist.o \
    help.o \
    lists.o \
    moves.o \
    parser.o \
    pgntags.o \
    history.o \
    wbres.o \
    wchat.o \
    wclipbrd.o \
    wedittags.o \
    wengineoutput.o \
    wevalgraph.o \
    wgamelist.o \
    whistory.o \
    winboard.o \
    wlayout.o \
    woptions.o \
    wsettings.o \
    wsnap.o \
    wsockerr.o \
    uci.o \
    zippy.o \

# make compiling less spammy
compile = @echo "$1"; $(CC) $(CFLAGS) $1

# Debugging?
USE_DEBUG = 1

# JAWS support? (Set to 2 for NVDA support!)
JAWS = 0
NVDADIR = .

# set this to 1 unless you don't mind linking with cygwin1.dll
USE_MINGW=1
# set up for cygwin or not
ifeq ($(USE_MINGW),1)
LFCYG = -lmsvcrt
endif

CC = gcc
# Flex version 2.5.4 or later is required.
FLEX = "flex.exe"
#      "c:/mingw/mingw32/bin/flex.exe"
HC="C:/Program Files/Help Workshop/hcrtf" -xn
DEFS = -DWINVER=0x0501 -D_WIN32_WINNT=0x0501
ifeq ($(JAWS),1)
    DEFS += -DJAWS
endif
ifeq ($(JAWS),2)
    DEFS += -DJAWS -DNVDA=$(NVDADIR)
endif
WARN = -Wall -Wextra
ARCH =
ifeq ($(USE_DEBUG),0)
    CFDEBUG = -DNDEBUG
    OPTS = -O2
else
    CFDEBUG = -D_DEBUG
    OPTS = -O0 -g
endif
CF = -c $(WARN) -pipe $(CFDEBUG) $(DEFS) -I. -I..
CFLAGS = $(CF) $(ARCH) $(OPTS)
LFLAGS = $(LFCYG) $(CFDEBUG) -fpic -s -mwindows -lwsock32 -lwinmm -lcomctl32 -lmsimg32
ifeq ($(JAWS),2)
    LFLAGS += $(NVDADIR)/nvdaControllerClient32.lib
endif

all: $(PROJ).exe


clean:
	rm -f *~ $(PROJ).exe $(PROJ).err $(PROJ).rbj $(PROJ).RES $(PROJ).res\
	*.sbr *.bsc *.o *.obj *.plg *.opt *.ncb *.debug *.bak *.gid *.GID\
	*.map *.pdb *.tmp $(PROJ).ini


# Update the help file if necessary
$(PROJ).hlp : $(PROJ).rtf
	$(HC) $(PROJ).hpj && @cat $(PROJ).err && @mv $(PROJ).hlp tmp.hlp && @mv tmp.hlp $(PROJ).hlp || true # these moves to get the case right


# Update the resource if necessary
wbres.o: $(PROJ).rc $(PROJ).h resource.h config.h
	windres $(DEFS) --use-temp-file --include-dir .. $< -O coff -o $@


$(PROJ).exe: $(OBJS) $(PROJ).hlp
	$(CC) $(guiflags) $(OBJS) $(LFLAGS) -o $(PROJ).exe


winboard.o: winboard.c config.h winboard.h ../common.h ../frontend.h ../backend.h \
	../moves.h defaults.h resource.h wclipbrd.h \
	wsockerr.h woptions.h wsnap.h ../lists.h help.h ../args.h jaws.c
	$(call compile, $<)

backend.o: ../backend.c config.h ../common.h ../frontend.h ../backend.h \
	../parser.h ../moves.h ../zippy.h ../backendz.h ../gettext.h ../lists.h
	$(call compile, $<)

parser.o: ../parser.c config.h ../common.h ../backend.h ../parser.h \
	../frontend.h ../moves.h ../lists.h
	$(call compile, $<)

moves.o: ../moves.c config.h ../backend.h ../common.h ../parser.h \
	../moves.h ../lists.h
	$(call compile, $<)

lists.o: ../lists.c config.h ../lists.h ../common.h
	$(call compile, $<)

gamelist.o: ../gamelist.c config.h ../lists.h ../common.h ../frontend.h \
	../backend.h ../parser.h
	$(call compile, $<)

wclipbrd.o: wclipbrd.c config.h ../common.h ../frontend.h ../backend.h \
	winboard.h resource.h wclipbrd.h ../lists.h
	$(call compile, $<)

wedittags.o: wedittags.c config.h ../common.h winboard.h resource.h ../frontend.h \
	../backend.h ../lists.h
	$(call compile, $<)

wgamelist.o: wgamelist.c config.h ../common.h winboard.h resource.h ../frontend.h \
	../backend.h ../lists.h
	$(call compile, $<)

woptions.o: woptions.c config.h ../common.h ../frontend.h ../backend.h ../lists.h \
	defaults.h winboard.h resource.h
	$(call compile, $<)

wengineoutput.o: wengineoutput.c ../engineoutput.h config.h ../common.h \
	../frontend.h ../backend.h ../lists.h winboard.h resource.h wsnap.h
	$(call compile, $<)

engineoutput.o: ../engineoutput.c ../engineoutput.h config.h ../common.h \
	../frontend.h ../backend.h ../lists.h
	$(call compile, $<)

whistory.o: whistory.c config.h ../common.h ../frontend.h ../backend.h \
	../lists.h winboard.h resource.h wsnap.h
	$(call compile, $<)

history.o: ../history.c config.h ../common.h ../frontend.h ../backend.h \
	../lists.h
	$(call compile, $<)

wevalgraph.o: wevalgraph.c ../evalgraph.h config.h ../common.h ../frontend.h \
	../backend.h ../lists.h winboard.h resource.h wsnap.h
	$(call compile, $<)

evalgraph.o: ../evalgraph.c ../evalgraph.h config.h ../common.h ../frontend.h \
	../backend.h ../lists.h
	$(call compile, $<)

wlayout.o: wlayout.c config.h ../common.h ../frontend.h winboard.h resource.h
	$(call compile, $<)

wsockerr.o: wsockerr.c wsockerr.h
	$(call compile, $<)

help.o: help.c help.h
	$(call compile, $<)

wsnap.o: wsnap.c wsnap.h
	$(call compile, $<)

pgntags.o: ../pgntags.c config.h ../common.h ../frontend.h ../backend.h \
	../parser.h ../lists.h
	$(call compile, $<)

zippy.o: ../zippy.c config.h ../common.h ../zippy.h ../frontend.h \
	../backend.h ../backendz.h ../lists.h
	$(call compile, $<)

book.o: ../book.c ../common.h ../backend.h ../lists.h
	$(call compile, $<)

uci.o: ../uci.c ../common.h ../backend.h ../frontend.h ../lists.h
	$(call compile, $<)

wsettings.o: wsettings.c ../common.h ../backend.h ../frontend.h ../lists.h
	$(call compile, $<)

wchat.o: wchat.c winboard.h wsnap.h ../common.h ../backend.h ../frontend.h ../lists.h
	$(call compile, $<)

%.o: %.c
	$(call compile, $<)
