-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneric_c.mk
More file actions
105 lines (84 loc) · 3.23 KB
/
generic_c.mk
File metadata and controls
105 lines (84 loc) · 3.23 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
# **************************************************************************** #
# #
# ::: :::::::: #
# generic_c.mk :+: :+: :+: #
# +:+ +:+ +:+ #
# By: pcarles <pcarles@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/01/23 17:30:55 by pcarles #+# #+# #
# Updated: 2019/04/12 15:32:42 by pcarles ### ########.fr #
# #
# **************************************************************************** #
# Default value for target's name (name of the current working directory)
CURRENT_DIR := $(shell pwd)
TARGETS ?= $(shell basename $(CURRENT_DIR))
# Default values for compilation flags
CC ?= gcc
C_FLAGS ?= -Wall -Wextra -Werror
LD_FLAGS ?=
DEP_FLAGS ?= -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td
# Default values for debug flags
DEBUG ?= 0
C_FLAGS_DEBUG ?= -g3 -fsanitize=address
LD_FLAGS_DEBUG ?= -g3 -fsanitize=address
# Default values for directories
SRCDIR ?= src
HDRDIR ?= includes
LIBDIR ?= lib
DEPDIR ?= .d
BINDIR ?= bin
# If in debug mode, compile with debug flags in a separate directory
ifeq ($(DEBUG),1)
BINDIR := $(BINDIR)/debug
C_FLAGS += $(C_FLAGS_DEBUG)
LD_FLAGS += $(LD_FLAGS_DEBUG)
else
BINDIR := $(BINDIR)/release
endif
# Template to generate linking rule (for each targets)
define LINKING_RULE_TEMPLATE
$(1)_BIN := $$(patsubst %.c,%.o,$$(addprefix $(BINDIR)/,$$($(1)_SRC)))
$(1): $$($(1)_BIN)
@$(CC) $(LD_FLAGS) -I$(HDRDIR) -L./$(BINDIR) -o $(1) $$($(1)_BIN) $(addprefix -l,$($(1)_LIB))
@echo "\033[32;1mCreated $(1)\033[0m"
endef
# Template to generate lib compilation rule
define COMPILE_LIB_TEMPLATE
$(1):
@$(MAKE) -C $(LIBDIR)/$(LIB) TARGET_PATH='$(CURRENT_DIR)/$(BINDIR)'
endef
define INCLUDE_DEP_TEMPLATE
include $$(wildcard $$(patsubst %.c,$(DEPDIR)/%.d,$$($(1)_SRC)))
endef
export DEBUG C_FLAGS LD_FLAGS
##
# Rule definition
##
all: $(LIBS) $(TARGETS)
# Generate libraries compilation rules
$(foreach LIB,$(LIBS),$(eval $(call COMPILE_LIB_TEMPLATE,$(strip $(LIB)))))
# Generate linking rule for each target
$(foreach TARGET,$(TARGETS),$(eval $(call LINKING_RULE_TEMPLATE,$(strip $(TARGET)))))
# Compilation rule
$(BINDIR)/%.o: $(SRCDIR)/%.c $(DEPDIR)/%.d
@mkdir -p $(dir $@) $(dir $(DEPDIR)/$*)
@$(CC) $(C_FLAGS) $(DEP_FLAGS) -I$(HDRDIR) -o $@ -c $<
@mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d && touch $@
@echo "\033[33mCompiling $@\033[0m"
$(DEPDIR)/%.d: ;
# Clean all binaries expect the targets..
clean:
@rm -rf $(shell dirname $(BINDIR)) $(DEPDIR)
@$(MAKE) -C $(addprefix $(LIBDIR)/,$(LIBS)) $@
# ..clean targets binaries
fclean: clean
@rm -f $(TARGETS)
@$(MAKE) -C $(addprefix $(LIBDIR)/,$(LIBS)) $@
re: fclean
@$(MAKE) all
norm:
@norminette $(SRCDIR) $(HDRDIR)
.PHONY: all clean fclean re norm
.PRECIOUS: $(DEPDIR)/%.d
# Include dependency files
$(foreach TARGET,$(TARGETS),$(eval $(call INCLUDE_DEP_TEMPLATE,$(strip $(TARGET)))))