#
# Makefile for testing the Faust compiler output
#

system := $(shell uname -s)
system := $(shell echo $(system) | grep MINGW > /dev/null && echo MINGW || echo $(system))
ifeq ($(system), MINGW)
 FAUST ?= ../../build/bin/faust.exe
 #DYNFAUST ?= ../../build/bin/dynamic-faust.exe
 echo "dynamic-faust.exe is missing"
 COMPARE := ./filesCompare.exe
else
 FAUST ?= ../../build/bin/faust
 DYNFAUST ?= ../../tools/benchmark/dynamic-faust
 COMPARE := ./filesCompare
endif
MAKE ?= make

GCCOPTIONS := -O3 -I../../architecture -Iarchs -pthread -std=c++11
outdir ?= llvm1/double
lang ?= cpp
ext  ?= o
arch ?= impulsearch1.cpp
precision ?=		# filesCompare precision (empty by default)
FAUSTOPTIONS := -double
ifeq ($(lang), c)
#	CXX = gcc
	ext = c
endif

.PHONY: test 
.DELETE_ON_ERROR:

dspfiles := $(wildcard dsp/*.dsp)

listfiles = $(dspfiles:dsp/%.dsp=ir/$1/%.ir) 
listintermediate = $(dspfiles:dsp/%.dsp=ir/$1/%) $(dspfiles:dsp/%.dsp=ir/$1/%.cpp)

TOOLSOPTIONS := -std=c++11 -O3 -I../../architecture
LIB    ?= ../../build/lib/libfaust.a
SRCDIR := tools

all: filesCompare ir/$(outdir) $(call listfiles,$(outdir))

test:
#	$(file > faustoptions.txt, "some text")
	@echo $(call listwebfiles,$(asmjsdir))

help:
	@echo "-------- FAUST impulse response tests --------"
	@echo "Available targets are:"
	@echo " 'all' (default): generate ir for all the dsp files using the given options"
	@echo
	@echo "Options:"
	@echo " 'outdir' 	   : define the output directory (default to '$(outdir)')"
	@echo " 'lang'         : used for faust -lang option (default to '$(lang)')"
	@echo " 'arch'         : used for faust -a option (default to '$(arch)')"
	@echo " 'FAUSTOPTIONS' : define additional faust options (default to $(FAUSTOPTIONS))"
	@echo " 'precision'    : define filesCompare expected precision (empty by default)"

#########################################################################
# output directories
ir/$(outdir):
	mkdir -p ir/$(outdir)

filesCompare:
	$(MAKE) filesCompare

#########################################################################
# precision issues 

#########################################################################
# rules 
ir/$(outdir)/%.ir: ir/$(outdir)/% reference/%.ir
	$< -n 60000 > $@
	$(COMPARE) $@ reference/$(notdir $@) $(precision)
	
ir/$(outdir)/% : ir/$(outdir)/%.$(ext)
	$(CXX) $(GCCOPTIONS) archs/$(arch) $< -o $@

# Specific rule to test 'control' primitive that currently uses the -lang ocpp backend (still compiling in scalar mode...)
ir/$(outdir)/control.cpp : dsp/control.dsp
	$(FAUST) -lang ocpp -double -i -A ../../architecture -a archs/$(arch) $< -o $@

# Specific rule to test 'enable/control' primitives that currently only work in scalar mode
ir/$(outdir)/osc_enable.$(ext) : dsp/osc_enable.dsp
	@echo "Can only be tested in scalar mode"
	$(DYNFAUST) -double -I dsp  $< -o $@

ir/$(outdir)/%.$(ext) : dsp/%.dsp
	$(DYNFAUST) $(FAUSTOPTIONS) -I dsp $< -o $@
