Răsfoiți Sursa

script, build-img: add script folder for each platforms

Add PLATFORM in Makefile to indicate the actual system setup. Put build
image script of each platforms to dedicated folders. This makes creating
test setups easier.

Signed-off-by: greatbridf <greatbridf@icloud.com>
greatbridf 1 săptămână în urmă
părinte
comite
38a3272a00

+ 4 - 2
.github/workflows/test-build.yaml

@@ -19,8 +19,10 @@ jobs:
         include:
           - arch: riscv64
             target: riscv64gc-unknown-none-elf
+            platform: virt
           - arch: loongarch64
             target: loongarch64-unknown-none-softfloat
+            platform: virt
     steps:
       - name: Checkout code
         uses: actions/checkout@v4
@@ -39,7 +41,7 @@ jobs:
       - name: Run build for ${{ matrix.arch }} targets
         run: |
           make build ARCH=${{ matrix.arch }} MODE=release
-          zstd -k build/boot-${{ matrix.arch }}.img
+          zstd -k build/boot-${{ matrix.arch }}-${{ matrix.platform }}.img
 
       - name: Upload build artifacts
         uses: actions/upload-artifact@v4
@@ -47,7 +49,7 @@ jobs:
           name: eonix-kernel-and-image-${{ matrix.arch }}
           path: |
             build/${{ matrix.target }}/release/eonix_kernel
-            build/boot-${{ matrix.arch }}.img.zst
+            build/boot-${{ matrix.arch }}-${{ matrix.platform }}.img.zst
 
       - name: Test run for ${{ matrix.arch }}
         run: |

+ 18 - 16
Makefile.src

@@ -54,6 +54,8 @@ endif
 
 ifeq ($(ARCH),riscv64)
 
+PLATFORM ?= virt
+
 BINARY_DIR_BASE := build/riscv64gc-unknown-none-elf
 BINARY_DIR := $(BINARY_DIR_BASE)/$(MODE)
 
@@ -61,7 +63,7 @@ QEMU_ARGS += \
 	-machine virt -kernel $(BINARY_DIR)/eonix_kernel \
 	-device virtio-blk-device,drive=disk0,bus=virtio-mmio-bus.0 \
 	-device virtio-net-device,netdev=mynet0 \
-	-drive id=disk0,file=build/boot-riscv64.img,format=raw,if=none \
+	-drive id=disk0,file=build/boot-riscv64-virt.img,format=raw,if=none \
 	-netdev user,id=mynet0 \
 	-rtc base=utc
 
@@ -73,11 +75,10 @@ endif
 
 CARGO_FLAGS += --target riscv64gc-unknown-none-elf
 
-.PHONY: build
-build: $(BINARY_DIR)/eonix_kernel build/boot-riscv64.img
-
 else ifeq ($(ARCH),loongarch64)
 
+PLATFORM ?= virt
+
 BINARY_DIR_BASE := build/loongarch64-unknown-none-softfloat
 BINARY_DIR := $(BINARY_DIR_BASE)/$(MODE)
 
@@ -85,7 +86,7 @@ QEMU_ARGS += \
 	-machine virt -kernel $(BINARY_DIR)/eonix_kernel -m 1G \
 	-device virtio-blk-pci,drive=disk0 \
 	-device virtio-net-pci,netdev=mynet0 \
-	-drive id=disk0,file=build/boot-loongarch64.img,format=raw,if=none \
+	-drive id=disk0,file=build/boot-loongarch64-virt.img,format=raw,if=none \
 	-netdev user,id=mynet0,hostfwd=tcp::5555-:5555,hostfwd=udp::5555-:5555 \
 	-rtc base=utc
 
@@ -97,11 +98,10 @@ endif
 
 CARGO_FLAGS += --target loongarch64-unknown-none-softfloat
 
-.PHONY: build
-build: $(BINARY_DIR)/eonix_kernel build/boot-loongarch64.img
-
 else ifeq ($(ARCH),x86_64)
 
