Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
333 lines (275 loc) · 16.9 KB
/
Copy pathMakefile
File metadata and controls
333 lines (275 loc) · 16.9 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# Model Gateway Makefile
# Provides convenient shortcuts for common development tasks
# Python bindings directory
PYTHON_DIR := bindings/python
# OpenAPI Generator CLI wrapper version (pinned for reproducibility)
OPENAPI_GENERATOR_CLI_VERSION := 2.30.0
# Auto-detect CPU cores and cap at reasonable limit to avoid thread exhaustion
# Can be overridden: make python-dev JOBS=4
NPROC := $(shell nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 8)
JOBS ?= $(shell echo $$(($(NPROC) > 16 ? 16 : $(NPROC))))
# Check if sccache is available and set RUSTC_WRAPPER accordingly. The note is printed for the goals that compile (the
# ones that build with cargo or maturin): the help, version and release targets print output that is read as such, and
# the note would come first in it; a goal that cleans, formats or sets up a tool has no compilation for it to precede.
SCCACHE := $(shell which sccache 2>/dev/null)
ifdef SCCACHE
export RUSTC_WRAPPER := $(SCCACHE)
endif
COMPILING_GOALS := $(filter build test docs check dev-setup pre-commit python-dev python-build python-build-release \
python-install python-check dev install generate-%,$(MAKECMDGOALS))
ifneq ($(strip $(COMPILING_GOALS)),)
ifdef SCCACHE
$(info Using sccache for compilation caching)
else
$(info sccache not found. Install it for faster builds: cargo install sccache)
endif
endif
.PHONY: help build test clean docs check fmt opencv-deps dev-setup pre-commit setup-sccache sccache-stats sccache-clean sccache-stop \
perf-build perf-base perf-bed perf-cpu perf-alloc perf-mem perf-live perf-report \
python-dev python-build python-build-release python-install python-clean python-test python-check \
generate-openapi generate-python-types generate-java-types generate-clients \
show-version bump-version check-versions \
release-plan release-cut release-bump release-pick release-pick-pr release-status release-tag release-notes release-bump-main release-publish-plan release-check release-ship
help: ## Show this help message
@echo "Model Gateway Development Commands"
@echo "=================================="
@echo ""
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-20s\033[0m %s\n", $$1, $$2}'
@echo ""
build: ## Build the project in release mode
@echo "Building Shepherd Model Gateway..."
@cargo build --release
test: ## Run all tests
@echo "Running tests..."
@cargo test
clean: ## Clean build artifacts
@echo "Cleaning build artifacts..."
@cargo clean
docs: ## Generate and open documentation
@echo "Generating documentation..."
@cargo doc --open
check: ## Run cargo check and clippy
@echo "Running cargo check..."
@cargo check
@echo "Running clippy..."
@cargo clippy --all-targets --all-features -- -D warnings
fmt: ## Format code with rustfmt
@echo "Formatting code..."
@rustup run nightly cargo fmt
opencv-deps: ## Install system OpenCV for the opencv-video feature (needed by check/--all-features)
@bash scripts/install_opencv.sh
# Development workflow shortcuts
dev-setup: build test ## Set up development environment
@echo "Development environment ready!"
pre-commit: fmt check test ## Run pre-commit checks
@echo "Pre-commit checks passed!"
# sccache management targets
setup-sccache: ## Install and configure sccache
@echo "Setting up sccache..."
@./scripts/setup-sccache.sh
sccache-stats: ## Show sccache statistics
@if [ -n "$(SCCACHE)" ]; then \
echo "sccache statistics:"; \
sccache -s; \
else \
echo "sccache not installed. Run 'make setup-sccache' to install it."; \
fi
sccache-clean: ## Clear sccache cache
@if [ -n "$(SCCACHE)" ]; then \
echo "Clearing sccache cache..."; \
sccache -C; \
echo "sccache cache cleared"; \
else \
echo "sccache not installed"; \
fi
sccache-stop: ## Stop the sccache server
@if [ -n "$(SCCACHE)" ]; then \
echo "Stopping sccache server..."; \
sccache --stop-server || true; \
else \
echo "sccache not installed"; \
fi
# Python bindings (maturin) targets
python-dev: ## Build Python bindings in development mode (fast, debug build)
@echo "Building Python bindings in development mode (using $(JOBS) parallel jobs with sccache)..."
@cd $(PYTHON_DIR) && CARGO_BUILD_JOBS=$(JOBS) maturin develop
python-build: ## Build Python wheel (release mode with vendored OpenSSL)
@echo "Building Python wheel (release, vendored OpenSSL, using $(JOBS) parallel jobs with sccache)..."
@cd $(PYTHON_DIR) && CARGO_BUILD_JOBS=$(JOBS) maturin build --release --out dist --features vendored-openssl
python-build-release: python-build ## Alias for python-build
python-install: python-build ## Build and install Python wheel
@echo "Installing Python wheel..."
@pip install --force-reinstall $(PYTHON_DIR)/dist/*.whl
@echo "Python package installed!"
python-clean: ## Clean Python build artifacts
@echo "Cleaning Python build artifacts..."
@rm -rf $(PYTHON_DIR)/dist/
@rm -rf $(PYTHON_DIR)/target/
@rm -rf $(PYTHON_DIR)/smg.egg-info/
@rm -rf $(PYTHON_DIR)/smg/__pycache__/
@find $(PYTHON_DIR) -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
@find $(PYTHON_DIR) -name "*.pyc" -delete 2>/dev/null || true
@echo "Python build artifacts cleaned!"
python-test: ## Run Python tests
@echo "Running Python tests..."
@pytest e2e_test/ -v
python-check: ## Check Python package with twine
@echo "Checking Python package..."
@cd $(PYTHON_DIR) && CARGO_BUILD_JOBS=$(JOBS) maturin build --release --out dist --features vendored-openssl
@pip install twine 2>/dev/null || true
@twine check $(PYTHON_DIR)/dist/*
@echo "Python package check passed!"
# Client SDK code generation
generate-openapi: ## Generate OpenAPI spec from Rust protocol types
@echo "Generating OpenAPI spec..."
@mkdir -p clients/openapi
@cargo run -p openapi-gen -- clients/openapi/smg-openapi.yaml
generate-python-types: generate-openapi ## Generate Python types from OpenAPI spec
@echo "Generating Python types..."
@uvx --from 'datamodel-code-generator==0.54.0' datamodel-codegen \
--input clients/openapi/smg-openapi.yaml \
--input-file-type openapi \
--output clients/python/smg_client/types/_generated.py \
--output-model-type pydantic_v2.BaseModel \
--use-annotated \
--field-constraints \
--target-python-version 3.10 \
--collapse-root-models \
--use-standard-collections \
--use-union-operator
@echo "Post-processing: converting enums to str enums..."
@sed -i.bak 's/class \(.*\)(Enum):/class \1(str, Enum):/' clients/python/smg_client/types/_generated.py
@rm -f clients/python/smg_client/types/_generated.py.bak
generate-java-types: generate-openapi ## Generate Java types from OpenAPI spec
@echo "Generating Java types..."
@rm -rf clients/java/src
@npx --yes @openapitools/openapi-generator-cli@$(OPENAPI_GENERATOR_CLI_VERSION) generate \
-i clients/openapi/smg-openapi.yaml \
-g java \
-o clients/java \
--model-package com.lightseek.smg.types \
--api-package com.lightseek.smg.api \
--global-property models,supportingFiles,modelDocs=false,modelTests=false \
--additional-properties serializationLibrary=jackson,dateLibrary=java8,openApiNullable=false,useJakartaEe=true,hideGenerationTimestamp=true,library=native
@echo "Post-processing generated Java files..."
@./scripts/fix_java_codegen.sh clients/java/src
generate-clients: generate-python-types generate-java-types ## Generate all client SDK types
@echo "All client types generated!"
# Combined shortcuts
dev: python-dev ## Quick development setup (build Python bindings in dev mode)
install: python-install ## Build and install everything
# Release management
VERSION_FILES := model_gateway/Cargo.toml \
bindings/golang/Cargo.toml \
bindings/python/Cargo.toml \
bindings/python/pyproject.toml \
bindings/python/src/smg/version.py
show-version: ## Show current version across all files
@echo "Current versions:"
@echo " model_gateway/Cargo.toml: $$(grep -m1 '^version = ' model_gateway/Cargo.toml | sed 's/version = "\(.*\)"/\1/')"
@echo " bindings/golang/Cargo.toml: $$(grep -m1 '^version = ' bindings/golang/Cargo.toml | sed 's/version = "\(.*\)"/\1/')"
@echo " bindings/python/Cargo.toml: $$(grep -m1 '^version = ' bindings/python/Cargo.toml | sed 's/version = "\(.*\)"/\1/')"
@echo " bindings/python/pyproject.toml: $$(grep -m1 '^version = ' bindings/python/pyproject.toml | sed 's/version = "\(.*\)"/\1/')"
@echo " bindings/python/.../version.py: $$(grep '__version__' bindings/python/src/smg/version.py | sed 's/__version__ = "\(.*\)"/\1/')"
bump-version: ## Bump version across all files (usage: make bump-version VERSION=0.3.3)
@if [ -z "$(VERSION)" ]; then \
echo "Usage: make bump-version VERSION=<new-version>"; \
echo "Example: make bump-version VERSION=0.3.3"; \
echo ""; \
echo "Current version:"; \
grep -m1 '^version = ' model_gateway/Cargo.toml | sed 's/version = "\(.*\)"/ \1/'; \
exit 1; \
fi
@echo "Bumping version to $(VERSION)..."
@# Update model_gateway Cargo.toml
@sed -i.bak 's/^version = ".*"/version = "$(VERSION)"/' model_gateway/Cargo.toml && rm -f model_gateway/Cargo.toml.bak
@# Update golang binding Cargo.toml
@sed -i.bak 's/^version = ".*"/version = "$(VERSION)"/' bindings/golang/Cargo.toml && rm -f bindings/golang/Cargo.toml.bak
@# Update python binding Cargo.toml
@sed -i.bak 's/^version = ".*"/version = "$(VERSION)"/' bindings/python/Cargo.toml && rm -f bindings/python/Cargo.toml.bak
@# Update pyproject.toml
@sed -i.bak 's/^version = ".*"/version = "$(VERSION)"/' bindings/python/pyproject.toml && rm -f bindings/python/pyproject.toml.bak
@# Update version.py
@sed -i.bak 's/__version__ = ".*"/__version__ = "$(VERSION)"/' bindings/python/src/smg/version.py && rm -f bindings/python/src/smg/version.py.bak
@echo "Version updated to $(VERSION) in all files:"
@echo " - model_gateway/Cargo.toml"
@echo " - bindings/golang/Cargo.toml"
@echo " - bindings/python/Cargo.toml"
@echo " - bindings/python/pyproject.toml"
@echo " - bindings/python/src/smg/version.py"
@echo ""
@echo "Verify with: make show-version"
check-versions: ## Check workspace crate versions against latest tag (usage: make check-versions [TAG=v1.0.0] [VERSION_OVERRIDE=1.3.1])
@VERSION_OVERRIDE="$(VERSION_OVERRIDE)" python3 scripts/check_release_versions.py $(TAG)
# Release process (scripts/release/README.md): release branches cut from main, moved by pull requests
RELEASE_TOOL := scripts/release/release.sh
release-plan: ## Before a cut: the bump the next cut writes against TAG (default: the highest final release tag), quietly; nothing is changed (usage: make release-plan [TAG=v1.13.0] [VERSION=1.14.0-rc.1]; VERSION defaults to the first candidate of main's development version)
@VERSION_OVERRIDE="$(VERSION)" python3 scripts/check_release_versions.py --plan $(TAG)
release-cut: ## Cut release/<x.y> from FROM (default origin/main), bump it to VERSION, and open the pull request that moves main to the next development version (usage: make release-cut VERSION=1.14.0-rc.1 [FROM=<sha>] [PUSH=1])
@PUSH="$(PUSH)" $(RELEASE_TOOL) cut "$(VERSION)" $(FROM)
release-bump: ## Bump an existing release branch to VERSION through a pull request (usage: make release-bump VERSION=1.14.0-rc.2 [PUSH=0])
@PUSH="$(PUSH)" $(RELEASE_TOOL) bump "$(VERSION)"
release-pick: ## Cherry-pick SHA (comma-separated) from main onto release/RELEASE through a pull request (usage: make release-pick RELEASE=1.14 SHA=<sha>[,<sha>] [PUSH=0])
@PUSH="$(PUSH)" $(RELEASE_TOOL) pick "$(RELEASE)" "$(SHA)"
release-pick-pr: ## After a resolved pick conflict: push the pick branch (the current one, or PICK=<branch> checked out first) and open its pull request (usage: make release-pick-pr RELEASE=1.14 [PICK=pick/1.14-<sha>])
@PUSH="$(PUSH)" $(RELEASE_TOOL) pick-pr "$(RELEASE)" $(PICK)
release-status: ## What is on main and not on release/RELEASE, its own commits, tags and open pull requests (usage: make release-status RELEASE=1.14)
@$(RELEASE_TOOL) status "$(RELEASE)"
release-tag: ## Check the versions at release/<x.y>'s head and tag it vVERSION (usage: make release-tag VERSION=1.14.0-rc.1 [PUSH=1])
@PUSH="$(PUSH)" $(RELEASE_TOOL) tag "$(VERSION)"
release-notes: ## The release notes skeleton for VERSION on stdout (usage: make release-notes VERSION=1.14.0 [SINCE=v1.13.0] > notes.md)
@$(RELEASE_TOOL) notes "$(VERSION)" $(SINCE)
release-bump-main: ## After a final: a pull request that moves main's crates and packages to at least vVERSION's; the gateway keeps its development version (usage: make release-bump-main VERSION=1.14.0 [PUSH=0])
@PUSH="$(PUSH)" $(RELEASE_TOOL) bump-main "$(VERSION)"
release-publish-plan: ## The publishing steps for vVERSION: the workflow dispatches to run against the tag and the artifacts they produce; prints, runs nothing (usage: make release-publish-plan VERSION=1.14.0)
@$(RELEASE_TOOL) publish-plan "$(VERSION)"
release-check: ## The release gate for vVERSION: what the repository can read (versions at the branch head, its CI, the tag, main's drift) and the lines the release owner fills (the four model-provider verifiers) (usage: make release-check VERSION=1.14.0-rc.1)
@$(RELEASE_TOOL) check "$(VERSION)"
release-ship: ## vVERSION on its existing release branch in one run: the preflight, the bump (pushed when the rules allow the caller one, else its pull request, waited for), the tag, the notes, the GitHub release and the publish dispatches; a rerun skips what is there (usage: make release-ship VERSION=1.14.1 [DRY_RUN=1] [FROM=tag])
@$(RELEASE_TOOL) ship "$(VERSION)" $(if $(DRY_RUN),--dry-run) $(if $(FROM),--from $(FROM))
# Gateway perf bed (crates/gateway_perf, the `gateway-perf` binary): the gateway's cost per
# request on a CPU mock bed, base against head. PERF_OUT is the run root (one stamp per make
# invocation; each target writes under it: PERF_OUT/perf-cpu, PERF_OUT/perf-alloc, ...); the
# cell targets (perf-cpu, perf-alloc, perf-mem) take PERF_PATHS, PERF_REQUESTS, PERF_REPEATS and
# PERF_GATES; PERF_ARGS holds extra driver flags valid for the target's mode (the cell targets
# take the cell flags such as "--rate-scale 0.5", perf-bed and perf-live the bed flags only,
# such as "--gw-cores 0-7"); TOKENIZER selects a tokenizer directory (default: a synthetic one).
PERF := target/release/gateway-perf
ifeq ($(origin PERF_OUT),undefined)
PERF_OUT := target/gateway-perf/$(shell date -u +%Y%m%dT%H%M%SZ)
endif
PERF_HEAD ?= target/gateway-perf/head/smg
PERF_BASE ?= target/gateway-perf/base/smg
PERF_BASE_REV ?= origin/main
PERF_BASE_TARGET ?= target/gateway-perf/base-target
PERF_PATHS ?= chat,stream,tools,json,messages
PERF_REQUESTS ?= 10000
PERF_REPEATS ?= 3
PERF_MEM_PATHS ?= stream
PERF_MEM_REQUESTS ?= 5000
PERF_ARGS ?=
PERF_GATES ?=
PERF_COMMON = --head $(PERF_HEAD) --out $(PERF_OUT)/$@ $(PERF_ARGS)
PERF_GATE_FLAG = $(if $(PERF_GATES),--gates $(PERF_GATES),)
PERF_CELLS = --base $(PERF_BASE) --paths $(PERF_PATHS) --requests $(PERF_REQUESTS) --repeats $(PERF_REPEATS) $(PERF_GATE_FLAG)
perf-build: ## Release binaries of the perf bed: the gateway alone (one cargo line, the same as perf-base), then the mock engine, the load client and the driver; the gateway copied under target/gateway-perf/head
@cargo build --release -p smg --bin smg
@mkdir -p target/gateway-perf/head && cp target/release/smg target/gateway-perf/head/smg
@cargo build --release -p mock-worker --bin mock-worker --bin stress_load
@cargo build --release -p gateway-perf
@git rev-parse HEAD > target/gateway-perf/head/commit 2>/dev/null || true
perf-base: perf-build ## The base revision's gateway for an A/B (usage: make perf-base [PERF_BASE_REV=origin/main]); its own target directory, so a full release build
@$(PERF) base $(PERF_BASE_REV) $(PERF_BASE) --target $(PERF_BASE_TARGET)
perf-bed: perf-build ## Bring the bed up (the gateway + mock engines), one request per path with the answer shown and the script counts, bed down
@$(PERF) bed smoke $(PERF_COMMON) --paths $(PERF_PATHS)
perf-cpu: perf-build ## Instructions, cycles and CPU time per request, base vs head, interleaved repeats (needs perf-base)
@$(PERF) cpu $(PERF_COMMON) $(PERF_CELLS)
perf-alloc: perf-build ## Allocation calls and KiB requested per request (jemalloc exit statistics), base vs head (needs perf-base)
@$(PERF) alloc $(PERF_COMMON) $(PERF_CELLS)
perf-mem: perf-build ## RSS over windows on one gateway, growth per 10k requests, the peak after a burst, base vs head (needs perf-base; PERF_MEM_PATHS, default stream)
@$(PERF) mem $(PERF_COMMON) --base $(PERF_BASE) --paths $(PERF_MEM_PATHS) --requests $(PERF_MEM_REQUESTS) $(PERF_GATE_FLAG)
perf-live: perf-build ## An open-loop ramp with the capacity rule (the head; the base too when its binary exists)
@$(PERF) live $(PERF_COMMON) $(if $(wildcard $(PERF_BASE)),--base $(PERF_BASE),)
perf-report: ## The Numbers table and the gate verdicts of a run (usage: make perf-report PERF_OUT=target/gateway-perf/<stamp>/perf-cpu [PERF_GATES=<toml>])
@case "$(origin PERF_OUT)" in command\ line|environment) ;; *) echo "perf-report: name the run: make perf-report PERF_OUT=target/gateway-perf/<stamp>/perf-cpu" >&2; exit 2 ;; esac
@$(PERF) report --out $(PERF_OUT) $(PERF_GATE_FLAG)