#
#  Rules for building the Allegro library with MSVC. This file is included
#  by the primary makefile, and should not be used directly.
#
#  To path MSVC so it can be used from the commandline, run the
#  vcvars32.bat file which can be found in your MSVC bin directory.
#
#  This build uses djgpp for building the assembler sources and calculating
#  source dependencies, so you'll need to have that installed as well.
#
#  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 = MSVC
RUNNER = obj/msvc/runner.exe
EXE = .exe
OBJ = .obj
HTML = html

PLATFORM_DIR = obj/msvc



ifdef STATICLINK

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

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

else

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

OBJ_DIR = obj/msvc/$(VERSION)
DLL_BASENAME = $(VERSION)$(LIBRARY_VERSION).dll
DLL_NAME = lib/msvc/$(DLL_BASENAME)
IMPLIB_BASENAME = $(VERSION).lib
IMPLIB_NAME = lib/msvc/$(IMPLIB_BASENAME)
LIB_NAME = $(DLL_NAME) $(IMPLIB_NAME)

endif



# -------- check that environment path variables are set --------

.PHONY: badwin badmsvc badspaces

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
badwin:
	@echo Your SYSTEMROOT or windir environment variable is not set!
endif

ifdef MSVCDIR
   MSVCDIR_U = $(subst \,/,$(MSVCDIR))
   MSVCDIR_D = $(subst /,\,$(MSVCDIR))
else
ifdef MSVCDir
   MSVCDIR_U = $(subst \,/,$(MSVCDir))
   MSVCDIR_D = $(subst /,\,$(MSVCDir))
else
badmsvc:
	@echo Your MSVCDIR environment variable is not set!
endif
endif

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

ifneq ($(findstring $(SPACE),$(MSVCDIR)),)
badspaces:
	@echo There are spaces in your MSVCDIR environment
	@echo variable: please change it to the 8.3 short filename
	@echo version, 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 WARNMODE
   WFLAGS = -W3 -WX
else
   WFLAGS = -W1
endif

ifdef DEBUGMODE

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

CFLAGS = -DDEBUGMODE=$(DEBUGMODE) $(WFLAGS) -Gd -Zi -MDd
SFLAGS = -DDEBUGMODE=$(DEBUGMODE) -Wall
LFLAGS = -debug -debugtype:cv

else
ifdef PROFILEMODE

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

CFLAGS = $(WFLAGS) -Gd -Ox -GB -MD
SFLAGS = -Wall
LFLAGS = -profile

else

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

CFLAGS = $(WFLAGS) -Gd -Ox -GB -MD
SFLAGS = -Wall
LFLAGS = -release

endif
endif



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

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

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

LIBRARIES = kernel32.lib user32.lib gdi32.lib comdlg32.lib ole32.lib \
		dinput.lib ddraw.lib dxguid.lib winmm.lib dsound.lib

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

$(MSVCDIR_U)/lib:
	mkdir $(MSVCDIR_U)/lib

ifndef STATICLINK

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

endif

$(MSVCDIR_U)/lib/$(IMPLIB_BASENAME): $(IMPLIB_NAME)
	cp lib/msvc/$(IMPLIB_BASENAME) $(MSVCDIR_U)/lib

$(MSVCDIR_U)/include:
	mkdir $(MSVCDIR_U)/include

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

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

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

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

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

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

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

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

$(MSVCDIR_U)/include/allegro/platform:
	mkdir $(MSVCDIR_U)/include/allegro/platform

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

else

$(MSVCDIR_U)/lib:
	md $(MSVCDIR_D)\lib

ifndef STATICLINK

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

endif

$(MSVCDIR_U)/lib/$(IMPLIB_BASENAME): $(IMPLIB_NAME)
	copy lib\msvc\$(IMPLIB_BASENAME) $(MSVCDIR_D)\lib

$(MSVCDIR_U)/include:
	md $(MSVCDIR_D)\include

$(MSVCDIR_U)/include/allegro.h: include/allegro.h
	copy include\allegro.h $(MSVCDIR_D)\include

$(MSVCDIR_U)/include/winalleg.h: include/winalleg.h
	copy include\winalleg.h $(MSVCDIR_D)\include

$(MSVCDIR_U)/include/allegro:
	md $(MSVCDIR_D)\include\allegro 

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

