-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
226 lines (188 loc) · 5.51 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# program name #
NAME = minishell
# shell #
SHELL := /bin/bash
# misc #
COUNT = -1
SLEEP := sleep 0.2
# colors #
DEFAULT=\033[39m
BLACK=\033[30m
DARK_RED=\033[31m
DARK_GREEN=\033[32m
DARK_YELLOW=\033[33m
DARK_BLUE=\033[34m
DARK_MAGENTA=\033[35m
DARK_CYAN=\033[36m
LIGHT_GRAY=\033[37m
DARK_GRAY=\033[90m
RED=\033[91m
GREEN=\033[92m
ORANGE=\033[93m
BLUE=\033[94m
MAGENTA=\033[95m
CYAN=\033[96m
WHITE=\033[97m
BG_DEFAULT=\033[49m
BG_BLACK=\033[40m
BG_DARK_RED=\033[41m
BG_DARK_GREEN=\033[42m
BG_DARK_YELLOW=\033[43m
BG_DARK_BLUE=\033[44m
BG_DARK_MAGENTA=\033[45m
BG_DARK_CYAN=\033[46m
BG_LIGHT_GRAY=\033[47m
BG_DARK_GRAY=\033[100m
BG_RED=\033[101m
BG_GREEN=\033[102m
BG_ORANGE=\033[103m
BG_BLUE=\033[104m
BG_MAGENTA=\033[105m
BG_CYAN=\033[106m
BG_WHITE=\033[107m
RESET=\033[0m
RESET_TERM=\r\033[K
# messages #
MANDATORY = Program compiled
LBONUS = Bonus Program compiled
CLEAN = Objects delete
FCLEAN = Program delete
LIBNAME = minishell
BLIBNAME = minishell_bonus
COMP = Compiling
# debug and normal flags #
DFLAGS = -Wall -Wextra -Werror -g3 # TO DEBBUG
CFLAGS = -Wall -Werror -Wextra -g3 -pedantic -flto -MD -MP # FOR DEPENDENCIES
LFLAGS = -march=native # TO OPTIMIZE FOR SPECIFIC ARCHITECTURE
FFLAGS = -lreadline # FLAGS THAT ONLY WORK AT THE END OF LINE (AFTER OBJECTS)
# paths #
SRC = src
BONUS = $(SRC)/bonus
INC = includes
OBJ = obj
# includes #
HEADERS = $(addprefix $(INC)/, minishell.h)
# files path #
ENTRANCE = $(SRC)/entrance
GRAMMAR = $(SRC)/grammar
TOKENIZER = $(SRC)/tokenizer
UTILS = $(SRC)/utils
CODEP = $(SRC)/code_pieces
AST = $(SRC)/ast
EXEC = $(SRC)/exec
EXPANSIONS = $(SRC)/expansions
BUILTINS = $(SRC)/builtins
SIGNALS = $(SRC)/signals
HEREDOC = $(SRC)/heredoc
REDIR = $(SRC)/redirection
# libs #
INCLUDES = -I$(INC)/ -Ilib/libft/includes/
LINCLUDES = -L$(LIBFT_PATH) -lft
LIBFT = lib/libft/libft.a
LIBFT_PATH = lib/libft
# main #
MAIN_SRC = $(ENTRANCE)/minishell.c
# files mandatory #
CFILES = $(addprefix $(ENTRANCE)/, environ.c)
CFILES += $(addprefix $(GRAMMAR)/, grammar_checker.c rules.c meta_checker.c)
CFILES += $(addprefix $(TOKENIZER)/, add_tokens.c memory_handler.c parenthesis_validation.c quotes_validation.c tokenizer.c trim_edges.c)
CFILES += $(addprefix $(UTILS)/, ambiguous.c str.c ft_lst_split.c is_fork.c which_token.c last_exit_status.c ft_clear_list.c ft_getenv.c)
CFILES += $(addprefix $(REDIR)/, redir_utils.c)
CFILES += $(addprefix $(AST)/, ast.c ast_split_node.c ast_memory_handler.c search_tokens.c)
CFILES += $(addprefix $(EXEC)/, execution.c handling_pipe.c handling_redirs.c tokens_to_args.c handling_and_or.c handling_block.c exec_utils.c)
CFILES += $(addprefix $(EXPANSIONS)/, expansions.c heredoc_expansion.c asterisk.c split_spaces.c)
CFILES += $(addprefix $(BUILTINS)/, builtins_caller.c cd.c cd_helper.c echo.c env.c exit.c export.c pwd.c unset.c export_sorting.c export_helpers.c)
CFILES += $(addprefix $(SIGNALS)/, signal_handler.c signals_initializer.c)
CFILES += $(addprefix $(HEREDOC)/, heredoc_func.c)
VAL = valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --log-file=valgrind-out.txt
VAL_TXT = valgrind-out.txt
# obj dir #
OBJECT = $(CFILES:%.c=$(OBJ)/%.o)
BIN_OBJ = $(MAIN_SRC:%.c=$(OBJ)/%.o)
# define bonus #
ifdef WITH_BONUS
NAME = $(BLIBNAME)
compile = compile_bonus
MANDATORY = $(LBONUS)
MAGENTA = $(YELLOW)
LIBNAME = $(BLIBNAME)
endif
# define debbug #
ifdef WITH_DEBBUG
CFLAGS = $(DFLAGS)
endif
# functions #
define create_objects_dir
mkdir -p $(dir $@)
endef
define compile
$(CC) -o $(NAME) $(CFLAGS) $(LFLAGS) $(INCLUDES) $(LINCLUDES) $(OBJECT) $(BIN_OBJ) $(LIBFT) $(FFLAGS)
$(SLEEP)
printf "\n$(MAGENTA)$(MANDATORY)\n$(RESET)"
endef
define compile_bonus
$(CC) -o $(NAME) $(CFLAGS) $(LFLAGS) $(INCLUDES) $(LINCLUDES) $(OBJECT) $(BIN_OBJ) $(LIBFT) $(FFLAGS)
$(SLEEP)
printf "\n$(MAGENTA)$(MANDATORY)\n$(RESET)"
endef
define compile_source
$(eval COUNT=$(shell expr $(COUNT) + 1))
$(MAKE) -sC $(LIBFT_PATH)
$(CC) -o $@ $(CFLAGS) $(INCLUDES) -c $<
printf "$(RESET_TERM)%d%% $(GREEN)$(COMP) $(notdir $<) -> $(notdir $@) to $(NAME) \r$(RESET)" $$(echo $$(($(COUNT) * 100 / $(words $(OBJECT)))))
endef
define clean
$(MAKE) fclean -sC $(LIBFT_PATH)
$(RM) -rf $(OBJ)/ && $(RM) -rf $(VAL_TXT)
$(SLEEP)
printf "$(RED)$(CLEAN)$(RESET)\n"
endef
define fclean
$(call eraseBins)
@$(SLEEP)
@printf "$(RED)$(FCLEAN)$(RESET)\n"
endef
define bonus
@make WITH_BONUS=TRUE -s
endef
define debug
$(call clean)
$(call fclean)
$(MAKE) WITH_DEBBUG=TRUE -s
endef
define eraseBins
$(if $(NAME),@$(RM) $(NAME))
$(if $(BLIBNAME),@$(RM) $(BLIBNAME))
endef
define help
echo -e "${BG_DARK_GREEN}${ORANGE}Available targets:${RESET}"
echo -e "${BG_DARK_GREEN}${ORANGE}all:${RESET}${LIGHT_GRAY}Build minishell${RESET}"
echo -e "${BG_DARK_GREEN}${ORANGE}bonus:${RESET}${LIGHT_GRAY}Build minishell_bonus${RESET}"
echo -e "${BG_DARK_GREEN}${ORANGE}re:${RESET}${LIGHT_GRAY}Rebuild the program${RESET}"
echo -e "${BG_RED}${ORANGE}clean:${RESET}${LIGHT_GRAY}Remove the object files${RESET}"
echo -e "${BG_RED}${ORANGE}fclean:${RESET}${LIGHT_GRAY}Remove the program and the object files${RESET}"
echo -e "${BG_BLUE}${ORANGE}debug:${RESET}${LIGHT_GRAY}Build the program with debugging information${RESET}"
endef
# rules #
all: $(NAME)
$(NAME): $(OBJECT) $(BIN_OBJ)
$(call create_objects_dir)
$(call compile)
$(OBJ)/%.o: %.c
$(call create_objects_dir)
$(call compile_source)
-include $(OBJECT:.o=.d)
clean:
$(call clean)
fclean: clean
$(call fclean)
re: fclean all
bonus:
$(call bonus)
debug:
$(call debug)
help:
$(call help)
.PHONY: all bonus clean fclean re debug help Makefile
.DEFAULT_GOAL := all
.SILENT: