
COMPILER = msvc

ifneq (,$(findstring /sh.exe,$(SHELL)))
UNIX_TOOLS=1
DATA_INSTALLER = cp -f
PROG_INSTALLER = cp -f
else
DATA_INSTALLER = copy /y
PROG_INSTALLER = copy /y
endif

ifeq ($(TERM), cygwin)
UNIX_TOOLS = 1
DATA_INSTALLER = cp -f
PROG_INSTALLER = cp -f
endif

include make/makefile.lst
EXT_HEADERS += $(EXT_HEADERS_PATH)/wgl_ext_defs.h $(EXT_HEADERS_PATH)/wgl_ext_api.h $(EXT_HEADERS_PATH)/wgl_ext_list.h $(EXT_HEADERS_PATH)/wgl_ext_alias.h


#-------------------------------#
# --- Compiler optimizations ---#

MSVC_OPT = -G5
ifdef PPROONLY
MSVC_OPT = -G6
endif

ifdef COMPILER_MSVC7
MSVC_OPT = -GL
endif
ifdef COMPILER_MSVC8
MSVC_OPT = -GL
endif

RUNTIME_LIB = -MD
ifdef STATICRUNTIME
RUNTIME_LIB = -MT
endif

ifdef DEBUGMODE
FLAGS = -DDEBUGMODE $(RUNTIME_LIB)d -Fd$(OBJ_DIR) -W3 -ZI -nologo -Gm -Od -FD -GR
ifdef LOGLEVEL
FLAGS += -DLOGLEVEL=$(LOGLEVEL)
endif
ifdef COMPILER_MSVC8
FLAGS += -RTC1 -EHsc
else
FLAGS += -GZ -GX -YX
endif
else
FLAGS = $(RUNTIME_LIB) -Fd$(OBJ_DIR) -W3 -Ox -Gs -GF -GA $(MSVC_OPT) -nologo
endif

ifdef STATICALLEGRO
FLAGS += -DALLEGRO_STATICLINK
endif

LINK_FLAGS = /nologo
ifdef COMPILER_MSVC7
LINK_FLAGS += -LTCG
endif
ifdef COMPILER_MSVC8
FLAGS += -D_CRT_SECURE_NO_DEPRECATE
LINK_FLAGS += -LTCG
endif

SRC_DIR_U = $(subst \,/,$(SRC_DIR))
SRC_DIR_D = $(subst /,\,$(SRC_DIR))

OBJ_DIR_U = $(subst \,/,$(OBJ_DIR))
OBJ_DIR_D = $(subst /,\,$(OBJ_DIR))

EXAMPLE_DIR_U = $(subst \,/,$(EXAMPLE_DIR))
EXAMPLE_DIR_D = $(subst /,\,$(EXAMPLE_DIR))

LIB_PATH_U = $(subst \,/,$(LIB_DIR))/$(LIB_NAME).lib
LIB_PATH_D = $(subst /,\,$(LIB_DIR))\$(LIB_NAME).lib

HEADERS_U = $(subst \,/,$(HEADERS))
HEADERS_D = $(subst /,\,$(HEADERS))
EXT_HEADERS_U = $(subst \,/,$(EXT_HEADERS))
EXT_HEADERS_D = $(subst /,\,$(EXT_HEADERS))
EXT_HEADERS_PATH_U = $(subst \,/,$(EXT_HEADERS_PATH))
EXT_HEADERS_PATH_D = $(subst /,\,$(EXT_HEADERS_PATH))

INCLUDE_PATHS = $(addprefix -I, $(INCLUDE_DIR))

SOURCE = $(C_SOURCE) $(WIN_SOURCE)
OBJECTS = $(addprefix $(OBJ_DIR_U)/, $(addsuffix .obj, $(basename $(notdir $(SOURCE)))))
EXAMPLES = $(addsuffix .exe, $(basename $(addprefix $(EXAMPLE_DIR)/, $(EXAMP_SOURCE))))

COMPILER_LINK_LIB = $(addsuffix .lib, $(LINK_LIB)) opengl32.lib glu32.lib user32.lib gdi32.lib

ifdef STATICALLEGRO
COMPILER_LINK_LIB += dsound.lib dinput.lib ddraw.lib winmm.lib dxguid.lib ole32.lib kernel32.lib
endif

COMPILER_LIB_DIR_U = "$(MSVCDIR_U)/lib"
COMPILER_LIB_DIR_D = "$(MSVCDIR_D)\lib"
COMPILER_INCLUDE_DIR_U = "$(MSVCDIR_U)/include"
COMPILER_INCLUDE_DIR_D = "$(MSVCDIR_D)\include"

#--------------------------------------#
#-- Check for environment variables ---#

.PHONY: badwin badmsvc badspaces

ifeq ($(OS),Windows_NT)
WINSYSDIR = $(SYSTEMROOT)
WINSUBDIR = system32

ifeq ($(WINSYSDIR),)        # Fix for Win2k
WINSYSDIR = $(SystemRoot)
endif

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



lib: $(LIB_PATH_U)
examples: $(EXAMPLES)

#----------------------------#
#---- Various targets -------#

include make/makefile.all


$(OBJ_DIR_U)/%.obj: $(SRC_DIR_U)/%.c $(addprefix include/,$(C_DEPEND)) $(EXT_HEADERS_U)
	@echo -Fo$@ > _tmp.arg
	@echo $(INCLUDE_PATHS) >> _tmp.arg
	cl -c $< @_tmp.arg $(FLAGS)
ifdef UNIX_TOOLS
	- @rm -f _tmp.arg
else
	- @del _tmp.arg
endif

$(OBJ_DIR_U)/glext.obj: src/glext.c $(addprefix include/,$(C_DEPEND)) $(EXT_HEADERS_U)
	@echo -Fo$@ > _tmp.arg
	@echo $(INCLUDE_PATHS) >> _tmp.arg
	cl -c $< @_tmp.arg $(subst -Ox,,$(FLAGS))
ifdef UNIX_TOOLS
	- @rm -f _tmp.arg
else
	- @del _tmp.arg
endif


$(LIB_PATH_U): $(OBJECTS) $(HEADERS_U)
	@echo $(LINK_FLAGS) > _tmp.arg
   define OUTPUT_OBJECTS
      $(foreach file, $(wildcard $(OBJECTS)), -@echo $(file) >> _tmp.arg
      )
   endef
	-$(OUTPUT_OBJECTS)
	lib -out:$@ @_tmp.arg
ifdef UNIX_TOOLS
	- @rm -f _tmp.arg
else
	- @del _tmp.arg
endif
	

$(EXAMPLE_DIR_U)/%.exe: $(EXAMPLE_DIR_U)/%.c $(HEADERS_U) $(LIB_PATH_U)
	@echo -Fe$@ > _tmp.arg
	@echo -Fo$(addsuffix .obj, $(basename $@)) >> _tmp.arg
	@echo $(INCLUDE_PATHS) >> _tmp.arg	
	@echo $(LIB_PATH_U) $(COMPILER_LINK_LIB) >> _tmp.arg
	cl $< @_tmp.arg $(FLAGS)
ifdef UNIX_TOOLS
	- @rm -f _tmp.arg
else
	- @del _tmp.arg
endif
