#
#  Rules for building the Allegro library with MingW32. This file is included
#  by the primary makefile, and should not be used directly.
#
#  The "depend" target uses sed.
#
#  See makefile.all for a list of the available targets.



# -------- define some variables that the primary makefile will use --------

PLATFORM = MinGW32
CC = gcc
EXE = .exe
OBJ = .o
HTML = html

ifndef CROSSCOMPILE
   PLUGIN_RUNNER = obj/mingw32/runner.exe
endif

PLATFORM_DIR = obj/mingw32

ifdef CROSSCOMPILE
   UNIX_TOOLS = 1
   CC = $(XPREFIX)gcc
endif

ifdef ALLEGRO_USE_CYGWIN
   UNIX_TOOLS = 1
endif

ifneq (,$(findstring /sh.exe,$(SHELL)))
   UNIX_TOOLS = 1
endif



ifdef STATICLINK

# -------- link as a static library --------

OBJ_DIR = obj/mingw32/$(VERSION)_s
IMPLIB_BASENAME = lib$(VERSION)_s.a
IMPLIB_NAME = lib/mingw32/$(IMPLIB_BASENAME)
LIB_NAME = $(IMPLIB_NAME)

else

# -------- link as a DLL --------

OBJ_DIR = obj/mingw32/$(VERSION)
DLL_BASENAME = $(VERSION)$(LIBRARY_VERSION).dll
DLL_NAME = lib/mingw32/$(DLL_BASENAME)
IMPLIB_BASENAME = lib$(VERSION).a
IMPLIB_NAME = lib/mingw32/$(IMPLIB_BASENAME)
LIB_NAME = $(DLL_NAME) $(IMPLIB_NAME)
EXPDEF_NAME = lib/mingw32/allegro.def

endif # STATICLINK



# -------- check that environment path variable is set --------

.PHONY: badwin badming

ifdef ALLEGRO_USE_CYGWIN

WINDIR_S := $(shell cygpath -S)
WINDIR_U = $(subst \,/,$(WINDIR_S))
WINDIR_D = $(subst /,\,$(WINDIR_S))

else

ifeq ($(OS),Windows_NT)
   WINSYSDIR = $(SYSTEMROOT)
   ifeq ($(WINSYSDIR),)
      WINSYSDIR = $(SystemRoot)
   endif
   WINSUBDIR = system32
else
   WINSYSDIR = $(WINDIR)
   ifeq ($(WINSYSDIR),)
      WINSYSDIR = $(windir)
   endif
   WINSUBDIR = system
endif

ifneq ($(WINSYSDIR),)
   WINDIR_U = $(subst \,/,$(WINSYSDIR)/$(WINSUBDIR))
   WINDIR_D = $(subst /,\,$(WINSYSDIR)\$(WINSUBDIR))
else
ifndef CROSSCOMPILE
badwin:
	@echo Your SYSTEMROOT or WINDIR environment variable is not set!
endif
endif

endif # ALLEGRO_USE_CYGWIN

ifdef MINGDIR
   MINGDIR_U = $(subst \,/,$(MINGDIR))
   MINGDIR_D = $(subst /,\,$(MINGDIR))
else
badming:
	@echo Your MINGDIR environment variable is not set!
	@echo See the docs/build/mingw32.txt file!
endif

NULLSTRING :=
SPACE := $(NULLSTRING) # special magic to get an isolated space character

ifneq ($(findstring $(SPACE),$(MINGDIR)),)
badspaces:
	@echo There are spaces in your MINGDIR environment variable:
	@echo please change it to the 8.3 short filename version,
	@echo or move your compiler to a different directory.
endif



# -------- give a sensible default target for make without any args --------

.PHONY: _default

_default: default



# -------- decide what compiler options to use --------

ifdef ALLEGRO_USE_CYGWIN
   PFLAGS = -mno-cygwin
endif

ifdef WARNMODE
   WFLAGS = -Wall -W -Wstrict-prototypes -Wno-unused -Werror
else
   WFLAGS = -Wall -Wno-unused
endif

ifdef TARGET_ARCH_COMPAT
   TARGET_ARCH = $(GCC_MTUNE)=$(TARGET_ARCH_COMPAT)
else
   ifdef TARGET_ARCH_EXCL
      TARGET_ARCH = -march=$(TARGET_ARCH_EXCL)
   else
      TARGET_ARCH = $(GCC_MTUNE)=i586
   endif
endif

ifndef TARGET_OPTS
   TARGET_OPTS = -O2 -funroll-loops -ffast-math
endif

OFLAGS = $(TARGET_ARCH) $(TARGET_OPTS)

