Skip to content

refactor(tool): 拆分运行入口与 Tool facade#117

Merged
ZR233 merged 3 commits into
drivercraft:mainfrom
MRNIU:refactor/tool-runner-input-boundaries
Jun 1, 2026
Merged

refactor(tool): 拆分运行入口与 Tool facade#117
ZR233 merged 3 commits into
drivercraft:mainfrom
MRNIU:refactor/tool-runner-input-boundaries

Conversation

@MRNIU
Copy link
Copy Markdown
Contributor

@MRNIU MRNIU commented May 29, 2026

Summary

  • 将 QEMU、U-Boot 和 board runner 从直接挂在 Tool 上的实现拆到模块级函数,运行入口改为接收显式的 process context、artifacts、arch/debug 等输入。
  • 保留现有 Tool runner/config 方法作为兼容 wrapper,并逐函数标注这些旧入口会在调用方迁移后移除。
  • 将 TFTP 和 menuconfig 相关路径解析改为使用显式项目布局、变量作用域和运行产物状态,减少 runner 模块对完整 Tool 的依赖。
  • legacy_context 继续只负责 InvocationState 与旧 AppContext 的状态桥接,并标注这些桥接 helper 的临时性质。

Approach

这次变更保留 Tool 作为现有 public API 的兼容 facade,但让 QEMU、U-Boot、board 和 TFTP 的核心流程依赖更窄的输入结构。Tool 负责把当前 invocation/build/runtime state 组装成 runner input,然后委托给对应模块函数;新代码可以直接调用模块级 API,旧调用方仍可通过 Tool 方法工作。

Changed Modules

  • ostool/src/run/qemu.rs: 提供显式 QemuRunInput 和模块级 QEMU config/run helper。
  • ostool/src/run/uboot.rs: 提供显式 UbootRunInput,并让本地/远程 U-Boot runner 消费该输入。
  • ostool/src/run/tftp.rs: TFTP server helper 改为接收 manifest dir 和 runtime artifacts,而不是完整 Tool
  • ostool/src/board/mod.rs: board run config 与 prepared board flow 改为使用显式 scope/input。
  • ostool/src/menuconfig.rs: QEMU config path 查询改为走显式 workspace/runtime arch。
  • ostool/src/tool.rs: 收敛为兼容 facade,保留旧 public runner/config 入口并委托到模块函数。
  • ostool/src/legacy_context.rs: 标注旧 AppContext 桥接 helper 的兼容层性质。

Compatibility

  • CLI、配置文件格式和 runtime 行为预期保持不变。
  • 既有 Tool public runner/config 方法仍保留,调用方不需要立即迁移。
  • 新增的 runner input/helper 主要用于缩小内部模块依赖面,不新增用户可见配置项。

Verification

已运行通过:

  • cargo fmt --all -- --check
  • CARGO_BUILD_JOBS=1 cargo check -p ostool --target x86_64-unknown-linux-gnu --all-features
  • CARGO_BUILD_JOBS=1 cargo test -p ostool -- --nocapture
  • CARGO_BUILD_JOBS=1 cargo clippy -p ostool --all-targets --all-features -- -D warnings
  • CARGO_BUILD_JOBS=1 cargo build -p ostool --target x86_64-unknown-linux-gnu --all-features
  • git diff --check

Risks / Follow-up

  • 本 PR 只做 host-side Rust 验证,没有实际启动 QEMU、U-Boot 或远程开发板 session。
  • Tool 兼容 facade 仍然存在;后续调用方迁移完成后,可以删除这些旧入口和对应桥接 helper。

Move QEMU, U-Boot, and board runner orchestration out of direct Tool implementations and into module-level functions that consume explicit runtime inputs.

Keep the existing Tool runner and config methods as documented compatibility wrappers so current public callers continue to work while new code can depend on narrower module APIs.

Route TFTP and menuconfig helpers through explicit project/config state instead of requiring runner modules to borrow Tool.

