From 4a2ad1fa584a36ebdb0e1f1b8e108cda48dbafdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Thu, 17 Jul 2025 16:07:26 +0200 Subject: [PATCH] build: overhaul Makefile --- .gitignore | 1 + Makefile | 48 ++++++++++++++++++++++++++++-------------- objects/.gitkeep | 0 objects/draw/.gitkeep | 0 objects/map/.gitkeep | 0 objects/utils/.gitkeep | 0 6 files changed, 33 insertions(+), 16 deletions(-) delete mode 100644 objects/.gitkeep delete mode 100644 objects/draw/.gitkeep delete mode 100644 objects/map/.gitkeep delete mode 100644 objects/utils/.gitkeep diff --git a/.gitignore b/.gitignore index 34efbb6..4d25683 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ cub3d *.o +*.d libft.a vgcore.* diff --git a/Makefile b/Makefile index d677eb7..5eeaca4 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,14 @@ -CC=cc -SANITIZERS=-fsanitize=address,undefined -fno-omit-frame-pointer -CFLAGS=-Wall -Wextra -Werror -g $(SANITIZERS) -I mlx -SOURCEFILES=\ +CC = cc +# -fno-omit-frame-pointer is to prevent malloc stacktraces from being truncated, +# see "My malloc stacktraces are too short" here: +# https://github.com/google/sanitizers/wiki/AddressSanitizer +SANITIZERS = -fsanitize=address,undefined -fno-omit-frame-pointer +ifeq ($(CFLAGS),) + CFLAGS = -Wall -Wextra -Werror -g +endif +IFLAGS = -I./mlx -I./libft + +SOURCEFILES = \ src/draw/draw_map.c \ src/draw/drawutils.c \ src/main.c \ @@ -13,31 +20,40 @@ SOURCEFILES=\ src/utils/hooks.c \ src/map/map_checker.c \ -OBJECTS=$(patsubst src/%.c,objects/%.o,$(SOURCEFILES)) -OBJDIR=objects -NAME=cub3d +OBJECTS = $(SOURCEFILES:.c=.o) +NAME = cub3d +DEPS = $(OBJECTS:.o=.d) + +.PHONY: all clean fclean bonus re sane all: $(OBJECTS) $(NAME) +-include $(DEPS) + $(NAME): $(OBJECTS) if [[ "$$(echo $$LD_LIBRARY_PATH | grep -c minilibx)" == "0" ]]; then $(MAKE) -C mlx/; fi - $(MAKE) -C libft/ - cp libft/libft.a . - $(CC) $(CFLAGS) $(OBJECTS) -o $(NAME) -L. -lft -Lmlx -lmlx -lz -lXext -lX11 + +$(MAKE) -C libft/ + $(CC) $(CFLAGS) $(IFLAGS) $(OBJECTS) -o $(NAME) -Llibft -Lmlx -lft -lmlx -lz -lXext -lX11 -$(OBJDIR)/%.o: src/%.c - $(CC) $(CFLAGS) -Imlx -c $< -o $@ +%.o: %.c + $(CC) -c $(CFLAGS) $(IFLAGS) -o $*.o $*.c + $(CC) -MM $(CFLAGS) $(IFLAGS) -MT $*.o $*.c > $*.d clean: - rm -f $(OBJECTS) + +make -C libft clean + find . -name '*.o' -print -delete + find . -name '*.d' -print -delete fclean: clean + +make -C libft fclean rm -f $(NAME) bonus: CFLAGS += -D BONUS=1 - bonus: all -re: fclean all +sane: CFLAGS += $(SANITIZERS) +sane: all -.PHONY: all clean fclean bonus re +re: + +make fclean + +make all diff --git a/objects/.gitkeep b/objects/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/objects/draw/.gitkeep b/objects/draw/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/objects/map/.gitkeep b/objects/map/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/objects/utils/.gitkeep b/objects/utils/.gitkeep deleted file mode 100644 index e69de29..0000000