# todo-ocaml: TodoMVC for the CARLOS bakeoff. See README.md.
SHELL := /bin/bash
VERSION ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo dev)
MESSAGE ?= Deploy todo-ocaml
CSS := static/base.css static/index.css
DUNE := dune build --root . --profile release

.PHONY: build build-arm64 test clean clean-deps bump unbump deploy

# The shared TodoMVC styles are served byte for byte, compiled in.
static/%.css: ../../shared/%.css
	cp $< $@

build: $(CSS)
	mkdir -p _build
	APP_VERSION=$(VERSION) tools/x86.sh $(DUNE) --build-dir $(CURDIR)/_build/x86 ./bin/main.exe
	mkdir -p dist && install -m 755 _build/x86/default/bin/main.exe dist/app

build-arm64: $(CSS)
	mkdir -p _build
	APP_VERSION=$(VERSION) LINK_FLAGS='-ccopt -static' \
	  tools/arm64.sh $(DUNE) --build-dir $(CURDIR)/_build/arm64 ./bin/main.exe
	mkdir -p dist && install -m 755 _build/arm64/default/bin/main.exe dist/app-linux-arm64
	echo $(VERSION) > dist/app-linux-arm64.version

test: $(CSS)
	mkdir -p _build
	tools/x86.sh dune test --root . --build-dir $(CURDIR)/_build/x86 --force

clean:
	rm -rf dist _build $(CSS)

# Deletes both opam switches and both SQLite builds (every compiled
# library) but keeps the opam repositories, opam's download cache and the
# SQLite tarball in .cache/src, so the next build compiles every library
# from its already-downloaded source.
clean-deps: clean
	rm -rf .cache/opam-arm64/default .cache/opam-x86/default .cache/sqlite-arm64 .cache/sqlite-x86

bump:
	sed -i 's/^let build_marker = "todomvc"$$/let build_marker = "todomvc-bumped"/' src/app.ml

unbump:
	sed -i 's/^let build_marker = "todomvc-bumped"$$/let build_marker = "todomvc"/' src/app.ml

# Ships the version the binary was built with, which it reports at /api/version.
deploy:
	carlos deploy --account bfj --app todo-ocaml --target linux-arm64 --no-prompt \
	  --version "$$(cat dist/app-linux-arm64.version)" --message "$(MESSAGE)" dist/app-linux-arm64
