dspsrc  := $(wildcard *.dsp)
cppsrc  := $(addprefix $(DEST), $(dspsrc:.dsp=.cpp))
appl 	:= $(addprefix $(DEST), $(dspsrc:.dsp=$(EXT)))

all :  $(appl)

LLVM_VERSION = $(shell llvm-config --version)

ifeq ($(LLVM_VERSION), 3.1)
LLVM_VERSION  = LLVM_31
LLVM_PREFIX = /opt/local/libexec/llvm-3.1/bin
endif

ifeq ($(LLVM_VERSION), 3.2)
LLVM_VERSION  = LLVM_32
LLVM_PREFIX = /opt/local/libexec/llvm-3.2/bin
endif

ifeq ($(LLVM_VERSION), 3.3)
LLVM_VERSION  = LLVM_33
LLVM_PREFIX = /opt/local/libexec/llvm-3.3/bin
endif

ifeq ($(LLVM_VERSION),$(filter $(LLVM_VERSION), 3.4.0 3.4.1 3.4.2))
LLVM_VERSION  = LLVM_34
LLVM_PREFIX = /opt/local/libexec/llvm-3.4/bin
endif

ifeq ($(LLVM_VERSION), 3.5.0)

LLVM_VERSION  = LLVM_35
LLVM_PREFIX = /opt/local/libexec/llvm-3.5/bin
endif

ifeq ($(LLVM_VERSION),$(filter $(LLVM_VERSION), 3.6.0 3.6.1 3.6.2))
LLVM_VERSION  = LLVM_36
LLVM_PREFIX = /opt/local/libexec/llvm-3.6/bin
endif

ifeq ($(LLVM_VERSION),$(filter $(LLVM_VERSION), 3.7.0 3.7.1 3.7.2))
LLVM_VERSION  = LLVM_37
LLVM_PREFIX = /opt/local/libexec/llvm-3.7/bin
endif

ifeq ($(LLVM_VERSION),$(filter $(LLVM_VERSION), 3.8.0 3.8.1))
LLVM_VERSION  = LLVM_38
LLVM_PREFIX = /opt/local/libexec/llvm-3.8/bin
endif

ifeq ($(LLVM_VERSION),$(filter $(LLVM_VERSION), 3.9.0 3.9.1))
LLVM_VERSION  = LLVM_39
LLVM_PREFIX = /opt/local/libexec/llvm-3.9/bin
endif

ifeq ($(LLVM_VERSION),$(filter $(LLVM_VERSION), 4.0.0 4.0.1))
LLVM_VERSION  = LLVM_40
LLVM_PREFIX = /opt/local/libexec/llvm-4.0/bin
endif

ifeq ($(LLVM_VERSION),$(filter $(LLVM_VERSION), 5.0.0 5.0.1))
LLVM_VERSION  = LLVM_50
LLVM_PREFIX = /opt/local/libexec/llvm-5.0/bin
endif

CLANG = $(LLVM_PREFIX)/clang++
OPT = $(LLVM_PREFIX)/opt
LLC = $(LLVM_PREFIX)/llc
LINK = $(LLVM_PREFIX)/llvm-link

$(DEST)% : %.dsp
	cp /usr/local/share/faust/$(ARCH) $(DEST)
	#faust -vec -lv 1  -lang llvm  -vs 64 -vls 4 $< -o $@.bc
	#faust -vec -lv 1  -lang llvm  -vs 512 $< -o $@.bc
	faust -sch  -lang llvm  -vs 64 $< -o $@.bc
	#faust  -lang llvm  $< -o $@.bc
	#faust -sch  -lang llvm  -vs 64 $< -o $@.bc
	$(OPT) -S -O3 $@.bc -o $@.ll
	$(OPT) -O3 $@.bc -o $@.bc
	$(LLC) -O3 $@.bc -o $@.s
	$(CLANG) -g -D$(LLVM_VERSION) -emit-llvm -S ../architecture/scheduler.cpp -o $@.scheduler.ll
	$(OPT) -O3 $@.scheduler.ll -o $@.scheduler.bc
	$(LINK) $@.scheduler.bc $@.bc -o $@_sch.bc
	#llvm-g++ ../architecture/scheduler.cpp -g -O1 -I/opt/local -mfpmath=sse -msse -msse2 -msse3 -mmmx -m3dnow -ffast-math -ftree-vectorize  -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS $(LIB) $(DEST)$(ARCH) $@.s -o $@
	#$(CLANG) -O3 -I/opt/local -msse -msse2 -msse3 -mmmx -m3dnow -ffast-math  -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS $(LIB) $(DEST)$(ARCH) $@.s $@.scheduler.ll -o $@
	$(CLANG) -g -I/opt/local  -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS $(LIB) $(DEST)$(ARCH) $@.s $@.scheduler.ll -o $@
	#llvm-g++ -g -I/opt/local -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS $(LIB) $(DEST)$(ARCH) $@.s -o $@

clean :
	rm -f $(DEST)
