NAME = minishell

CC = clang

CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -g

VPATH  = $(DIR_SRCS)
DIR_SRCS = srcs srcs/builtins

INCLUDES = -I$(HEADERS_D) -I$(LIBFT_D)

HEADERS_D = ./headers
HEADERS =	minishell.h \
			minishell_structs.h minishell_prototypes.h \
			minishell_macro.h minishell_term_colors.h \
			minishell_user_macro.h

LIBS =	-L $(LIBFT_D) -lft \
		-lreadline -ltermcap

LIBFT_D = ./libft
LIBFT = $(LIBFT_D)/libft.a

SRCS =	main.c init.c free.c generic.c \
		env.c exit.c

DIR_OBJS = builds
OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o)

# --------------------
# ------ RULES -------
# --------------------

all: subsystem $(NAME)

subsystem:
	@cd $(LIBFT_D) && $(MAKE)

$(LIBFT): # dispensable. utile seulement pour un appel direct à $(NAME), si $(LIBFT) n'existe pas
	cd $(LIBFT_D) && $(MAKE)

$(DIR_OBJS)/%.o: %.c | $(DIR_OBJS)
	$(CC) $(CFLAGS) -c $< -o $@

$(DIR_OBJS):
	mkdir $@

$(OBJS): $(HEADERS:%=$(HEADERS_D)/%)

$(NAME): $(OBJS) $(LIBFT)
	$(CC) $(OBJS) -o $(NAME) $(LIBS)

clean:
	rm -f $(OBJS)

fclean: clean
	rm -f $(NAME)

re: fclean all

valgrind: $(NAME)
	valgrind --leak-check=full --leak-resolution=low --show-reachable=yes ./$(NAME)

run: $(NAME)
	./$(NAME)

.PHONY : all clean fclean re bonus subsystem run valgrind
