-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
114 lines (85 loc) · 3.22 KB
/
Copy pathMakefile
File metadata and controls
114 lines (85 loc) · 3.22 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
# Do not use make's built-in implicit rules (-r) or built-in variables (-R).
# Disabling these built-ins avoids behavior that is difficult to debug and
# improves build performance.
MAKEFLAGS += -rR --warn-undefined-variables
SHELL := /usr/bin/env bash
.SHELLFLAGS := -eu -o pipefail -c
# If a rule's recipe exits with a non-zero status, automatically delete the
# target file.
.DELETE_ON_ERROR:
# The default toolchain is GCC; however, Clang is also supported and tested.
CC := gcc
CPPFLAGS := -D_POSIX_C_SOURCE=200809L -I./include
CFLAGS := -std=c17 -pipe -pthread
RELEASE_CFLAGS := -march=native -O2 -flto=auto -DNDEBUG
DEBUG_CFLAGS := -Og -g3 -fno-omit-frame-pointer -DDEBUG
ASAN_CFLAGS := -fsanitize=address,leak,undefined -fno-sanitize-recover=all
LDFLAGS := -Wl,--sort-common,--as-needed
RELEASE_LDFLAGS := -Wl,-O1
DEBUG_LDFLAGS := -Wl,--fatal-warnings
LDLIBS :=
WARNS := -Wall -Wextra -Wpedantic -pedantic-errors -Wformat=2 \
-Wshadow -Wstrict-prototypes -Wstrict-overflow=2 \
-Wredundant-decls -Wnested-externs -Wcast-qual \
-Wfloat-equal -Wdouble-promotion -Wpointer-arith \
-Wundef -Winit-self -Wwrite-strings -Wconversion \
-Wmissing-prototypes -Wmissing-declarations -Wvla \
-Walloca -Wnull-dereference -Wcast-align
DEBUG_WARNS := -Werror
# Additional warnings that may not be supported by all compilers, but are
# supported by GCC.
GCC_WARNS := -Wjump-misses-init -Wlogical-op -Walloc-zero \
-Wduplicated-branches -Wduplicated-cond
# Detect the compiler from its `$(CC) --version` output rather than parsing
# $(CC), which may be a symlink, alias, or cross-compiler prefix.
CC_VERSION_TEXT := $(shell LC_ALL=C $(CC) --version 2>/dev/null | head -n 1)
ifneq ($(findstring gcc,$(CC_VERSION_TEXT)),)
WARNS += $(GCC_WARNS)
endif
# Additional CFLAGS and warnings defined only for the language server, clangd,
# unrelated to the final build of the project.
LSP_CFLAGS := $(CFLAGS) $(RELEASE_CFLAGS) $(WARNS) \
-Wreserved-identifier -Wno-unused-function \
-Wno-empty-translation-unit
LSP_CFLAGS_FILE := compile_flags.txt
DEPFLAGS := -MMD -MP
SRC_DIR := ./src
OBJ_DIR := ./obj
BUILD_DIR := ./build
SOURCES := $(wildcard $(SRC_DIR)/*.c)
OBJECTS := $(SOURCES:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
DEPS := $(OBJECTS:.o=.d)
TARGET_NAME := cobalt
TARGET := $(BUILD_DIR)/$(TARGET_NAME)
.DEFAULT_GOAL := all
.PHONY: all
all: release
.PHONY: clean
clean:
@rm -rf -- $(OBJ_DIR) $(BUILD_DIR)
@rm -f -- $(LSP_CFLAGS_FILE)
.PHONY: lsp-cflags-file
lsp-cflags-file: $(LSP_CFLAGS_FILE)
.PHONY: debug
debug: CFLAGS += $(DEBUG_CFLAGS)
debug: LDFLAGS += $(DEBUG_LDFLAGS)
debug: WARNS += $(DEBUG_WARNS)
debug: $(TARGET)
.PHONY: asan
asan: CFLAGS += $(DEBUG_CFLAGS) $(ASAN_CFLAGS)
asan: LDFLAGS += $(DEBUG_LDFLAGS)
asan: WARNS += $(DEBUG_WARNS)
asan: $(TARGET)
.PHONY: release
release: CFLAGS += $(RELEASE_CFLAGS)
release: LDFLAGS += $(RELEASE_LDFLAGS)
release: $(TARGET)
$(TARGET): $(OBJECTS) | $(BUILD_DIR)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) -o $@ $(LDLIBS)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR) $(LSP_CFLAGS_FILE)
$(CC) $(CPPFLAGS) $(CFLAGS) $(WARNS) $(DEPFLAGS) -c $< -o $@
$(OBJ_DIR) $(BUILD_DIR):
@mkdir -p $@
$(LSP_CFLAGS_FILE): Makefile
@printf "%s\n" $(LSP_CFLAGS) $(CPPFLAGS) > $@
-include $(DEPS)