default: all
#==============================================================================#
#--------------------------------- SHELL --------------------------------------#
#==============================================================================#
SHELL			:= /bin/sh
OS				 = $(shell uname)
#==============================================================================#
#------------------------------ DIRECTORIES -----------------------------------#
#==============================================================================#
SRCS_DIR		 = src/
OBJS_DIR		 = obj/
LFT_DIR			 = libft/
LFT_INCS_DIR	 = ${LFT_DIR}include/
LFT_SRCS_DIR	 = ${LFT_DIR}src/
#==============================================================================#
#--------------------------------- FILES --------------------------------------#
#==============================================================================#
SRCS_NAME		 = b_cd
SRCS_NAME		+= b_echo
SRCS_NAME		+= b_env
SRCS_NAME		+= b_exit
SRCS_NAME		+= b_export
SRCS_NAME		+= b_export_next
SRCS_NAME		+= b_export_mute
SRCS_NAME		+= b_pwd
SRCS_NAME		+= b_type
SRCS_NAME		+= b_unset
SRCS_NAME		+= e_builtins
SRCS_NAME		+= e_externs
SRCS_NAME		+= e_externs_next
SRCS_NAME		+= e_externs_pipes
SRCS_NAME		+= e_line
SRCS_NAME		+= e_pipes
SRCS_NAME		+= f_alloc
SRCS_NAME		+= f_chdir
SRCS_NAME		+= f_com
SRCS_NAME		+= f_errno
SRCS_NAME		+= f_exec
SRCS_NAME		+= f_fail
SRCS_NAME		+= f_file
SRCS_NAME		+= f_redir
SRCS_NAME		+= f_shlvl
SRCS_NAME		+= m_argv
SRCS_NAME		+= m_comm
SRCS_NAME		+= m_funptr
SRCS_NAME		+= m_init
SRCS_NAME		+= m_loop
SRCS_NAME		+= m_loop_next
SRCS_NAME		+= m_minishell
SRCS_NAME		+= m_mshrc
SRCS_NAME		+= m_prompt
SRCS_NAME		+= m_redirs
SRCS_NAME		+= s_destroy
SRCS_NAME		+= s_com
SRCS_NAME		+= s_init
SRCS_NAME		+= s_line
SRCS_NAME		+= s_lvars
SRCS_NAME		+= s_lpipes
SRCS_NAME		+= p_line
SRCS_NAME		+= p_lcom
SRCS_NAME		+= p_lcom_next
SRCS_NAME		+= p_split
SRCS_NAME		+= u_utils
SRCS_NAME		+= u_vars
SRCS_NAME		+= u_vars_next
#------------------------------------------------------------------------------#
SRCS			 = $(addprefix ${SRCS_DIR}, $(addsuffix .c, ${SRCS_NAME}))
#------------------------------------------------------------------------------#
INCS_NAME		 = b_builtins
INCS_NAME		+= d_enum
INCS_NAME		+= d_define
INCS_NAME		+= s_struct
#------------------------------------------------------------------------------#
INCS			 = $(addprefix ${SRCS_DIR}, $(addsuffix .h, ${INCS_NAME}))
INCS			+= $(patsubst %.c,%.h,${SRCS})
#------------------------------------------------------------------------------#
OBJS			 = $(patsubst ${SRCS_DIR}%.c,${OBJS_DIR}%.o,${SRCS})
#------------------------------------------------------------------------------#
NAME			 = minishell
#------------------------------------------------------------------------------#
LFT_SRCS		 = $(shell find ${LFT_SRCS_DIR} -name "*.c")
#==============================================================================#
#-------------------------------- COMPILER ------------------------------------#
#==============================================================================#
ifeq (${OS}, FreeBSD)
CC				 = /usr/bin/cc
endif
ifeq (${OS}, Linux)
CC				 = /usr/bin/clang-9
endif
ifeq (${OS}, Darwin)
CC				 = clang
endif
CFLAGS			 = -std=c89
CFLAGS			+= -Wall
CFLAGS			+= -Wextra
CFLAGS			+= -Werror
CFLAGS			+= -pedantic
#------------------------------------------------------------------------------#
LDFLAGS			 = -L${LFT_DIR}
LDFLAGS			+= -lft
#==============================================================================#
#--------------------------------- UNIX ---------------------------------------#
#==============================================================================#
RM				= rm -rf
MKDIR			= mkdir -p
#==============================================================================#
#--------------------------------- RULES --------------------------------------#
#==============================================================================#
LFTRULE			= all
#------------------------------------------------------------------------------#
${OBJS_DIR}%.o:		${SRCS_DIR}%.c ${INCS}
	@${MKDIR} ${OBJS_DIR}
	${CC} -c ${CFLAGS} ${CDEFS} -I${LFT_INCS_DIR} -o $@ $< 
#------------------------------------------------------------------------------#
$(NAME):	${OBJS} ${LFT_SRCS} ${LFT_INCS_DIR}libft.h
	@$(MAKE) --no-print-directory -C ${LFT_DIR} ${LFTRULE}
	${CC} ${CFLAGS} -o ${NAME} ${OBJS} ${LDFLAGS}
#------------------------------------------------------------------------------#
all: ${NAME}
#------------------------------------------------------------------------------#
debug: CFLAGS += -glldb
debug: LFTRULE = debug
debug: all
#------------------------------------------------------------------------------#
asan: CFLAGS += -glldb
asan: CFLAGS += -fsanitize=address
asan: LFTRULE = asan
asan: all
#------------------------------------------------------------------------------#
msan: CFLAGS += -glldb
msan: CFLAGS += -fsanitize=memory
msan: CFLAGS += -fsanitize-memory-track-origins
msan: CFLAGS += -fno-common
msan: CFLAGS += -fno-omit-frame-pointer
msan: LFTRULE = msan
msan: all
#------------------------------------------------------------------------------#
clean: 
	@$(MAKE) --no-print-directory -C ${LFT_DIR} clean
	${RM} ${OBJS_DIR} vgcore*
#------------------------------------------------------------------------------#
fclean: clean
	@$(MAKE) --no-print-directory -C ${LFT_DIR} fclean
	${RM} ${NAME} ${NAME}.core ${NAME}.dSYM/ qwe minishell_history
#------------------------------------------------------------------------------#
re: fclean all
#------------------------------------------------------------------------------#
run: all
	clear; ./${NAME}
#------------------------------------------------------------------------------#
.PHONY:	all clean clean fclean re debug asan run