####################
# Operating system #
####################

# OSTYPE needs to be one of: linux, solaris, cygwin, darwin
# This makefile will try to detect which one of these is appropriate.
# If this detection does not work, or you wish to override it,
# either uncomment one of the lines directly below
# or pass a value to make directly, e.g.: make OSTYPE=linux

#OSTYPE = linux
#OSTYPE = solaris
#OSTYPE = cygwin
#OSTYPE = darwin

ifdef OSTYPE
	# Look for common variants, e.g. gnu-linux -> linux
	ifneq (,$(findstring linux, $(OSTYPE)))
	  OSTYPE = linux
	endif
	ifneq (,$(findstring solaris, $(OSTYPE)))
	  OSTYPE = solaris
	endif
	ifneq (,$(findstring cygwin, $(OSTYPE)))
	  OSTYPE = cygwin
	endif
	# For Cygwin , OSTYPE is sometimes set to "posix"
	ifneq (,$(findstring posix, $(OSTYPE)))
	  OSTYPE = cygwin
	endif
	ifneq (,$(findstring darwin, $(OSTYPE)))
	  OSTYPE = darwin
	endif
else
	# If OSTYPE is not defined/available, try uname
	ifneq (,$(findstring Linux, $(shell uname -s)))
		OSTYPE = linux
	endif
	ifneq (,$(findstring SunOS, $(shell uname -s)))
		OSTYPE = solaris
	endif
	ifneq (,$(findstring CYGWIN, $(shell uname -s)))
		OSTYPE = cygwin
	endif
	ifneq (,$(findstring Darwin, $(shell uname -s)))
		OSTYPE = darwin
	endif
endif


########
# Java #
########

# JAVA_DIR needs to be set to the location of your Java installation.
# This makefile will try to detect this automatically based on the location of the javac command.
# If this detection does not work, or you wish to override it,
# either set the variable yourself by uncommenting and/or modifying one of the lines below
# or pass a value to make directly, e.g.: make JAVA_DIR=/usr/java

# Find javac
DETECT_JAVAC = $(shell include/scripts/findjavac.sh 2> /dev/null)

# Find directory containing javac
ifeq ("$(DETECT_JAVAC)","")
  JAVA_DIR =
else
  ifneq (darwin,$(OSTYPE))
    JAVA_DIR = $(shell dirname "$(DETECT_JAVAC)" | sed 's/\/bin//')
  else
    JAVA_DIR = $(shell dirname "$(DETECT_JAVAC)" | sed 's/\/Commands//')
  endif
endif

#JAVA_DIR =	/usr/java
#JAVA_DIR =	/usr/java/j2sdk1.4.2
#JAVA_DIR =	/bham/java/packages/j2sdk1.4.2
#JAVA_DIR =	/cygdrive/c/java/j2sdk1.4.2
#JAVA_DIR =	/System/Library/Frameworks/JavaVM.framework
#JAVA_DIR=/c/”Program Files“/Java/jdk1.8.0_151/

ifneq (darwin,$(OSTYPE))
	JAVAC_DIR =$(JAVA_DIR)/bin/
else
	JAVAC_DIR =$(JAVA_DIR)/Commands/
endif


######################
# Commands and option#
######################
CC=g++
GCC=nvcc

CCOPTION=-c -o
GCCOPTION=-x cu -I. -std=c++11 -dc -o

ifeq ($(OSTYPE), linux)
	CCOPTION=-std=c++11 -c -fPIC -o
	GCCOPTION=-x cu -I. -std=c++11 -dc -Xcompiler -fPIC -o
	FINALCCOPTION=-shared -fPIC -o
	TAR=lib/libjdd.so
	GTAR=lib/libgpu.so
else ifeq ($(OSTYPE), darwin)
	CCOPTION=-c -o
	GCCOPTION=-x cu -I. -std=c++11 -dc -o
	TAR=lib/libjdd.dylib
    GTAR=lib/libgpu.dylib
else
	CCOPTION=-c -o
	GCCOPTION=-x cu -I. -std=c++11 -dc -o
    TAR=lib/jdd.dll
    GTAR=lib/gpu.dll
endif

###############
# Directories #
###############

# Now we locate the JNI header files jni.h and jni_md.h
# (in fact this is the only reason we need JAVA_DIR)
JAVA_JNI_H_DIR = $(shell \
	if [ -f "$(JAVA_DIR)"/include/jni.h ]; then echo "$(JAVA_DIR)"/include; \
	elif [ -f "$(JAVA_DIR)"/Headers/jni.h ]; then echo "$(JAVA_DIR)"/Headers; \
	else echo ""; fi )
JAVA_JNI_MD_H_DIR = $(shell (ls "$(JAVA_JNI_H_DIR)"/jni_md.h "$(JAVA_JNI_H_DIR)"/*/jni_md.h | head -n 1 | sed 's/\/jni_md.h//') 2>/dev/null)
JAVA_INCLUDES = -I "$(JAVA_JNI_H_DIR)" -I "$(JAVA_JNI_MD_H_DIR)"

current_dir = $(shell pwd)

MK_DIRS=obj obj/gpu obj/simulationMethod

##directory for gpu
GCFLAGS=-I$(current_dir)/include/gpu/ \
$(JAVA_INCLUDES)
#-I/Library/java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/include \
#-I/Library/java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/include/darwin


OGDIR=obj/gpu

_GOBJ=German.o 
GOBJ=$(patsubst %,$(OGDIR)/%,$(_GOBJ))

UGDIR=obj/simulationMethod

_USRGOBJ=GPU.o 
USEGOBJ=$(patsubst %,$(UGDIR)/%,$(_USRGOBJ))

#GTAR=lib/libgpu.dylib

SGDIR=src/gpu

SUGDIR=src/simulationMethod

$(OGDIR)%.o: $(SGDIR)%.cc 
	$(CC) $(CCOPTION) $@ $< $(GCFLAGS)

$(UGDIR)%.o: $(SUGDIR)%.cc 
	$(GCC) $(GCCOPTION) $@ $< $(GCFLAGS)


target=targpu

	
all: make_dirs $(target)

make_dirs:
	@mkdir -p $(MK_DIRS)
	@echo JAVAC_DIR="$(JAVAC_DIR)" >include/scripts/parameters.sh

tarbdd:$(OBJ) $(USEOBJ)
	$(CC) -L$(CUDDLIB) $(FINALCCOPTION) $(TAR) $(OBJ) $(USEOBJ) $(LIBS)


targpu:$(GOBJ) $(USEGOBJ)
	$(GCC) -shared -o $(GTAR) $(GOBJ) $(USEGOBJ)


RM := rm -rf

smallclean:
	-$(RM) $(OBJ) $(USEOBJ) $(TAR) $(GOBJ) $(USEGOBJ) $(GTAR) $(MK_DIRS)

clean: 
	-$(RM) $(OBJ) $(USEOBJ) $(TAR) $(GOBJ) $(USEGOBJ) $(GTAR) $(MK_DIRS)

