-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
94 lines (72 loc) · 1.82 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
NAME = downloader
# Compiler and linker
CC = clang
LD = clang
AR = ar
# Paths
SRCDIR = src
INCDIR = include
LIBDIR = lib
OBJDIR = obj
BINDIR = .
# Library dependencies
LIBS = $(addprefix $(LIBDIR)/, libft/libft.a)
LIBDIRS = $(dir $(LIBS))
LIBINCS = $(addsuffix $(INCDIR), $(LIBDIRS))
LIBARS = $(notdir $(LIBS))
# Sources
INCS = $(LIBINCS) $(INCDIR)
SRCS = $(addprefix $(SRCDIR)/,\
main.c\
http.c\
sock_cli.c\
)
OBJS = $(SRCS:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
DEPS = $(OBJS:.o=.d)
# Flags
CFLAGS = -Wall -Wextra -Werror $(INCS:%=-I%)
DFLAGS = -MT $@ -MMD -MP -MF $(OBJDIR)/$*.d
LDFLAGS = $(LIBDIRS:%=-L%)
LDLIBS = $(LIBARS:lib%.a=-l%) -lssl
ARFLAGS = -rcus
# Compiling commands
COMPILE.c = $(CC) $(DFLAGS) $(CFLAGS) -c
COMPILE.o = $(LD) $(LDFLAGS)
ARCHIVE.o = $(AR) $(ARFLAGS)
all: $(BINDIR)/$(NAME)
# Directories
$(OBJDIR) $(BINDIR):
@echo "MK $@"
mkdir -p "$@"
# Libraries
$(LIBS): %.a: FORCE
make -C $(dir $@) NAME=$(@F)
# Objects
$(OBJS): $(OBJDIR)/%.o: $(SRCDIR)/%.c $(OBJDIR)/%.d | $(OBJDIR)
@mkdir -p '$(@D)'
@echo "CC $<"
$(COMPILE.c) $< -o $@
# Dependencies
$(DEPS): $(OBJDIR)/%.d:
include $(wildcard $(DEPS))
# Binaries
$(BINDIR)/$(NAME): $(OBJS) $(LIBS) | $(BINDIR)
@echo "LD $@"
$(COMPILE.o) $^ -o $@ $(LDLIBS)
# Use @echo "AR $@"
# $(ARCHIVE.o) $@ $^
# instead when building a static library
clean:
$(foreach dir, $(LIBDIRS),\
echo "MK $(addprefix -C , $(dir)) $@" && make -C $(dir) $@ ; ):
@rm -r "$(OBJDIR)" 2>/dev/null && echo "RM $(OBJDIR)" || :
fclean: clean
$(foreach dir, $(LIBDIRS),\
echo "MK $(addprefix -C, $(dir)) $@" && make -C $(dir) $@ ; ):
@rm "$(BINDIR)/$(NAME)" 2>/dev/null && echo "RM $(BINDIR)/$(NAME)" || :
@rmdir "$(BINDIR)" 2>/dev/null && echo "RM $(BINDIR)" || :
re: fclean all
FORCE: ;
.PHONY: all clean fclean re FORCE
# Assign a value to VERBOSE to enable verbose output
$(VERBOSE).SILENT: