#
#  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
EXE = .exe
OBJ = .o
HTML = html

PLATFORM_DIR = obj/mingw32

ifdef CROSSCOMPILE
   UNIX_TOOLS = 1
endif

ifdef ALLEGRO_USE_CYGWIN
   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 Please specify your MinGW32 directory via MINGDIR=
	@echo See the docs/build/mingw32.txt file!
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 -Werror -Wno-unused
else
   WFLAGS = -Wall -Wno-unused
endif

ifdef TARGET_ARCH_COMPAT
   TARGET_ARCH = -mcpu=$(TARGET_ARCH_COMPAT)
else
   ifdef TARGET_ARCH_EXCL
      TARGET_ARCH = -march=$(TARGET_ARCH_EXCL)
   else
      TARGET_ARCH = -mcpu=pentium
   endif
endif

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

OFLAGS = $(TARGET_ARCH) $(TARGET_OPTS)

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 -fno-strength-reduce
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) cmiscs
CFLAGS += -DALLEGRO_USE_C

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 scrsave wfixicon

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

DISTCLEAN_FILES += $(addprefix tests/win/, dibgrab.exe dibhello.exe dibsound.exe scrsave.scr) tools/win/wfixicon.exe



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

ifdef UNIX_TOOLS

ifndef STATICLINK

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

endif # STATICLINK

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

$(MINGDIR_U)/include:
	mkdir $(MINGDIR_U)/include

$(MINGDIR_U)/include/allegro.h: include/allegro.h $(MINGDIR_U)/include
	cp include/allegro.h $(MINGDIR_U)/include

$(MINGDIR_U)/include/winalleg.h: include/winalleg.h $(MINGDIR_U)/include
	cp include/winalleg.h $(MINGDIR_U)/include

$(MINGDIR_U)/include/allegro: $(MINGDIR_U)/include
	mkdir $(MINGDIR_U)/include/allegro 

$(MINGDIR_U)/include/allegro/%.h: include/allegro/%.h
	cp $< $@

$(MINGDIR_U)/include/allegro/internal:
	mkdir $(MINGDIR_U)/include/allegro/internal

$(MINGDIR_U)/include/allegro/internal/%.h: include/allegro/internal/%.h
	cp $< $@

$(MINGDIR_U)/include/allegro/inline:
	mkdir $(MINGDIR_U)/include/allegro/inline

$(MINGDIR_U)/include/allegro/inline/%.inl: include/allegro/inline/%.inl
	cp $< $@

$(MINGDIR_U)/include/allegro/platform:
	mkdir -p $(MINGDIR_U)/include/allegro/platform

$(MINGDIR_U)/include/allegro/platform/%.h: include/allegro/platform/%.h
	cp $< $@

else

ifndef STATICLINK

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

endif # STATICLINK

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

$(MINGDIR_U)/include:
	md $(MINGDIR_D)\include

$(MINGDIR_U)/include/allegro.h: include/allegro.h $(MINGDIR_U)/include
	copy include\allegro.h $(MINGDIR_D)\include

$(MINGDIR_U)/include/winalleg.h: include/winalleg.h $(MINGDIR_U)/include
	copy include\winalleg.h $(MINGDIR_D)\include

$(MINGDIR_U)/include/allegro: $(MINGDIR_U)/include
	md $(MINGDIR_D)\include\allegro 

$(MINGDIR_U)/include/allegro/%.h: include/allegro/%.h
	copy $(subst /,\,$< $@)

$(MINGDIR_U)/include/allegro/internal:
	md $(MINGDIR_D)\include\allegro\internal

$(MINGDIR_U)/include/allegro/internal/%.h: include/allegro/internal/%.h
	copy $(subst /,\,$< $@)

$(MINGDIR_U)/include/allegro/inline:
	md $(MINGDIR_D)\include\allegro\inline

$(MINGDIR_U)/include/allegro/inline/%.inl: include/allegro/inline/%.inl
	copy $(subst /,\,$< $@)

$(MINGDIR_U)/include/allegro/platform:
	md $(MINGDIR_D)\include\allegro\platform

$(MINGDIR_U)/include/allegro/platform/%.h: include/allegro/platform/%.h
	copy $(subst /,\,$< $@)

endif # UNIX_TOOLS


