-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
118 lines (98 loc) · 3.13 KB
/
Copy pathMakefile
File metadata and controls
118 lines (98 loc) · 3.13 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
CC := gcc
BUILD ?= debug
TARGET_EXEC ?= $(notdir $(CURDIR)).out
# Compiler flags
WARNFLAGS := \
-Wall -Wextra -Wpedantic -Werror -pedantic-errors \
-Wshadow \
-Wconversion -Wsign-conversion \
-Wcast-qual -Wcast-align \
-Wpointer-arith \
-Wwrite-strings \
-Wdouble-promotion \
-Wstrict-prototypes \
-Wold-style-definition \
-Wswitch-enum -Wswitch-default \
-Wformat=2 -Wformat-truncation=2 \
-Wundef \
-Wbad-function-cast \
-Wredundant-decls \
-Wvla \
-Wimplicit-fallthrough=5 \
-Wduplicated-cond \
-Wduplicated-branches \
-Wnull-dereference \
-fno-common
DEBUGFLAGS := -O0 \
-g3 \
-fno-omit-frame-pointer
# Sanitizer flags
SANITIZEFLAGS := -fsanitize=address,undefined
# Linker flags
LDFLAGS := -L$(LIB_DIR)
# The libraries to link with
LDLIBS :=
ifeq ($(BUILD),debug)
CFLAGS := -fdiagnostics-color=always -std=c99 $(WARNFLAGS) $(DEBUGFLAGS) $(SANITIZEFLAGS)
# Add sanitizer flags to the linker libraries
LDLIBS += $(SANITIZEFLAGS)
else ifeq ($(BUILD),release)
CFLAGS := -fdiagnostics-color=always -O2 -DNDEBUG -std=c99 $(WARNFLAGS)
CFLAGS += -fstack-protector-strong -D_FORTIFY_SOURCE=3
else
$(error BUILD must be either 'debug' or 'release')
endif
SRC_DIR := ./src
INC_DIR := ./include
BIN_DIR := ./bin
# store compiled object files
OBJ_DIR := ./obj
# holds static libraries
LIB_DIR := ./lib
RM := rm -f
SOURCES := $(shell find $(SRC_DIR) -name '*.c')
OBJECTS := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SOURCES))
DEPFILES := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.d,$(SOURCES))
INC_PARAMS := $(addprefix -I,$(shell find $(INC_DIR) -type d))
# C preprocessor flags
CPPFLAGS := $(INC_PARAMS)
# Automatically detect static libraries and convert to linker flags
LIBRARIES := $(wildcard $(LIB_DIR)/lib*.a)
LIB_FLAGS := $(patsubst $(LIB_DIR)/lib%.a,-l%,$(LIBRARIES))
# The libraries to link with
LDLIBS += -Wl,--start-group $(LIB_FLAGS) -Wl,--end-group
# Ensure the object directory exists before trying to include dependency files
$(OBJ_DIR):
mkdir -p $(OBJ_DIR)
-include $(DEPFILES)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
@mkdir -p $(dir $@)
$(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -MF $(OBJ_DIR)/$*.d -c $< -o $@
$(BIN_DIR)/$(TARGET_EXEC): $(OBJECTS) | $(OBJ_DIR)
@mkdir -p $(BIN_DIR)
$(CC) $^ $(LDFLAGS) $(LDLIBS) $(SANITIZEFLAGS) -o $@
.PHONY: all
all: $(BIN_DIR)/$(TARGET_EXEC)
.PHONY: run
run: all
$(BIN_DIR)/$(TARGET_EXEC)
.PHONY: clean
clean:
$(RM) $(BIN_DIR)/$(TARGET_EXEC)
find $(OBJ_DIR) -name "*.o" -type f -delete
find $(OBJ_DIR) -name "*.d" -type f -delete
.PHONY: distclean
distclean:
$(RM) -r $(OBJ_DIR) $(BIN_DIR)
.PHONY: rebuild
rebuild: clean all
.PHONY: help
help:
@echo "\033[1;34mUsage: make [all|run|clean|distclean|rebuild|help] [BUILD=debug|release]\033[0m"
@echo " \033[1;32mall\033[0m: Compile the project"
@echo " \033[1;32mrun\033[0m: Compile and run the project"
@echo " \033[1;32mclean\033[0m: Remove compiled files"
@echo " \033[1;32mdistclean\033[0m: Remove compiled files and directories"
@echo " \033[1;32mrebuild\033[0m: Clean and recompile the project"
@echo " \033[1;32mhelp\033[0m: Display this help message"
@echo " \033[1;32mBUILD\033[0m: Set to debug (default) or release"