-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
163 lines (133 loc) · 5.65 KB
/
Copy pathMakefile
File metadata and controls
163 lines (133 loc) · 5.65 KB
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
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: thamahag <BTP_Magna@proton.me> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2025/06/15 21:59:08 by thamahag #+# #+# #
# Updated: 2025/06/15 21:59:08 by thamahag ### ########.fr #
# #
# **************************************************************************** #
NAME := libft.a
CC := cc
CFLAG += -Wall -Wextra -Werror
RM := rm -rf
# Define Directory
SRCS_DIR = srcs
BONUS_SRCS_DIR = bonus_srcs
OBJS_DIR = objs
BONUS_OBJS_DIR = bonus_objs
INCS_DIR = includes
BONUS_INCS_DIR = includes
# Defind files to create
SRCS := ft_memset ft_bzero ft_memcpy ft_memmove ft_memchr ft_memcmp ft_strlen\
ft_strdup ft_strncpy ft_strncpy ft_strcat ft_strncat ft_strlcat ft_strlcpy\
ft_strchr ft_strrchr ft_strstr ft_strnstr ft_strcmp ft_strncmp ft_atoi \
ft_isalpha ft_isdigit ft_isalnum ft_isascii ft_isprint ft_toupper \
ft_tolower ft_calloc ft_substr ft_strjoin ft_strtrim ft_split ft_itoa \
ft_strmapi ft_striteri ft_putchar_fd ft_putstr_fd ft_putendl_fd \
ft_putnbr_fd
BONUS := ft_lstnew ft_lstadd_front ft_lstsize ft_lstlast ft_lstadd_back \
ft_lstdelone ft_lstclear ft_lstiter ft_lstmap
# Add Prefix directory to compile .c $(addprefix src/,foo bar)
# the result src/foo src/bar.
# String substitution $(patsubst pattern,replaceme`nt, text)
# $(patsubst %.c,%.o,x.c.c bar.c)
# produces the value ‘x.c.o bar.o’.
# $(patsubst %.o,%.c,$(objects)) = $(object:.o=.c)
# Assign OBJS with all substitute .c files at individual .o files
SRCS := $(addsuffix .c, $(SRCS))
BONUS := $(addsuffix _bonus.c, $(BONUS))
OBJS := $(SRCS:.c=.o)
BONUS_OBJS := $(BONUS:.c=.o)
SRCS := $(addprefix $(SRCS_DIR)/, $(SRCS))
OBJS := $(addprefix $(OBJS_DIR)/, $(OBJS))
BONUS := $(addprefix $(BONUS_SRCS_DIR)/, $(BONUS))
BONUS_OBJS := $(addprefix $(BONUS_OBJS_DIR)/, $(BONUS_OBJS))
# Set condition for OBJ_LIST
ifdef BONUS_POINT
OBJ_LIST := $(OBJS) $(BONUS_OBJS)
else
OBJ_LIST := $(OBJS)
endif
# Mandtory only target
all : $(NAME)
# Bonus target: rebuild libft.a with both OBJS and BONUS_OBJS
bonus :
@$(MAKE) BONUS_POINT=1
$(NAME) : $(OBJ_LIST)
ar rcs $@ $^
# pattern match of target with correspond prerequirement one by one
# -I add directory search list for #include files with relative file names.
$(OBJS_DIR)/%.o: $(SRCS_DIR)/%.c
@mkdir -p $(OBJS_DIR)
$(CC) $(CFLAG) -I $(INCS_DIR) -c $< -o $@
$(BONUS_OBJS_DIR)/%.o: $(BONUS_SRCS_DIR)/%.c
@mkdir -p $(BONUS_OBJS_DIR)
$(CC) $(CFLAG) -I $(BONUS_INCS_DIR) -c $< -o $@
clean :
$(RM) $(OBJS) $(BONUS_OBJS)
fclean : clean
$(RM) $(NAME)
re : fclean all
# Tell makefile that any file with these name won't cause conflict with make
PHONY: all clean fclean re bonus
# @echo - normally run but if add in make -s it will silent any @ line
# Automatic Vriable
# $@ - Current Target name
# $< - First prerequistic only
# $^ - All prerequisites
# $? - Prerequisite that have changed
# $| - Order-only prerequisite - Need more example
# $(TARGET) : $(OBJS) | $(DIR)
# Rules
# There are implicit default rule to built different file type
# so it would be better to define your own
# Assignment Variable Operator
# = - verbatim assignment assign value to variable as is
# := - simple expansion, unlimited recursion expand till it all text
# != - Shell output
# ?= - Will set variable if it didn't set before
# += - Append to the variable
# Built in function
# Text function
# $(SRCS:.c=.o)
# Filename function
# $(addprefix build/,$(OBJS))
# Conditional function
# $(if ..) $(or ..) $(and ..)
# $(foreach var,list,text)
# $(value (VARIABLE))
# Shell Function
# $(shell ..)
# Contraol Function
# $(error ..) Halt the building at that point
# $(warning ..) Give warning
# $(info ..) Give info
# Compiling flag
# -Ldir Adds directory linker searches list for library
# -lname Links with object library libname.so, or libname.a.
# -l and name after the lib like libfoo.a -lfoo
# -Idir Adds directory to searched for #include files with relative file names.
# Step
# 1. Should print all command it running
# 2. Shouldn't make unnessary command
# 3. All .c files are in srcs directory
# ft_putchar.c, ft_swap.c, ft_putstr.c, ft_strlen.c, ft_strcmp.c
# 4. Put header files into includes directory
# 5. Compile c files with -Wall -Wextra -Werror
# 6. -c object files should output in the same place as srcs directory
# 7. libft.a should be at root of the ex01
# 8. Required rule: clean, fclean, re, all, and libft.a
# 9. Make = all, all should make libft.a
# 10. clean - should remove all temp .o files
# 11. fclean - run clean then remove all generate binary
# 12. re - run make fclean then make all
# Warning no wildcard
# libft.a are usually called static library are usually create by ar archive
# which can be use with compiler as linker input file with option -l(name)
# eg: libft.a can be link with -lft the ar add phony hidden name of the object
# as a lookup table for linker to search from appropriate symbol index to
# add correct object to unresolve symbol reference when compile
# difference from dynamic library which has extension of .so