# Configuration variables

Build = build
Pictures = pictures
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 = ${notdir ${wildcard ${Pictures}/*.dat}}
GraphSRC := ${GraphSRC:.dat=.pdf}		# substitute ".dat" 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} ${PicSRC} ${BibSRC} ${BibRep}/pl.bib ${LaTMac}/common.tex Makefile | ${Build}
	echo ${PicSRC}
	echo ${GraphSRC}
	${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-cppemu.pdf: string-graph-peq-cppemu.dat | ${Build}
	gnuplot plot-peg-cppemu.gp

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

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

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

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

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

#-include ${Build}/*.d
