
CFABUILD = ~/cfa2/build-perf
LIBCFA = $(CFABUILD)/libcfa/*/src/.libs/libcfa.so

CFA = $(CFABUILD)/driver/cfa
PERFFLAGS_CFA = -nodebug -O2
PERFFLAGS_CXX = -DNDEBUG -O2


# function: convert to upper case
define uc
$(shell echo $(1) | tr  '[:lower:]' '[:upper:]')
endef

# function: project numbered element of filename named by hyphen-delimited tuple
# (call hyphProj,q-w-e-r.txt,1) is Q
define ucHyphProj
$(call uc,$(word $(2),$(subst -, ,$(basename $(1)))))
endef

# function: cross two lists, adding hyphen delimiters
# (call hyphCross,a b c,1 2) is a-1 a-2 b-1 b-2 c-1 c-2
define hyphCross
$(foreach x,$(1),$(foreach xs,$(2),$(x)-$(xs)))
endef

define hyphCross3
$(call hyphCross,$(1),$(call hyphCross,$(2),$(3)))
endef

define hyphCross4
$(call hyphCross,$(1),$(call hyphCross3,$(2),$(3),$(4)))
endef

define hyphCross5
$(call hyphCross,$(1),$(call hyphCross4,$(2),$(3),$(4),$(5)))
endef

OPERATIONS=pta peq
ALLOCS=reuse fresh
CFA_APILEVELS=hl ll
CFA_SHARINGS=share noshare
PLATFORMS=cfa stl buhr94

ifneq ($(filter cfa,$(PLATFORMS)),)
	CFA_PERFPROGS=$(call hyphCross5,perfexp-cfa,$(CFA_APILEVELS),$(OPERATIONS),$(CFA_SHARINGS),$(ALLOCS))
endif

ifneq ($(filter stl,$(PLATFORMS)),)
	STL_PERFPROGS=$(call hyphCross3,perfexp-stl,$(OPERATIONS),$(ALLOCS))
endif

ifneq ($(filter buhr94,$(PLATFORMS)),)
	BUHR94_PERFPROGS=$(call hyphCross3,perfexp-buhr94,$(OPERATIONS),$(ALLOCS))
endif

PERFPROGS = $(CFA_PERFPROGS) $(STL_PERFPROGS) $(BUHR94_PERFPROGS)

all : $(PERFPROGS)



perfexp-cfa-%: CFA_APILEVEL=$(call ucHyphProj,$@,3)
perfexp-cfa-%: OPERATION=$(call ucHyphProj,$@,4)
perfexp-cfa-%: CFA_SHARING=$(call ucHyphProj,$@,5)
perfexp-cfa-%: ALLOC=$(call ucHyphProj,$@,6)
perfexp-cfa-%: prog.cfa $(LIBCFA)
	$(CFA) $(PERFFLAGS_CFA) $< -o $@ -DIMPL_CFA_$(CFA_APILEVEL)_$(CFA_SHARING) -DOP_$(OPERATION) -DALLOC_$(ALLOC)

perfexp-stl-%: OPERATION=$(call ucHyphProj,$@,3)
perfexp-stl-%: ALLOC=$(call ucHyphProj,$@,4)
perfexp-stl-%: prog.cfa
	$(CXX) -xc++ $(PERFFLAGS_CXX) $< -o $@ -DIMPL_STL -DOP_$(OPERATION) -DALLOC_$(ALLOC)

perfexp-buhr94-%.o: OPERATION=$(call ucHyphProj,$@,3)
perfexp-buhr94-%.o: ALLOC=$(call ucHyphProj,$@,4)
perfexp-buhr94-%.o: prog.cfa
	$(CXX) -xc++ -c $(PERFFLAGS_CXX) $< -o $@ -DIMPL_BUHR94 -DOP_$(OPERATION) -DALLOC_$(ALLOC)

buhr94-string.o:
	$(CXX) -xc++ -c $(PERFFLAGS_CXX) ~/usys1/sm/string/StringSharing/src/string.cc -o $@

buhr94-VbyteSM.o:
	$(CXX) -xc++ -c $(PERFFLAGS_CXX) ~/usys1/sm/string/StringSharing/src/VbyteSM.cc -o $@

perfexp-buhr94-% : perfexp-buhr94-%.o buhr94-string.o buhr94-VbyteSM.o
	$(CXX) $(PERFFLAGS_CXX) $^ -o $@

clean:
	rm -f *.o perfexp*

MEASURE = $(PERFPROGS)
CORPORI = corpus-100-*-1.txt

measurement: $(MEASURE)
	tofile=measurement-`date '+%F--%H-%M-%S'`.csv ; \
	echo $$tofile ; \
	for prog in $(MEASURE) ; do \
	    for corpus in $(CORPORI) ; do \
			corpusbody=`cat $$corpus` ; \
			printed=`./$$prog 100 10 $$corpusbody` ; \
			echo $$prog,$$corpus,$$printed  >>  $$tofile ; \
			echo $$prog,$$corpus,$$printed  ; \
		done ; \
	done
