# Configuration variables

Build = build

Benchmarks = benchmarks
Pictures = pictures
Plots = plots
Programs = programs

LaTMac = ../../LaTeXmacros
BibRep = ../../bibliography

TeXSRC = ${wildcard *.tex}
PicSRC = ${notdir ${wildcard ${Pictures}/*.png}} ${notdir ${wildcard ${Pictures}/*.fig}}
PicSRC := ${PicSRC:.fig=.pdf}			# substitute ".fig" with ".pdf"
GraphSRC_OLD = ${notdir ${wildcard ${Pictures}/*.dat}}
GraphSRC_OLD := ${GraphSRC_OLD:.dat=.pdf}	# substitute ".dat" with ".pdf"
PlotINPUTS = ${wildcard ${Plots}/*.gp} ${wildcard ${Plots}/*.py}
PlotINPUTS := ${addsuffix .INPUTS,${PlotINPUTS}}
PlotSRC = ${notdir ${wildcard ${Plots}/*.gp}}
PlotSRC := ${addprefix ${Build}/plot-,${PlotSRC:.gp=.pdf}} # substitute ".gp" with ".pdf"
DemoPgmSRC = ${notdir ${wildcard ${Programs}/*-demo.cfa}}
PgmSRC = ${notdir ${wildcard ${Programs}/*}}
RunPgmSRC = ${notdir ${wildcard ${Programs}/*.run.*}}
BibSRC = ${wildcard *.bib}

TeXLIB = .:${LaTMac}:${Build}:			# common latex macros
BibLIB = .:${BibRep}:				# common citation repository

#MAKEFLAGS = --no-print-directory # --silent
VPATH = ${Build} ${Pictures} ${Programs} # extra search path for file names used in document

DOCUMENT = uw-ethesis.pdf
BASE = ${basename ${DOCUMENT}}			# remove suffix

RunPgmExe = ${addprefix ${Build}/,${basename ${basename ${RunPgmSRC}}}}
RunPgmOut = ${RunPgmExe:%=%.out}
DemoPgmExe = ${addprefix ${Build}/,${basename ${basename ${DemoPgmSRC}}}}
DemoPgmOut = ${DemoPgmExe:%=%.out}

# Commands

LaTeX = TEXINPUTS=${TeXLIB} && export TEXINPUTS && pdflatex -halt-on-error -output-directory=${Build}
BibTeX = BIBINPUTS=${BibLIB} && export BIBINPUTS && bibtex
CFA = cfa -O0 -g
CC  = gcc -O0 -g
CXX = g++-11 --std=c++20 -O0 -g

# Rules and Recipes

.PHONY : all clean				# not file names
.SECONDARY:
#.PRECIOUS : ${Build}/%				# don't delete intermediates
.ONESHELL :

all : ${DOCUMENT}

clean :
	@rm -frv ${DOCUMENT} ${Build}

# File Dependencies

${DOCUMENT}: ${TeXSRC} $(RunPgmOut) ${DemoPgmOut} ${GraphSRC_OLD} ${PlotSRC} ${PicSRC} ${BibSRC} ${BibRep}/pl.bib ${LaTMac}/common.tex Makefile | ${Build}
	echo ${PicSRC}
	echo ${GraphSRC_OLD}
	${LaTeX} ${BASE}
	${BibTeX} ${Build}/${BASE}
	${LaTeX} ${BASE}
	# if needed, run latex again to get citations
	if fgrep -s "LaTeX Warning: Citation" ${basename $@}.log ; then ${LaTeX} ${BASE} ; fi
#	${Glossary} ${Build}/${BASE}
#	${LaTeX} ${BASE}
	cp ${Build}/$@ $@

${Build}:
	mkdir -p $@

${Build}/%-demo: ${Programs}/%-demo.cfa | ${Build}
	${CFA} $< -o $@

${Build}/%: ${Programs}/%-demo.cfa | ${Build}
	${CFA} $< -o $@

${Build}/%: ${Programs}/%.run.cfa | ${Build}	# cfa cannot handle pipe
	sed -f ${Programs}/sedcmd $< > ${Build}/tmp.cfa; ${CFA} ${Build}/tmp.cfa -o $@

${Build}/%: ${Programs}/%.run.c | ${Build}
	sed -f ${Programs}/sedcmd $< | ${CC} -x c -I ${Programs} -o $@ -

${Build}/%: ${Programs}/%.run.cpp | ${Build}
	sed -f ${Programs}/sedcmd $< | ${CXX} -x c++ -I ${Programs} -o $@ -

${Build}/%.out: ${Build}/% | ${Build}
	$< > $@

string-graph-peq-sharing.pdf: string-graph-peq-sharing.dat plot-peq-sharing.gp | ${Build}
	gnuplot plot-peq-sharing.gp

string-graph-pta-sharing.pdf: string-graph-pta-sharing.dat plot-pta-sharing.gp | ${Build}
	gnuplot plot-pta-sharing.gp

string-graph-pbv.pdf: string-graph-pbv.dat plot-pbv.gp | ${Build}
	gnuplot plot-pbv.gp

string-graph-allocn.pdf: string-graph-allocn.dat plot-allocn.gp | ${Build}
	gnuplot plot-allocn.gp

%.pdf: %.fig | ${Build}
	fig2dev -L pdf $< > ${Build}/$@

-include $(Plots)/string-peq-cppemu.d

${Build}/plot-%.dat: ${Plots}/%.py ${Plots}/%.py.INPUTS | ${Build}
	echo ${PlotINPUTS}	
	python3 $< > $@

${Build}/plot-%.pdf: ${Plots}/%.gp ${Plots}/%.gp.INPUTS | ${Build}
	gnuplot $<

#-include ${Build}/*.d
