PREFIX  ?= /usr
DESTDIR ?=
# Rust target triple. Empty builds for this machine, into target/release.
RUST_TARGET ?=

CARGO_FLAGS = --release $(if $(RUST_TARGET),--target $(RUST_TARGET))
RELEASE_DIR = target/$(if $(RUST_TARGET),$(RUST_TARGET)/)release

BINDIR      = $(DESTDIR)$(PREFIX)/bin
APPSDIR     = $(DESTDIR)$(PREFIX)/share/applications
METAINFODIR = $(DESTDIR)$(PREFIX)/share/metainfo

LAUNCH      = LTK_THEMES_DIR=$(CURDIR)/../ltk/themes $(CURDIR)/target/release/eydos-calculator

# The calculator in a browser, for forge/web: the module and its JavaScript in target/web, as Crate's. The page's resources are forge/web's to build, one tree for every client. wasm-bindgen is the one `make web-tools` builds into target/tools with the installed ltk's script, or one on PATH.
LTK          ?= /usr/share/cargo/registry/ltk-0.3.0
WEB_TARGET    = wasm32-unknown-unknown
WEB_OUT       = target/web
WEB_TOOLS     = target/tools
WASM_BINDGEN ?= $(or $(wildcard $(WEB_TOOLS)/bin/wasm-bindgen),$(shell command -v wasm-bindgen 2>/dev/null))
WEBDIR        = $(DESTDIR)$(PREFIX)/share/eydos-calculator/web

.PHONY: all arm64 arm64-deps run test install uninstall deb deb-arm64 clean distclean web web-tools web-deps install-web

all:
	cargo build $(CARGO_FLAGS)

# An installed ltk keeps its package's timestamps, so cargo cannot tell it changed. Stripping at link time would drop the custom sections wasm-bindgen reads.
web:
	@test -n "$(WASM_BINDGEN)" || { echo "No wasm-bindgen; run make web-deps"; exit 1; }
	cargo clean --release --target $(WEB_TARGET) -p ltk
	cargo build --release --target $(WEB_TARGET) --config profile.release.strip=false
	rm -rf $(WEB_OUT)
	$(WASM_BINDGEN) --target web --no-typescript --out-dir $(WEB_OUT) target/$(WEB_TARGET)/release/eydos-calculator.wasm

# wasm-bindgen-cli at the version the installed ltk pins.
web-tools:
	$(LTK)/scripts/wasm-bindgen-cli.sh $(WEB_TOOLS)

# Debian's std for wasm32, and wasm-bindgen-cli.
web-deps:
	sudo apt-get install -y libstd-rust-dev-wasm32 curl
	$(MAKE) web-tools

# For the eydos-calculator-web package, which forge-web-server serves at /calculator.
install-web:
	install -d $(WEBDIR)
	install -m644 $(WEB_OUT)/* $(WEBDIR)/

arm64: export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER = aarch64-linux-gnu-gcc
arm64: export PKG_CONFIG = aarch64-linux-gnu-pkg-config
arm64: export PKG_CONFIG_ALLOW_CROSS = 1
arm64:
	$(MAKE) all RUST_TARGET=aarch64-unknown-linux-gnu

arm64-deps:
	sudo dpkg --add-architecture arm64
	sudo apt-get update
	sudo apt-get install -y crossbuild-essential-arm64 libstd-rust-dev:arm64 libwayland-dev:arm64 libxkbcommon-dev:arm64 libegl-dev:arm64 libgles2-mesa-dev:arm64

run: all
	$(LAUNCH)

test:
	cargo test

install:
	install -Dm755 $(RELEASE_DIR)/eydos-calculator           $(BINDIR)/eydos-calculator
	install -Dm644 data/io.eydos.Calculator.desktop         $(APPSDIR)/io.eydos.Calculator.desktop
	install -Dm644 data/io.eydos.Calculator.metainfo.xml    $(METAINFODIR)/io.eydos.Calculator.metainfo.xml

uninstall:
	rm -f $(BINDIR)/eydos-calculator
	rm -f $(APPSDIR)/io.eydos.Calculator.desktop
	rm -f $(METAINFODIR)/io.eydos.Calculator.metainfo.xml

# `deb-arm64` cross-builds the arm64 package from amd64; see `make arm64-deps`.
deb:
	dpkg-buildpackage -us -uc -b

deb-arm64:
	dpkg-buildpackage -us -uc -b -a arm64

clean:
	cargo clean

distclean: clean
	rm -f Cargo.lock
	dh_clean
