TOKENIZERS_VERSION = v0.9.1
UNAME := $(shell uname)
ARCH := $(shell uname -m)

#--------------------
# GOLANG THINGS
#--------------------
MELODY_STATIC_LIB := ./target/release/libcohere_melody.a

# `tkzrs` adds tokenizer FFI (`free_tokenizer`, etc.). A plain release build leaves them out,
# but libcohere_melody.a still exists — so we probe the archive instead of only testing -e.
check-build-with-tokenizers:
	@if [ ! -f "$(MELODY_STATIC_LIB)" ] || ! strings "$(MELODY_STATIC_LIB)" 2>/dev/null | grep -qF 'free_tokenizer'; then \
		cargo build -p melody-parsing --release --features tkzrs; \
	fi

golang-bindings-test: check-build-with-tokenizers
	go test -v -count=1 -race ./gobindings/...

# we kind of assume that you're running this on a macOS machine - it just builds locally
release-darwin-%:
	cargo build --release --target $*-apple-darwin --features tkzrs
	mkdir -p artifacts/darwin-$*
	cp target/$*-apple-darwin/release/libcohere_melody.* artifacts/darwin-$*
	cd artifacts/darwin-$* && \
		rm -f libcohere_melody.darwin-$*.tar.gz && \
		tar -czf libcohere_melody.darwin-$*.tar.gz libcohere_melody.a
	mkdir -p artifacts/all
	cp artifacts/darwin-$*/libcohere_melody.darwin-$*.tar.gz artifacts/all/libcohere_melody.darwin-$*.tar.gz

release-linux-%:
	docker buildx build --no-cache --platform linux/$* -f release/Dockerfile . -t melody.linux-$*
	mkdir -p artifacts/linux-$*
	docker run -v $(PWD)/artifacts/linux-$*:/mnt --entrypoint cp melody.linux-$* /workspace/libcohere_melody.linux.tar.gz /mnt/libcohere_melody.linux.tar.gz
	mkdir -p artifacts/all
	cp artifacts/linux-$*/libcohere_melody.linux.tar.gz artifacts/all/libcohere_melody.linux-$*.tar.gz

clean-artifacts:
	@if [ -d "artifacts" ]; then \
		rm -r artifacts; \
    fi;

release: clean-artifacts release-darwin-aarch64 release-darwin-x86_64 release-linux-arm64 release-linux-x86_64
	cp artifacts/all/libcohere_melody.darwin-aarch64.tar.gz artifacts/all/libcohere_melody.darwin-arm64.tar.gz
	cp artifacts/all/libcohere_melody.linux-arm64.tar.gz artifacts/all/libcohere_melody.linux-aarch64.tar.gz
	cp artifacts/all/libcohere_melody.linux-x86_64.tar.gz artifacts/all/libcohere_melody.linux-amd64.tar.gz

#--------------------
# RUST THINGS
#--------------------

rust-test:
	cargo test --verbose

rust-lint:
	cargo clippy --all-features  -- -Dwarnings

rust-format:
	cargo fmt

rust-build:
	cargo clean && cargo build --release

rust-build-with-tokenizers:
	cargo clean && cargo build --release --features tkzrs

#--------------------
# GENERATE TOOL
#--------------------

generate-build:
	cargo build --release -p template_generation

target/release/template_generation:
	$(MAKE) generate-build

generate-run: target/release/template_generation
	./target/release/template_generation $(ARGS)

generate-dev:
	cargo run -p template_generation -- $(ARGS)

#--------------------
# PYTHON THINGS
#--------------------

venv-setup:
	uv venv --allow-existing && uv pip install maturin pytest ty

python-bindings: venv-setup
	uv run maturin develop --features python_ffi

python-bindings-test: venv-setup python-bindings
	uv run pytest tests

python-bindings-format:
	uvx black .