ifndef OS
	OS=$(shell uname)
endif
ifneq ($(OS), Windows_NT)
	OPT+= -std=c99 -Wpointer-arith -D_GNU_SOURCE -fPIC
	OBJFILEEXT=o
	LIBREBOUND=librebound.so
	EXEREBOUND=rebound
	RM=rm -f
	LINKORCOPYLIBREBOUND=ln -s -f ../../src/$(LIBREBOUND) .
	LINKORCOPYLIBREBOUNDMAIN=ln -s -f src/$(LIBREBOUND) .
	CCPROBLEM=$(CC) -I../../src/ -Wl,-rpath,./ $(OPT) $(PREDEF) $< -L. -lrebound $(LIB) -o $(EXEREBOUND)
else
ifeq ($(OPENGL), 1)
$(warning OpenGL not supported on Windows. Setting OPENGL=0)
	OPENGL=0
endif
ifeq ($(MPI), 1)
$(error MPI currently not supported on Windows. Please set MPI=0)
endif
ifeq ($(OPENMP), 1)
$(error OPENMP currently not supported on Windows. Please set OPENMP=0)
endif
ifeq ($(AVX512), 1)
$(error AVX512 currently not supported on Windows. Please set AVX512=0)
endif
	OPT+= /D_GNU_SOURCE /Ox /fp:precise 
	OBJFILEEXT=obj
	LIBREBOUND=librebound.dll
	EXEREBOUND=rebound.exe
	RM=del
	ifeq (, $(shell copy))
		LINKORCOPYLIBREBOUND=cp ../../src/librebound.* . & cp ../../src/rebound.h .
		LINKORCOPYLIBREBOUNDMAIN=cp src/librebound.* . & cp src/rebound.h .
	else
		LINKORCOPYLIBREBOUND=copy ..\..\src\librebound.* . & copy ..\..\src\rebound.h .
		LINKORCOPYLIBREBOUNDMAIN=copy src\librebound.* . & copy src\rebound.h .
	endif
	CCPROBLEM=$(CC) $(OPT) $(PREDEF) $< librebound.lib /Fe: $(EXEREBOUND)
endif
# Removed -march=native for now
ifeq ($(OS), Linux)
	OPT+= -Wall -g 
	LIB+= -lm -lrt
endif
ifeq ($(OS), Darwin)
	OPT+= -I/usr/local/include -Wall -g #-Wsign-compare
	PREDEF+= -D_APPLE
	LIB+= -L/usr/local/lib
endif

ifeq ($(MPI), 1)
	ifeq ($(origin CC),default)
		CC:=mpicc
	else
		CC?=mpicc
	endif
	PREDEF+= -DMPI
else
	ifeq ($(OS),Windows_NT) 
		ifeq ($(origin CC),default)
			CC:=cl
		endif
	else
		CC?=cc
	endif
endif

ifeq ($(FFTW), 1)
	PREDEF+= -DFFTW
	LIB+= -lfftw3
endif

ifneq ($(SERVER), 0)
PREDEF+= -DSERVER
endif

ifeq ($(OPENGL), 1)
PREDEF+= -DOPENGL
ifeq ($(OS), Darwin)
	OPT+= -I/opt/homebrew/include
	OPT+= -L/opt/homebrew/lib
	LIB+= -lglfw -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo
else
	LIB+= -lglfw
endif
endif

ifeq ($(AVX512), 1)
	PREDEF+= -DAVX512
endif

ifeq ($(QUADRUPOLE), 1)
	PREDEF+= -DQUADRUPOLE
endif

ifeq ($(PROFILING), 1)
	PREDEF+= -DPROFILING
endif

ifeq ($(OPENMP), 1)
	PREDEF+= -DOPENMP
ifeq ($(CC), icc)
	OPT+= -openmp
	LIB+= -openmp
else
	OPT+= -fopenmp
	LIB+= -fopenmp
endif
else
ifeq ($(OPENMPCLANG), 1)
	PREDEF+= -DOPENMP
	OPT+= -I$(brew --prefix libomp)/include -Xpreprocessor -fopenmp
	LIB+= -lomp
else
	ifneq ($(OS), Windows_NT)
		OPT+= -Wno-unknown-pragmas
	endif
endif
endif

ifndef GITHASH
	GITHASH = $(shell git rev-parse HEAD || echo '0000000000gitnotfound0000000000000000000')
	PREDEF+= -DGITHASH=$(GITHASH)
endif
