#########################
# Include configuration #
#########################
include makefile.pa
####################
# 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

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
FINALCCOPTION=-shared -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/libjdd.dylib
    GTAR=lib/libgpu.dylib
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)

CFLAGS=-I$(current_dir)/include/ \
-I$(current_dir)/cudd/cudd \
-I$(current_dir)/cudd/dddmp \
-I$(current_dir)/cudd/epd \
-I$(current_dir)/cudd/mtr \
-I$(current_dir)/cudd/st \
-I$(current_dir)/cudd/util \
-I$(current_dir)/cudd \
$(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

##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

MK_DIRS=obj obj/dd obj/gpu obj/jdd obj/simulationMethod

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)


ODIR=obj/dd

_OBJ = dd_abstr.o dd_basics.o dd_cudd.o dd_export.o dd_info.o dd_matrix.o dd_term.o dd_vars.o dd.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))

UDIR=obj/jdd/

_USROBJ=JDD.o AliasMethod.o Random.o
USEOBJ=$(patsubst %,$(UDIR)/%,$(_USROBJ))

SDIR=src/dd

SUDIR=src/jdd

CUDDLIB=$(current_dir)/cudd/cudd/.libs

LIBS=-lcudd 

#-lepd -ldddmp -l mtr -lst -lutil

#TAR=lib/libjdd.dylib

$(ODIR)%.o: $(SDIR)%.cc 
	$(CC) $(CCOPTION) $@ $< $(CFLAGS)
	
$(UDIR)%.o: $(SUDIR)%.cc 
	$(CC) $(CCOPTION) $@ $< $(CFLAGS)

target= targpudecision
ifeq ($(enableGPU), true)
target=targpu
endif
	
all: make_dirs tarbdd $(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)

targpudecision:
	@echo gpu is not compiled. 

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


RM := rm -rf

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


