#############################################################################
# FLAGS
#############################################################################
UNAME := $(shell uname)

THIS_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))

GG=g++
#GG=clang++
#GG=/usr/bin/g++     17s
#GG=/usr/bin/clang++ 14s
#GG=g++-mp-4.3       15.5s
#GG=g++-mp-4.7       19.9s

CFLAGS += -Wall
CFLAGS += -std=c++11

ifeq ($(UNAME), Linux)
	DEFAULT_PREFIX=/usr/local/
else
	DEFAULT_PREFIX=/opt/local/
	# I guess arch only works in Mac OSX
	AFLAGS+=-arch x86_64 -m64 -march=corei7-avx
endif

# Default parameters for the IGL group members based on there computer's
# username
ifndef IGL_USERNAME
	IGL_USERNAME := $(shell whoami)
endif

ifeq ($(IGL_USERNAME),whitinge)
	DEFAULT_PREFIX=/usr/local/
	MOSEKPLATFORM=osx64x86
	MOSEKVERSION=7
	IGL_WITH_TETGEN=1
	IGL_WITH_MOSEK=1
	IGL_WITH_BBW=1
	IGL_WITH_SVD3X3=1
	IGL_WITH_PNG=1
	# I don't use llvm
#AFLAGS = -m64 -march="corei7-avx"
	# msse4.2 is necessary for me to get embree to compile correctly
	AFLAGS=-m64 -msse4.2
	OPENMP=-fopenmp
	EIGEN3_INC=-I$(DEFAULT_PREFIX)/include/eigen3 -I$(DEFAULT_PREFIX)/include/eigen3/unsupported
endif

ifeq ($(IGL_USERNAME),ajx)
        LIBIGL_USE_STATIC_LIBRARY=1
	MOSEKPLATFORM=osx64x86
	MOSEKVERSION=7
	IGL_WITH_VIEWER=1
	IGL_WITH_TETGEN=1
	IGL_WITH_BOOLEAN=1
	IGL_WITH_EMBREE=1
	IGL_WITH_MATLAB=1
	IGL_WITH_MOSEK=1
	IGL_WITH_CGAL=1
	IGL_WITH_BBW=1
	IGL_WITH_SVD3X3=1
	IGL_WITH_PNG=1
	IGL_WITH_XML=1
	IGL_WITH_BOOST=1
	# I don't use llvm
#AFLAGS = -m64 -march="corei7-avx"
	# msse4.2 is necessary for me to get embree to compile correctly
	AFLAGS=-m64 -msse4.2
	OPENMP=-fopenmp
	EIGEN3_INC=-I$(DEFAULT_PREFIX)/include/eigen3 -I$(DEFAULT_PREFIX)/include/eigen3/unsupported
	#EIGEN3_INC=-I/Users/ajx/Documents/eigen -I/Users/ajx/Documents/eigen/unsupported
endif

ifeq ($(IGL_USERNAME),alecjaco)
	DEFAULT_PREFIX=/home1/alecjaco/
	#MOSEKPLATFORM=linux64x86
	IGL_WITH_TETGEN=1
	IGL_WITH_MATLAB=0
	#IGL_WITH_MOSEK=1
	OPENGL_INC=-I$(DEFAULT_PREFIX)/include
	OPENGL_LIB=-lGL -lGLU
	# Glut is needed only for examples
	GLUT_LIB=-lglut
	ANTTWEAKBAR_LIB=-lAntTweakBar
	OPENMP=-fopenmp
endif

ifeq ($(IGL_USERNAME),jacobson)
	CFLAGS+=
endif

ifeq ($(IGL_USERNAME),sorkineo)
	MOSEKPLATFORM=osx64x86
	IGL_WITH_TETGEN=1
	IGL_WITH_MATLAB=0
	IGL_WITH_MOSEK=1
	IGL_WITH_PNG=0
endif

ifeq ($(IGL_USERNAME),jalec_linux)
	MOSEKPLATFORM=linux64x86
	IGL_WITH_TETGEN=1
	IGL_WITH_MATLAB=0
	IGL_WITH_MOSEK=1
	OPENGL_LIB=-lGL -lGLU
	# Glut is needed only for examples
	GLUT_LIB=-lglut
	ANTTWEAKBAR_LIB=-lAntTweakBar
	IGL_WITH_PNG=1
	OPENMP=-fopenmp
endif

ifeq ($(IGL_USERNAME),daniele)
	IGL_WITH_MATLAB=0
	IGL_WITH_XML=1
	AFLAGS=-m64
	GG=g++-4.8 -Wfatal-errors
	EIGEN3_INC=-I/usr/local/include/eigen3
endif

ifeq ($(IGL_USERNAME),olkido)
    IGL_WITH_MATLAB=1
	IGL_WITH_COMISO=1
	IGL_WITH_XML=1
	COMISO=/Users/olkido/Documents/igl/MIQ/src/CoMISo
	  IGL_WITH_XML=1
    AFLAGS= -m64
    IGL_WITH_EMBREE=1
    IGL_WITH_PNG=1
	IGL_WITH_VIEWER=1
	MATLAB=/Applications/MATLAB_R2014b.app/
endif