$(MSVCDIR_U)/include/allegro/internal:
	md $(MSVCDIR_D)\include\allegro\internal

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

$(MSVCDIR_U)/include/allegro/inline:
	md $(MSVCDIR_D)\include\allegro\inline

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

$(MSVCDIR_U)/include/allegro/platform:
	md $(MSVCDIR_D)\include\allegro\platform

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

endif


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

INSTALL_FILES = $(MSVCDIR_U)/lib/$(IMPLIB_BASENAME)             \
		$(MSVCDIR_U)/include/allegro.h                  \
		$(MSVCDIR_U)/include/allegro                    \
		$(MSVCDIR_U)/include/allegro/internal           \
		$(MSVCDIR_U)/include/allegro/inline             \
		$(MSVCDIR_U)/include/allegro/platform           \
		$(MSVCDIR_U)/include/winalleg.h                 \
		$(MSVCDIR_U)/include/allegro/platform/aintwin.h \
		$(MSVCDIR_U)/include/allegro/platform/al386vc.h \
		$(MSVCDIR_U)/include/allegro/platform/almsvc.h  \
		$(MSVCDIR_U)/include/allegro/platform/alplatf.h \
		$(MSVCDIR_U)/include/allegro/platform/alwin.h   \
		$(HEADERS)

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

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

UNINSTALL_FILES = $(MSVCDIR_U)/lib/alleg.lib                \
		  $(MSVCDIR_U)/lib/alld.lib                 \
		  $(MSVCDIR_U)/lib/allp.lib                 \
		  $(MSVCDIR_U)/include/allegro.h            \
		  $(MSVCDIR_U)/include/winalleg.h           \
		  $(MSVCDIR_U)/include/allegro/*.h          \
		  $(MSVCDIR_U)/include/allegro/internal/*.h \
		  $(MSVCDIR_U)/include/allegro/inline/*.inl \
		  $(MSVCDIR_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 $(MSVCDIR_U)/include/allegro/platform
	-rmdir $(MSVCDIR_U)/include/allegro/inline
	-rmdir $(MSVCDIR_U)/include/allegro/internal
	-rmdir $(MSVCDIR_U)/include/allegro
else
   define RM_FILES
      $(foreach file, $(wildcard $(UNINSTALL_FILES)), del $(subst /,\,$(file))
      )
   endef
	-$(RM_FILES)
	-rd $(MSVCDIR_D)\include\allegro\platform
	-rd $(MSVCDIR_D)\include\allegro\inline
	-rd $(MSVCDIR_D)\include\allegro\internal
	-rd $(MSVCDIR_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/msvc/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... --------

ifdef STATICLINK

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

define MAKE_LIB
   $(RUNNER) lib @ -nologo -out:$(LIB_NAME) $(OBJECTS)
endef

COMPILE_FLAGS = -DALLEGRO_STATICLINK 

else

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

define MAKE_LIB
   $(RUNNER) link @ -nologo $(LFLAGS) -dll -def:lib/msvc/allegro.def -out:$(DLL_NAME) -implib:$(IMPLIB_NAME) $(OBJECTS) $(LIBRARIES)
endef

endif

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

$(OBJ_DIR)/%.obj: %.c $(RUNNER)
	$(RUNNER) cl @ -nologo $(COMPILE_FLAGS) -I. -I./include -Fo$@ -c $<

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

$(OBJ_DIR)/%.obj: %.rc
	rc -i"include" -fo$@ $<

$(OBJ_DIR)/%.res: %.rc
	rc -fo$@ $<

tests/win/dibsound.exe: $(OBJ_DIR)/dibsound.obj $(OBJ_DIR)/dibsound.res $(IMPLIB_NAME) $(RUNNER)
	$(RUNNER) link @ -nologo $(LFLAGS) -subsystem:windows -out:tests/win/dibsound.exe $(OBJ_DIR)/dibsound.obj $(OBJ_DIR)/dibsound.res $(IMPLIB_NAME) $(LIBRARIES)

tests/win/%.exe: $(OBJ_DIR)/%.obj $(IMPLIB_NAME) $(RUNNER)
	$(RUNNER) link @ -nologo $(LFLAGS) -subsystem:windows -out:$@ $< $(IMPLIB_NAME) $(LIBRARIES)

tests/win/scrsave.scr: $(OBJ_DIR)/scrsave.obj $(OBJ_DIR)/scrsave.res $(IMPLIB_NAME) $(RUNNER)
	$(RUNNER) link @ -nologo $(LFLAGS) -subsystem:windows -out:tests/win/scrsave.scr $(OBJ_DIR)/scrsave.obj $(OBJ_DIR)/scrsave.res $(IMPLIB_NAME) $(LIBRARIES)

tools/win/%.exe: $(OBJ_DIR)/%.obj $(IMPLIB_NAME) $(RUNNER)
	$(RUNNER) link @ -nologo $(LFLAGS) -subsystem:console -out:$@ $< $(IMPLIB_NAME) $(LIBRARIES)

*/%.exe: $(OBJ_DIR)/%.obj $(IMPLIB_NAME) $(RUNNER)
	$(RUNNER) link @ -nologo $(LFLAGS) -subsystem:windows -out:$@ $< $(IMPLIB_NAME) $(LIBRARIES)

