
COMPILER = mingw32

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

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

include make/makefile.gcc
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 ---#
GCC_OPT  := $(GCC_MTUNE)=i686
ifdef PPROONLY
GCC_OPT  := -march=i686
endif


FLAGS = -W -Wall -Wno-unused

ifdef DEBUGMODE
FLAGS += -DDEBUGMODE -g
ifdef LOGLEVEL
FLAGS += -DLOGLEVEL=$(LOGLEVEL)
endif
else
FLAGS += -O2 -ffast-math -fomit-frame-pointer $(GCC_OPT)
endif

ifdef STATICALLEGRO
FLAGS += -DALLEGRO_STATICLINK
endif

ifdef ALLEGRO_USE_CYGWIN
   FLAGS += -mno-cygwin
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$(LIB_NAME).a
LIB_PATH_D = $(subst /,\,$(LIB_DIR))\lib$(LIB_NAME).a

DLL_PATH_U = $(subst \,/,$(LIB_DIR))/$(LIB_NAME).dll

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 .o, $(basename $(notdir $(SOURCE)))))
EXAMPLES = $(addsuffix .exe, $(basename $(addprefix $(EXAMPLE_DIR)/, $(EXAMP_SOURCE))))

COMPILER_LINK_LIB = $(addprefix -l, $(LINK_LIB)) -luser32 -lgdi32 -lglu32 -lopengl32

ifdef STATICALLEGRO
COMPILER_LINK_LIB += -ldsound -ldinput -lddraw -lwinmm -ldxguid -lole32 -lkernel32
endif

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


COMPILER_LIB_DIR_U = $(MINGDIR_U)/lib
COMPILER_LIB_DIR_D = $(MINGDIR_D)\lib
COMPILER_INCLUDE_DIR_U = $(MINGDIR_U)/include
COMPILER_INCLUDE_DIR_D = $(MINGDIR_D)\include


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

.PHONY: badming

ifdef MINGDIR
  MINGDIR_U = $(subst \,/,$(MINGDIR))
  MINGDIR_D = $(subst /,\,$(MINGDIR))
else
badming:
	@echo Please specify your mingw32 directory via MINGDIR=.
	@echo Example: make MINGDIR=c:/mingw32
endif


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

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

include make/makefile.all


$(OBJ_DIR_U)/%.o: $(SRC_DIR_U)/%.c $(addprefix include/,$(C_DEPEND)) $(EXT_HEADERS_U)
	$(CC) -c $< -o $@ $(FLAGS) $(INCLUDE_PATHS)

$(LIB_PATH_U): $(OBJECTS) $(HEADERS_U)
	$(AR) rs $@ $(OBJECTS)

$(DLL_PATH_U): $(OBJECTS) $(HEADERS_U)
	$(CC) -shared -o $@ $(OBJECTS) $(COMPILER_LINK_LIB)
	
$(EXAMPLE_DIR)/%.exe: $(EXAMPLE_DIR)/%.c $(HEADERS_U) $(LIB_PATH_U)
	$(CC) $< -o $@ -mwindows $(FLAGS) $(INCLUDE_PATHS) $(LIB_PATH_U) $(COMPILER_LINK_LIB)

