penguin/monok8s

k8s image for Mono Gateway Dev Kit

commit 5d421b67430dd92215534bd6d7eb42d41e7d31f9ca233f861d006a291c3cced6

author斟酌 鵬兄 <tgckpg@gmail.com>
date2026-03-22T17:19:24Z
subjectRewrite makefile to use out/ dir
commit 5d421b67430dd92215534bd6d7eb42d41e7d31f9ca233f861d006a291c3cced6
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date:   2026-03-22T17:19:24Z

    Rewrite makefile to use out/ dir
---
 README.md                      |   2 +-
 docker/initramfs.Dockerfile    |  12 ++--
 docker/itb.Dockerfile          |  10 +--
 docker/kernel-build.Dockerfile |  13 +++-
 docker/release.Dockerfile      |   4 --
 makefile                       | 156 +++++++++++++++++++++++++++++++----------
 out/.keep                      |   0
 7 files changed, 141 insertions(+), 56 deletions(-)

diff --git a/README.md b/README.md
index 5d6ab52..f964891 100644
--- a/README.md
+++ b/README.md
@@ -5,7 +5,7 @@ Kubernetes image for Mono Gateway Development Kit
 https://docs.mono.si/gateway-development-kit/getting-started
 
 ## DISCLAIMER
-I used ChatGPT a lot for this. I also tested them thoroughly enough that until confident that's what we need.
+I leverage ChatGPT heavily for this. I also tested them thoroughly enough that until I am confident that's what we need.
 
 ### The device's dts files are located at here
 https://github.com/we-are-mono/OpenWRT-ASK/tree/mono-25.12.0-rc3/target/linux/layerscape/files/arch/arm64/boot/dts/freescale
diff --git a/docker/initramfs.Dockerfile b/docker/initramfs.Dockerfile
index e1f47d9..81bcbcf 100644
--- a/docker/initramfs.Dockerfile
+++ b/docker/initramfs.Dockerfile
@@ -1,11 +1,5 @@
 ARG TAG=dev
 ARG DOCKER_IMAGE_ROOT=monok8s
-FROM --platform=$BUILDPLATFORM ${DOCKER_IMAGE_ROOT}/kernel-build:${TAG} AS kernel
-
-# Selective mods
-RUN mkdir /out/selected-mods
-COPY initramfs/selective-mods.sh /
-RUN /selective-mods.sh /out/rootfs/lib/modules/$(ls /out/rootfs/lib/modules/) /out/selected-mods
 
 FROM --platform=$BUILDPLATFORM ${DOCKER_IMAGE_ROOT}/build-base:${TAG} AS build
 
@@ -35,7 +29,11 @@ COPY initramfs/init init
 RUN chmod +x init
 RUN mkdir -p bin sbin etc proc sys dev lib usr/bin usr/sbin
 
