# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
# Makronix Mobile -- kernel, boot image, flash, for any device under devices/.
#
#   make devices                  list the supported phones
#   make kernel  DEVICE=hotdogb   build the kernel Image
#   make initramfs DEVICE=hotdogb build/initramfs-<device>.cpio.gz (our /init, our busybox)
#   make bootimg DEVICE=hotdogb   pack build/boot_<device>.img
#   make flash   DEVICE=hotdogb   flash it to the test slot (phone in fastboot)
#   make rootimg DEVICE=hotdogb   root file system image (BASE=void, the default, or debian)
#   make roottar DEVICE=hotdogb   the same tree as a tar, for update-root
#   make update-root DEVICE=hotdogb  unpack it next to the running system:
#                                 /system/current is the new one, /system/previous
#                                 the old one (keeps /data)
#   make flash-root DEVICE=hotdogb  write it to the root partition (phone in fastboot)
#   make userland [PROG=cat]      the NetBSD programs of mobile/userland for the
#                                 phone (docs/NETBSD-USERLAND.md, PORTING-USERLAND.md)
#   make userland-test PROG=cat   run a port's test on the phone (QEMU=1: on the host)
#   make tooldir                  the phone's toolchain: build.sh tools for
#                                 aarch64-linux-gnu against the sysroot (long)
#   make targetlibs               gcc's headers, libgcc, libstdc++ for it
#   make mxheaders                Makronix/Linux's sysroot: our kernel's headers
#   make glibc                    glibc of ext/src/glibc into it (mx/ recipe)

DEVICE ?= hotdogb
KERNEL_TREE ?= build/linux-$(DEVICE)
# ours (base/initramfs, make initramfs), pinned in device.conf; since
# 2026-11-08 instead of the pmOS one of the baseline, which still boots the
# same root: INITRAMFS=$(HOME)/.local/var/pmbootstrap/chroot_rootfs_oneplus-hotdogb/boot/initramfs
INITRAMFS ?= build/initramfs-$(DEVICE).cpio.gz
SLOT ?=
BASE ?= void
ROOT_PASSWORD ?= makronix

DEV = devices/$(DEVICE)
IMG = build/boot_$(DEVICE).img
IMAGE = $(KERNEL_TREE)/arch/arm64/boot/Image
ROOTFS = build/rootfs-$(BASE)-$(DEVICE).tar
ROOTIMG = build/root-$(BASE)-$(DEVICE).img
ROOTTREE = build/system-$(DEVICE).tar

.PHONY: src userland userland-test sysroot toolchain tooldir targetlibs mxheaders glibc devices linux kernel initramfs rootfs rootfs-debian rootfs-void rootimg roottar update-root bootimg flash flash-root check
devices:
	@for d in devices/*/; do \
	  n=$$(basename $$d); \
	  printf '%-12s %s\n' $$n "$$(sed -n 's/^DEVICE_NAME=//p' $$d/device.conf | tr -d '"')"; \
	done

# Makronix/Linux's toolchain and sysroot, in LFS's order: our kernel's
# headers, the tooldir (build.sh tools of src/, gcc and binutils), glibc
# (ext/src/glibc), gcc's runtime libraries against it, the compiler
# wrappers; then mobile/userland through NetBSD make
SYSROOT = build/mxsysroot-$(DEVICE)
MXSYSROOT = $(SYSROOT)
TOOLCHAIN = build/toolchain-$(DEVICE)
mxheaders:
	@[ -f $(SYSROOT)/usr/include/linux/version.h ] || sh tools/mkmxheaders.sh $(DEV) $(SYSROOT)
tooldir: mxheaders
	sh tools/mktooldir.sh $(DEV)
sysroot: mxheaders
	@[ -f $(SYSROOT)/usr/lib/libc.so.6 ] || sh tools/mkglibc.sh $(DEV)
toolchain: sysroot
	@[ -x $(TOOLCHAIN)/bin/cc ] || sh tools/mktoolchain.sh $(DEV) $(SYSROOT) $(TOOLCHAIN)
userland: toolchain
	cd userland && sh ../tools/mxmake.sh ../$(DEV) $(if $(PROG),PROG=$(PROG)) all
glibc: mxheaders
	sh tools/mkglibc.sh $(DEV)
# NetBSD's normal build of src/$(DIR) into the sysroot (tools/mxsrc.sh)
src:
	@[ -n "$(DIR)" ] || { echo "make src DIR=<dir under src/, e.g. bin>" >&2; exit 2; }
	sh tools/mxsrc.sh $(DEV) $(DIR) $(TARGETS)
targetlibs:
	sh tools/mktargetlibs.sh $(DEV)
userland-test:
	@[ -n "$(PROG)" ] || { echo "make userland-test PROG=<prog>" >&2; exit 2; }
	sh tools/userland-test.sh $(DEV) $(PROG) $(if $(QEMU),--qemu)

linux:
	@mkdir -p build
	sh tools/fetch-kernel.sh $(DEV) $(KERNEL_TREE)

kernel: linux
	sh tools/build-kernel.sh $(DEV) $(KERNEL_TREE)

rootfs: rootfs-$(BASE)

# one glibc base per directory under base/; tools/mkrootfs-<base>.sh builds it
rootfs-void:
	@mkdir -p build
	sh tools/mkrootfs-void.sh $(DEV) build/rootfs-void-$(DEVICE).tar

rootfs-debian:
	@mkdir -p build
	sh tools/mkrootfs-debian.sh $(DEV) build/rootfs-debian-$(DEVICE).tar

check:
	sh tools/check.sh $(DEV) $(INITRAMFS)

# the pins are checked after the inputs are built (the initramfs is ours)
bootimg: kernel initramfs
	sh tools/check.sh $(DEV) $(INITRAMFS)
	@mkdir -p build
	sh tools/mkbootimg.sh $(DEV) $(IMAGE) $(INITRAMFS) $(IMG)

flash: bootimg
	sh tools/flash.sh $(DEV) $(IMG) $(SLOT)

rootimg: kernel
	sh tools/mkrootimg.sh $(DEV) $(ROOTFS) $(KERNEL_TREE) $(ROOTIMG) $(ROOT_PASSWORD)

# the same tree as a tar, for a running phone (tools/update-root.sh)
roottar: kernel
	sh tools/mkroottar.sh $(DEV) $(ROOTFS) $(KERNEL_TREE) $(ROOTTREE) $(ROOT_PASSWORD)

# /system/current and /system/previous, no version in a name: the tree
# carries /etc/makronix-release (tools/assemble-rootfs.sh); keeps /data.
# There is no version variable here at all.  It was SYSTEM_VERSION, never
# VERSION: a variable given on make's command line is passed to every
# sub-make, and VERSION is the first line of the kernel's own Makefile --
# make update-root VERSION=2026-11-10a built the kernel as release
# "2026-11-10a.0.0-..." and stopped in fs/pstore/ram.c:809
# (build/logs/update-root-2026-11-10.log; the same error on 2026-11-09).
update-root: roottar
	sh tools/update-root.sh $(DEV) $(ROOTTREE) $(REBOOT)

flash-root:
	sh tools/flash-root.sh $(DEV) $(ROOTIMG)

initramfs:
	sh tools/build-busybox.sh $(DEV) build/busybox-$(DEVICE)
	sh tools/mkinitramfs.sh build/busybox-$(DEVICE) build/initramfs-$(DEVICE).cpio.gz
