.PHONY: build debug-c debug-vm test bench

BENCH_FILE ?= benchmarks/loop.fzs
RUNS ?= 7
WARMUP ?= 2
MAX_ITER ?= 2000000

current_time = python3 -c "import time; print(time.time())"

build-debug:
	cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
	cmake --build build

build:
	cmake -S . -B build -DCMAKE_BUILD_TYPE=Release $(COMPARING)
	cmake --build build $(COMPARING)

install:
	cmake --install build

debug-c: build-debug
	gdb --args ./build/fzc -v ${FILE}

debug-vm: build-debug
	gdb --args ./build/fz ${FILE}

test-debug: build-debug
	ctest --output-on-failure --test-dir build

test: build
	ctest --output-on-failure --test-dir build

bench: COMPARING=&>/dev/null
bench: build
	@./build/fzc -o build/benchmark.fzb $(BENCH_FILE)
	@python3 scripts/benchmark.py --runs $(RUNS) --warmup $(WARMUP) \
		--max-iter $(MAX_ITER) --vm ./build/fz --program build/benchmark.fzb

compare: COMPARING=&>/dev/null
compare: build
	@echo "=== Million sums benchmark ==="

	@./build/fzc -o build/bench.fzb $(BENCH_FILE) 1>/dev/null 2>/dev/null

	@ST=$$($(current_time)); \
	./build/fz --allow-infinite build/bench.fzb 1>/dev/null 2>/dev/null; \
	ET=$$($(current_time)); \
	python3 -c "delta = ($$ET - $$ST) * 1000; print(f'FZLang : {delta:.5f} ms')"

	@ST=$$($(current_time)); \
	./benchmarks/loop-c 1>/dev/null 2>/dev/null; \
	ET=$$($(current_time)); \
	python3 -c "delta = ($$ET - $$ST) * 1000; print(f'C      : {delta:.5f} ms')"

	@ST=$$($(current_time)); \
	python3 ./benchmarks/loop.py 1>/dev/null 2>/dev/null; \
	ET=$$($(current_time)); \
	python3 -c "delta = ($$ET - $$ST) * 1000; print(f'CPython: {delta:.5f} ms')"
