PATHS = /usr/bin/i686-pc-mingw32- /usr/bin/i686-w64-mingw32-
CC := $(or $(firstword $(realpath $(foreach path,$(PATHS),$(path)gcc))), $(error Couldn't find gcc))
CXX := $(or $(firstword $(realpath $(foreach path,$(PATHS),$(path)g++))), $(error Couldn't find g++))
AR := $(or $(firstword $(realpath $(foreach path,$(PATHS),$(path)ar))), $(error Couldn't find ar))
WINDRES := $(or $(firstword $(realpath $(foreach path,$(PATHS),$(path)windres))), $(error Couldn't find windres))

EXE = .exe

ARCHOPTS = -m32 -march=core2 -mfpmath=sse -mtune=native
COPTS = -fno-strict-overflow -fno-math-errno -fno-trapping-math
COPTS += -Wall -Wextra -Wshadow
DEFINES = -U_FORTIFY_SOURCE
LDOPTS = -static-libgcc -static-libstdc++

ifdef DEBUG
	COPTS += -O1 -ggdb3 -fno-omit-frame-pointer
	DEFINES += -DDEBUG
else
	COPTS += -O3 -fomit-frame-pointer

	# Workaround for g++ 4.5.2
	COPTS += -fno-caller-saves -fno-gcse

	# No commandline window
	#COPTS += -mwindows

	DEFINES += -DNDEBUG

	LDOPTS += -s
endif

OPENMPOPTS = -fopenmp

CXXOPTS = $(COPTS)
CFLAGS = $(ARCHOPTS) $(OPENMPOPTS) $(INCLUDES) $(DEFINES) $(COPTS)
CXXFLAGS = $(ARCHOPTS) $(OPENMPOPTS) $(INCLUDES) $(DEFINES) $(CXXOPTS)
LDFLAGS = $(LIBDIR) $(LDOPTS)


LIBS = -lm
GLLIBS = -lgluit -lwinmm -lglu32 -lopengl32 -lgdi32


$(OBJDIR)/%.o: %.c
	@ echo "Compiling $<"
	@ rm -f $@
	@ $(CC) $(CFLAGS) -MMD -MP -c $< -o $@

$(OBJDIR)/%.o: %.cc
	@ echo "Compiling $<"
	@ rm -f $@
	@ $(CXX) $(CXXFLAGS) -MMD -MP -c $< -o $@

$(OBJDIR)/%.o: %.cpp
	@ echo "Compiling $<"
	@ rm -f $@
	@ $(CXX) $(CXXFLAGS) -MMD -MP -c $< -o $@

ifneq (,$(findstring -L,$(LIBDIR)))
VPATH = $(subst -L,,$(LIBDIR))
endif

define LINK
	@ echo "Linking $@"
	@ rm -f $@
	@ $(CXX) $(CXXFLAGS) $^ $(LDFLAGS) $(LIBS) -o $@
endef

define STATICLIB
	@ echo "Creating library $@"
	@ rm -f $@
	@ $(AR) -rcs $@ $^
endef

-include $(OBJDIR)/*.d