-COPY --from=kernel /out/selected-mods/* ./lib/
+# Selective mods
+RUN mkdir -p /out/selected-mods /out/kernel
+COPY initramfs/selective-mods.sh /
+COPY out/rootfs /out/rootfs
+RUN /selective-mods.sh /out/rootfs/lib/modules/$(ls /out/rootfs/lib/modules/) ./lib/
 
 RUN find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../initramfs.cpio.gz
 
diff --git a/docker/itb.Dockerfile b/docker/itb.Dockerfile
index a04dce0..91080b7 100644
--- a/docker/itb.Dockerfile
+++ b/docker/itb.Dockerfile
@@ -10,11 +10,11 @@ ARG DEVICE_TREE_TARGET
 RUN mkdir /image
 WORKDIR /image
 
-COPY --from=kernel /out/kernel/Image.gz ./Image.gz
-COPY --from=kernel /out/kernel/System.map ./
-COPY --from=kernel /out/kernel/.config ./
-COPY --from=fit /out/initramfs.cpio.gz ./
-COPY --from=kernel /out/${DEVICE_TREE_TARGET}.dtb ./
+COPY [ "./out/Image.gz" \
+	, "./out/System.map" \
+	, "./out/.config" \
+	, "./out/initramfs.cpio.gz" \
+	, "./out/${DEVICE_TREE_TARGET}.dtb", "./" ]
 
 COPY ./board.its ./_board.its
 
diff --git a/docker/kernel-build.Dockerfile b/docker/kernel-build.Dockerfile
index b48ae37..b3e0a39 100644
--- a/docker/kernel-build.Dockerfile
+++ b/docker/kernel-build.Dockerfile
@@ -1,6 +1,6 @@
 ARG TAG=dev
 ARG DOCKER_IMAGE_ROOT=monok8s
-FROM --platform=$BUILDPLATFORM ${DOCKER_IMAGE_ROOT}/build-base:${TAG}
+FROM --platform=$BUILDPLATFORM ${DOCKER_IMAGE_ROOT}/build-base:${TAG} AS build
 
 ARG NXP_VERSION
 ARG ARCH
@@ -46,3 +46,14 @@ RUN depmod -b /out/rootfs $(ls /out/rootfs/lib/modules/)
 
 RUN find /out/kernel -name "${DEVICE_TREE_TARGET}.dtb" -exec cp {} /out/ \;
 RUN test -f "/out/${DEVICE_TREE_TARGET}.dtb"
+
+FROM scratch
+ARG DEVICE_TREE_TARGET
+
+COPY --from=build \
+	/out/kernel/System.map \
+	/out/kernel/.config \
+	/out/kernel/Image.gz \
+	/out/${DEVICE_TREE_TARGET}.dtb \
+	./
+COPY --from=build /out/rootfs/ ./rootfs
diff --git a/docker/release.Dockerfile b/docker/release.Dockerfile
deleted file mode 100644
index 810a462..0000000
--- a/docker/release.Dockerfile
+++ /dev/null
@@ -1,4 +0,0 @@
-ARG RELEASE_IMAGE
-FROM scratch
-
-COPY --from artifacts $RELEASE_IMAGE /$RELEASE_IMAGE
diff --git a/makefile b/makefile
index 6fc1ea4..22002ed 100644
--- a/makefile
+++ b/makefile
@@ -4,85 +4,143 @@ export
 TAG ?= dev
 
 PACKAGES_DIR := packages
+OUT_DIR      := out
 
 BUSYBOX_TAR := $(PACKAGES_DIR)/busybox-$(BUSYBOX_VERSION).tar.gz
 ALPINE_TAR  := $(PACKAGES_DIR)/alpine-minirootfs-$(ALPINE_VER)-$(ALPINE_ARCH).tar.gz
 NXP_TAR     := $(PACKAGES_DIR)/$(NXP_VERSION).tar.gz
 CRIO_TAR    := $(PACKAGES_DIR)/$(CRIO_VERSION).tar.gz
 
-BOARD_ITB     := out/board.itb
-INITRAMFS     := out/initramfs.cpio.gz
-RELEASE_IMAGE := out/monok8s-$(TAG).img
-
-ALPINE_SERIES := $(word 1,$(subst ., ,${ALPINE_VER})).$(word 2,$(subst ., ,${ALPINE_VER}))
-
-$(BUSYBOX_TAR):
-	@mkdir -p $(PACKAGES_DIR)
+BOARD_ITB     := $(OUT_DIR)/board.itb
+INITRAMFS     := $(OUT_DIR)/initramfs.cpio.gz
+RELEASE_IMAGE := $(OUT_DIR)/monok8s-$(TAG).img
+
+KERNEL_IMAGE  := $(OUT_DIR)/Image.gz
+
+BUILD_BASE_STAMP := $(OUT_DIR)/.build-base-$(TAG).stamp
+
+ALPINE_SERIES := $(word 1,$(subst ., ,$(ALPINE_VER))).$(word 2,$(subst ., ,$(ALPINE_VER)))
+
+# ---- File groups -------------------------------------------------------------
+
+# Use find so adding new files under these dirs is automatically picked up.
+ALPINE_SRCS    := $(shell find alpine -type f 2>/dev/null)
+INITRAMFS_SRCS := $(shell find initramfs -type f 2>/dev/null)
+KERNEL_SRCS    := $(shell find kernel-build -type f 2>/dev/null)
+
+BUILD_BASE_DEPS := \
+	docker/build-base.Dockerfile \
+	build.env \
+	makefile
+
+KERNEL_DEPS := \
+	$(BUILD_BASE_STAMP) \
+	$(NXP_TAR) \
+	docker/kernel-build.Dockerfile \
+	kernel-extra.config \
+	$(KERNEL_SRCS) \
+	build.env \
+	makefile
+
+INITRAMFS_DEPS := \
+	$(KERNEL_IMAGE) \
+	$(BUSYBOX_TAR) \
+	docker/initramfs.Dockerfile \
+	$(INITRAMFS_SRCS) \
+	build.env \
+	makefile
+
+ITB_DEPS := \
+	$(INITRAMFS) \
+	docker/itb.Dockerfile \
+	board.its \
+	build.env \
+	makefile
+
+RELEASE_DEPS := \
+	$(BUILD_BASE_STAMP) \
+	$(BOARD_ITB) \
+	$(ALPINE_TAR) \
+	$(CRIO_TAR) \
+	docker/alpine.Dockerfile \
+	$(ALPINE_SRCS) \
+	build.env \
+	makefile
+
+# ---- Directory creation ------------------------------------------------------
+
+$(PACKAGES_DIR):
+	mkdir -p $@
+
+$(OUT_DIR):
+	mkdir -p $@
+
+# ---- Package downloads -------------------------------------------------------
+
+$(BUSYBOX_TAR): | $(PACKAGES_DIR)
 	curl -L -o $@ "https://github.com/mirror/busybox/archive/refs/tags/$(BUSYBOX_VERSION).tar.gz"
 
-$(ALPINE_TAR):
-	@mkdir -p $(PACKAGES_DIR)
+$(ALPINE_TAR): | $(PACKAGES_DIR)
 	curl -L -o $@ "https://dl-cdn.alpinelinux.org/alpine/v$(ALPINE_SERIES)/releases/$(ALPINE_ARCH)/alpine-minirootfs-$(ALPINE_VER)-$(ALPINE_ARCH).tar.gz"
 
-$(NXP_TAR):
-	@mkdir -p $(PACKAGES_DIR)
+$(NXP_TAR): | $(PACKAGES_DIR)
 	curl -L -o $@ "https://github.com/nxp-qoriq/linux/archive/refs/tags/$(NXP_VERSION).tar.gz"
 
-$(CRIO_TAR):
-	@mkdir -p $(PACKAGES_DIR)
+$(CRIO_TAR): | $(PACKAGES_DIR)
 	curl -L -o $@ "https://storage.googleapis.com/cri-o/artifacts/$(CRIO_VERSION).tar.gz"
 
-build-base:
+# ---- Build stages ------------------------------------------------------------
+
+$(BUILD_BASE_STAMP): $(BUILD_BASE_DEPS) | $(OUT_DIR)
 	docker build \
-	-f docker/build-base.Dockerfile \
-	--build-arg TAG=$(TAG) \
-	-t $(DOCKER_IMAGE_ROOT)/build-base:$(TAG) .
+		-f docker/build-base.Dockerfile \
+		--build-arg TAG=$(TAG) \
+		-t $(DOCKER_IMAGE_ROOT)/build-base:$(TAG) .
+	touch $@
 
-$(KERNEL_IMAGE): build-base $(NXP_TAR)
+$(KERNEL_IMAGE): $(KERNEL_DEPS) | $(OUT_DIR)
 	docker build \
-	-f docker/kernel-build.Dockerfile \
+		-f docker/kernel-build.Dockerfile \
 		--build-arg DOCKER_IMAGE_ROOT=$(DOCKER_IMAGE_ROOT) \
 		--build-arg TAG=$(TAG) \
 		--build-arg ARCH=$(ARCH) \
 		--build-arg CROSS_COMPILE=$(CROSS_COMPILE) \
 		--build-arg NXP_VERSION=$(NXP_VERSION) \
 		--build-arg DEVICE_TREE_TARGET=$(DEVICE_TREE_TARGET) \
-		--output type=local,dest=./out .
+		--output type=local,dest=./$(OUT_DIR) .
+	test -f $@
 
-	@test -f $(KERNEL_IMAGE)
-
-$(INITRAMFS): $(KERNEL_IMAGE) $(BUSYBOX_TAR)
+$(INITRAMFS): $(INITRAMFS_DEPS) | $(OUT_DIR)
 	docker build \
-	-f docker/initramfs.Dockerfile \
+		-f docker/initramfs.Dockerfile \
 		--build-arg DOCKER_IMAGE_ROOT=$(DOCKER_IMAGE_ROOT) \
 		--build-arg TAG=$(TAG) \
 		--build-arg ARCH=$(ARCH) \
 		--build-arg CROSS_COMPILE=$(CROSS_COMPILE) \
 		--build-arg BUSYBOX_VERSION=$(BUSYBOX_VERSION) \
-		--output type=local,dest=./out .
-
-	@test -f $(INITRAMFS)
+		--output type=local,dest=./$(OUT_DIR) .
+	test -f $@
 
-$(BOARD_ITB): $(INITRAMFS)
+$(BOARD_ITB): $(ITB_DEPS) | $(OUT_DIR)
 	docker build \
-	-f docker/itb.Dockerfile \
+		-f docker/itb.Dockerfile \
 		--build-arg DOCKER_IMAGE_ROOT=$(DOCKER_IMAGE_ROOT) \
 		--build-arg TAG=$(TAG) \
 		--build-arg ARCH=$(ARCH) \
 		--build-arg DEVICE_TREE_TARGET=$(DEVICE_TREE_TARGET) \
-		--output type=local,dest=./out .
-
-	@test -f $(BOARD_ITB)
+		--output type=local,dest=./$(OUT_DIR) .
+	test -f $@
 
-release: $(BOARD_ITB) build-base
+$(RELEASE_IMAGE): $(RELEASE_DEPS) | $(OUT_DIR)
 	docker build \
-	-f docker/alpine.Dockerfile \
+		-f docker/alpine.Dockerfile \
 		--build-arg DOCKER_IMAGE_ROOT=$(DOCKER_IMAGE_ROOT) \
 		--build-arg TAG=$(TAG) \
 		--build-arg ALPINE_ARCH=$(ALPINE_ARCH) \
 		--build-arg ALPINE_VER=$(ALPINE_VER) \
 		--build-arg CRIO_VERSION=$(CRIO_VERSION) \
-	-t $(DOCKER_IMAGE_ROOT)/buildenv-alpine:$(TAG) .
+		-t $(DOCKER_IMAGE_ROOT)/buildenv-alpine:$(TAG) .
+
 	@cid=$$(docker create \
 		--privileged \
 		-v /cache/apk:/var/cache/apk \
@@ -93,7 +151,29 @@ release: $(BOARD_ITB) build-base
 		$(DOCKER_IMAGE_ROOT)/buildenv-alpine:$(TAG) \
 		bash -lc '/build-rootfs.sh'); \
 	docker start -a $$cid; \
-	docker cp $$cid:/build/output.img $(RELEASE_IMAGE); \
+	docker cp $$cid:/build/output.img $@; \
 	docker rm $$cid
 
-	@test -f $(RELEASE_IMAGE)
+	test -f $@
+
+# ---- User targets ------------------------------------------------------------
+
+release: $(RELEASE_IMAGE)
+
+kernel: $(KERNEL_IMAGE)
+initramfs: $(INITRAMFS)
+itb: $(BOARD_ITB)
+build-base: $(BUILD_BASE_STAMP)
+
+clean:
+	rm -f \
+		$(BUILD_BASE_STAMP) \
+		$(KERNEL_IMAGE) \
+		$(INITRAMFS) \
+		$(BOARD_ITB) \
+		$(RELEASE_IMAGE)
+
+distclean: clean
+	rm -rf $(OUT_DIR) $(PACKAGES_DIR)
+
+.PHONY: release kernel initramfs itb build-base clean distclean
diff --git a/out/.keep b/out/.keep
deleted file mode 100644
index 473a0f4..0000000