NAME	:= cub3d
CFLAGS	:= -Wextra -Wall -Werror -Wunreachable-code -Ofast
LIBMLX	:= ./lib/mlx

LIBFT	:= ./lib/libft
LIBFT_A	:= $(LIBFT)/libft.a

FTPRINTF	:= ./lib/ft_printf
FT_PRINTF_A	:= ./lib/ft_printf/libftprintf.a

GNL			:= ./lib/gnl
GNL_A		:= ./lib/gnl/get_next_line.a

HEADERS	:= -I ./include -I $(LIBMLX)/include -I $(LIBFT) -I $(FTPRINTF) -I $(GNL)
LIBS := $(LIBMLX)/build/libmlx42.a $(GNL_A) $(LIBFT_A) $(FT_PRINTF_A) -ldl -lglfw -pthread -lm
SRCS	:= $(shell find ./src -iname "*.c")
OBJS	:= ${SRCS:.c=.o}

all: libmlx libft ft_printf gnl $(NAME)

libmlx:
	@cmake $(LIBMLX) -B $(LIBMLX)/build && make -C $(LIBMLX)/build -j4

libft:
	@$(MAKE) -C $(LIBFT)

ft_printf:
	@$(MAKE) -C $(FTPRINTF)

gnl:
	@$(MAKE) -C $(GNL)

%.o: %.c
	@$(CC) $(CFLAGS) -o $@ -c $< $(HEADERS) && printf "Compiling: $(notdir $<)"

$(NAME): $(OBJS)
	@$(CC) $(OBJS) $(LIBS) $(HEADERS) -o $(NAME)

clean:
	@rm -rf $(OBJS)
	@rm -rf $(LIBMLX)/build
	@$(MAKE) -C $(LIBFT) clean
	@$(MAKE) -C $(FTPRINTF) clean
	@$(MAKE) -C $(GNL) clean

fclean: clean
	@rm -rf $(NAME)
	@$(MAKE) -C $(LIBFT) fclean
	@$(MAKE) -C $(FTPRINTF) fclean
	@$(MAKE) -C $(GNL) fclean

re: fclean all

.PHONY: all, clean, fclean, re, libmlx, libft, ft_printf, gnl