This repository was archived by the owner on May 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmakefile
More file actions
114 lines (93 loc) · 3.67 KB
/
Copy pathmakefile
File metadata and controls
114 lines (93 loc) · 3.67 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
#==============================================================================
# MACROS
#==============================================================================
CC := gcc
CSTD := -std=c11
LIB_CFLAGS := -D_GNU_SOURCE
DEV_CFLAGS := -Wall -Wextra -Wpedantic -Wformat=2 -Wno-unused-parameter \
-Wshadow -Wwrite-strings -Wstrict-prototypes \
-Wold-style-definition -Wredundant-decls -Wnested-externs \
-Wmissing-include-dirs -Og
# Core sources
SRC_DIR := src/
OBJ_DIR := obj/
DEP_DIR := dep/
BIN_DIR := bin/
SOURCES := $(shell ls $(SRC_DIR)*.c)
OBJECTS := $(subst $(SRC_DIR),$(OBJ_DIR),$(subst .c,.o,$(SOURCES)))
DEPFILES := $(subst $(SRC_DIR),$(DEP_DIR),$(subst .c,.d,$(SOURCES)))
# Additional sources
INC_DIR := includes/
INC_SRC_DIR := ${addprefix ${SRC_DIR},${INC_DIR}}
INC_OBJ_DIR := ${addprefix ${OBJ_DIR},${INC_DIR}}
INC_DEP_DIR := ${addprefix ${DEP_DIR},${INC_DIR}}
INC_SOURCES := $(shell ls $(INC_SRC_DIR)*.c)
INC_OBJECTS := $(subst $(INC_SRC_DIR),$(INC_OBJ_DIR),$(subst .c,.o,$(INC_SOURCES)))
INC_DEPFILES := $(subst $(INC_SRC_DIR),$(INC_DEP_DIR),$(subst .c,.d,$(INC_SOURCES)))
# Tests
CMD_NAME := sanity
TEST_DIR := ${BIN_DIR}test/
TST_SRC_DIR := $(addprefix $(SRC_DIR),"test/")
TEST_SRC := $(addprefix $(TST_SRC_DIR),$(addsuffix .c,$(CMD_NAME)))
TEST_CMD := $(addprefix $(BIN_DIR),$(CMD_NAME))
#==============================================================================
# TARGETS
#==============================================================================
#
# DEFAULT TARGET
#
default : help
#
# VISIBLE TARGETS
#
# Helpful rule which lists all other rules and encourages documentation
#
# target: help - Display all targets in makefile
#
help :
@egrep "^# target:" makefile
# Run sanity check
#
# target: sane - Run sanity check
sane : $(TEST_CMD)
@$(TEST_CMD) $(TEST_DIR)
# Clean up files produced by the makefile. Any invocation should execute, regardless of file modification date, hence
# dependency on FRC.
#
# target: clean - Remove all files produced by this makefile
clean : FRC
@rm -rf $(BIN_DIR) $(OBJ_DIR) $(DEP_DIR)
#
# HIDDEN TARGETS
#
# Force build of dependency and object files to import additional makefile targets
#
-include $(DEPFILES) ${INC_DEPFILES}
# Link executable binary for sanity check
#
$(TEST_CMD) : $(OBJECTS) ${INC_OBJECTS}
@mkdir -p $(BIN_DIR)
$(CC) $(TEST_SRC) $^ -o $@ $(CSTD) $(LIB_CFLAGS) $(DEV_CFLAGS)
# Compile all source files, but do not link. As a side effect, compile a dependency file for each source file.
#
# Dependency files are a common makefile feature used to speed up builds by auto-generating granular makefile targets.
# These files minimize the number of targets that need to be recomputed when source files are modified and can lead to
# massive build-time improvements.
#
# For more information, see the "-M" option documentation in the GCC man page, as well as this paper:
# https://web.archive.org/web/20150319074420/http://aegis.sourceforge.net/auug97.pdf
#
$(addprefix $(DEP_DIR),%.d) : $(addprefix $(SRC_DIR),%.c)
@mkdir -p $(OBJ_DIR)
@mkdir -p $(DEP_DIR)
$(CC) -MD -MP -MF $@ -MT '$@ $(subst $(DEP_DIR),$(OBJ_DIR),$(@:.d=.o))' \
$< -c -o $(subst $(DEP_DIR),$(OBJ_DIR),$(@:.d=.o)) $(CSTD) $(LIB_CFLAGS) $(DEV_CFLAGS)
# Same as above, but for additional dependencies.
$(addprefix $(INC_DEP_DIR),%.d) : $(addprefix $(INC_SRC_DIR),%.c)
@mkdir -p $(INC_OBJ_DIR)
@mkdir -p $(INC_DEP_DIR)
$(CC) -MD -MP -MF $@ -MT '$@ $(subst $(INC_DEP_DIR),$(INC_OBJ_DIR),$(@:.d=.o))' \
$< -c -o $(subst $(INC_DEP_DIR),$(INC_OBJ_DIR),$(@:.d=.o)) $(CSTD) $(LIB_CFLAGS) $(DEV_CFLAGS)
# Special pseudo target which always needs to be recomputed. Forces full rebuild of target every time when used as a
# component.
FRC :