CFLAGS = -DALLEGRO_LIB_BUILD



ifdef DEBUGMODE

# -------- debugging build --------

CFLAGS += -DDEBUGMODE=$(DEBUGMODE) $(WFLAGS) $(PFLAGS) -g
SFLAGS = -DDEBUGMODE=$(DEBUGMODE) $(WFLAGS)
LFLAGS += -g

else
ifdef PROFILEMODE

# -------- profiling build --------

CFLAGS += $(WFLAGS) $(OFLAGS) $(PFLAGS) -pg
SFLAGS = $(WFLAGS)
LFLAGS += -pg

else

# -------- optimised build --------

CFLAGS += $(WFLAGS) $(OFLAGS) $(PFLAGS) -fomit-frame-pointer
SFLAGS = $(WFLAGS)
ifndef SYMBOLMODE
   LFLAGS += -s
endif

endif
endif



# -------- list platform specific objects and programs --------

VPATH = src/win src/misc tests/win tools/win

ifdef ALLEGRO_USE_C

# ------ build a C-only version ------

VPATH += src/c
MY_OBJECTS = $(C_OBJECTS)
CFLAGS += -DALLEGRO_NO_ASM
SFLAGS += -DALLEGRO_NO_ASM

else

# ------ build the normal asm version ------

VPATH += src/i386
MY_OBJECTS = $(I386_OBJECTS)

endif # ALLEGRO_USE_C

OBJECT_LIST = $(COMMON_OBJECTS) $(MY_OBJECTS) $(basename $(notdir $(ALLEGRO_SRC_WIN_FILES)))

LIBRARIES = -lkernel32 -luser32 -lgdi32 -lcomdlg32 -lole32 -ldinput -lddraw -ldxguid -lwinmm -ldsound

ifdef PROFILEMODE
   LIBRARIES += -lgmon
endif

ifdef ALLEGRO_USE_CYGWIN
   LIBRARIES += -L/lib/mingw -lmoldname -lmsvcrt
endif

PROGRAMS = dibgrab dibhello dibsound dxwindow scrsave wfixicon

dibgrab: tests/win/dibgrab.exe
dibhello: tests/win/dibhello.exe
dibsound: tests/win/dibsound.exe
dxwindow: tests/win/dxwindow.exe
scrsave: tests/win/scrsave.scr
wfixicon: tools/win/wfixicon.exe



# -------- rules for installing and removing the library files --------

INSTALLDIR = $(MINGDIR_U)
LIBDIR = lib
INCDIR = include


ifdef UNIX_TOOLS

$(MINGDIR_U)/lib/$(IMPLIB_BASENAME): $(IMPLIB_NAME) $(MINGDIR_U)/lib
	cp lib/mingw32/$(IMPLIB_BASENAME) $(MINGDIR_U)/lib

$(WINDIR_U)/$(DLL_BASENAME): $(DLL_NAME)
	cp lib/mingw32/$(DLL_BASENAME) $(WINDIR_U)

else

$(MINGDIR_U)/lib/$(IMPLIB_BASENAME): $(IMPLIB_NAME) $(MINGDIR_U)/lib
	copy lib\mingw32\$(IMPLIB_BASENAME) $(MINGDIR_D)\lib

$(WINDIR_U)/$(DLL_BASENAME): $(DLL_NAME)
	copy lib\mingw32\$(DLL_BASENAME) $(WINDIR_D)

endif # UNIX_TOOLS


HEADERS = $(MINGDIR_U)/include/winalleg.h                  \
          $(MINGDIR_U)/include/allegro/platform/aintwin.h  \
          $(MINGDIR_U)/include/allegro/platform/al386gcc.h \
          $(MINGDIR_U)/include/allegro/platform/almngw32.h \
          $(MINGDIR_U)/include/allegro/platform/alplatf.h  \
          $(MINGDIR_U)/include/allegro/platform/astdint.h  \
          $(MINGDIR_U)/include/allegro/platform/alwin.h

INSTALL_FILES = $(MINGDIR_U)/lib/$(IMPLIB_BASENAME)

ifndef CROSSCOMPILE
   ifndef STATICLINK
      INSTALL_FILES += $(WINDIR_U)/$(DLL_BASENAME)
   endif
endif

INSTALL_FILES += $(HEADERS)

install: generic-install
	@echo The $(DESCRIPTION) $(PLATFORM) library has been installed.