HEADERS = $(addprefix $(MINGDIR_U)/,$(wildcard include/allegro/*.h))          \
          $(addprefix $(MINGDIR_U)/,$(wildcard include/allegro/internal/*.h)) \
          $(addprefix $(MINGDIR_U)/,$(wildcard include/allegro/inline/*.inl))

INSTALL_FILES = $(MINGDIR_U)/lib/$(IMPLIB_BASENAME)              \
		$(MINGDIR_U)/include/allegro.h                   \
		$(MINGDIR_U)/include/winalleg.h                  \
		$(MINGDIR_U)/include/allegro                     \
		$(MINGDIR_U)/include/allegro/internal            \
		$(MINGDIR_U)/include/allegro/inline              \
		$(MINGDIR_U)/include/allegro/platform            \
		$(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/alwin.h    \
		$(HEADERS)

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

install: $(INSTALL_FILES)
	@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)/include/allegro.h            \
                  $(MINGDIR_U)/include/winalleg.h           \
                  $(MINGDIR_U)/include/allegro/*.h          \
                  $(MINGDIR_U)/include/allegro/internal/*.h \
                  $(MINGDIR_U)/include/allegro/inline/*.inl \
                  $(MINGDIR_U)/include/allegro/platform/*.h \
                  $(WINDIR_U)/alleg$(LIBRARY_VERSION).dll   \
                  $(WINDIR_U)/alld$(LIBRARY_VERSION).dll    \
                  $(WINDIR_U)/allp$(LIBRARY_VERSION).dll

uninstall:
ifdef UNIX_TOOLS
	-rm -fv $(UNINSTALL_FILES)
	-rmdir $(MINGDIR_U)/include/allegro/platform
	-rmdir $(MINGDIR_U)/include/allegro/inline
	-rmdir $(MINGDIR_U)/include/allegro/internal
	-rmdir $(MINGDIR_U)/include/allegro
else
   define RM_FILES
      $(foreach file, $(wildcard $(UNINSTALL_FILES)), del $(subst /,\,$(file))
      )
   endef
	-$(RM_FILES)
	-rd $(MINGDIR_D)\include\allegro\platform
	-rd $(MINGDIR_D)\include\allegro\inline
	-rd $(MINGDIR_D)\include\allegro\internal
	-rd $(MINGDIR_D)\include\allegro
endif
	@echo All gone!



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

.PHONY: compress

compress: $(PROGRAMS)
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 assembler capabilities --------

include makefile.tst



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

ifndef CROSSCOMPILE
   DLLWRAP = dllwrap
   WINDRES = windres
else
   DLLWRAP = i386-mingw32msvc-dllwrap
   WINDRES = i386-mingw32msvc-windres
endif

ifdef STATICLINK

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

define MAKE_LIB
   ar rs $(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
	gcc $(COMPILE_FLAGS) -I. -I./include -o $@ -c $<

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

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

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

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

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

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

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

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

LINK_CONSOLE_DEPS = $(IMPLIB_NAME)

define LINK_CONSOLE
   gcc $(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
	gcc $(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
	gcc -O $(WFLAGS) $(PFLAGS) -I. -I./include -o obj/mingw32/asmdef.exe src/i386/asmdef.c

endif # CROSSCOMPILE

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

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

define MAKE_PLUGIN_LIB
   ar rs $(PLUGIN_LIB) $(PLUGIN_OBJS)
endef

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

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



# -------- demo program iconification --------

.PHONY: fixdemo

$(OBJ_DIR)/demo.res: demo/demo.dat tools/win/wfixicon.exe
	tools/win/wfixicon.exe $(OBJ_DIR)/demo.ico -ro -d demo/demo.dat SHIP3 GAME_PAL

fixdemo: $(OBJ_DIR)/demo.o $(OBJ_DIR)/demo.res $(IMPLIB_NAME)
	gcc $(LFLAGS) $(PFLAGS) -Wl,--subsystem,windows -o demo/demo.exe $(OBJ_DIR)/demo.o $(OBJ_DIR)/demo.res $(IMPLIB_NAME) $(LIBRARIES)



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

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

depend:
	gcc $(DEPEND_PARAMS) src/*.c src/c/*.c src/i386/*.c src/misc/*.c src/win/*.c demo/*.c examples/*.c setup/*.c tests/*.c tests/win/*.c tools/*.c tools/win/*.c tools/plugins/*.c > _depend.tmp
	gcc $(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
