Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
142 lines (114 loc) · 5.68 KB
/
Copy pathMakefile
File metadata and controls
142 lines (114 loc) · 5.68 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
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
# Cosmopolitan build for King Discord bot
# Produces an Actually Portable Executable (APE)
#
# Usage (Docker-first — preferred on macOS; only Docker needed on the host):
# make docker-test — build + run unit tests in Docker
# make docker-lint — clang-format + clang-tidy in Docker (LLVM 21)
# make docker-king — write ./king APE binary via Docker (no host cosmocc)
# make docker-image — build runtime scratch image tagged king:local
# make docker-run — run king:local with .env / env vars
#
# Native (downloads cosmocc onto the host):
# make — builds the 'king' APE binary
# 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
# ── Docker-first wrappers (no host cosmocc / clang) ──────────────
DOCKER ?= docker
DOCKER_IMAGE ?= king
export DOCKER_BUILDKIT ?= 1
# Skip cosmocc download for Docker / lint / clean goals
DOCKER_GOALS = docker-test docker-lint docker-king docker-image docker-run docker-builder
SKIP_COSMOCC_GOALS = lint clean distclean mbedclean $(DOCKER_GOALS)
# ── cosmocc toolchain (auto-downloaded for native builds) ────────
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/docker targets)
ifneq ($(filter-out $(SKIP_COSMOCC_GOALS),$(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
# ── Docker targets ───────────────────────────────────────────────
# All compile/test/lint work happens inside the image. The host only
# needs Docker; cosmocc and LLVM are not installed locally.
docker-builder:
$(DOCKER) build --target builder -t $(DOCKER_IMAGE):builder .
docker-test:
$(DOCKER) build --target test -t $(DOCKER_IMAGE):test .
docker-lint:
$(DOCKER) build --target lint -t $(DOCKER_IMAGE):lint .
# Extract the portable APE (pre-assimilate) to ./king on the host.
docker-king: docker-builder
@cid=$$($(DOCKER) create $(DOCKER_IMAGE):builder); \
$(DOCKER) cp $$cid:/artifacts/king ./king; \
$(DOCKER) rm $$cid >/dev/null; \
echo "wrote ./king"
docker-image:
$(DOCKER) build --target runtime -t $(DOCKER_IMAGE):local .
docker-run: docker-image
@if [ -f .env ]; then \
$(DOCKER) run --rm --env-file .env $(DOCKER_IMAGE):local; \
else \
$(DOCKER) run --rm \
-e DISCORD_TOKEN -e DISCORD_REPLY -e DISCORD_REACTION -e LOG_LEVEL \
$(DOCKER_IMAGE):local; \
fi
.PHONY: all test lint clean mbedclean distclean \
docker-builder docker-test docker-lint docker-king docker-image docker-run