BENCH := $(abspath ..)

.PHONY: all clean
all: gdb_debug.elf

# Wrap each benchmark function's raw bytes into an .o with predictable
# symbols _binary_func_NN_bin_{start,end}, regardless of the cwd-dependent
# symbol names that `ld -b binary` generates.
define WRAP_BIN
$1.o: $(BENCH)/$2/func.bin
	cp $$< $1.bin
	ld -r -b binary -o $$@.raw $1.bin
	rm -f $1.bin
	objcopy $$$$(nm $$@.raw | awk '/_func_bin_start$$$$/{printf " --redefine-sym %s=_binary_$1_bin_start",$$$$3} /_func_bin_end$$$$/{printf " --redefine-sym %s=_binary_$1_bin_end",$$$$3}') $$@.raw $$@
	rm -f $$@.raw
endef

$(eval $(call WRAP_BIN,func_01,01_memset))
$(eval $(call WRAP_BIN,func_02,02_memcpy32))
$(eval $(call WRAP_BIN,func_03,03_magic_memset))
$(eval $(call WRAP_BIN,func_04,04_train_phy_block))

gdb_debug.elf: harness.c func_01.o func_02.o func_03.o func_04.o
	gcc -O0 -g -Wall -o $@ $^

clean:
	rm -f gdb_debug.elf func_*.o *.bin
