#
# Makefile for Windows systems using gmake
#
#
# Try "make" to do a default build, using cygwin.
#
# Try "make CONSOLE=yes" to use PDCurses, as long as you set
# PDCURSES_INCLUDE_DIR and PDCURSES_LIB.
#
# Other options:
#   OBJ_DIR=x will place .o files into directory 'x'
#   MINGW=yes will use the mingw compiler
#

# Executable name and default target
EXE = angband.exe
all: ../$(EXE)

ifneq (,$(findstring \system32,$(PATH)))
  CP = copy
  RM = del
else
  CP = -cp
  RM = -rm
endif


ifndef MINGW
  CYGWIN=yes
endif

# Include list of object files and add system-specific ones
include Makefile.inc

ifdef CONSOLE
  CFLAGS = -DUSE_GCU -DWIN32_CONSOLE_MODE -I$(PDCURSES_INC)
  LIBS = -s $(PDCURSES_LIB)
  IOBJS = $(BASEOBJS) main-gcu.o main.o

  #PDCURSES_INC = ../../pdcurses/include
  #PDCURSES_LIB = ../../pdcurses/lib/pdcurses.a
else
  CFLAGS = -DWINDOWS
  IOBJS = $(BASEOBJS) win/angband.res main-win.o win/readdib.o
  LIBS = -s -mwindows -lwinmm
endif

CC = $(CROSS)gcc
WRES = $(CROSS)windres
CFLAGS += -Wall -W -std=iso9899:199409 -pedantic -O2 -I.
LIBS += -s

ifdef BUILD_ID
  CFLAGS += -DBUILD_ID="$(BUILD_ID)"
endif

ifdef CYGWIN

  LIBS += -mno-cygwin -e _mainCRTStartup
  CFLAGS += -mno-cygwin

  # Note the additional -DNEAR= flag to cope with the 'NEAR'
  # keyword used in readdib.c
  win/readdib.o: win/readdib.c win/readdib.h
	$(CC) $(CFLAGS) -DNEAR= $(INCDIRS) -c -o $@ $<

endif


# Handle cross compiles
ifdef CROSS
  CFLAGS += -DCROSS_COMPILE

  ifdef MINGW
    # This is an issue on mingw32 on linux
    CFLAGS += -D_stdcall=
  endif
endif

# Handle object output in a different directory
ifdef OBJDIR
  OBJS = $(patsubst %.o, $(OBJDIR)%.o, $(IOBJS))
  mkdirs:; -mkdir -p $(OBJDIR) $(OBJDIR)win
  $(EXE): mkdirs
else
  OBJS = $(IOBJS)
endif



#
# Targets
#

../$(EXE): $(EXE)
	$(CP) $(EXE) ..

$(EXE): $(OBJS)
	$(CC) -o $(EXE) $(OBJS) $(LIBS)

clean:
	$(RM) -f $(EXE) $(OBJS)


#
# Rules
#

win/angband.res: win/angband.rc
	$(WRES) $< -O coff -o $@

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