# Makefile for record-asr — a Go HTTP transcription service built on
# sherpa-onnx (SenseVoiceSmall int8 + silero VAD).
#
# Targets:
#   make            build a *portable* native binary into dist/<arch>/,
#                   with the sherpa/onnxruntime .so files copied next to it
#                   and an $ORIGIN/lib rpath baked in so it runs anywhere.
#   make build-amd64   same, for linux/amd64 (native here).
#   make build-arm64   cross-compile for linux/arm64
#                      (needs: apt-get install gcc-aarch64-linux-gnu).
#   make dist          build both arches and produce tarballs in dist/.
#   make run           build native, then run with ./data and default env.
#   make tidy/clean/check.
#
# All runtime behavior is controlled by ASR_* environment variables, not build
# flags — see README.md.

VERSION ?= $(shell git describe --tags --always 2>/dev/null || echo dev)
MODCACHE := $(shell go env GOMODCACHE)
SHERPA_LINUX := $(MODCACHE)/github.com/k2-fsa/sherpa-onnx-go-linux@v1.13.7

# RPATH baked into every binary so it finds the .so files shipped in ./lib
# next to it, regardless of where the bundle is unpacked. $$ORIGIN -> $ORIGIN
# (a dynamic-loader token = directory of the executable).
ORIGIN_RPATH := '-Wl,-rpath,$$ORIGIN/lib'

.PHONY: all build build-amd64 build-arm64 dist run tidy clean check

all: build

# Portable native build for whatever arch `go env GOARCH` reports.
build:
	@arch=$$(go env GOARCH); \
	case $$arch in \
	  amd64) lib=x86_64-unknown-linux-gnu;; \
	  arm64) lib=aarch64-unknown-linux-gnu;; \
	  arm)   lib=arm-unknown-linux-gnueabihf;; \
	  *) echo "unsupported arch: $$arch"; exit 1;; \
	esac; \
	dist=dist/$$arch; mkdir -p $$dist/lib; \
	echo ">> building $$dist/record-asr ($$arch)"; \
	CGO_ENABLED=1 CGO_LDFLAGS=$(ORIGIN_RPATH) go build -trimpath -o $$dist/record-asr .; \
	cp $(SHERPA_LINUX)/lib/$$lib/*.so $$dist/lib/; \
	echo ">> built $$dist/record-asr (run with: ./$$dist/record-asr)"

# Explicit linux/amd64 (native toolchain).
build-amd64:
	@dist=dist/amd64; mkdir -p $$dist/lib; \
	echo ">> building $$dist/record-asr (amd64)"; \
	CGO_ENABLED=1 CGO_LDFLAGS=$(ORIGIN_RPATH) go build -trimpath -o $$dist/record-asr .; \
	cp $(SHERPA_LINUX)/lib/x86_64-unknown-linux-gnu/*.so $$dist/lib/; \
	echo ">> built $$dist/record-asr"

# Cross-compile to linux/arm64. Requires a C cross-compiler:
#   sudo apt-get install gcc-aarch64-linux-gnu
build-arm64:
	@dist=dist/arm64; mkdir -p $$dist/lib; \
	echo ">> building $$dist/record-asr (arm64)"; \
	CGO_ENABLED=1 CC=aarch64-linux-gnu-gcc GOOS=linux GOARCH=arm64 \
	  CGO_LDFLAGS=$(ORIGIN_RPATH) go build -trimpath -o $$dist/record-asr .; \
	cp $(SHERPA_LINUX)/lib/aarch64-unknown-linux-gnu/*.so $$dist/lib/; \
	echo ">> built $$dist/record-asr (run on aarch64 with qemu or on-device)"

# Both arches + tarballs.
dist: build-amd64 build-arm64
	@cd dist && \
	  tar czf record-asr-$(VERSION)-linux-amd64.tar.gz amd64 && \
	  tar czf record-asr-$(VERSION)-linux-arm64.tar.gz arm64 && \
	  ls -lh *.tar.gz

# Build native then run from the build dir with a local ./data.
run: build
	@ASR_DATA_DIR=$$PWD/data ./dist/$$(go env GOARCH)/record-asr

tidy:
	go mod tidy

clean:
	rm -rf dist

check:
	go vet ./...
