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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
TARGET := vfetch
MAIN_SRC := main.c
LIB_SRC := lib/miniaudio.c lib/pl_mpeg.c
APP_SRCS := $(wildcard src/*.c)
OBJS := $(MAIN_SRC:.c=.o) $(LIB_SRC:.c=.o) $(APP_SRCS:.c=.o)
DEPS := $(OBJS:.o=.d)
PROJECT_SRCS := $(MAIN_SRC) $(APP_SRCS) $(wildcard src/*.h)
PKGS := chafa glib-2.0
INSTALL_PATH := /usr/local/bin
CC := $(firstword $(shell command -v clang gcc))
ifeq ($(CC),)
$(error No compatible C compiler found.)
endif
PKG_CONFIG := $(shell command -v pkg-config)
ifeq ($(PKG_CONFIG),)
$(error 'pkg-config' not found.)
endif
PKG_CFLAGS := $(shell pkg-config --cflags $(PKGS) | sed 's/-I/-isystem /g')
PKG_LDFLAGS := $(shell pkg-config --libs $(PKGS))
DEPFLAGS := -MMD -MP
CORE_CFLAGS := \
-D_XOPEN_SOURCE=700 \
-std=c11 \
-isystem $(CURDIR)/lib \
$(PKG_CFLAGS)
STRICT_CFLAGS := \
-pedantic-errors \
-Wall \
-Wextra \
-Wpedantic \
-Werror \
-Wformat=2 \
-Wformat-security \
-Wformat-nonliteral \
-Wformat-signedness \
-Wstack-protector \
-Wvla \
-Wodr \
-Wstrict-prototypes \
-Wmissing-prototypes \
-Wmissing-declarations \
-Wmissing-include-dirs \
-Wmissing-field-initializers \
-Wredundant-decls \
-Wpointer-arith \
-Wpointer-sign \
-Wabsolute-value \
-Wconversion \
-Wsign-conversion \
-Wundef \
-Winline \
-Wcast-qual \
-Wfloat-equal \
-Wold-style-definition \
-Woverlength-strings \
-Wwrite-strings \
-Wlogical-not-parentheses \
-Wpacked \
-Winit-self \
-Wstrict-overflow=5 \
-Wunused-macros \
-Wunreachable-code \
-Wdate-time \
-Wbad-function-cast \
-Wnested-externs \
-Wswitch-default \
-Wswitch-enum \
-Wuninitialized \
-Wdouble-promotion \
-Wcast-function-type \
-Wnull-dereference \
-Wdisabled-optimization \
-Wno-padded
CLANG_SPECIFIC_STRICT_FLAGS := \
-Wcomma \
-Wshadow \
-Wcast-align \
-Wdocumentation \
-Wstrict-aliasing \
-Wpadded-bitfield \
-Wformat-overflow \
-Wformat-truncation \
-Wimplicit-fallthrough
GCC_SPECIFIC_STRICT_FLAGS := \
-Walloc-zero \
-Wsuggest-attribute=const \
-Wsuggest-attribute=format \
-Wshadow=local \
-Wcast-align=strict \
-Wimplicit-fallthrough=5 \
-Wstrict-aliasing=2 \
-Wformat-overflow=2 \
-Wformat-truncation=2 \
-Wjump-misses-init \
-Wlogical-op \
-Wuseless-cast \
-Wduplicated-cond \
-Wduplicated-branches \
-Wswitch-unreachable \
-Wpacked-bitfield-compat \
-Wunsafe-loop-optimizations \
-Wtrampolines
ALL_STRICT_FLAGS := $(STRICT_CFLAGS) $(CLANG_SPECIFIC_STRICT_FLAGS) $(GCC_SPECIFIC_STRICT_FLAGS)
RELEASE_CFLAGS := \
-O3 \
-flto=auto \
-fno-semantic-interposition \
-fstack-protector-strong \
-fstack-clash-protection \
-fPIE \
-D_FORTIFY_SOURCE=2 \
-ffunction-sections \
-fdata-sections \
-fcf-protection \
-march=native
DEBUG_CFLAGS_COMMON := -g -Og -fno-omit-frame-pointer -fno-common -flto=auto
ANALYZER_FLAGS_GCC := -fanalyzer
SANITIZER_ASAN_FLAGS := -fsanitize=address,undefined,leak
SANITIZER_TSAN_FLAGS := -fsanitize=thread
COMMON_LDFLAGS := -pthread $(PKG_LDFLAGS) -lm -latomic
RELEASE_LDFLAGS := -flto=auto -Wl,--gc-sections -Wl,--build-id=none -s -pie
.PHONY: all fallback debug debug-threaded check clean install uninstall tidy iwyu format format-check
.DELETE_ON_ERROR:
CFLAGS := $(CORE_CFLAGS)
LDFLAGS := $(COMMON_LDFLAGS)
all: CFLAGS += $(RELEASE_CFLAGS)
all: LDFLAGS += $(RELEASE_LDFLAGS)
all: $(TARGET)
fallback: $(TARGET)
debug: CC := $(CC)
debug: CFLAGS += $(STRICT_CFLAGS) $(DEBUG_CFLAGS_COMMON) $(SANITIZER_ASAN_FLAGS)
debug: CFLAGS += $(if $(findstring clang,$(notdir $(CC))),$(CLANG_SPECIFIC_STRICT_FLAGS))
debug: CFLAGS += $(if $(findstring gcc,$(notdir $(CC))),$(GCC_SPECIFIC_STRICT_FLAGS) $(ANALYZER_FLAGS_GCC))
debug: LDFLAGS += $(SANITIZER_ASAN_FLAGS)
debug: LDFLAGS += -flto=auto
debug: $(TARGET)
debug-threaded: CC := $(CC)
debug-threaded: CFLAGS += $(STRICT_CFLAGS) $(DEBUG_CFLAGS_COMMON) $(SANITIZER_TSAN_FLAGS)
debug-threaded: CFLAGS += $(if $(findstring clang,$(notdir $(CC))),$(CLANG_SPECIFIC_STRICT_FLAGS))
debug-threaded: CFLAGS += $(if $(findstring gcc,$(notdir $(CC))),$(GCC_SPECIFIC_STRICT_FLAGS) $(ANALYZER_FLAGS_GCC))
debug-threaded: LDFLAGS += $(SANITIZER_TSAN_FLAGS)
debug-threaded: LDFLAGS += -flto=auto
debug-threaded: $(TARGET)
-include $(DEPS)
$(MAIN_SRC:.c=.o): $(MAIN_SRC)
@echo " CC (Strict) $<"
$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@
src/%.o: src/%.c
@echo " CC (Strict) $<"
$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@
lib/%.o: lib/%.c
@echo " CC (Lax) $<"
$(CC) $(DEPFLAGS) $(filter-out $(ALL_STRICT_FLAGS),$(CFLAGS)) -c $< -o $@
IWYU := $(shell command -v include-what-you-use 2>/dev/null)
$(TARGET): $(OBJS)
@echo " CCLD $@"
$(CC) $(OBJS) -o $@ $(LDFLAGS)
@echo "Build complete. Executable '$(TARGET)' created."
check: debug tidy iwyu format-check
clean:
@echo "Cleaning up..."
rm -f $(TARGET) $(OBJS) $(DEPS)
@echo "Clean complete."
install: all
@echo "Installing $(TARGET) to $(INSTALL_PATH)..."
install -Dm755 "$(TARGET)" "$(INSTALL_PATH)/$(TARGET)"
@echo "Installation complete."
uninstall:
@echo "Uninstalling $(TARGET)..."
rm -f "$(INSTALL_PATH)/$(TARGET)"
$(MAKE) clean
@echo "Uninstall complete."
tidy:
@echo " TIDY Running clang-tidy..."
clang-tidy $(MAIN_SRC) $(APP_SRCS) -- $(CORE_CFLAGS) $(STRICT_CFLAGS) $(CLANG_SPECIFIC_STRICT_FLAGS)
iwyu:
@if [ -z "$(IWYU)" ]; then \
echo " SKIP include-what-you-use not found on this system."; \
else \
for file in $(MAIN_SRC) $(APP_SRCS); do \
echo " IWYU $$file"; \
$(IWYU) -Xiwyu --error \
-Xiwyu --check_also="src/*.h" \
$(CORE_CFLAGS) $(STRICT_CFLAGS) $$file || exit 1; \
done; \
fi
format:
@echo " FORMAT Applying source code formatting..."
clang-format -i $(PROJECT_SRCS)
format-check:
@echo " FORMAT Checking source code formatting..."
clang-format --dry-run -Werror $(PROJECT_SRCS)
|