# todo-haskell. Every cabal store and build directory lives under .cache/,
# so nothing here touches ~/.cabal or the arm64 root's /root.
#
# The x86 build uses ghcup's GHC 9.10.3. The shipped linux-arm64 binary is
# built inside the emulated arm64 root (scripts/arm64.sh) with Debian's
# GHC 9.6.6, linked fully static.

ROOT    := $(abspath ../..)
CACHE   := $(abspath .cache)
ARM64   := $(ROOT)/scripts/arm64.sh
VERSION := $(shell git rev-parse --short HEAD)
MESSAGE ?= Deploy $(VERSION)

export PATH := $(HOME)/.ghcup/bin:$(PATH)

# ghcup's GHC links -lgmp, and this machine has only libgmp.so.10, so
# .cache/x86-lib holds the libgmp.so link the linker looks for.
X86_CABAL   := CABAL_DIR=$(CACHE)/cabal-x86 LIBRARY_PATH=$(CACHE)/x86-lib cabal
ARM64_CABAL := $(ARM64) env CABAL_DIR=$(CACHE)/cabal-arm64 cabal

X86_BIN   := $(CACHE)/dist-x86/build/x86_64-linux/ghc-9.10.3/todo-haskell-0.1.0.0/x/todo-haskell/build/todo-haskell/todo-haskell
ARM64_BIN := $(CACHE)/dist-arm64/build/aarch64-linux/ghc-9.6.6/todo-haskell-0.1.0.0/x/todo-haskell/build/todo-haskell/todo-haskell

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

build: gen/BuildVersion.hs $(CACHE)/x86-lib/libgmp.so
	$(X86_CABAL) build -j --builddir=$(CACHE)/dist-x86 exe:todo-haskell
	@mkdir -p dist
	cp $(X86_BIN) dist/app.new && mv dist/app.new dist/app

build-arm64: gen/BuildVersion.hs
	@[ -d $(CACHE)/cabal-arm64/packages ] || $(ARM64_CABAL) update
	$(ARM64_CABAL) build -j --builddir=$(CACHE)/dist-arm64 exe:todo-haskell
	@mkdir -p dist
	cp $(ARM64_BIN) dist/app-linux-arm64.new && mv dist/app-linux-arm64.new dist/app-linux-arm64

test: gen/BuildVersion.hs $(CACHE)/x86-lib/libgmp.so
	$(X86_CABAL) test -j --enable-tests --builddir=$(CACHE)/dist-x86-test --test-show-details=direct

clean:
	rm -rf dist $(CACHE)/dist-x86 $(CACHE)/dist-x86-test $(CACHE)/dist-arm64 gen

# Compiled libraries go; downloaded sources (.cache/cabal-*/packages) stay.
clean-deps: clean
	rm -rf $(CACHE)/cabal-x86/store $(CACHE)/cabal-arm64/store

# The string is Todo.App's serverName, sent as the Server header.
bump:
	sed -i 's/^serverName = "todo-haskell"$$/serverName = "todo-haskell (bumped)"/' src/Todo/App.hs

unbump:
	sed -i 's/^serverName = "todo-haskell (bumped)"$$/serverName = "todo-haskell"/' src/Todo/App.hs

deploy:
	carlos deploy --account bfj --app todo-haskell --target linux-arm64 \
	  --version $(VERSION) --no-prompt --message "$(MESSAGE)" dist/app-linux-arm64

# The build version is the git short SHA, compiled in from a generated
# module. It's rewritten only when the SHA changes, so an unchanged tree
# doesn't recompile.
gen/BuildVersion.hs: FORCE
	@mkdir -p gen
	@printf 'module BuildVersion (buildVersion) where\n\nbuildVersion :: String\nbuildVersion = "%s"\n' '$(VERSION)' > gen/BuildVersion.hs.new
	@if cmp -s gen/BuildVersion.hs.new $@; then rm gen/BuildVersion.hs.new; else mv gen/BuildVersion.hs.new $@; fi

$(CACHE)/x86-lib/libgmp.so:
	@mkdir -p $(dir $@)
	ln -sf /usr/lib/x86_64-linux-gnu/libgmp.so.10 $@
