-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMakefile
More file actions
315 lines (254 loc) · 8.67 KB
/
Makefile
File metadata and controls
315 lines (254 loc) · 8.67 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
SHELL := /bin/bash
.PHONY: all build stage1 stage2 stage3 runtime selfcheck smoke test test-pcre2 \
fixpoint install install-user update-seed clean seed deps print-version \
emit-c-test emit-c-fixpoint emit-c-roundtrip cross \
pcre2-migrate pcre2-build pcre2-test pcre2-promote \
regex-migrate regex-build regex-test regex-promote
ROOT_DIR := $(CURDIR)
REPO_FULL_NAME ?= QuixiAI/with
WITH_BUILD_ENV := WITH_OUT_DIR="$(ROOT_DIR)/out"
OUT ?= out
OUT_BIN_DIR := $(OUT)/bin
OUT_RELEASE_BIN_DIR := $(OUT)/release/bin
OUT_TMP_DIR := $(OUT)/tmp
OUT_GEN_DIR := $(OUT)/gen
CANONICAL_BIN := $(OUT_RELEASE_BIN_DIR)/with
SEED_PATH := src/main
SEED_VERSION ?=
SEED_ASSET ?= auto
# Seed compiler: WITH env var, out/release/bin/with, `with` on PATH, or src/main.
WITH ?= $(shell \
if [ -x "$(CANONICAL_BIN)" ]; then \
printf '%s\n' "$(CANONICAL_BIN)"; \
elif command -v with >/dev/null 2>&1; then \
command -v with; \
elif [ -x "$(SEED_PATH)" ]; then \
printf '%s\n' "$(SEED_PATH)"; \
fi)
STAGE0_BIN := $(WITH)
VERSION_SOURCE_FILE := src/version
define RESOLVE_VERSION_SH
set -euo pipefail; \
base="$$(sed -n '1{s/[[:space:]]*$$//;p;}' "$(VERSION_SOURCE_FILE)")"; \
if [ -z "$$base" ]; then \
echo "error: empty or missing version in $(VERSION_SOURCE_FILE)" >&2; \
exit 1; \
fi; \
if [ -n "$${WITH_VERSION:-}" ]; then \
printf '%s\n' "$${WITH_VERSION}"; \
exit 0; \
fi; \
if git -C "$(ROOT_DIR)" rev-parse --is-inside-work-tree >/dev/null 2>&1; then \
short_hash="$$(git -C "$(ROOT_DIR)" rev-parse --short=9 HEAD 2>/dev/null || true)"; \
commit_count="$$(git -C "$(ROOT_DIR)" rev-list --count HEAD 2>/dev/null || true)"; \
if [ -n "$$short_hash" ] && [ -n "$$commit_count" ]; then \
printf '%s-%s-g%s\n' "$$base" "$$commit_count" "$$short_hash"; \
exit 0; \
fi; \
fi; \
printf '%s\n' "$$base"
endef
REPO_SERIAL_LOCK := $(OUT_TMP_DIR)/repo-serial.lock
define WITH_REPO_LOCK
@set -euo pipefail; \
lock="$(REPO_SERIAL_LOCK)"; \
owner_file="$$lock/owner"; \
acquired=0; \
if mkdir "$$lock" 2>/dev/null; then \
acquired=1; \
elif [ -f "$$owner_file" ]; then \
owner="$$(cat "$$owner_file")"; \
owner_pid="$$(printf '%s\n' "$$owner" | sed -n 's/.*pid=\([0-9][0-9]*\).*/\1/p')"; \
if [ -n "$$owner_pid" ] && ! kill -0 "$$owner_pid" 2>/dev/null; then \
rm -rf "$$lock"; \
if mkdir "$$lock" 2>/dev/null; then \
acquired=1; \
fi; \
fi; \
fi; \
if [ "$$acquired" -eq 1 ]; then \
trap 'rm -rf "$$lock"' EXIT INT TERM HUP; \
printf 'target=%s pid=%s started=%s\n' "$@" "$$$$" "$$(date -u +%Y-%m-%dT%H:%M:%SZ)" > "$$owner_file"; \
export WITH_REPO_LOCKED=1; $(1); \
else \
if [ -f "$$owner_file" ]; then \
owner="$$(cat "$$owner_file")"; \
else \
owner="target=<unknown> pid=<unknown> started=<unknown>"; \
fi; \
echo "error: another top-level target is already running: $$owner" >&2; \
exit 1; \
fi
endef
define RUN_GRAPH_TARGET
@if [ -z "$(STAGE0_BIN)" ]; then echo "error: no seed compiler — set WITH, add with to PATH, or run: make seed" >&2; exit 1; fi
$(WITH_BUILD_ENV) $(STAGE0_BIN) build :$(1)
endef
$(OUT_TMP_DIR):
@mkdir -p "$@"
# --- Public targets (all delegate to with build via the graph) ---
all: build
build: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __build)
stage1: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __stage1)
stage2: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __stage2)
stage3: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __stage3)
runtime: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __runtime)
selfcheck: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __selfcheck)
smoke: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __smoke)
test: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __test)
test-pcre2: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __test-pcre2)
fixpoint: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __fixpoint)
install: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __install)
install-user: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __install-user)
update-seed: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __update-seed)
clean: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __clean)
seed: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __seed)
deps: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __deps)
pcre2-migrate: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __regex-migrate)
pcre2-build: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __regex-build)
pcre2-test: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __regex-test)
pcre2-promote: | $(OUT_TMP_DIR)
$(call WITH_REPO_LOCK,$(MAKE) --no-print-directory __regex-promote)
regex-migrate: pcre2-migrate
regex-build: pcre2-build
regex-test: pcre2-test
regex-promote: pcre2-promote
print-version:
@$(RESOLVE_VERSION_SH)
emit-c-test:
$(call RUN_GRAPH_TARGET,emit-c-test)
emit-c-fixpoint:
$(call RUN_GRAPH_TARGET,emit-c-fixpoint)
emit-c-roundtrip:
$(call RUN_GRAPH_TARGET,emit-c-roundtrip)
# --- Internal targets ---
__build:
$(call RUN_GRAPH_TARGET,build)
__stage1:
$(call RUN_GRAPH_TARGET,stage1)
__stage2:
$(call RUN_GRAPH_TARGET,stage2)
__stage3:
$(call RUN_GRAPH_TARGET,stage3)
__runtime:
$(call RUN_GRAPH_TARGET,runtime)
__selfcheck:
$(call RUN_GRAPH_TARGET,selfcheck)
__smoke:
$(call RUN_GRAPH_TARGET,selfcheck)
__test:
$(call RUN_GRAPH_TARGET,test)
__test-pcre2: __regex-test
__fixpoint:
$(call RUN_GRAPH_TARGET,fixpoint)
__install:
$(call RUN_GRAPH_TARGET,install)
__install-user:
$(call RUN_GRAPH_TARGET,install-user)
__update-seed:
$(call RUN_GRAPH_TARGET,update-seed)
__deps:
$(call RUN_GRAPH_TARGET,deps)
__clean:
@$(WITH_BUILD_ENV) "$(WITH)" build :clean
__regex-migrate:
$(call RUN_GRAPH_TARGET,pcre2-migrate)
__regex-build:
$(call RUN_GRAPH_TARGET,pcre2-build)
__regex-test:
$(call RUN_GRAPH_TARGET,pcre2-test)
__regex-promote:
$(call RUN_GRAPH_TARGET,pcre2-promote)
__seed:
@set -euo pipefail; \
if [ -n "$(STAGE0_BIN)" ]; then \
$(WITH_BUILD_ENV) $(STAGE0_BIN) build :seed; \
exit $$?; \
fi; \
dest="$(SEED_PATH)"; \
repo="$(REPO_FULL_NAME)"; \
asset="$(SEED_ASSET)"; \
if [ "$$asset" = "auto" ]; then \
case "$$(uname -s):$$(uname -m)" in \
Darwin:arm64|Darwin:aarch64) asset="with-darwin-aarch64" ;; \
Linux:x86_64) asset="with-linux-x86_64" ;; \
*) asset="" ;; \
esac; \
fi; \
if [ -z "$$asset" ]; then \
echo "error: unsupported seed host: $$(uname -s)/$$(uname -m)" >&2; \
echo "set SEED_ASSET to a published release asset name" >&2; \
exit 1; \
fi; \
if [ -x "$$dest" ]; then \
echo "seed binary already exists: $$dest"; \
echo "remove it first if you want to re-download"; \
exit 0; \
fi; \
tag="$(SEED_VERSION)"; \
if [ -z "$$tag" ]; then \
tag="$$( \
gh release list --repo "$$repo" --limit 10 --json tagName,isDraft -q '.[] | select(.isDraft | not) | .tagName' 2>/dev/null | \
while IFS= read -r candidate; do \
if [ -z "$$candidate" ]; then continue; fi; \
if gh release view "$$candidate" --repo "$$repo" --json assets -q '.assets[].name' 2>/dev/null | grep -qx "$$asset"; then \
printf '%s\n' "$$candidate"; \
break; \
fi; \
done \
|| true \
)"; \
if [ -z "$$tag" ]; then \
echo "error: could not find a release with seed binary" >&2; \
echo "install gh CLI and authenticate, or specify one:" >&2; \
echo " make seed SEED_VERSION=v0.5.2-uaf" >&2; \
exit 1; \
fi; \
echo "latest seed release: $$tag"; \
fi; \
url="https://github.com/$$repo/releases/download/$$tag/$$asset"; \
echo "downloading seed from: $$url"; \
curl -fSL -o "$$dest" "$$url"; \
chmod +x "$$dest"; \
echo "seed installed: $$dest"
# Cross-compile via emit-c + zig cc. Requires a working build.
CROSS_TARGET ?=
cross: build
@if [ -z "$(CROSS_TARGET)" ]; then \
echo "usage: make cross CROSS_TARGET=aarch64-linux"; \
exit 1; \
fi
@echo "=== cross-compile: $(CROSS_TARGET) ==="
mkdir -p out/cross/$(CROSS_TARGET)
$(WITH_BUILD_ENV) ./out/release/bin/with build out/gen/main.w --emit-c -o out/cross/$(CROSS_TARGET)/with.c
@bash "$(ROOT_DIR)/scripts/generate_wl_stubs.sh" runtime/llvm_bridge.c out/cross/$(CROSS_TARGET)/with.c $(OUT_GEN_DIR)
cd out/cross/$(CROSS_TARGET) && zig cc \
-target $(CROSS_TARGET) \
-o with \
with.c \
../../../$(OUT_GEN_DIR)/wl_stubs.c \
../../../runtime/with_runtime.c \
-I../../../runtime \
-include ../../../$(OUT_GEN_DIR)/wl_decls.h \
-lc
@echo "=== built: out/cross/$(CROSS_TARGET)/with ==="
@file out/cross/$(CROSS_TARGET)/with