ifeq ($(IGL_USERNAME),chrsch)
  ifeq ($(UNAME), Linux)
    DEFAULT_PREFIX=/usr
    IGL_WITH_XML=1
    IGL_WITH_TETGEN=0
    IGL_WITH_MATLAB=0
    IGL_WITH_PNG=0
    IGL_WITH_MOSEK=0
    #MOSEKPLATFORM=linux64x86
    OPENGL_LIB=-lGL -lGLU
    # Glut is needed only for examples
    GLUT_LIB=-lglut
    ANTTWEAKBAR_LIB=-lAntTweakBar
    OPENMP=-fopenmp
    AFLAGS = -Wfatal-errors
  else
    IGL_WITH_XML=1
    IGL_WITH_TETGEN=0
    IGL_WITH_MATLAB=0
    IGL_WITH_PNG=0
    IGL_WITH_MOSEK=0
    OPENGL_INC=-I$(DEFAULT_PREFIX)/include
    OPENGL_LIB=-lGL -lGLU
    # Glut is needed only for examples
    GLUT_LIB=-lglut
    ANTTWEAKBAR_LIB=-lAntTweakBar
    OPENMP=-fopenmp
    AFLAGS=-m64
  endif
endif

ifeq ($(IGL_USERNAME),stefanmessmer)
	GG=clang++
	IGL_WITH_TETGEN=0
	IGL_WITH_EMBREE=0
	IGL_WITH_MATLAB=0
	IGL_WITH_MOSEK=0
	IGL_WITH_BBW=1
	IGL_WITH_PNG=0
	IGL_WITH_XML=0
	IGL_WITH_BOOST=0
	IGL_WITH_SVD3X3=1
	#OPENGL_INC=-I$(DEFAULT_PREFIX)/include
	#OPENGL_LIB=-lGL -lGLU
	# Glut is needed only for examples
	#GLUT_LIB=-lglut
	#ANTTWEAKBAR_LIB=-lAntTweakBar
	OPENPMP = -openmp
	AFLAGS = -DIGL_NO_MOSEK -DIGL_NO_ANTTWEAKBAR -DIGL_NO_OPENGL -arch armv7s -arch armv7 -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk
	AFLAGS_SIMULATOR = -DIGL_NO_MOSEK -DIGL_NO_ANTTWEAKBAR -DIGL_NO_OPENG -arch i386 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk
endif

ifeq ($(IGL_USERNAME),wenzel)
	IGL_WITH_TETGEN=0
	IGL_WITH_MATLAB=0
	IGL_WITH_MOSEK=0
	OPENGL_LIB=-lGL -lGLU
	GLUT_LIB=-lglut
	ANTTWEAKBAR_LIB=-lAntTweakBar
	IGL_WITH_PNG=1
	OPENMP=-fopenmp
	CFLAGS += -msse4 -D__SSE4__ -DIGL_NO_MOSEK
endif

#############################################################################
# DEFAULTS (USUALLY TO SOMETHING THAT WORKS FOR SURE ON MAC OS X
#############################################################################

ifndef OPENGL_LIB
	OPENGL_LIB=-framework OpenGL
endif
ifndef GLUT_LIB
	GLUT_LIB=-framework GLUT
endif
# Eigen dependency
ifndef EIGEN3_INC
	EIGEN3_INC=-I$(DEFAULT_PREFIX)/include/eigen3 -I$(DEFAULT_PREFIX)/include/eigen3/unsupported
endif

LIBIGL=$(THIS_DIR)/../
LIBIGL_INC=-I$(LIBIGL)/include

ifndef ANTTWEAKBAR_INC
	ANTTWEAKBAR_INC=-I$(LIBIGL)/external/AntTweakBar/include
endif
ifndef ANTTWEAKBAR_LIB
	# AntTweakBar needs AppKit on mac os x
	ANTTWEAKBAR_LIB=-L$(LIBIGL)/external/AntTweakBar/lib -lAntTweakBar -framework AppKit -liglanttweakbar
endif

ifndef SINGULAR_VALUE_DECOMPOSITION_INC
	SINGULAR_VALUE_DECOMPOSITION_INC=\
	-I$(LIBIGL)/external/Singular_Value_Decomposition/
endif

ifndef TETGEN
# By default I'm using the libigl version. Adjust accordingly
	TETGEN=$(LIBIGL)/external/tetgen
	TETGEN_LIB=-L$(TETGEN) -ligltetgen -ltet 
	TETGEN_INC=-I$(TETGEN)
endif

ifndef EMBREE
	EMBREE=$(LIBIGL)/external/embree
	EMBREE_INC=-I$(EMBREE)/ -I$(EMBREE)/include
	EMBREE_LIB=-L$(EMBREE)/build -lembree -lsys
endif
ifndef YIMG
	YIMG=$(LIBIGL)/external/yimg
	YIMG_LIB=-L$(YIMG) -lyimg -lz -L/usr/X11/lib -L$(DEFAULT_PREFIX)/lib -lpng -bind_at_load
	YIMG_INC=-I/usr/X11/include -I$(YIMG) 
endif

ifdef LIBIGL_USE_STATIC_LIBRARY
	CFLAGS += -DIGL_STATIC_LIBRARY
	LIBIGL_LIB=-L$(LIBIGL)/lib -ligl
endif

OPTFLAGS+=-O3 -DNDEBUG $(OPENMP)
