all:
include ../subdir-inc.make
#$(error $(TOP_SRCDIR))
#CPPFLAGS += -I$(TOP_INCDIR)
#CPPFLAGS += -I$(TOP_SRCDIR)/include
CPPFLAGS += -I$(TOP_SRCDIR)/src# workaround for in-tree sqlite3.h
CPPFLAGS+=-DCWAL_ENABLE_TRACE=0

#INCLUDES_PATH ?= $(HOME)/include /usr/local/include /usr/include

LIBFOSSIL.LDFLAGS := -L.. -lfossil -lz

ifeq (,$(strip $(filter distclean clean,$(MAKECMDGOALS))))
$(LIBFOSSIL.LDFLAGS):
	$(MAKE) -C ..
endif

########################################################################
# INC_SEARCH: $(call)able function. $(1) should be the the name of
# a C header file to search for under $(INCLUDES_PATH).
define INC_SEARCH
$(call ShakeNMake.CALL.FIND_FILE,$(1),$(INCLUDES_PATH))
endef

########################################################################
# Look for -ldl or -lltdl for module loading
ENABLE_MODULES ?= @FSL_ENABLE_MODULE_LOADER@
ifeq (1,$(ENABLE_MODULES))
  ifeq (1,@HAVE_LIBDL@)
    DL_IMPL := dl
    DL_LDFLAGS := -ldl
  else
    ifeq (1,@HAVE_LIBLTDL@)
      DL_IMPL := ltdl
      DL_LDFLAGS := -lltdl
    endif
  endif
endif
ifeq (,$(DL_IMPL))
  ENABLE_MODULES := 0
endif

# cgiish...
cgiish.o th1ish_cgi.o: CPPFLAGS+=-DTH1ISH_AMALGAMATION_BUILD=1
cgiish.o th1ish_cgi.o: th1ish_amalgamation.h
ifeq (0,1)# enable this block to build cgiish as a loadable module
ifeq (1,$(ENABLE_MODULES))
  cgiish.DLL.OBJECTS := cgiish.o th1ish_cgi.o
  $(eval $(call ShakeNMake.CALL.RULES.DLLS,cgiish))
  $(cgiish.DLL): $(libth1ish.LIB)
  all: $(cgiish.DLL)
endif
endif
# /cgiish

# /ENABLE_MODULES
########################################################################

########################################
# Set up file-specific CPPFLAGS/LDFLAGS.
# Set up the loadable modules flags...
ifeq (1,$(ENABLE_MODULES))
  fossi1ish.BIN.LDFLAGS += -export-dynamic $(DL_LDFLAGS)
  th1ish_amalgamation.o shell.o: CPPFLAGS+=-DTH1ISH_ENABLE_MODULES=1
  ifeq (dl,$(DL_IMPL)) # use libdl
    th1ish_amalgamation.o: CPPFLAGS+=-DTH1ISH_HAVE_DLOPEN=1
  else # use libltdl
    th1ish_amalgamation.o: CPPFLAGS+=-DTH1ISH_HAVE_LTDLOPEN=1
  endif
endif
# basic module setup

# Module-related setup
########################################################################

shell.o: CPPFLAGS+=-DTH1ISH_AMALGAMATION_BUILD=1
shell.o: CPPFLAGS+=-DTH1ISH_SHELL_EXTEND
shell.o: shell_extend.c
Makefile:
shell.o shell_extend.o: Makefile
fossi1ish.BIN.OBJECTS += shell.o th1ish_amalgamation.o cgiish.o th1ish_cgi.o shell_extend.o
fossi1ish.BIN.LDFLAGS += -lm $(LIBFOSSIL.LDFLAGS)# -lsqlite3 -lz
$(eval $(call ShakeNMake.CALL.RULES.BIN,fossi1ish))
#$(fossi1ish.BIN): $(LIBFOSSIL.LDFLAGS)
all: $(fossi1ish.BIN)

TH1.SHELL := ./$(fossi1ish.BIN)
TH1.SHELL.FLAGS ?=-x -c -L -a
ifeq (0,1)
  # To stress the allocator more, disable some optimizations:
  TH1.SHELL.FLAGS += --r --t --p --s
else
  # Force-enable all recycling/memory optimizations:
  TH1.SHELL.FLAGS += -r -t -p -s
endif
# That has been known to reveal libcwal-level bugs (but not recently!;)

UNIT_SCRIPT_LIST := $(sort $(subst ./,,$(wildcard unit/???-???.th1ish)))
ifeq (,$(UNIT_SCRIPT_LIST))
$(error UNIT_SCRIPT_LIST is empty?)
endif
UNIT_SCRIPT_FLAGS :=# --verbose
UNIT_SCRIPT_RUNNER := unit-run.th1ish
.PHONY: unit
unit: $(fossi1ish.BIN)
	@echo "Running unit test scripts..."; \
	export LD_LIBRARY_PATH="$(TOP_SRCDIR):$${LD_LIBRARY_PATH}"; \
	for i in $(UNIT_SCRIPT_LIST); do \
		cmd="$(TH1.SHELL) $(TH1.SHELL.FLAGS) -f $(UNIT_SCRIPT_RUNNER) -- $(UNIT_SCRIPT_FLAGS) $$i"; \
		$$cmd || exit $$?; \
		true; \
	done