Co-authored-by: OpenAI Codex <codex@openai.com>
Signed-off-by: Niu Zhihong <zhihong@nzhnb.com>
@MRNIU MRNIU force-pushed the refactor/tool-runner-input-boundaries branch from cc63ef0 to dc19fae Compare May 29, 2026 05:07
让 U-Boot runner 消费已准备的 runtime image,而不是在 Tool facade 里无条件补 raw BIN。这样 ELF-only U-Boot artifacts 可以进入运行路径,缺失 runtime image 时在 runner 入口返回明确错误。

保留 QEMU 的 to_bin 语义:crate 内部 run_qemu_with_config 在 config.to_bin=true 时要求调用方已准备 BIN,避免未来绕过 Tool::run_qemu 后静默回退到 ELF。

同步 runtime artifact 和 cargo-osrun 文案,避免把 Cargo ELF、runtime image 和 raw BIN 混为一类。补充回归测试覆盖 U-Boot ELF-only 输入和 QEMU to_bin=true 的 BIN 要求。

Validation: cargo fmt --all -- --check; cargo test -p ostool -- --nocapture; cargo clippy -p ostool --all-targets --all-features -- -D warnings; git diff --check.

Co-authored-by: OpenAI Codex <codex@openai.com>
Signed-off-by: Niu Zhihong <zhihong@nzhnb.com>
@MRNIU MRNIU marked this pull request as ready for review May 29, 2026 07:05
Copy link
Copy Markdown

@mai-team-app mai-team-app Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review: refactor(tool) - 拆分运行入口与 Tool facade

总体评价

这是一个质量很高的重构 PR。将 QEMU、U-Boot、board 和 TFTP 的 runner 逻辑从 Tool impl 块中提取到模块级函数,并引入 QemuRunInput / UbootRunInput 等窄输入结构,大幅减少了 runner 模块对完整 Tool 的依赖。兼容性 wrapper 保留完整,且每个都有明确的移除标注,便于后续迁移。

设计方面

输入边界清晰: QemuRunInput 和 UbootRunInput 只包含 runner 实际需要的字段(process_context、artifacts、arch、debug),而非整个 Tool。

向后兼容: Tool 上的旧 public 方法全部保留为 compatibility wrapper,现有调用方无需改动。

U-Boot 运行镜像语义改进: 从 ensure_runtime_bin() + require_bin() 改为 runtime_image(),允许 ELF-only 的 U-Boot artifact 直接进入运行路径,同时在缺失镜像时返回更明确的错误信息。

QEMU to_bin 语义明确化: to_bin=true 时在 module-level run_qemu_with_config 入口检查 BIN artifact 是否存在,将准备 BIN 的责任推给调用方。Tool::run_qemu wrapper 仍然调用 ensure_runtime_bin() 以保持兼容。

细节确认

  • process_context.workdir() 与旧的 manifest_dir() 值相同(Tool::process_context() 用 self.manifest_dir.clone() 作为 workdir),TFTP 调用语义无变化。
  • legacy_context.rs 中的 bridge helper 标注清晰,方便后续清理。
  • resolve_qemu_config_path 工具函数删除,只保留 resolve_qemu_config_path_in_dir,减少不必要的间接层。

测试覆盖

新增三个针对性测试:

  1. run_qemu_with_to_bin_rejects_elf_only_artifacts - 验证 QEMU to_bin=true 在缺少 BIN 时的错误路径
  2. uboot_run_input_accepts_elf_only_artifacts - 验证 U-Boot 输入结构接受 ELF-only artifact
  3. run_uboot_rejects_missing_runtime_image - 验证 U-Boot 缺失 runtime image 时的错误信息

既有测试也已同步更新。

小建议(非阻塞)

  • UbootRunInput 和 QemuRunInput 的字段可见性不一致:QemuRunInput 字段用了 pub(crate),UbootRunInput 字段是私有的。从一致性角度可以考虑统一。
  • 未来移除兼容 wrapper 时,建议同时删除 run_board_with_build_config 这个 private 中间方法。

结论

代码质量好,方向正确,测试充分。Approve。