UNINSTALL_FILES = $(MINGDIR_U)/lib/liballeg.a             \
                  $(MINGDIR_U)/lib/liballd.a              \
                  $(MINGDIR_U)/lib/liballp.a              \
                  $(MINGDIR_U)/lib/liballeg_s.a           \
                  $(MINGDIR_U)/lib/liballd_s.a            \
                  $(MINGDIR_U)/lib/liballp_s.a            \
                  $(WINDIR_U)/alleg$(LIBRARY_VERSION).dll \
                  $(WINDIR_U)/alld$(LIBRARY_VERSION).dll  \
                  $(WINDIR_U)/allp$(LIBRARY_VERSION).dll  \
                  $(HEADERS)

uninstall: generic-uninstall
	@echo All gone!



# -------- helper function for compressing the executables --------

.PHONY: compress

compress:
ifdef UPX_BIN
	$(UPX_BIN) demo/*.exe examples/*.exe setup/*.exe tests/*.exe tools/*.exe lib/mingw32/all*.dll
else
	@echo No executable compressor specified! You must set the environment variable
	@echo UPX_BIN to point to upx.exe.
endif



# -------- test capabilities --------

TEST_CPP = $(CC) -fno-exceptions -c src/misc/test.cpp -o $(PLATFORM_DIR)/test.o

include makefile.tst



# -------- finally, we get to the fun part... --------

ifndef CROSSCOMPILE
   DLLWRAP = dllwrap
   WINDRES = windres
   AR = ar
else
   DLLWRAP = $(XPREFIX)dllwrap
   WINDRES = $(XPREFIX)windres
   AR = $(XPREFIX)ar
endif

ifdef STATICLINK

# -------- link as a static library --------

define MAKE_LIB
   $(AR) crs $(LIB_NAME) $(OBJECTS)
endef

COMPILE_FLAGS = -DALLEGRO_STATICLINK 

else

# -------- link as a DLL --------

define MAKE_LIB
   $(DLLWRAP) $(PFLAGS) --target i386-mingw32 --def $(EXPDEF_NAME) -o $(DLL_NAME) $(OBJECTS) $(LIBRARIES) --output-lib $(IMPLIB_NAME)
endef

endif # STATICLINK

COMPILE_FLAGS += $(subst src/,-DALLEGRO_SRC ,$(findstring src/, $<))$(CFLAGS)

$(OBJ_DIR)/%.o: %.c
	$(CC) $(COMPILE_FLAGS) -I. -I./include -o $@ -c $<

$(OBJ_DIR)/%.o: %.cpp
	$(CC) -fno-exceptions $(COMPILE_FLAGS) -I. -I./include -o $@ -c $<

$(OBJ_DIR)/%.o: %.s
	$(CC) $(SFLAGS) -I. -I./include -x assembler-with-cpp -o $@ -c $<

$(OBJ_DIR)/%.o: %.rc
	$(WINDRES) --include-dir include -O coff -o $@ -i $<

obj/mingw32/%.res: %.rc
	$(WINDRES) -O coff -o $@ -i $<

tests/win/dibsound.exe: $(OBJ_DIR)/dibsound.o obj/mingw32/dibsound.res $(IMPLIB_NAME)
	$(CC) $(LFLAGS) $(PFLAGS) -Wl,--subsystem,windows -o tests/win/dibsound.exe $(OBJ_DIR)/dibsound.o obj/mingw32/dibsound.res $(IMPLIB_NAME) $(LIBRARIES)

tests/win/dxwindow.exe: $(OBJ_DIR)/dxwindow.o obj/mingw32/dxwindow.res $(IMPLIB_NAME)
	$(CC) $(LFLAGS) $(PFLAGS) -Wl,--subsystem,windows -o tests/win/dxwindow.exe $(OBJ_DIR)/dxwindow.o obj/mingw32/dxwindow.res $(IMPLIB_NAME) $(LIBRARIES)

tests/win/%.exe: $(OBJ_DIR)/%.o $(IMPLIB_NAME)
	$(CC) $(LFLAGS) $(PFLAGS) -Wl,--subsystem,windows -o $@ $< $(IMPLIB_NAME) $(LIBRARIES)

tests/win/scrsave.scr: $(OBJ_DIR)/scrsave.o obj/mingw32/scrsave.res $(IMPLIB_NAME)
	$(CC) $(LFLAGS) $(PFLAGS) -Wl,--subsystem,windows -o tests/win/scrsave.scr $(OBJ_DIR)/scrsave.o obj/mingw32/scrsave.res $(IMPLIB_NAME) $(LIBRARIES)

tools/win/%.exe: $(OBJ_DIR)/%.o $(IMPLIB_NAME)
	$(CC) $(LFLAGS) $(PFLAGS) -Wl,--subsystem,console -o $@ $< $(IMPLIB_NAME) $(LIBRARIES)

ifdef CROSSCOMPILE

demo/demo.exe: $(IMPLIB_NAME)
	$(CC) $(LFLAGS) $(PFLAGS) -Wl,--subsystem,windows -o $@ $(OBJECTS_DEMO) $(IMPLIB_NAME) $(LIBRARIES)

setup/setup.exe: $(OBJ_DIR)/setup.o $(IMPLIB_NAME)
	$(CC) $(LFLAGS) $(PFLAGS) -Wl,--subsystem,windows -o $@ $< $(IMPLIB_NAME) $(LIBRARIES)

else

obj/mingw32/demo.res: demo/demo.dat tools/win/wfixicon.exe
ifdef STATICLINK
	tools/win/wfixicon.exe obj/mingw32/demo.ico -ro -d demo/demo.dat SHIP3 GAME_PAL
else
   ifdef UNIX_TOOLS
	cp tools/win/wfixicon.exe lib/mingw32/wfixicon.exe
   else
	copy tools\win\wfixicon.exe lib\mingw32\wfixicon.exe
   endif
	lib/mingw32/wfixicon.exe obj/mingw32/demo.ico -ro -d demo/demo.dat SHIP3 GAME_PAL
   ifdef UNIX_TOOLS
	rm lib/mingw32/wfixicon.exe
   else
	del lib\mingw32\wfixicon.exe
   endif
endif

demo/demo.exe: obj/mingw32/demo.res $(IMPLIB_NAME)
	$(CC) $(LFLAGS) $(PFLAGS) -Wl,--subsystem,windows -o $@ $(OBJECTS_DEMO) obj/mingw32/demo.res $(IMPLIB_NAME) $(LIBRARIES)

obj/mingw32/setupdat.s: setup/setup.dat tools/dat2s.exe
ifdef STATICLINK
	tools/dat2s.exe -p setup -o obj/mingw32/setupdat.s setup/setup.dat
else
   ifdef UNIX_TOOLS
	cp tools/dat2s.exe lib/mingw32/dat2s.exe
   else
	copy tools\dat2s.exe lib\mingw32\dat2s.exe
   endif
	lib/mingw32/dat2s.exe -p setup -o obj/mingw32/setupdat.s setup/setup.dat
   ifdef UNIX_TOOLS
	rm lib/mingw32/dat2s.exe
   else
	del lib\mingw32\dat2s.exe
   endif
endif

obj/mingw32/setupdat.o: obj/mingw32/setupdat.s
	$(CC) -o obj/mingw32/setupdat.o -c obj/mingw32/setupdat.s

$(OBJ_DIR)/setup.o: setup/setup.c
	$(CC) $(COMPILE_FLAGS) -DSETUP_USE_COMPILED_DATAFILES -I. -I./include -o $(OBJ_DIR)/setup.o -c setup/setup.c

setup/setup.exe: $(OBJ_DIR)/setup.o obj/mingw32/setupdat.o $(IMPLIB_NAME)
	$(CC) $(LFLAGS) $(PFLAGS) -Wl,--subsystem,windows -o setup/setup.exe $(OBJ_DIR)/setup.o obj/mingw32/setupdat.o $(IMPLIB_NAME) $(LIBRARIES)

endif # CROSSCOMPILE

examples/%.exe setup/%.exe tests/%.exe: $(OBJ_DIR)/%.o $(IMPLIB_NAME)
	$(CC) $(LFLAGS) $(PFLAGS) -Wl,--subsystem,windows -o $@ $< $(IMPLIB_NAME) $(LIBRARIES)

LINK_CONSOLE_DEPS = $(IMPLIB_NAME)

define LINK_CONSOLE
   $(CC) $(LFLAGS) $(PFLAGS) -Wl,--subsystem,console -o $@ $< $(IMPLIB_NAME) $(LIBRARIES)
endef

ifdef CROSSCOMPILE

obj/mingw32/asmdef.s: src/i386/asmdef.c include/*.h include/allegro/*.h obj/mingw32/asmcapa.h
	$(CC) $(WFLAGS) -I. -I./include -S -o obj/mingw32/asmdef.s src/i386/asmdef.c

obj/mingw32/asmdef.inc: obj/mingw32/asmdef.s
	if [ ! -x misc/asmdef.sh ]; then chmod a+x misc/asmdef.sh; fi
	misc/asmdef.sh obj/mingw32/asmdef.s obj/mingw32/asmdef.inc

else

obj/mingw32/asmdef.inc: obj/mingw32/asmdef.exe
	obj/mingw32/asmdef.exe obj/mingw32/asmdef.inc

obj/mingw32/asmdef.exe: src/i386/asmdef.c
	$(CC) -O $(WFLAGS) $(PFLAGS) -I. -I./include -o obj/mingw32/asmdef.exe src/i386/asmdef.c

obj/mingw32/runner.exe: src/misc/runnergw.c
	$(CC) -O -Wall -Werror -o obj/mingw32/runner.exe src/misc/runnergw.c

endif # CROSSCOMPILE

define LINK_WITHOUT_LIB
   $(CC) $(LFLAGS) $(PFLAGS) -Wl,--subsystem,console -o $@ $^
endef

PLUGIN_LIB = lib/mingw32/lib$(VERY_SHORT_VERSION)dat.a
PLUGIN_DEPS = $(IMPLIB_NAME) $(PLUGIN_LIB) $(PLUGIN_RUNNER)
PLUGINS_H = obj/mingw32/plugins.h
PLUGIN_SCR = scm

ifdef UNIX_TOOLS
   define GENERATE_PLUGINS_H
      cat tools/plugins/*.inc > obj/mingw32/plugins.h
   endef
else
   define GENERATE_PLUGINS_H
      copy /B tools\plugins\*.inc obj\mingw32\plugins.h
   endef
endif

define MAKE_PLUGIN_LIB
   $(AR) crs $(PLUGIN_LIB) $(PLUGIN_OBJS)
endef

define LINK_WITH_PLUGINS
   $(PLUGIN_RUNNER) $(CC) $(LFLAGS) $(PFLAGS) -Wl,--subsystem,windows -o $@ $< $(strip $(PLUGIN_LIB) $(addprefix @,$(PLUGIN_SCRIPTS)) $(IMPLIB_NAME) $(LIBRARIES))
endef

define LINK_CONSOLE_WITH_PLUGINS
   $(PLUGIN_RUNNER) $(CC) $(LFLAGS) $(PFLAGS) -Wl,--subsystem,console -o $@ $< $(strip $(PLUGIN_LIB) $(addprefix @,$(PLUGIN_SCRIPTS)) $(IMPLIB_NAME) $(LIBRARIES))
endef



# -------- generate automatic dependencies --------

DEPEND_PARAMS = -MM -MG -I. -I./include -DSCAN_DEPEND -DALLEGRO_MINGW32

depend:
	$(CC) $(DEPEND_PARAMS) src/*.c src/c/*.c src/i386/*.c src/misc/*.c src/win/*.c demo/*.c > _depend.tmp
	$(CC) $(DEPEND_PARAMS) docs/src/makedoc/*.c examples/*.c setup/*.c tests/*.c tests/win/*.c >> _depend.tmp
	$(CC) $(DEPEND_PARAMS) tools/*.c tools/win/*.c tools/plugins/*.c >> _depend.tmp
	$(CC) $(DEPEND_PARAMS) -x c tests/*.cpp >> _depend.tmp
	$(CC) $(DEPEND_PARAMS) -x assembler-with-cpp src/c/*.s src/i386/*.s src/misc/*.s src/win/*.s >> _depend.tmp
	sed -e "s/^[a-zA-Z0-9_\/]*\///" _depend.tmp > _depend2.tmp
ifdef UNIX_TOOLS
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/mingw32\/alleg\/\1\.o:/" _depend2.tmp > obj/mingw32/alleg/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/mingw32\/alld\/\1\.o:/" _depend2.tmp > obj/mingw32/alld/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/mingw32\/allp\/\1\.o:/" _depend2.tmp > obj/mingw32/allp/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/mingw32\/alleg_s\/\1\.o:/" _depend2.tmp > obj/mingw32/alleg_s/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/mingw32\/alld_s\/\1\.o:/" _depend2.tmp > obj/mingw32/alld_s/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/mingw32\/allp_s\/\1\.o:/" _depend2.tmp > obj/mingw32/allp_s/makefile.dep
	rm _depend.tmp _depend2.tmp
else
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/mingw32\/alleg\/\1\.o:/" _depend2.tmp > obj\mingw32\alleg\makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/mingw32\/alld\/\1\.o:/" _depend2.tmp > obj\mingw32\alld\makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/mingw32\/allp\/\1\.o:/" _depend2.tmp > obj\mingw32\allp\makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/mingw32\/alleg_s\/\1\.o:/" _depend2.tmp > obj\mingw32\alleg_s\makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/mingw32\/alld_s\/\1\.o:/" _depend2.tmp > obj\mingw32\alld_s\makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/mingw32\/allp_s\/\1\.o:/" _depend2.tmp > obj\mingw32\allp_s\makefile.dep
	del _depend.tmp
	del _depend2.tmp
endif