+PLATFORM ?= acpi_compat
+
 BINARY_DIR_BASE := build/x86_64-unknown-none
 BINARY_DIR := $(BINARY_DIR_BASE)/$(MODE)
 
@@ -110,7 +110,7 @@ QEMU_ARGS += \
 	-device ahci,id=ahci \
 	-device ide-hd,drive=disk0,bus=ahci.0 \
 	-device e1000e,netdev=mynet0 \
-	-drive id=disk0,file=build/boot-x86_64.img,format=raw,if=none \
+	-drive id=disk0,file=build/boot-x86_64-acpi_compat.img,format=raw,if=none \
 	-netdev user,id=mynet0
 
 ifneq ($(IMG),)
@@ -121,11 +121,13 @@ endif
 
 CARGO_FLAGS += --target x86_64-unknown-none.json
 
-.PHONY: build
-build: $(BINARY_DIR)/eonix_kernel build/boot-x86_64.img
-
 endif
 
+TARGET := $(ARCH)-$(PLATFORM)
+
+.PHONY: build
+build: $(BINARY_DIR)/eonix_kernel build/boot-$(TARGET).img
+
 .PHONY: run
 run: build build/kernel.sym
 	$(QEMU) $(QEMU_ARGS) -display none -serial mon:stdio
@@ -174,7 +176,7 @@ build/kernel.sym: $(BINARY_DIR)/eonix_kernel
 	CARGO_TARGET_DIR=build cargo objcopy -q $(CARGO_FLAGS) -- --only-keep-debug build/kernel.sym
 
 build/fs-%.img: user-programs/init_script_%.sh script/build-img/build-img $(USER_PROGRAMS)
-	script/build-img/build-img -a $* -o $@
+	script/build-img/build-img -t $* -o $@
 
 build/mbr.bin: $(BINARY_DIR)/eonix_kernel
 	CARGO_TARGET_DIR=build cargo objcopy -q $(CARGO_FLAGS) -- -O binary -j .mbr build/mbr.bin
@@ -186,7 +188,7 @@ build/kernel.bin: $(BINARY_DIR)/eonix_kernel
 	CARGO_TARGET_DIR=build cargo objcopy -q $(CARGO_FLAGS) -- -O binary --strip-debug \
 		-R .mbr -R .stage1 build/kernel.bin
 
-build/boot-x86_64.img: build/fs-x86_64.img build/mbr.bin build/stage1.bin build/kernel.bin
+build/boot-x86_64-acpi_compat.img: build/fs-x86_64-acpi_compat.img build/mbr.bin build/stage1.bin build/kernel.bin
 	dd if=build/mbr.bin of=$@ bs=512 count=1 conv=notrunc 2> /dev/null
 	dd if=build/stage1.bin of=$@ bs=512 seek=1 conv=notrunc 2> /dev/null
 	dd if=build/kernel.bin of=$@ bs=4096 seek=1 conv=notrunc 2> /dev/null
@@ -195,13 +197,13 @@ build/boot-x86_64.img: build/fs-x86_64.img build/mbr.bin build/stage1.bin build/
 	sh -c 'echo n; echo; echo; echo 8192; echo; echo a; echo w' \
 		| $(FDISK) $@ 2> /dev/null > /dev/null
 
-build/boot-riscv64.img: build/fs-riscv64.img
+build/boot-riscv64-virt.img: build/fs-riscv64-virt.img
 	dd if=$< of=$@ bs=$(shell expr 4 \* 1024 \* 1024) \
 		seek=1 conv=notrunc 2> /dev/null
 	sh -c 'echo n; echo; echo; echo 8192; echo; echo a; echo w' \
 		| $(FDISK) $@ 2> /dev/null > /dev/null
 
