-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
executable file
·63 lines (53 loc) · 1.83 KB
/
Copy pathMakefile
File metadata and controls
executable file
·63 lines (53 loc) · 1.83 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
#!/usr/bin/make -f
PROJECT_NAME := simple_shell
# Compiler
CC := clang
# Directory for source files
SRC_DIR := .
# Directory for object files
OBJ_DIR := obj
# Directories with header files
INCL_DIRS := $(shell find . -mount -maxdepth 1 -name '*.h' -type f -exec dirname '{}' \;)
# All source files
SRCS := $(shell find $(SRC_DIR) -mount -maxdepth 1 -name '*.c' -type f)
# Object files are stored in the builds directory
OBJS = $(SRCS:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
# Dependency files for make
# https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html#index-M
DEP_FILES = $(OBJS:.o=.d)
# Include flags
INCL_FLAGS = $(addprefix -iquote,$(INCL_DIRS))
OPTIMISATION_FLAGS := -O0
# https://www.gnu.org/software/make/manual/html_node/Automatic-Prerequisites.html
# https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html#index-MMD
CPPFLAGS := -MMD
# Warnings
C_STANDARD := --std=gnu89
WARN_FLAGS = $(C_STANDARD) -pedantic -Wall -Werror -Wextra
SANITISERS := -fsanitize=undefined -fsanitize=address -fsanitize=leak
# Debugging flags
DEBUG_FLAGS := -g3 -fno-omit-frame-pointer
CFLAGS = $(CPPFLAGS) $(INCL_FLAGS) $(WARN_FLAGS) $(SANITISERS) $(OPTIMISATION_FLAGS) $(DEBUG_FLAGS)
# First rule that will be run by make on default
debug: $(PROJECT_NAME)
release: CPPFLAGS += -DNDEBUG
release: DEBUG_FLAGS :=
release: C_STANDARD := --std=c99
release: SANITISERS :=
release: OPTIMISATION_FLAGS := -O2
release: $(PROJECT_NAME)
# $@ - the target
$(OBJ_DIR):
@mkdir -vp $@
# $^ - all the prerequisites
$(PROJECT_NAME): $(OBJS)
$(CC) $(CFLAGS) -o $@ $^
# https://www.gnu.org/software/make/manual/html_node/Prerequisite-Types.html
# $< - name of only the first prequisite
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
$(CC) $(CFLAGS) -o $@ -c $<
.PHONY: clean
clean:
@$(RM) -vrd --preserve-root $(OBJ_DIR) $(PROJECT_NAME)
# Include the dependencies
-include $(DEP_FILES)