LINK_CONSOLE_DEPS = $(IMPLIB_NAME) $(RUNNER)

define LINK_CONSOLE
   $(RUNNER) link @ -nologo $(LFLAGS) -subsystem:console -out:$@ $< $(IMPLIB_NAME) $(LIBRARIES)
endef

obj/msvc/asmdef.inc: obj/msvc/asmdef.exe
	obj/msvc/asmdef.exe obj/msvc/asmdef.inc

obj/msvc/asmdef.exe: src/i386/asmdef.c include/*.h include/allegro/*.h obj/msvc/asmcapa.h $(RUNNER)
	$(RUNNER) cl @ -nologo $(WFLAGS) -I. -I./include -Foobj/msvc/asmdef.obj -Feobj/msvc/asmdef.exe src/i386/asmdef.c

obj/msvc/runner.exe: src/misc/runner.c
	gcc -O -Wall -Werror -o obj/msvc/runner.exe src/misc/runner.c

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

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

define MAKE_PLUGIN_LIB
   $(RUNNER) lib @ -nologo -out:$(PLUGIN_LIB) $(PLUGIN_OBJS)
endef

define LINK_WITH_PLUGINS
   $(RUNNER) link @ -nologo $(LFLAGS) -subsystem:windows -out:$@ $< $(strip $(PLUGIN_LIB) $(addprefix @,$(PLUGIN_SCRIPTS)) $(IMPLIB_NAME) $(LIBRARIES))
endef

define LINK_CONSOLE_WITH_PLUGINS
   $(RUNNER) link @ -nologo $(LFLAGS) -subsystem:console -out:$@ $< $(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.obj $(OBJ_DIR)/demo.res $(IMPLIB_NAME) $(RUNNER)
	$(RUNNER) link @ -nologo $(LFLAGS) -subsystem:windows -out:demo/demo.exe $(OBJ_DIR)/demo.obj $(OBJ_DIR)/demo.res $(IMPLIB_NAME) $(LIBRARIES)



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

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

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\/msvc\/alleg\/\1\.obj:/" _depend2.tmp > obj/msvc/alleg/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/msvc\/alld\/\1\.obj:/" _depend2.tmp > obj/msvc/alld/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/msvc\/allp\/\1\.obj:/" _depend2.tmp > obj/msvc/allp/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/msvc\/alleg_s\/\1\.obj:/" _depend2.tmp > obj/msvc/alleg_s/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/msvc\/alld_s\/\1\.obj:/" _depend2.tmp > obj/msvc/alld_s/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/msvc\/allp_s\/\1\.obj:/" _depend2.tmp > obj/msvc/allp_s/makefile.dep
	rm _depend.tmp _depend2.tmp
else
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/msvc\/alleg\/\1\.obj:/" _depend2.tmp > obj\msvc\alleg\makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/msvc\/alld\/\1\.obj:/" _depend2.tmp > obj\msvc\alld\makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/msvc\/allp\/\1\.obj:/" _depend2.tmp > obj\msvc\allp\makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/msvc\/alleg_s\/\1\.obj:/" _depend2.tmp > obj\msvc\alleg_s\makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/msvc\/alld_s\/\1\.obj:/" _depend2.tmp > obj\msvc\alld_s\makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/msvc\/allp_s\/\1\.obj:/" _depend2.tmp > obj\msvc\allp_s\makefile.dep
	del _depend.tmp
	del _depend2.tmp
endif
