#
#  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

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 = gfxlib.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)
WINSUBDIR = system32
else
WINSYSDIR = $(windir)
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 MSDEVDIR
MSVCDIR_U = $(subst \,/,$(MSDEVDIR))
MSVCDIR_D = $(subst /,\,$(MSDEVDIR))
else
badmsvc:
	@echo Your MSVCDIR or MSDEVDIR environment variable is not set!
endif
endif

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

ifneq ($(findstring $(SPACE),$(MSVCDIR)$(MSDEVDIR)),)
badspaces:
	@echo There are spaces in your MSVCDIR or MSDEVDIR environment
	@echo variables: please change these 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 -MTd
SFLAGS = -DDEBUGMODE=$(DEBUGMODE) -Wall
LFLAGS = -debug -debugtype:cv

else
ifdef PROFILEMODE

# -------- profiling build --------
CFLAGS = $(WFLAGS) -Gd -Ox -GB -MT
SFLAGS = -Wall
LFLAGS = -profile

else

# -------- optimised build --------
CFLAGS = $(WFLAGS) -Gd -Ox -GB -MT
SFLAGS = -Wall
LFLAGS = -release

endif
endif



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

VPATH = src/win src/i386 src/misc wintests

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 scrsave

dibgrab: wintests/dibgrab.exe
dibhello: wintests/dibhello.exe
scrsave: wintests/scrsave.scr



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

ifneq ($(wildcard $(DJDIR)/bin/cp.exe),)
HAVE_CP = 1
endif

ifndef STATICLINK

$(WINDIR_U)/$(DLL_BASENAME): $(DLL_NAME)
ifdef HAVE_CP
	$(DJDIR)/bin/cp lib/msvc/$(DLL_BASENAME) $(WINDIR_U)
else
	copy lib\msvc\$(DLL_BASENAME) $(WINDIR_D)
endif

endif

$(MSVCDIR_U)/lib/$(IMPLIB_BASENAME): $(IMPLIB_NAME)
ifdef HAVE_CP
	$(DJDIR)/bin/cp lib/msvc/$(IMPLIB_BASENAME) $(MSVCDIR_U)/lib
else
	copy lib\msvc\$(IMPLIB_BASENAME) $(MSVCDIR_D)\lib
endif

$(MSVCDIR_U)/include/allegro.h: include/allegro.h
ifdef HAVE_CP
	$(DJDIR)/bin/cp include/allegro.h $(MSVCDIR_U)/include
else
	copy include\allegro.h $(MSVCDIR_D)\include
endif

$(MSVCDIR_U)/include/winalleg.h: include/winalleg.h
ifdef HAVE_CP
	$(DJDIR)/bin/cp include/winalleg.h $(MSVCDIR_U)/include
else
	copy include\winalleg.h $(MSVCDIR_D)\include
endif

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

$(MSVCDIR_U)/include/allegro/%.h: include/allegro/%.h
ifdef HAVE_CP
	$(DJDIR)/bin/cp $< $@
else
	copy $(subst /,\,$< $@)
endif

HEADERS = $(addprefix $(MSVCDIR_U)/,$(wildcard include/allegro/*.h))

INSTALL_FILES = $(MSVCDIR_U)/lib/$(IMPLIB_BASENAME) \
		$(MSVCDIR_U)/include/allegro.h \
		$(MSVCDIR_U)/include/winalleg.h \
		$(MSVCDIR_U)/include/allegro \
		$(HEADERS)

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

install: $(INSTALL_FILES)
	@echo The $(DESCRIPTION) MSVC 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 \
		  $(WINDIR_U)/all$(LIBRARY_VERSION).dll \
		  $(WINDIR_U)/ald$(LIBRARY_VERSION).dll \
		  $(WINDIR_U)/alp$(LIBRARY_VERSION).dll
uninstall:
	-rm -fv $(UNINSTALL_FILES)
	-rd $(MSVCDIR_D)\include\allegro
	@echo All gone!



# -------- 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

GCC2MSVC = -D_MSC_VER -D_WIN32 -D_M_IX86 -U__GNUC__ -UDJGPP -U__unix__

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 $(GCC2MSVC) $(SFLAGS) -I. -I./include -x assembler-with-cpp -o $@ -c $<

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

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

$(OBJ_DIR)/scrsave.res: wintests/scrsave.rc
	rc -fo$(OBJ_DIR)/scrsave.res wintests/scrsave.rc

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 $(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

ifneq ($(wildcard $(DJDIR)/bin/cat.exe),)

define GENERATE_PLUGINS_H
$(DJDIR)/bin/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



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

DEPEND_PARAMS = $(GCC2MSVC) -MM -MG -I. -I./include -DSCAN_DEPEND

depend:
	gcc $(DEPEND_PARAMS) src/*.c src/win/*.c src/i386/*.c src/misc/*.c demo/*.c examples/*.c setup/*.c tests/*.c tools/*.c tools/plugins/*.c wintests/*.c > _depend.tmp
	gcc $(DEPEND_PARAMS) -x assembler-with-cpp src/i386/*.s src/win/*.s >> _depend.tmp
	sed -e "s/^[a-zA-Z0-9_\/]*\///" _depend.tmp > _depend2.tmp
	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

