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-crate

# Crate in a browser, for forge/web: the module and its JavaScript in target/web. 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-crate/web

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

all:
	cargo build $(CARGO_FLAGS)

# libltk-dev's files carry its changelog's date, so a reinstalled ltk can look older than its last build and cargo would keep that build: it is always cleaned first. 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-crate.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-crate-web package, which forge-web-server serves at /crate.
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)

start: all
	$(LAUNCH) --mode mobile

start-desktop: all
	$(LAUNCH) --mode desktop

test:
	cargo test

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

uninstall:
	rm -f $(BINDIR)/eydos-crate
	rm -f $(APPSDIR)/io.eydos.Crate.desktop
	rm -f $(METAINFODIR)/io.eydos.Crate.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
