-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
93 lines (74 loc) · 3.75 KB
/
Makefile
File metadata and controls
93 lines (74 loc) · 3.75 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
# Cosmopolitan build for King Discord bot
# Produces an Actually Portable Executable (APE)
#
# Usage:
# make — builds the 'king' APE binary (auto-downloads cosmocc)
# make test — builds and runs tests
# make lint — runs clang-format and clang-tidy checks
# make clean — removes build artifacts
# make distclean — also removes downloaded cosmocc toolchain
# ── cosmocc toolchain (auto-downloaded) ──────────────────────────
COSMOCC_VERSION = 3.9.2
COSMOCC_SHA256 = f4ff13af65fcd309f3f1cfd04275996fb7f72a4897726628a8c9cf732e850193
COSMOCC_DIR = .cosmocc/$(COSMOCC_VERSION)
COSMOCC_BIN = $(COSMOCC_DIR)/bin
# Download cosmocc if not already present (skip for lint/clean targets)
ifneq ($(filter-out lint clean distclean mbedclean,$(MAKECMDGOALS)),)
DOWNLOAD := $(shell scripts/download-cosmocc.sh $(COSMOCC_DIR) $(COSMOCC_VERSION) $(COSMOCC_SHA256))
endif
ifeq ($(MAKECMDGOALS),)
DOWNLOAD := $(shell scripts/download-cosmocc.sh $(COSMOCC_DIR) $(COSMOCC_VERSION) $(COSMOCC_SHA256))
endif
CC = $(CURDIR)/$(COSMOCC_BIN)/cosmocc
AR = $(CURDIR)/$(COSMOCC_BIN)/cosmoar
CFLAGS = -Os -Wall -Wextra -Wno-unused-parameter
LDFLAGS =
# ── mbedtls (git submodule at vendor/mbedtls) ───────────────────
MBEDTLS_SRC = vendor/mbedtls
MBEDTLS_INC = $(MBEDTLS_SRC)/include
MBEDTLS_LIB = $(MBEDTLS_SRC)/library
MBEDTLS_LIBS = $(MBEDTLS_LIB)/libmbedtls.a \
$(MBEDTLS_LIB)/libmbedx509.a \
$(MBEDTLS_LIB)/libmbedcrypto.a
# ── bot sources ──────────────────────────────────────────────────
BIN = king
SRCS = src/king.c src/net.c src/ws.c src/json.c src/str.c
OBJS = $(SRCS:.c=.o)
# ── targets ──────────────────────────────────────────────────────
all: $(BIN)
$(BIN): $(OBJS) $(MBEDTLS_LIBS)
$(CC) $(LDFLAGS) -o $@ $(OBJS) -L$(MBEDTLS_LIB) -lmbedtls -lmbedx509 -lmbedcrypto
src/%.o: src/%.c $(MBEDTLS_LIBS)
$(CC) $(CFLAGS) -I$(MBEDTLS_INC) -Isrc -c $< -o $@
# Build mbedtls from the vendored submodule.
# WARNING_CFLAGS= suppresses warnings from mbedtls source that cosmocc flags.
# APPLE_BUILD=0 prevents ranlib issues under cosmocc.
$(MBEDTLS_LIBS):
@if [ ! -f $(MBEDTLS_SRC)/include/mbedtls/ssl.h ]; then \
echo "error: vendor/mbedtls not found. Run: git submodule update --init" >&2; \
exit 1; \
fi
$(MAKE) -C $(MBEDTLS_LIB) CC=$(CC) AR=$(AR) \
CFLAGS="-Os -I../include" WARNING_CFLAGS= APPLE_BUILD=0
# ── tests ────────────────────────────────────────────────────────
TEST_BIN = test_king
TEST_SRCS = src/test_king.c src/json.c src/str.c
TEST_OBJS = $(TEST_SRCS:.c=.o)
test: $(TEST_BIN)
./$(TEST_BIN)
$(TEST_BIN): $(TEST_OBJS)
$(CC) $(LDFLAGS) -o $@ $(TEST_OBJS)
# ── lint ─────────────────────────────────────────────────────────
LINT_SRCS = src/king.c src/net.c src/ws.c src/json.c src/str.c src/test_king.c
LINT_HDRS = src/json.h src/str.h src/net.h src/ws.h src/log.h src/test.h src/cacerts.h
lint:
clang-format --dry-run --Werror $(LINT_SRCS) $(LINT_HDRS)
clang-tidy $(LINT_SRCS) -- -Isrc -I$(MBEDTLS_INC) -Wno-implicit-function-declaration
clean:
rm -f $(BIN) $(TEST_BIN) $(OBJS) src/test_king.o *.com.dbg *.aarch64.elf
rm -rf .aarch64
mbedclean:
$(MAKE) -C $(MBEDTLS_LIB) clean 2>/dev/null || true
distclean: clean mbedclean
rm -rf .cosmocc
.PHONY: all test lint clean mbedclean distclean