Powered by mimo-v2.5-pro

Make QemuRunInput match the U-Boot runner input shape by keeping its fields private and routing construction through a crate-internal constructor.

Tool::run_qemu and QEMU tests now use QemuRunInput::new(), so runner modules expose the input type without exposing mutable field-level assembly details.

Validation: cargo fmt --all -- --check; cargo test -p ostool run_qemu_with_to_bin_rejects_elf_only_artifacts -- --nocapture; cargo clippy -p ostool --all-targets --all-features -- -D warnings; git diff --check.

Co-authored-by: OpenAI Codex <codex@openai.com>
Signed-off-by: Niu Zhihong <zhihong@nzhnb.com>
@MRNIU
Copy link
Copy Markdown
Contributor Author

MRNIU commented May 29, 2026

UbootRunInput 和 QemuRunInput 一致性问题已修复

Copy link
Copy Markdown

@mai-team-app mai-team-app Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review: refactor(tool) - 拆分运行入口与 Tool facade

总体评价

这是一个结构清晰的重构 PR。将 QEMU、U-Boot、board 和 TFTP 的 runner 逻辑从 impl Tool 提取为模块级函数,引入 QemuRunInput / UbootRunInput 等窄输入结构,显著降低了 runner 模块对完整 Tool 的耦合度。兼容 wrapper 保留完整且标注了移除时机,方便后续逐步迁移。

设计亮点

  • 输入边界清晰: QemuRunInputUbootRunInput 只封装 runner 实际需要的字段(process_contextartifactsarchdebug),字段私有 + new() 构造函数,控制组装入口。
  • 向后兼容完整: Tool 上的旧 public 方法全部保留为 compatibility wrapper,现有调用方无需立即修改。
  • U-Boot 运行镜像语义改进: 从 ensure_runtime_bin() + require_bin() 改为 runtime_image(),允许 ELF-only artifact 直接进入 U-Boot 运行路径,缺失镜像时错误信息也更明确("U-Boot runner requires a prepared runtime image")。
  • QEMU to_bin 检查前置: run_qemu_with_config 在模块级入口检查 BIN artifact 存在性,而 Tool::run_qemu wrapper 仍调用 ensure_runtime_bin() 保持兼容。
  • Runner / QemuRunner 去生命周期: 原先 QemuRunner<'a>Runner<'a, B> 持有 &'a mut Tool,重构后分别持有 QemuRunInput / UbootRunInput(owned),不再引入生命周期约束。

行为变更确认

  • Tool::run_uboot() 不再自动调用 ensure_runtime_bin(),BIN 准备责任推到调用方。对于通过 board 路径进入的场景,prepare_runtime_artifacts() 已覆盖该步骤;直接调用 run_uboot() 时 ELF 也可作为 runtime image 使用。变更合理。
  • manifest_dir() accessor 移除后,TFTP 通过 input.process_context.workdir() 获取同等路径(ProcessContext::workdir() 来源于 Tool::manifest_dir),语义一致。
  • resolve_qemu_config_path wrapper 删除,只保留 resolve_qemu_config_path_in_dir,减少不必要的间接层。

测试覆盖

新增三个针对性测试:

  1. run_qemu_with_to_bin_rejects_elf_only_artifacts — 验证 QEMU to_bin=true 在缺少 BIN 时的错误路径
  2. uboot_run_input_accepts_elf_only_artifacts — 验证 U-Boot 输入结构接受 ELF-only artifact
  3. run_uboot_rejects_missing_runtime_image — 验证 U-Boot 缺失 runtime image 时的错误信息

既有测试也已同步更新(QemuRunner 使用 qemu_input() helper 构造输入,resolve_qemu_config_path 调用改为 resolve_qemu_config_path_in_dir)。

小建议(非阻塞)

  • legacy_context.rs 文档注释风格:///// 混用在同一类 helper 上。建议统一为 ///(doc comment)以保持 rustdoc 一致性。
  • 未来移除兼容 wrapper 时,建议同时清理 run_board_with_build_config 这个 private 中间方法。

