# For correctness, see test-correctness.sh.
# For performance:
#	pushd ~/cfax
#	~/setcfa build-fast
# 	popd
#	make perfprogs CFA=$cfa -j8 MODE=performance
# 	make results-latest.csv RUNARGS=5 RUN_NUM_REPS=5


CFA = cfa
CXX = g++-11
UXX =  ~/u++/u++-7.0.0/bin/u++

MODE=performance
EXTRA_COMP_FLAGS=
RUN_NUM_REPS=3
RUNARGS=

ifeq "$(MODE)" "performance"
PERFFLAGS_CFA = -nodebug -O3
PERFFLAGS_CC  = -DNDEBUG -O3
else ifeq "$(MODE)" "correctness"
PERFFLAGS_CFA = -debug -O0 -g
PERFFLAGS_CC = -O0 -g
else
$(error Bad MODE ($(MODE)); should be performance or correctness)
endif

PERFFLAGS_CXX = $(PERFFLAGS_CC)
PERFFLAGS_UXX = $(PERFFLAGS_CC)

SHELL = /usr/bin/bash

# function: project an element from a filename that contains a delimited tuple
# (call proj,-,a-b-c.hfa,3)
# is
# c
define proj
$(word $(3),$(subst $(1), ,$(basename $(2))))
endef

# functions: cross two lists, adding given delimiter between
# (call cross,-,a b c,1 2)
# is
# a-1 a-2 b-1 b-2 c-1 c-2
define cross
$(foreach x,$(2),$(foreach xs,$(3),$(x)$(1)$(xs)))
endef
define cross3
$(call cross,$(1),$(2),$(call cross,$(1),$(3),$(4)))
endef
define cross4
$(call cross,$(1),$(2),$(call cross3,$(1),$(3),$(4),$(5)))
endef
define cross5
$(call cross,$(1),$(2),$(call cross4,$(1),$(3),$(4),$(5),$(6)))
endef

OP_MOVEMENTS=stack queue
OP_POLARITIES=insfirst inslast
OP_ACCESSORS=allhead inselem remelem
FX_SOLUTIONS=lq-tailq lq-list cfa-cfa upp-upp cpp-stlref

OPS=$(call cross3,-,$(OP_MOVEMENTS),$(OP_POLARITIES),$(OP_ACCESSORS))
FXS=$(FX_SOLUTIONS)

all : perfprogs results-latest.csv

# Want to add functional dependency:
# if current FX_SOLUTION is lq-list then
# current OP_MOVEMENT must be stack and
# current OP_POLARITY must be insfirst
LQ_LIST_INCOMPAT_OP_MOVEMENTS=$(filter-out stack,$(OP_MOVEMENTS))
LQ_LIST_INCOMPAT_OP_POLARITIES=$(filter-out insfirst,$(OP_POLARITIES))
LQ_LIST_INCOMPAT_OPS=$(call cross3,-,$(LQ_LIST_INCOMPAT_OP_MOVEMENTS),$(OP_POLARITIES),$(OP_ACCESSORS)) \
                     $(call cross3,-,$(OP_MOVEMENTS),$(LQ_LIST_INCOMPAT_OP_POLARITIES),$(OP_ACCESSORS))
INCOMPAT=$(call cross,--,lq-list,$(LQ_LIST_INCOMPAT_OPS))
define filterFds
$(filter-out $(INCOMPAT),$(1))
endef


CORES_FULL=$(call cross,--,$(FXS),$(OPS))                # lq-tailq--stack-inslast-allhead
CORES=$(call filterFds,$(CORES_FULL))                    # lq-tailq--stack-inslast-allhead


PERFPROGS=$(call cross,--,perfexp,$(CORES))

perfprogs : $(PERFPROGS)

perfexp--% driver--%.o result--%.1csv : FX=$(call proj,--,$@,2)
perfexp--% driver--%.o result--%.1csv : FX_COARSE=$(call proj,-,$(FX),1)
perfexp--% driver--%.o result--%.1csv : OP=$(call proj,--,$@,3)
perfexp--% driver--%.o result--%.1csv : OP_MOVEMENT=$(call proj,-,$(OP),1)
perfexp--% driver--%.o result--%.1csv : OP_POLARITY=$(call proj,-,$(OP),2)
perfexp--% driver--%.o result--%.1csv : OP_ACCESSOR=$(call proj,-,$(OP),3)
perfexp--% driver--%.o result--%.1csv : OP_DEFINES=-DOP_MOVEMENT=$(OP_MOVEMENT) -DOP_POLARITY=$(OP_POLARITY) -DOP_ACCESSOR=$(OP_ACCESSOR)

perfexp--cfa-% driver--cfa-%.o : COMPILER=$(CFA) $(PERFFLAGS_CFA)
perfexp--lq-%  driver--lq-%.o  : COMPILER=$(CC)  $(PERFFLAGS_CC)
perfexp--cpp-% driver--cpp-%.o : COMPILER=$(CXX) $(PERFFLAGS_CXX)
perfexp--upp-% driver--upp-%.o : COMPILER=$(UXX) $(PERFFLAGS_UXX)
perfexp--%     driver--%.o     : COMPILER=NO-COMPILER-FOR-$(FX_COARSE)

# Without this %.d rule, ordinary make runs have noise about the recipe for driver--%.o being ill-formed when called on a *.d.
# https://stackoverflow.com/questions/3714041/why-does-this-makefile-execute-a-target-on-make-clean
# Whatever you -include gets called as a target first.
# One such example is driver--upp-upp--stack-insfirst-allhead.d
# Without this %.d rule, `make make driver--upp-upp--stack-insfirst-allhead.d` leads to the rule for driver--%.o firing.
# Though my dumb human eyes don't see the pattern as matching.
%.d:
	@touch $@

perfexp--% : driver--%.o observation.o
	$(COMPILER) $(EXTRA_COMP_FLAGS) $^ -o $@

driver--%.o : driver.c
	$(COMPILER) $(EXTRA_COMP_FLAGS) -c $< $(OP_DEFINES) -include op-$(OP).h -include fx-$(FX).h -o $@ -MMD 

sayhi:
	echo $(PERFPROGS)


RUN_REP_IDS=$(shell echo {1..$(RUN_NUM_REPS)})              # 1 2 3
RUN_REP_EXTS=$(call cross3,,run,$(RUN_REP_IDS),.1csv)       # run1.1csv run2.1cav run3.1csv

RESULT1S=$(call cross,.,$(CORES),$(RUN_REP_EXTS))   # lq-tailq--stack-inslast-allhead.run2.1csv

RESULT1S_SHUFD=$(shell shuf -e $(RESULT1S))

%.1csv : CORE=$(basename $(basename $@))
%.1csv : REP_ID=$(subst .run,,$(suffix $(basename $@)))
%.1csv : REP_TIME=$(shell date '+%F %H:%M:%S')
%.1csv : perfprogs FORCE
	./perfexp--$(CORE) $(RUNARGS) | xargs -n 1 printf '%s,%s,%s\n' "$(REP_TIME)" "$(REP_ID)" | tee $@

BATCHTIME=$(shell date '+%F--%H-%M-%S')

results--$(BATCHTIME).csv : $(RESULT1S_SHUFD)
	cat $^ | tee $@
	rm $^

results-latest.csv : results--$(BATCHTIME).csv
	rm -f $@
	ln -s $< $@



clean :
	rm -f *.o *.d perfexp--*

# The FORCE business means any target that mentions it is also phony
# https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html says: To always rebuild a pattern rule consider using a “force target” (see [https://www.gnu.org/software/make/manual/html_node/Force-Targets.html]).
.PHONY: all perfprogs results-latest.csv clean
FORCE:

.PRECIOUS: result--%.1csv driver--%.o perfexp--% %.o

-include *.d
