CC = gcc
CFLAGS = -Wall -Wextra -O0 -no-pie -lm -g -std=c99
SRC = $(wildcard *.c)
TGT = $(subst .c,,$(SRC))
BUILD_DIR = build
DUMP_DIR = dump


pframe:
	curl -o pframe.tgz  https://dept-info.labri.fr/~thibault/SecuLog/pframe.tgz && \
		tar -xf pframe.tgz &&\
		rm -rf pframe.tgz

.gdbinit:

configure: pframe .gdbinit
	$(shell echo "python import pframe" > .gdbinit) 

$(BUILD_DIR)/%_32: %.c
	$(shell mkdir -p $(BUILD_DIR))
	$(CC) $(CFLAGS) -m32 -o $@ $<


$(BUILD_DIR)/%_64: %.c
	$(shell mkdir -p $(BUILD_DIR))
	$(CC) $(CFLAGS) -m64 -o $@ $<

build: $(addprefix $(BUILD_DIR)/, $(addsuffix _32, $(TGT))) \
	   $(addprefix $(BUILD_DIR)/, $(addsuffix _64, $(TGT)))


PHONY: %
%: $(addprefix $(BUILD_DIR)/, $(addsuffix _32, %))
	PYTHONPATH=${PWD}/pframe${PYTHONPATH:+:${PYTHONPATH}} gdb $< --command=$@.gdb


PHONY: clean
clean:
	@rm -rf $(BUILD_DIR) pframe .gdbinit
