Skip to content

Commit e8b12ed

Browse files
committed
chore(extend): add makefile and clang format config
1 parent c7153f7 commit e8b12ed

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

extend/.clang-format

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
BasedOnStyle: WebKit
2+
AlwaysBreakAfterReturnType: TopLevelDefinitions
3+
UseTab: Never
4+
IndentWidth: 4
5+
SortIncludes: true
6+
AlignConsecutiveDeclarations: true
7+
AlignConsecutiveAssignments: true
8+
AlignConsecutiveMacros: true
9+
ColumnLimit: 120
10+
AllowShortFunctionsOnASingleLine: Empty
11+
AllowShortIfStatementsOnASingleLine: false
12+
AlignTrailingComments: true

extend/Makefile

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Makefile
2+
CC := clang
3+
CFLAGS := -Wall -Wextra -Werror -pedantic -std=c23 \
4+
-Wshadow -Wconversion -Wnull-dereference -Wformat=2 -Wundef
5+
LDFLAGS :=
6+
DEBUG_CFLAGS := -g -fsanitize=address,undefined,leak -O0
7+
DEBUG_LDFLAGS := -fsanitize=address,undefined,leak
8+
RELEASE_CFLAGS := -O3
9+
RELEASE_LDFLAGS := -flto
10+
11+
TARGET := extend
12+
LIBS :=
13+
INCLUDE_DIR := include
14+
SOURCE_DIR := src
15+
BUILD_DIR := build
16+
TEST_DIR := tests
17+
18+
# do not modify
19+
INCLUDES := -I$(INCLUDE_DIR)
20+
SOURCES := $(shell find $(SOURCE_DIR) -type f -name '*.c')
21+
OBJS := $(patsubst $(SOURCE_DIR)/%.c, $(BUILD_DIR)/%.o, $(SOURCES))
22+
DEPS := $(OBJS:.o=.d)
23+
-include $(DEPS)
24+
25+
.PHONY: all
26+
all: release
27+
28+
.PHONY: release
29+
release: CFLAGS += $(RELEASE_CFLAGS)
30+
release: LDFLAGS += $(RELEASE_LDFLAGS)
31+
release: $(TARGET)
32+
33+
.PHONY: debug
34+
debug: CFLAGS += $(DEBUG_CFLAGS)
35+
debug: LDFLAGS += $(DEBUG_LDFLAGS)
36+
debug: $(TARGET)
37+
38+
# for static libs use: ar rcs $@ $^ $(LIBS)
39+
$(TARGET): $(OBJS)
40+
$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
41+
42+
$(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.c | $(BUILD_DIR)
43+
$(CC) -c $< -o $@ $(CFLAGS) $(INCLUDES) -MMD -MP
44+
45+
$(BUILD_DIR):
46+
mkdir -p $(BUILD_DIR)
47+
48+
clean:
49+
rm -rf $(BUILD_DIR) $(TARGET)
50+
51+
setup:
52+
@mkdir -p $(SOURCE_DIR) $(INCLUDE_DIR) $(TEST_DIR)
53+
54+
dev:
55+
bear -- $(MAKE) debug
56+
57+
format:
58+
@echo "Formatting code..."
59+
@fd -ec -eh . -x clang-format -i {}
60+
61+
watch-%:
62+
@echo "Watching for changes..."
63+
@fd -ec -eh . | entr -cd $(MAKE) -s $*
64+
65+
.SECONDEXPANSION:
66+
test-%: CFLAGS += $(DEBUG_CFLAGS)
67+
test-%: LDFLAGS += $(DEBUG_LDFLAGS)
68+
test-%: $(TEST_DIR)/%.c $(SOURCE_DIR)/%.c $$(TEST_DEPS_$$*) | $(BUILD_DIR)
69+
$(CC) -o $(BUILD_DIR)/test_$* $^ $(CFLAGS) $(INCLUDES) $(LDFLAGS)
70+
@./$(BUILD_DIR)/test_$*
71+
72+
.PHONY: clean setup dev format test-% watch-%

0 commit comments

Comments
 (0)