# **************************************************************************** #
#                                                                              #
#                                                         ::::::::             #
#    Makefile                                           :+:    :+:             #
#                                                      +:+                     #
#    By: qmennen <qmennen@student.codam.nl>           +#+                      #
#                                                    +#+                       #
#    Created: 2024/10/15 11:48:46 by whaffman      #+#    #+#                  #
#    Updated: 2025/02/20 11:10:42 by whaffman      ########   odam.nl          #
#                                                                              #
# **************************************************************************** #

NAME = minishell

SRC_PATH = src
INC_PATH = inc
LIB_PATH = lib

LIBFT_PATH = $(LIB_PATH)/libft
LIBFT_INC_PATH = $(LIBFT_PATH)/inc
LIBFT = $(LIBFT_PATH)/libft.a

OBJ_PATH = obj

VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils:
VPATH += src/executor:src/parser:src/expander:src/debug:src/signal:src/builtin
SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c"))

OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))
DEPENDS = ${OBJECTS:.o=.d}

CC = cc
RM = rm -rf

INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH)
CFLAGS = -Wall -Wextra -Werror -fsanitize=address,undefined -MMD -g3

UNAME_S	:= $(shell uname -s)
ifeq ($(UNAME_S),Linux)
	LDLIBS	:= -L$(LIBFT_PATH) -lft -lreadline
endif

all: $(NAME)
	echo $(SOURCES)

$(NAME): $(LIBFT) $(OBJECTS)
	$(CC) $(CFLAGS) $(OBJECTS) $(LDLIBS) -o $(NAME)

-include ${DEPENDS}

$(LIBFT): $(LIBFT_PATH)
	$(MAKE) -C $(LIBFT_PATH)

$(LIBFT_PATH):
	git submodule add https://gitea.duinvoetje.nl/willem/libft.git $(LIBFT_PATH)

$(OBJ_PATH):
	mkdir -p $@

$(OBJ_PATH)/%.o: %.c $(LIBFT) | $(OBJ_PATH)
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

clean:
	$(RM) $(OBJECTS) $(OBJ_PATH)
	$(MAKE) -C $(LIBFT_PATH) clean


fclean: clean
	$(RM) $(NAME)
	$(MAKE) -C $(LIBFT_PATH) fclean

re: fclean all

run: all
	./$(NAME)

.PHONY: all clean fclean re run
