

SRC = src/main.c src/helper.c src/io.c src/md2.c src/md2_impls/md2_common.c src/md2_impls/md2_0.c src/md2_impls/md2_1.c src/md2_impls/md2_2.c src/md2_impls/md2_3.c
OBJ = ${subst src,build,${SRC:.c=.o}}
CC = gcc
CFLAGS = -Ilib -ggdb -std=c11 -g -Wall -Wextra -no-pie -O3 
LDFLAGS = -pthread
TESTFILES = t/10000 t/1 t/10 t/100 t/1000 #t/2000 t/5000 t/10000
TESTFILES_SIZES = ${subst t/,,${TESTFILES}}

all: md2

clean:
	rm -rf md2 build

help:
	@echo === Project Build Help ===
	@echo
	@echo Available targets:
	@echo - all: build everything
	@echo - clean: clean distfiles
	@echo - help: show this help
	@echo - benchmarks: run benchmarks (only works on linux!)

build/%.o: src/%.c
	@mkdir -p build/md2_impls
	${CC} -c ${CFLAGS} -o $@ $<

md2: ${OBJ} 
	${CC} -o $@ $(OBJ) ${LDFLAGS}

t/%:
	@echo
	@echo "=== Generating ${subst t/,,$@}MB of random data... ==="
	dd if=/dev/random of=$@ bs=1M count=${subst t/,,$@} status=progress
	@echo "===                done                     ==="
	@echo

benchmarks.csv: md2 ${TESTFILES}
	@rm -f $@
	@for i in 0 1 2 3; do \
		echo ;\
		echo "=== Testing implementation $$i ===";\
		for t in $(TESTFILES_SIZES); do \
			echo -n "- with $${t}MB ... "; \
			if ! rr=$$(./md2 t/$${t} -B1 -V$${i}); then \
				echo; \
				echo "SKIPPED!"; \
				echo "$${i};$${t};0" >> $@; \
			else \
				r=$$(echo $$rr | xargs | sed -e 's/.*took \(.*\) seconds.*/\1/'); \
				echo "$${r}s"; \
				echo "$${i};$${t};$${r}" >> $@; \
			fi; \
		done; \
		echo "===           done           ===";\
		echo;\
    done
	
benchmarks: benchmarks.csv

.PHONY: all clean help benchmarks