#	$(TH1.SHELL) $(TH1.SHELL.FLAGS) -f $(UNIT_SCRIPT_RUNNER) -- $(UNIT_SCRIPT_FLAGS) $(UNIT_SCRIPT_LIST)

########################################################################
# Valgrind sanity checks...
VG := $(call ShakeNMake.CALL.FIND_FILE,valgrind)
ifneq (,$(VG))
VG_REPORT = VG.report.csv
VG_FLAGS ?= --leak-check=full -v --show-reachable=yes --track-origins=yes
#SCRIPT_LIST := $(shell ls -1 test-[0-9]*.th1ish | sort)
UNIT_RUN_CMD = ./$(fossi1ish.BIN) $(TH1.SHELL.FLAGS) -f $(UNIT_SCRIPT_RUNNER) -- $(UNIT_SCRIPT_FLAGS)
.PHONY: vg
vg: $(fossi1ish.BIN) Makefile
	@echo "Running: $(VG) $(VG_FLAGS) ./$(fossi1ish.BIN) $(TH1.SHELL.FLAGS) ..."; \
	export LD_LIBRARY_PATH="$(TOP_SRCDIR):$${LD_LIBRARY_PATH}"; \
	massif_tmp=tmp.massif; \
	for i in $(UNIT_SCRIPT_LIST); do \
		vgout=$$i.vg; \
		cmd="$(VG) $(VG_FLAGS) $(UNIT_RUN_CMD) $$i"; \
		echo -n "**** Valgrinding [$$i]: "; \
		$$cmd 2>&1 | tee $$vgout | grep 'total heap usage' || exit $$?; \
		grep 'ERROR SUMMARY' $$vgout | grep -v ' 0 errors' && echo "See $$vgout!"; \
		vgout=$$i.massif; \
		cmd="$(VG) --tool=massif --time-unit=B --massif-out-file=$$vgout --heap-admin=0  $(UNIT_RUN_CMD) $$i"; \
		echo -n "**** Massifing [$$i]: "; \
		$$cmd > $$massif_tmp 2>&1 || exit $$?; \
		echo -n "==> $$vgout Peak RAM: "; \
	    ms_print $$vgout | perl -n -e 'if(m/^(\s+)([KM]B)$$/){my $$x=$$2; $$_=<>; m/^(\d[^^]+)/; print $$1." ".$$x; exit 0;}'; \
		echo; \
	done; \
	rm -f $$massif_tmp
	@echo "Done running through th1ish scripts. Collecting stats..."; \
	echo 'script,allocs,frees,totalMemory,peakMemUsage,peakMemUsageUnit' > $(VG_REPORT); \
	for i in $$(ls -1 unit/*.th1ish.vg | sort); do \
		base=$${i%%.vg}; \
		echo -n "$$base,"; \
		grep 'total heap usage' $$i | sed -e 's/,//g' -e 's/^\.\///' | awk '{printf "%d,%d,%d,",$$5,$$7,$$9}'; \
		ms_print $${base}.massif | perl -n -e 'if(m/^(\s+)([KM]B)$$/){my $$x=$$2; $$_=<>; m/^(\d[^^]+)/; print $$1.",".$$x; exit 0;}'; \
		echo; \
	done >> $(VG_REPORT); \
	rm -f ms_print.tmp.*; \
	echo "Stats are in $(VG_REPORT):"; \
	tr ',' '\t' < $(VG_REPORT)

CLEAN_FILES += $(wildcard ms_print.tmp.* unit/*.vg unit/*.massif unit/*.db unit/*~ unit/*.uncompressed unit/*.z unit/*.zip)
# 'vg' proxies which tweak various th1ish/cwal-level optimizations...
vgp: TH1.SHELL.FLAGS+=--p
vgp: VG_REPORT:=VG.report-p.csv
vgp: vg
vgr: TH1.SHELL.FLAGS+=--r
vgr: VG_REPORT:=VG.report-r.csv
vgr: vg
vgs: TH1.SHELL.FLAGS+=--s
vgs: VG_REPORT:=VG.report-s.csv
vgs: vg
vgt: TH1.SHELL.FLAGS+=--t
vgt: VG_REPORT:=VG.report-t.csv
vgt: vg
vgrs: TH1.SHELL.FLAGS+=--r --s
vgrs: VG_REPORT:=VG.report-rs.csv
vgrs: vg
vgrst: TH1.SHELL.FLAGS+=--r --s --t
vgrst: VG_REPORT:=VG.report-rst.csv
vgrst: vg
vgprst: TH1.SHELL.FLAGS+=--r --s --t --p
vgprst: VG_REPORT:=VG.report-prst.csv
vgprst: vg
endif
#/$(VG)
########################################################################
