MAKEFLAGS += -j

CC := gcc
LD := $(CC)
CFLAGS := -g -O0 -Wall -Wextra -Werror -MMD

OBJ := $(patsubst src/%.c,build/%.o,$(wildcard src/*.c))
DEP := $(patsubst %.o,%.d,$(OBJ))
NAME := libmalloc.so

all: $(NAME)

$(NAME): $(OBJ)
	@printf 'LD      %s\n' "$@"
	$(LD) -shared -o $(NAME) $(OBJ)

build/%.o: src/%.c
	@printf 'CC     %s\n' "$@"
	@mkdir -p $(@D)
	@$(CC) $(CFLAGS) -fPIC -c -o $@ $<

clean:
	@printf 'RM      build\n'
	@rm -rf build/

fclean:
	@printf 'RM      build %s\n' "$(NAME)"
	@rm -rf build/ $(NAME) bozo bozo.d

re:
	@make --no-print-directory fclean
	@make --no-print-directory all

test:
	@make --no-print-directory all
	@$(CC) $(CFLAGS) test/main.c -o bozo -L. -lmalloc
	@export LD_LIBRARY_PATH=$(shell pwd) && valgrind ./bozo

.PHONY: all clean fclean re test

-include $(DEP)