# This has the target platform defined, this is modified by fix.bat or fix.sh
include target.os

#set to debug or release
BUILD = release

# Suggested by GNU Coding Stardards
SHELL = /bin/sh
# platform specific stuff:

# ------------------
# DJGPP target
# ------------------
ifeq ($(TARGET),DJGPP)

PLATFORMDIR=djgpp
GPP = gxx
BINSUF = .exe

ifeq ($(BUILD),release)
CFLAGS = -Iinclude -O3 -Wall -s -ffast-math -fomit-frame-pointer
LFLAGS = -lm -O3 -Wall -s -ffast-math -fomit-frame-pointer -lalleg
endif
ifeq ($(BUILD),debug)
CFLAGS = -Iinclude -g -Wall
LFLAGS = -g -lalleg
endif

endif

# ------------------
# MingW32
# ------------------
ifeq ($(TARGET),MINGW32)

PLATFORMDIR=mingw
GPP = g++
GCC = gcc
BINSUF = .exe
ifeq ($(BUILD),release)
CFLAGS = -Iinclude -O3 -Wall -s -ffast-math -fomit-frame-pointer -D__GTHREAD_HIDE_WIN32API 
LFLAGS = -lm -O3 -Wall -s -ffast-math -fomit-frame-pointer -mwindows -lalspc -ltegel -lalleg -D__GTHREAD_HIDE_WIN32API 
endif
ifeq ($(BUILD),debug)
CFLAGS = -Iinclude -g -Wall -D__GTHREAD_HIDE_WIN32API 
LFLAGS = -g -mwindows -lalspc -ltegel -lalleg -D__GTHREAD_HIDE_WIN32API 
endif
ICONOBJ = obj/mingw/icon.o

endif

# ------------------
# Linux
# ------------------
ifeq ($(TARGET),LINUX)

PLATFORMDIR=linux
GPP = g++
BINSUF =
ifeq ($(BUILD),release)
CFLAGS = -Iinclude -O3 -Wall -s -ffast-math -fomit-frame-pointer
LFLAGS = -lm -O3 -Wall -s -ffast-math -fomit-frame-pointer `allegro-config --libs`
endif
ifeq ($(BUILD),debug)
CFLAGS = -Iinclude -g -Wall
LFLAGS = -g `allegro-config --libs`
endif

endif

#below is platform non-specific
OBJDIR = obj/$(PLATFORMDIR)

SRC = $(wildcard src/*.cpp)
OBJ = $(addprefix $(OBJDIR)/,$(addsuffix .o, $(basename $(notdir $(SRC)))))
DEP = $(addprefix $(OBJDIR)/,$(addsuffix .d, $(basename $(notdir $(SRC)))))
BIN = xelda$(BINSUF)
RMOBJ = $(BIN) $(DEP) $(OBJ)
RMOBJ_D = $(subst /,\,$(RMOBJ))

all: $(BIN)

$(BIN) : $(OBJ) $(ICONOBJ)
	$(GPP) $^ -o $@ $(LFLAGS)

$(OBJ) : $(OBJDIR)/%.o : src/%.cpp
	$(GPP) $(CFLAGS) -MMD -c $< -o $@

obj/mingw/icon.o : icon.rc icon.ico
	windres -I rc -O coff -i icon.rc -o obj\mingw\icon.o

# include the .d files
# they contain extra rules about how .o files depend on headers
-include $(DEP)

.PHONY: clean
clean: 
ifeq ($(TARGET),MINGW32)
	del /q $(RMOBJ_D)
else
	rm -rf $(RMOBJ)
endif

