From 184e803869d93f7793d33dabcab508f0bec8b5e9 Mon Sep 17 00:00:00 2001 From: jiangjianfeng Date: Thu, 8 Jan 2026 09:49:51 +0000 Subject: [PATCH] Use accurate build time only when publishing --- .github/workflows/publish_aster_nixos.yml | 1 + Cargo.lock | 27 ++++++++++++++++++++++ Cargo.toml | 1 + kernel/Cargo.toml | 1 + kernel/src/net/uts_ns.rs | 28 +++++++++++++++++++---- osdk/src/commands/build/mod.rs | 14 +----------- 6 files changed, 54 insertions(+), 18 deletions(-) diff --git a/.github/workflows/publish_aster_nixos.yml b/.github/workflows/publish_aster_nixos.yml index 4408b639e..111951974 100644 --- a/.github/workflows/publish_aster_nixos.yml +++ b/.github/workflows/publish_aster_nixos.yml @@ -98,6 +98,7 @@ jobs: image: asterinas/asterinas:0.17.0-20251228 options: --privileged -v /dev:/dev -v ${{ github.workspace }}:/root/asterinas run: | + export ASTER_BUILD_TIMESTAMP=`date '+%a %b %e %H:%M:%S %Z %Y'` make iso RELEASE=1 AUTO_INSTALL=false ARCH=${{ matrix.arch }} iso_path=$(realpath ./target/nixos/iso_image/iso/*.iso) echo "iso_path=$iso_path" diff --git a/Cargo.lock b/Cargo.lock index 147d45f25..e238ec2fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -191,6 +191,7 @@ dependencies = [ "bitvec", "cfg-if", "component", + "const_format", "controlled", "core2", "cpio-decoder", @@ -522,6 +523,26 @@ dependencies = [ "toml", ] +[[package]] +name = "const_format" +version = "0.2.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7faa7469a93a566e9ccc1c73fe783b4a65c274c5ace346038dca9c39fe0030ad" +dependencies = [ + "const_format_proc_macros", +] + +[[package]] +name = "const_format_proc_macros" +version = "0.2.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d57c2eccfb16dbac1f4e61e206105db5820c9d26c3c472bc17c774259ef7744" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "controlled" version = "0.1.0" @@ -1926,6 +1947,12 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + [[package]] name = "universal-hash" version = "0.4.1" diff --git a/Cargo.toml b/Cargo.toml index e6e275665..b5f8df9b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -150,6 +150,7 @@ volatile = "0.6.1" # a capability it currently lacks. aes-gcm = { version = "0.9.4", features = ["force-soft"] } bittle = "0.5.6" +const_format = "0.2.35" ctr = "0.8.0" font8x8 = { version = "0.2.5", default-features = false, features = [ "unicode" ] } getset = "0.1.2" diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index f8fb85818..b92ec8c5f 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -29,6 +29,7 @@ bitflags.workspace = true bitvec.workspace = true cfg-if.workspace = true component.workspace = true +const_format.workspace = true controlled.workspace = true core2.workspace = true cpio-decoder.workspace = true diff --git a/kernel/src/net/uts_ns.rs b/kernel/src/net/uts_ns.rs index 76da0374a..20ad677da 100644 --- a/kernel/src/net/uts_ns.rs +++ b/kernel/src/net/uts_ns.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MPL-2.0 -use ostd::sync::RwMutexReadGuard; +use ostd::{const_assert, sync::RwMutexReadGuard}; use spin::Once; use crate::{ @@ -153,11 +153,29 @@ impl UtsName { /// The version name. pub const VERSION: &str = { - if let Some(version) = option_env!("OSDK_BUILD_TIMESTAMP") { - version + const BUILD_TIMESTAMP: &str = if let Some(timestamp) = option_env!("ASTER_BUILD_TIMESTAMP") + { + timestamp } else { - "unknown" - } + const UNIX_EPOCH: &str = "Thu Jan 1 00:00:00 UTC 1970"; + UNIX_EPOCH + }; + + // The definition of Linux's UTS_VERSION can be found at: + // . + // Linux specifies that the total length of this string must not exceed 64 bytes. + + // In Linux, the BUILD_VERSION represents the compilation count, which + // increments each time the kernel is built within the same source tree. + // We use a fixed value of '1' here to ensure build determinism. + // Reference: . + const BUILD_VERSION: usize = 1; + const SMP_FLAGS: &str = "SMP "; + const PREEMPT_FLAGS: &str = ""; + const VERSION: &str = + const_format::formatcp!("#{BUILD_VERSION} {SMP_FLAGS}{PREEMPT_FLAGS}{BUILD_TIMESTAMP}"); + const_assert!(VERSION.len() <= 64); + VERSION }; /// The machine name. diff --git a/osdk/src/commands/build/mod.rs b/osdk/src/commands/build/mod.rs index 693fa96bf..a7729260b 100644 --- a/osdk/src/commands/build/mod.rs +++ b/osdk/src/commands/build/mod.rs @@ -250,19 +250,7 @@ fn build_kernel_elf( .unwrap_or_else(|_| "unknown".to_string()) ), ); - // Use system date command to get the formatted time string - let date_output = std::process::Command::new("date") - // Reference: - .arg("+%a %b %e %H:%M:%S %Z %Y") - .output() - .ok() - .and_then(|output| { - String::from_utf8(output.stdout) - .ok() - .map(|s| s.trim().to_string()) - }) - .unwrap_or_else(|| "Thu Jan 1 00:00:00 UTC 1970".to_string()); - command.env("OSDK_BUILD_TIMESTAMP", format!("#1 {}", date_output)); + command.env("RUSTFLAGS", rustflags.join(" ")); command.arg("build"); command.arg("--features").arg(features.join(" "));