CC = gcc
CXX = g++
#CC = clang
#CXX = clang++
#CXX = clang++ -stdlib=libc++
AR = ar

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
XLIBDIR = /usr/X11R6/lib

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

ifeq (,$(shell $(CXX) --version | grep clang))
	# Real gcc, not clang
	COPTS += -fno-delete-null-pointer-checks
	OPENMPOPTS = -fopenmp
endif

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


LIBS = -lm
GLLIBS = -L$(XLIBDIR) -lgluit -lGLU -lGL -lX11


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

