REGISTRY ?= /usr/share/cargo/registry/ltk
DOCDIR   ?= /usr/share/doc/libltk-doc/html

# The Android build uses Debian's own packages: rustc and rust-src of the
# same version, whose std `-Zbuild-std` compiles for the target, and the NDK
# that google-android-ndk-r27c-installer (non-free) unpacks.
NDK            ?= /usr/lib/android-sdk/ndk/27.2.12479018
ANDROID_API    ?= 26
ANDROID_TARGET ?= aarch64-linux-android
NDK_BIN         = $(NDK)/toolchains/llvm/prebuilt/linux-x86_64/bin
NDK_CC          = $(NDK_BIN)/$(ANDROID_TARGET)$(ANDROID_API)-clang
ANDROID_ENV     = RUSTC_BOOTSTRAP=1 \
	CC_$(subst -,_,$(ANDROID_TARGET))=$(NDK_CC) \
	CXX_$(subst -,_,$(ANDROID_TARGET))=$(NDK_CC)++ \
	AR_$(subst -,_,$(ANDROID_TARGET))=$(NDK_BIN)/llvm-ar \
	CARGO_TARGET_$(shell echo $(ANDROID_TARGET) | tr a-z- A-Z_)_LINKER=$(NDK_CC)
ANDROID_CARGO   = -Zbuild-std=std,panic_abort --target $(ANDROID_TARGET) --no-default-features --features android
# Where `android-assets` writes the `ltk/` tree an APK carries under `assets/`.
ANDROID_ASSETS ?= target/android/assets
# The example APK, packaged with aapt, zipalign, apksigner and
# android-framework-res, and installed with adb (android-sdk-platform-tools).
ANDROID_ABI    ?= arm64-v8a
ANDROID_APK    ?= target/android/ltk-hello.apk

# Targets are recipes, not files. Listing them as PHONY tells make to
# ignore filesystem entries with the same name — without this `examples`
# silently no-ops because the `examples/` directory exists, and `doc`
# would do the same once `target/doc` is around.
.PHONY: all deps android-deps test doctest-md stylecheck clippy audit doc install examples clean distclean android-check android-assets android-apk android-install

all:
	cargo build --release

# What building, testing and linting need, from Debian.
deps:
	sudo apt-get install -y cargo rustc rust-clippy pkg-config libwayland-dev libegl-dev libxkbcommon-dev

# What the android-* targets need, from Debian: std's sources for -Zbuild-std,
# the NDK installer (non-free), the APK tools, rsvg-convert for SVG icons, javac, dx and
# android.jar (non-free) for the keep-alive service, and adb.
android-deps:
	sudo apt-get install -y rust-src google-android-ndk-r27c-installer aapt zipalign apksigner android-framework-res librsvg2-bin default-jdk-headless art-standalone google-android-platform-34-installer android-sdk-platform-tools

test:
	cargo test --features test-support

# Typecheck the `no_run` snippets in docs/*.md by feeding each markdown
# file to `rustdoc --test`. Catches API drift in cookbook + widget
# reference: drops a renamed builder, removed function, etc. surface
# here just like `cargo test --doc` does for in-source doctests.
doctest-md:
	./scripts/doctest-md.sh

# Mechanical style checks (tabs, attribute-bracket spacing) — the
# grep-verifiable subset of code_style_guide.md.
stylecheck:
	./scripts/style-check.sh

# Semantic lints, mirroring the CI invocation. Warnings are errors so a
# local run predicts the CI verdict.
clippy:
	cargo clippy --workspace --all-targets --features test-support -- -D warnings

# Typecheck the Android backend. Needs rust-src and the NDK (see NDK above).
android-check:
	@test -x $(NDK_CC) || { echo "No NDK compiler at $(NDK_CC); install google-android-ndk-r27c-installer or set NDK="; exit 1; }
	$(ANDROID_ENV) cargo check $(ANDROID_CARGO)

# The in-tree default theme and the fonts it names, as an APK carries them.
android-assets:
	./scripts/android-assets.sh $(ANDROID_ASSETS) themes default

# examples/android_hello.rs as an APK, with the in-tree default theme.
android-apk: android-assets
	@test -x $(NDK_CC) || { echo "No NDK compiler at $(NDK_CC); install google-android-ndk-r27c-installer or set NDK="; exit 1; }
	$(ANDROID_ENV) cargo build --release --example android_hello $(ANDROID_CARGO)
	ANDROID_ABI=$(ANDROID_ABI) MIN_SDK=$(ANDROID_API) ./scripts/android-apk.sh $(ANDROID_APK) \
		target/$(ANDROID_TARGET)/release/examples/libandroid_hello.so net.liberux.ltk.Hello "ltk hello" $(ANDROID_ASSETS)

android-install: android-apk
	adb install -r $(ANDROID_APK)

audit:
	@command -v cargo-audit >/dev/null 2>&1 || cargo install cargo-audit --locked
	cargo audit

doc:
	cargo doc --no-deps

install: doc
	install -d $(REGISTRY)
	cp -r src benches examples locales protocols keyboards scripts java build.rs Cargo.toml liberux.toml $(REGISTRY)/
	cp debian/cargo-checksum.json $(REGISTRY)/.cargo-checksum.json
	install -d $(DOCDIR)
	cp -r target/doc/* $(DOCDIR)/

examples:
	LTK_THEMES_DIR=themes cargo run --release --example showcase
	LTK_THEMES_DIR=themes cargo run --release --example responsive
	LTK_THEMES_DIR=themes cargo run --release --example inputs
	LTK_THEMES_DIR=themes cargo run --release --example scroll
	LTK_THEMES_DIR=themes cargo run --release --example sliders
	LTK_THEMES_DIR=themes cargo run --release --example combo
	LTK_THEMES_DIR=themes cargo run --release --example mini_shell
	LTK_THEMES_DIR=themes cargo run --release --example widgets
	LTK_THEMES_DIR=themes cargo run --release --example pickers
	LTK_THEMES_DIR=themes cargo run --release --example dialog
	LTK_THEMES_DIR=themes cargo run --release --example carousel
	LTK_THEMES_DIR=themes cargo run --release --example clip_path
	LTK_THEMES_DIR=themes cargo run --release --example tab_strip

clean:
	dh_clean
	cargo clean
	rm -rf target

distclean: clean
	rm -f Cargo.lock
