Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ env = { "RUSTFLAGS" = "-C link-arg=-Tlinker.ld" }
# while other builds/runs use Release.
profile = "Release"

# To disable automatic Cargo argument injection from someboot build-info.toml, set this to true.
# disable_someboot_build_config = true

# Additional cargo arguments
args = []

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ env = { "RUSTFLAGS" = "-C link-arg=-Tlinker.ld" }
# 省略时保持兼容行为:QEMU --debug 使用 Debug,其它构建/运行使用 Release。
profile = "Release"

# 如需禁用从 someboot build-info.toml 自动注入 Cargo 参数,可显式设置为 true。
# disable_someboot_build_config = true

# 额外的 cargo 参数
args = []

Expand Down
1 change: 1 addition & 0 deletions ostool/src/bin/cargo-osrun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ async fn try_main() -> anyhow::Result<()> {
build_dir,
bin_dir,
debug: args.debug,
disable_someboot_build_config: false,
})?;

tool.prepare_elf_artifact(args.elf, args.to_bin).await?;
Expand Down
1 change: 1 addition & 0 deletions ostool/src/board/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ dtb_file = "${package}/board.dtb"
log: None,
extra_config: None,
profile: None,
disable_someboot_build_config: false,
args: vec![],
pre_build_cmds: vec![],
post_build_cmds: vec![],
Expand Down
63 changes: 62 additions & 1 deletion ostool/src/build/cargo_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl<'a> CargoBuilder<'a> {

// Auto-detected args from someboot/build-info.toml
let workspace_manifest = self.tool.workspace_dir().join("Cargo.toml");
if workspace_manifest.exists() {
if self.tool.someboot_build_config_enabled(self.config) && workspace_manifest.exists() {
let detected_args = someboot::detect_build_config_for_package(
&workspace_manifest,
&self.config.package,
Expand Down Expand Up @@ -594,6 +594,33 @@ mod tests {
select_executable_artifact(artifacts, explicit_bin, default_run, package)
}

fn write_someboot_workspace(root: &Path) {
fs::write(
root.join("Cargo.toml"),
"[workspace]\nmembers = [\"app\", \"someboot\"]\nresolver = \"3\"\n",
)
.unwrap();
fs::create_dir_all(root.join("app/src")).unwrap();
fs::write(
root.join("app/Cargo.toml"),
"[package]\nname = \"app\"\nversion = \"0.1.0\"\nedition = \"2024\"\n\n[dependencies]\nsomeboot = { path = \"../someboot\" }\n",
)
.unwrap();
fs::write(root.join("app/src/main.rs"), "fn main() {}\n").unwrap();
fs::create_dir_all(root.join("someboot/src")).unwrap();
fs::write(
root.join("someboot/Cargo.toml"),
"[package]\nname = \"someboot\"\nversion = \"0.1.0\"\nedition = \"2024\"\n",
)
.unwrap();
fs::write(root.join("someboot/src/lib.rs"), "pub fn marker() {}\n").unwrap();
fs::write(
root.join("someboot/build-info.toml"),
"[x86_64-unknown-none]\ncargoargs = [\"--someboot-cargoarg\"]\nrustflags = [\"-Cdebuginfo=2\"]\n",
)
.unwrap();
}

#[test]
fn select_executable_artifact_uses_explicit_bin_first() {
let artifacts = vec![
Expand Down Expand Up @@ -685,6 +712,39 @@ mod tests {
assert!(rendered.contains("kernel-uboot"));
}

#[tokio::test]
async fn build_cargo_command_skips_someboot_args_when_cargo_config_disables_them() {
let temp = tempfile::tempdir().unwrap();
write_someboot_workspace(temp.path());

let config = Cargo {
package: "app".into(),
target: "x86_64-unknown-none".into(),
disable_someboot_build_config: true,
profile: Some(CargoBuildProfile::Debug),
..Default::default()
};

let mut tool = Tool::new(ToolConfig {
manifest: Some(temp.path().to_path_buf()),
..Default::default()
})
.unwrap();
let mut builder = CargoBuilder::build(&mut tool, &config, None).skip_objcopy(true);
let cmd = builder.build_cargo_command().await.unwrap();
let args: Vec<String> = cmd
.get_args()
.map(|arg| arg.to_string_lossy().into_owned())
.collect();

assert!(!args.iter().any(|arg| arg == "--someboot-cargoarg"));
assert!(
!args
.iter()
.any(|arg| arg.contains("target.x86_64-unknown-none.rustflags"))
);
}

/// Verifies resolved Cargo artifacts are recorded into runtime state.
///
/// This covers post-resolution Tool state, not serde/config loading.
Expand Down Expand Up @@ -713,6 +773,7 @@ mod tests {
log: None,
extra_config: None,
profile: Some(CargoBuildProfile::Debug),
disable_someboot_build_config: false,
args: vec![],
pre_build_cmds: vec![],
post_build_cmds: vec![],
Expand Down
70 changes: 70 additions & 0 deletions ostool/src/build/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ use std::collections::HashMap;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

fn is_false(value: &bool) -> bool {
!*value
}

/// Root build configuration structure.
///
/// This is the top-level configuration that specifies which build system
Expand Down Expand Up @@ -96,6 +100,9 @@ pub struct Cargo {
/// Cargo's dev profile, and normal builds/runs use Cargo's release profile.
#[serde(default)]
pub profile: Option<CargoBuildProfile>,
/// Disable automatic Cargo argument injection from someboot build metadata.
#[serde(default, skip_serializing_if = "is_false")]
pub disable_someboot_build_config: bool,
/// Additional Cargo command-line arguments.
pub args: Vec<String>,
/// Shell commands to run before the build.
Expand Down Expand Up @@ -143,3 +150,66 @@ pub enum LogLevel {
/// Error level logging.
Error,
}

#[cfg(test)]
mod tests {
use super::Cargo;

#[test]
fn cargo_config_defaults_someboot_injection_to_enabled_when_field_is_absent() {
let cargo: Cargo = toml::from_str(
r#"
env = {}
target = "x86_64-unknown-none"
package = "kernel"
features = []
args = []
pre_build_cmds = []
post_build_cmds = []
to_bin = false
"#,
)
.unwrap();

assert!(!cargo.disable_someboot_build_config);
}

#[test]
fn cargo_config_omits_someboot_disable_field_when_false() {
let cargo = Cargo {
env: Default::default(),
target: "x86_64-unknown-none".into(),
package: "kernel".into(),
features: Vec::new(),
args: Vec::new(),
pre_build_cmds: Vec::new(),
post_build_cmds: Vec::new(),
to_bin: false,
..Cargo::default()
};

let rendered = toml::to_string(&cargo).unwrap();

assert!(!rendered.contains("disable_someboot_build_config"));
}

#[test]
fn cargo_config_serializes_someboot_disable_field_when_true() {
let cargo = Cargo {
env: Default::default(),
target: "x86_64-unknown-none".into(),
package: "kernel".into(),
features: Vec::new(),
disable_someboot_build_config: true,
args: Vec::new(),
pre_build_cmds: Vec::new(),
post_build_cmds: Vec::new(),
to_bin: false,
..Cargo::default()
};

let rendered = toml::to_string(&cargo).unwrap();

assert!(rendered.contains("disable_someboot_build_config = true"));
}
}
24 changes: 3 additions & 21 deletions ostool/src/run/output_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use regex::Regex;

pub(crate) const MATCH_DRAIN_DURATION: Duration = Duration::from_millis(500);
const MAX_MATCH_WINDOW_BYTES: usize = 2048;
const DEFAULT_FAIL_PATTERNS: &[&str] = &[r"(?i)\bpanic(?:ked)?\b", r"(?i)kernel panic"];
const MATCH_EXCERPT_CONTEXT_CHARS: usize = 120;
const MATCH_EXCERPT_MAX_CHARS: usize = 240;

Expand Down Expand Up @@ -46,17 +45,7 @@ pub(crate) fn compile_regexes(
.map(|p| Regex::new(p).map_err(|e| anyhow!("success regex error: {e}")))
.collect::<Result<Vec<_>, _>>()?;

let mut merged_fail_patterns = fail_patterns.to_vec();
for pattern in DEFAULT_FAIL_PATTERNS {
if !merged_fail_patterns
.iter()
.any(|existing| existing == pattern)
{
merged_fail_patterns.push((*pattern).to_string());
}
}

let fail_regex = merged_fail_patterns
let fail_regex = fail_patterns
.iter()
.map(|p| Regex::new(p).map_err(|e| anyhow!("fail regex error: {e}")))
.collect::<Result<Vec<_>, _>>()?;
Expand Down Expand Up @@ -295,17 +284,10 @@ mod tests {
}

#[test]
fn compile_regexes_appends_builtin_panic_patterns() {
fn compile_regexes_keeps_empty_fail_patterns_empty() {
let (_success, fail) = compile_regexes(&[], &[]).unwrap();

assert!(
fail.iter()
.any(|regex| regex.as_str() == r"(?i)\bpanic(?:ked)?\b")
);
assert!(
fail.iter()
.any(|regex| regex.as_str() == r"(?i)kernel panic")
);
assert!(fail.is_empty());
}

#[test]
Expand Down
3 changes: 3 additions & 0 deletions ostool/src/run/qemu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,7 @@ fail_regex = []
log: None,
extra_config: None,
profile: None,
disable_someboot_build_config: false,
args: vec![],
pre_build_cmds: vec![],
post_build_cmds: vec![],
Expand All @@ -950,6 +951,7 @@ fail_regex = []
log: None,
extra_config: None,
profile: None,
disable_someboot_build_config: false,
args: vec![],
pre_build_cmds: vec![],
post_build_cmds: vec![],
Expand Down Expand Up @@ -1113,6 +1115,7 @@ timeout = 0
log: None,
extra_config: None,
profile: None,
disable_someboot_build_config: false,
args: vec![],
pre_build_cmds: vec![],
post_build_cmds: vec![],
Expand Down
2 changes: 2 additions & 0 deletions ostool/src/run/uboot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,7 @@ timeout = 0
log: None,
extra_config: None,
profile: None,
disable_someboot_build_config: false,
args: vec![],
pre_build_cmds: vec![],
post_build_cmds: vec![],
Expand Down Expand Up @@ -1822,6 +1823,7 @@ baud_rate = "115200"
log: None,
extra_config: None,
profile: None,
disable_someboot_build_config: false,
args: vec![],
pre_build_cmds: vec![],
post_build_cmds: vec![],
Expand Down
Loading