From bf5360d7215e9c4bd1100708a00915028374171e Mon Sep 17 00:00:00 2001 From: Chen Chengjun Date: Tue, 19 Aug 2025 08:56:19 +0000 Subject: [PATCH] Enable arch-aware make check --- .github/actions/test/action.yml | 4 ++-- Makefile | 14 ++++++++------ osdk/src/arch.rs | 15 +++++++++++++++ test/Makefile | 8 ++++---- test/README.md | 4 ++-- 5 files changed, 31 insertions(+), 14 deletions(-) diff --git a/.github/actions/test/action.yml b/.github/actions/test/action.yml index c419e3332..ca4aa7d7f 100644 --- a/.github/actions/test/action.yml +++ b/.github/actions/test/action.yml @@ -70,7 +70,7 @@ runs: [[ "${{ matrix.id }}" == "compile" ]] && CMD+="make build FEATURES=all" [[ "${{ matrix.id }}" == "usermode_test" ]] && CMD+="make test" [[ "${{ matrix.id }}" == "ktest" ]] && CMD+="make ktest NETDEV=tap" - [[ -n "${{ inputs.arch }}" ]] && CMD+=" ARCH=${{ inputs.arch }}" + [[ -n "${{ inputs.arch }}" ]] && CMD+=" OSDK_TARGET_ARCH=${{ inputs.arch }}" echo "Executing: $CMD" eval $CMD @@ -86,7 +86,7 @@ runs: [[ -n "${{ inputs.smp }}" ]] && CMD+=" SMP=${{ inputs.smp }}" [[ -n "${{ inputs.netdev }}" ]] && CMD+=" NETDEV=${{ inputs.netdev }}" [[ -n "${{ inputs.scheme }}" ]] && CMD+=" SCHEME=${{ inputs.scheme }}" - [[ -n "${{ inputs.arch }}" ]] && CMD+=" ARCH=${{ inputs.arch }}" + [[ -n "${{ inputs.arch }}" ]] && CMD+=" OSDK_TARGET_ARCH=${{ inputs.arch }}" [[ -n "${{ inputs.extra_blocklists }}" ]] && CMD+=" EXTRA_BLOCKLISTS_DIRS=${{ inputs.extra_blocklists }}" [[ -n "${{ inputs.syscall_test_suite }}" ]] && CMD+=" SYSCALL_TEST_SUITE=${{ inputs.syscall_test_suite }}" [[ -n "${{ inputs.syscall_test_workdir }}" ]] && CMD+=" SYSCALL_TEST_WORKDIR=${{ inputs.syscall_test_workdir }}" diff --git a/Makefile b/Makefile index 5e4af963b..ae277498a 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ # =========================== Makefile options. =============================== # Global build options. -ARCH ?= x86_64 +OSDK_TARGET_ARCH ?= x86_64 BENCHMARK ?= none BOOT_METHOD ?= grub-rescue-iso BOOT_PROTOCOL ?= multiboot2 @@ -52,7 +52,7 @@ SHELL := /bin/bash CARGO_OSDK := ~/.cargo/bin/cargo-osdk # Common arguments for `cargo osdk` `build`, `run` and `test` commands. -CARGO_OSDK_COMMON_ARGS := --target-arch=$(ARCH) +CARGO_OSDK_COMMON_ARGS := --target-arch=$(OSDK_TARGET_ARCH) # The build arguments also apply to the `cargo osdk run` command. CARGO_OSDK_BUILD_ARGS := --kcmd-args="ostd.log_level=$(LOG_LEVEL)" CARGO_OSDK_TEST_ARGS := @@ -105,9 +105,9 @@ BOOT_METHOD = qemu-direct OVMF = off endif -ifeq ($(ARCH), riscv64) +ifeq ($(OSDK_TARGET_ARCH), riscv64) SCHEME = riscv -else ifeq ($(ARCH), loongarch64) +else ifeq ($(OSDK_TARGET_ARCH), loongarch64) SCHEME = loongarch endif @@ -141,7 +141,7 @@ CARGO_OSDK_COMMON_ARGS += --grub-boot-protocol=$(BOOT_PROTOCOL) endif ifeq ($(ENABLE_KVM), 1) - ifeq ($(ARCH), x86_64) + ifeq ($(OSDK_TARGET_ARCH), x86_64) CARGO_OSDK_COMMON_ARGS += --qemu-args="-accel kvm" endif endif @@ -314,7 +314,6 @@ format: @$(MAKE) --no-print-directory -C test format .PHONY: check -# FIXME: Make `make check` arch-aware. check: initramfs $(CARGO_OSDK) @# Check formatting issues of the Rust code @./tools/format_all.sh --check @@ -345,6 +344,9 @@ check: initramfs $(CARGO_OSDK) done @for dir in $(OSDK_CRATES); do \ echo "Checking $$dir"; \ + # Exclude linux-bzimage-setup since it only supports x86-64 currently and will panic \ + # in other architectures. \ + [ "$$dir" = "ostd/libs/linux-bzimage/setup" ] && [ "$(OSDK_TARGET_ARCH)" != "x86_64" ] && continue; \ (cd $$dir && cargo osdk clippy -- -- -D warnings) || exit 1; \ done @ diff --git a/osdk/src/arch.rs b/osdk/src/arch.rs index 5e0c40f91..cdd9297f2 100644 --- a/osdk/src/arch.rs +++ b/osdk/src/arch.rs @@ -79,7 +79,22 @@ impl Display for Arch { } /// Get the default architecture implied by the host rustc's default architecture. +/// +/// If the environment variable `OSDK_TARGET_ARCH` is set, use it to determine the default +/// architecture directly. pub fn get_default_arch() -> Arch { + if let Ok(arch) = std::env::var("OSDK_TARGET_ARCH") { + return match arch.as_str() { + "aarch64" => Arch::Aarch64, + "riscv64" => Arch::RiscV64, + "x86_64" => Arch::X86_64, + "loongarch64" => Arch::LoongArch64, + _ => panic!( + "The environment variable `OSDK_TARGET_ARCH` specifies an unsupported native architecture" + ), + }; + }; + let output = crate::util::new_command_checked_exists("rustc") .arg("-vV") .output() diff --git a/test/Makefile b/test/Makefile index 6b7bd01f1..0c17cd660 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,6 +1,6 @@ # SPDX-License-Identifier: MPL-2.0 -ARCH ?= x86_64 +OSDK_TARGET_ARCH ?= x86_64 SMP ?= 1 VERBOSE ?= 1 SYSCALL_TEST_SUITE ?= ltp @@ -47,7 +47,7 @@ endif all: build .PHONY: build -ifeq ($(ARCH), loongarch64) +ifeq ($(OSDK_TARGET_ARCH), loongarch64) build: $(EXT2_IMAGE) $(EXFAT_IMAGE) @echo "For loongarch, we generate a fake initramfs to successfully test or build." @touch $(INITRAMFS_IMAGE) @@ -59,7 +59,7 @@ endif $(INITRAMFS_IMAGE): $(INITRAMFS) @nix-build \ --tarball-ttl $(NIXPKGS_CACHE_TTL) \ - --argstr target $(ARCH) \ + --argstr target $(OSDK_TARGET_ARCH) \ --arg enableBasicTest $(ENABLE_BASIC_TEST) \ --arg enableBenchmark $(ENABLE_BENCHMARK) \ --arg enableSyscallTest $(ENABLE_SYSCALL_TEST) \ @@ -74,7 +74,7 @@ $(INITRAMFS_IMAGE): $(INITRAMFS) $(INITRAMFS): @nix-build \ --tarball-ttl $(NIXPKGS_CACHE_TTL) \ - --argstr target $(ARCH) \ + --argstr target $(OSDK_TARGET_ARCH) \ --arg enableBasicTest $(ENABLE_BASIC_TEST) \ --arg enableBenchmark $(ENABLE_BENCHMARK) \ --arg enableSyscallTest $(ENABLE_SYSCALL_TEST) \ diff --git a/test/README.md b/test/README.md index a16e44b14..cddadd6ac 100644 --- a/test/README.md +++ b/test/README.md @@ -38,9 +38,9 @@ While most tests rely on `Nix` for compilation, the `gvisor` syscall test suite The test suite supports building for multiple architectures, including `x86_64` and `riscv64`. You can specify the desired architecture by running: ```bash -make build ARCH=x86_64 +make build OSDK_TARGET_ARCH=x86_64 # or -make build ARCH=riscv64 +make build OSDK_TARGET_ARCH=riscv64 ``` The build artifacts (initramfs) can be found in the `test/build` directory after the compilation.