VALGRINDEXTRA =
VALGRINDARGS	= --tool=memcheck --num-callers=8 --leak-resolution=high \
		  --leak-check=yes -v --suppressions=suppressions --keep-debuginfo=yes \
		  --trace-children=yes $(VALGRINDEXTRA)

PGOGEN_BUILD = -fprofile-generate=prof
PGO_BUILD = @PGO_BUILD@
PGO=
CFLAGS		+= $(PGO)
CONFIGUREFLAGS	= {CFLAGS=-O3 -flto -march=native}
PACKAGE_LOAD = "package ifneeded $(PACKAGE_NAME) $(PACKAGE_VERSION) [list apply {{} {load `@CYGPATH@ $(PKG_LIB_FILE)` $(PACKAGE_NAME); ::$(PACKAGE_NAME)::_setdir `@CYGPATH@ $(srcdir)`}}]"
PACKAGE_LOAD_EMBED = "package\ ifneeded\ $(PACKAGE_NAME)\ $(PACKAGE_VERSION)\ [list\ apply\ {{}\ {load\ `@CYGPATH@ $(PKG_LIB_FILE)`\ $(PACKAGE_NAME);\ ::$(PACKAGE_NAME)::_setdir\ `@CYGPATH@ $(srcdir)`}}]"

CONTAINER = cyanogilvie/alpine-tcl:v0.9.66-gdb

benchmark: binaries libraries
	$(TCLSH) `@CYGPATH@ $(srcdir)/bench/run.tcl` $(BENCHFLAGS) -load $(PACKAGE_LOAD)

tags: generic/*
	ctags-exuberant generic/*

vim-core:
	$(TCLSH_ENV) $(PKG_ENV) vim -c 'packadd termdebug' -c "set mouse=a" -c "set number" -c "set foldlevel=100" -c "Termdebug $(TCLSH_PROG) core" -c Winbar generic/

vim-gdb: binaries libraries
	$(TCLSH_ENV) $(PKG_ENV) vim -c "set number" -c "set mouse=a" -c "set foldlevel=100" -c "Termdebug -ex set\ print\ pretty\ on --args $(TCLSH_PROG) tests/all.tcl $(TESTFLAGS) -singleproc 1 -load $(PACKAGE_LOAD_EMBED)" -c "2windo set nonumber" -c "1windo set nonumber" generic/

pgo:
	rm -rf prof
	make -C . PGO="$(PGOGEN_BUILD)" clean binaries libraries test benchmark
	make -C . PGO="$(PGO_BUILD)" clean binaries libraries

coverage:
	make -C . PGO="--coverage" clean binaries libraries test

test-container:
	docker run --rm -it -v "$(realpath $(srcdir)):/src/local:ro" $(CONTAINER) /src/local/teabase/dtest.tcl "$(TESTFLAGS)"

build-container:
	mkdir -p "$(top_builddir)/dockerbuild"
	docker run --rm -it -v "$(realpath $(srcdir)):/src/local:ro" -v "$(top_builddir)/dockerbuild:/install" $(CONTAINER) /src/local/teabase/dbuild.tcl "$(shell id -u)" "$(shell id -g)"

benchmark-container:
	mkdir -p "$(top_builddir)/rundata"
	docker run --rm -it -v "$(realpath $(srcdir)):/src/local:ro" -v "$(realpath $(srcdir))/rundata:/src/local/rundata" $(CONTAINER) /src/local/teabase/dbench.tcl "-rundata /src/local/rundata $(BENCHFLAGS)" "$(CONFIGUREFLAGS)"

benchmark-container-pgo:
	mkdir -p "$(top_builddir)/rundata"
	docker run --rm -it -v "$(realpath $(srcdir)):/src/local:ro" -v "$(realpath $(srcdir))/rundata:/src/local/rundata" $(CONTAINER) /src/local/teabase/dbench.tcl "-rundata /src/local/rundata $(BENCHFLAGS)" "$(CONFIGUREFLAGS)" pgo

#doc: doc/reuri.n README.md
#
#doc/reuri.n: doc/reuri.md
#	pandoc --standalone --from markdown --to man doc/reuri.md --output doc/reuri.n
#
#README.md: doc/reuri.md
#	pandoc --standalone --wrap=none --from markdown --to gfm doc/reuri.md --output README.md

.PHONY: vim-gdb vim-core pgo coverage benchmark test-container build-container benchmark-container