-build/boot-loongarch64.img: build/fs-loongarch64.img
+build/boot-loongarch64-virt.img: build/fs-loongarch64-virt.img
 	dd if=$< of=$@ bs=$(shell expr 4 \* 1024 \* 1024) \
 		seek=1 conv=notrunc 2> /dev/null
 	sh -c 'echo n; echo; echo; echo 8192; echo; echo a; echo w' \

+ 16 - 47
script/build-img/build-img

@@ -1,26 +1,24 @@
 #!/bin/sh
+# shellcheck source=./lib/lib.sh
+
+. "$(dirname "$(realpath "$0")")/lib/lib.sh"
+
+BASE_DIR="$(dirname "$(realpath "$0")")"
 
 KB=1024
 MB=$((1024 * KB))
 
-OS=$(uname -s)
-SUDO=$(which sudo || :)
-
-ARCH=
+TARGET=
 IMAGE=
 MOUNTPOINT="$(mktemp -d)"
 
-die() {
-    echo "error: $1" >&2
-    exit 1
-}
-
 usage() {
     cat >&2 <<EOF
-Usage: $0 -a <ARCH> -o <OUTPUT> [-h]
+Usage: $0 -t <TARGET> -o <OUTPUT> [-h]
 
 Options
-    -a          The arch building image for [available: riscv64 x86 loongarch64]
+    -t          The target building image for
+                    [available: riscv64 x86 loongarch64]
     -o          Output image path
     -h          Show help message
 EOF
@@ -28,10 +26,6 @@ EOF
     exit "$1"
 }
 
-sudo() {
-    "$SUDO" "$@"
-}
-
 ensure_mount_macos() {
     hdiutil detach "$MOUNTPOINT" > /dev/null 2>&1 || :
     hdiutil attach "$IMAGE" -mountpoint "$MOUNTPOINT"
@@ -51,13 +45,6 @@ ensure_mount() {
     ensure_mount_linux
 }
 
-copy_to_image() {
-    _prefix=sudo
-    [ "$OS" = Darwin ] && _prefix=
-
-    $_prefix cp "$1" "$MOUNTPOINT/$2"
-}
-
 unmount_macos() {
     hdiutil detach "$MOUNTPOINT"
 }
@@ -80,9 +67,9 @@ cleanup() {
 
 set -eu
 
-while getopts "a:o:h" opt; do
+while getopts "t:o:h" opt; do
     case "$opt" in
-        a) ARCH="$OPTARG";;
+        t) TARGET="$OPTARG";;
         o) IMAGE="$OPTARG";;
         h) usage 0;;
         ?) usage 1;;
@@ -91,10 +78,10 @@ done
 
 shift $((OPTIND - 1))
 
-[ -z "$ARCH" ] && die "ARCH is not set"
+[ -z "$TARGET" ] && die "ARCH is not set"
 [ -z "$IMAGE" ] && die "output image path is not set"
 
-echo "Build image with ARCH=$ARCH OUTPUT=$IMAGE MOUNTPOINT=$MOUNTPOINT"
+echo "Build image with ARCH=$TARGET OUTPUT=$IMAGE MOUNTPOINT=$MOUNTPOINT"
 
 dd if=/dev/zero of="$IMAGE" bs=$((1 * MB)) count=$((1024 - 4))
 mkfs.fat -n SYSTEM "$IMAGE"
@@ -102,24 +89,6 @@ mkfs.fat -n SYSTEM "$IMAGE"
 ensure_mount
 trap cleanup EXIT
 
-if [ "$ARCH" = "x86_64" ]; then
-    copy_to_image ./user-programs/init.out init
-    copy_to_image ./user-programs/int.out int
-    copy_to_image ./user-programs/dynamic_test dynamic_test
-    copy_to_image ./user-programs/busybox busybox
-    copy_to_image ./user-programs/busybox-minimal busybox_
-    copy_to_image ./user-programs/ld-musl-i386.so.1 ld-musl-i386.so.1
-    copy_to_image ./user-programs/pthread_test pthread_test
-    copy_to_image ./user-programs/init_script_x86_64.sh initsh
-elif [ "$ARCH" = "riscv64" ]; then
-    copy_to_image ./user-programs/busybox.static busybox
-    copy_to_image ./user-programs/init_script_riscv64.sh initsh
-elif [ "$ARCH" = "loongarch64" ]; then
-    copy_to_image ./user-programs/busybox.la64 busybox
-    copy_to_image ./user-programs/init_script_loongarch64.sh initsh
-fi
-
-# Add your custom files here
-
-
-# End of custom files
+for name in $(iter_files_sorted "$BASE_DIR/platform/$TARGET"); do
+    ( . "$name" )
+done

+ 39 - 0
script/build-img/lib/lib.sh

@@ -0,0 +1,39 @@
+#!/bin/sh
+
+OS=$(uname -s)
+SUDO=$(which sudo || :)
+
+info() {
+    echo "info : $1"
+}
+
+warn() {
+    echo "warn : $1" >&2
+}
+
+error() {
+    echo "fatal: $1" >&2
+}
+
+die() {
+    error "$1" && exit 1
+}
+
+sudo() {
+    "$SUDO" "$@"
+}
+
+copy_to_image() {
+    _prefix=sudo
+    [ "$OS" = Darwin ] && _prefix=
+
+    $_prefix cp "$1" "$MOUNTPOINT/$2"
+}
+
+iter_files() {
+    find -L "$1" -maxdepth 1 -type f
+}
+
+iter_files_sorted() {
+    iter_files "$@" | sort
+}

+ 5 - 0
script/build-img/platform/loongarch64-virt/10-base.sh

@@ -0,0 +1,5 @@
+#!/bin/sh
+# shellcheck source=./../../lib/lib.sh
+
+copy_to_image user-programs/busybox.la64 busybox
+copy_to_image user-programs/init_script_loongarch64-virt.sh initsh

+ 5 - 0
script/build-img/platform/riscv64-virt/10-base.sh

@@ -0,0 +1,5 @@
+#!/bin/sh
+# shellcheck source=./../../lib/lib.sh
+
+copy_to_image user-programs/busybox.static busybox
+copy_to_image user-programs/init_script_riscv64-virt.sh initsh

+ 21 - 0
script/build-img/platform/riscv64-virt/20-tests.sh

@@ -0,0 +1,21 @@
+#!/bin/sh
+
+name="$(mktemp)"
+
+cat > "$name" <<EOF
+#!/bin/sh
+
+forks=100
+
+getpid() {
+    echo "\$(exec sh -c 'echo \$PPID')"
+}
+
+for i in \$(seq \$forks); do
+    ( getpid )
+done
+
+echo good
+EOF
+
+copy_to_image "$name" test-fork

+ 7 - 0
script/build-img/platform/x86_64-acpi_compat/10-base.sh

@@ -0,0 +1,7 @@
+#!/bin/sh
+# shellcheck source=./../../lib/lib.sh
+
+copy_to_image user-programs/init.out init
+copy_to_image user-programs/busybox busybox
+copy_to_image user-programs/busybox-minimal busybox_
+copy_to_image user-programs/init_script_x86_64-acpi_compat.sh initsh

+ 7 - 0
script/build-img/platform/x86_64-acpi_compat/20-tests.sh

@@ -0,0 +1,7 @@
+#!/bin/sh
+# shellcheck source=./../../lib/lib.sh
+
+copy_to_image user-programs/int.out int
+copy_to_image user-programs/dynamic_test dynamic_test
+copy_to_image user-programs/ld-musl-i386.so.1 ld-musl-i386.so.1
+copy_to_image user-programs/pthread_test pthread_test

+ 0 - 0
user-programs/init_script_loongarch64.sh → user-programs/init_script_loongarch64-virt.sh


+ 0 - 0
user-programs/init_script_riscv64.sh → user-programs/init_script_riscv64-virt.sh


+ 0 - 0
user-programs/init_script_x86_64.sh → user-programs/init_script_x86_64-acpi_compat.sh