结论

重构方向正确,兼容性处理得当,测试覆盖充分,代码质量好。Approve。

Powered by mimo-v2.5-pro

@MRNIU
Copy link
Copy Markdown
Contributor Author

MRNIU commented May 29, 2026

Code Review: refactor(tool) - 拆分运行入口与 Tool facade

总体评价

这是一个结构清晰的重构 PR。将 QEMU、U-Boot、board 和 TFTP 的 runner 逻辑从 impl Tool 提取为模块级函数,引入 QemuRunInput / UbootRunInput 等窄输入结构,显著降低了 runner 模块对完整 Tool 的耦合度。兼容 wrapper 保留完整且标注了移除时机,方便后续逐步迁移。

设计亮点

  • 输入边界清晰: QemuRunInputUbootRunInput 只封装 runner 实际需要的字段(process_contextartifactsarchdebug),字段私有 + new() 构造函数,控制组装入口。
  • 向后兼容完整: Tool 上的旧 public 方法全部保留为 compatibility wrapper,现有调用方无需立即修改。
  • U-Boot 运行镜像语义改进: 从 ensure_runtime_bin() + require_bin() 改为 runtime_image(),允许 ELF-only artifact 直接进入 U-Boot 运行路径,缺失镜像时错误信息也更明确("U-Boot runner requires a prepared runtime image")。
  • QEMU to_bin 检查前置: run_qemu_with_config 在模块级入口检查 BIN artifact 存在性,而 Tool::run_qemu wrapper 仍调用 ensure_runtime_bin() 保持兼容。
  • Runner / QemuRunner 去生命周期: 原先 QemuRunner<'a>Runner<'a, B> 持有 &'a mut Tool,重构后分别持有 QemuRunInput / UbootRunInput(owned),不再引入生命周期约束。

行为变更确认

  • Tool::run_uboot() 不再自动调用 ensure_runtime_bin(),BIN 准备责任推到调用方。对于通过 board 路径进入的场景,prepare_runtime_artifacts() 已覆盖该步骤;直接调用 run_uboot() 时 ELF 也可作为 runtime image 使用。变更合理。
  • manifest_dir() accessor 移除后,TFTP 通过 input.process_context.workdir() 获取同等路径(ProcessContext::workdir() 来源于 Tool::manifest_dir),语义一致。
  • resolve_qemu_config_path wrapper 删除,只保留 resolve_qemu_config_path_in_dir,减少不必要的间接层。

测试覆盖

新增三个针对性测试:

  1. run_qemu_with_to_bin_rejects_elf_only_artifacts — 验证 QEMU to_bin=true 在缺少 BIN 时的错误路径
  2. uboot_run_input_accepts_elf_only_artifacts — 验证 U-Boot 输入结构接受 ELF-only artifact
  3. run_uboot_rejects_missing_runtime_image — 验证 U-Boot 缺失 runtime image 时的错误信息

既有测试也已同步更新(QemuRunner 使用 qemu_input() helper 构造输入,resolve_qemu_config_path 调用改为 resolve_qemu_config_path_in_dir)。

小建议(非阻塞)

  • legacy_context.rs 文档注释风格:///// 混用在同一类 helper 上。建议统一为 ///(doc comment)以保持 rustdoc 一致性。
  • 未来移除兼容 wrapper 时,建议同时清理 run_board_with_build_config 这个 private 中间方法。

结论

重构方向正确,兼容性处理得当,测试覆盖充分,代码质量好。Approve。

Powered by mimo-v2.5-pro

legacy_context.rs 文档后面会删掉,注释风格问题暂不修改

@MRNIU
Copy link
Copy Markdown
Contributor Author

MRNIU commented May 29, 2026

@ZR233 hi,麻烦 review

@ZR233 ZR233 merged commit 663ee70 into drivercraft:main Jun 1, 2026
1 check passed
@MRNIU MRNIU deleted the refactor/tool-runner-input-boundaries branch June 1, 2026 02:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants