Skip to content

Fix Rust build example configuration handling#11584

Open
yanhu7150-tech wants to merge 1 commit into
RT-Thread:masterfrom
yanhu7150-tech:bugfix-rust-build-target-artifact
Open

Fix Rust build example configuration handling#11584
yanhu7150-tech wants to merge 1 commit into
RT-Thread:masterfrom
yanhu7150-tech:bugfix-rust-build-target-artifact

Conversation

@yanhu7150-tech

@yanhu7150-tech yanhu7150-tech commented Jul 7, 2026

Copy link
Copy Markdown

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

本 PR 修复 RT-Thread Rust 构建脚本中多处配置处理不一致的问题,主要涉及 Rust core、Rust application examples、Rust component examples、Rust module examples 的构建入口、Cargo 产物识别、Kconfig 选项门控、Cargo feature 传递、Rust target 推断、Cargo 失败诊断以及 SCons 链接参数生成。
原有实现中,部分 Rust 构建路径存在 Kconfig 配置、Cargo 实际产物和 SCons 链接设置不一致的问题。在特定配置组合下,可能导致 Rust 示例被错误跳过、使用错误的链接库名、向 Cargo 传入 manifest 未声明的 feature、在 Rust artifact 未生成时仍加入 SCons group,或者在缺少底层系统能力时仍构建相关 Rust example。这些问题会影响 Rust 构建系统在不同 BSP、不同 Rust target 和不同 example 配置组合下的稳定性、可诊断性和配置一致性。
本 PR 主要解决以下问题:

  1. Cargo 产物名与 SCons 链接库名可能不一致
    原有部分构建脚本会根据固定名称推断 Rust staticlib 的链接名,而不是根据 Cargo 实际生成的 artifact 反推出 SCons LIBS 中应使用的库名。当 Cargo package name、lib name 或实际 artifact 名称和脚本假设不一致时,可能出现 Cargo 构建成功,但 SCons 链接阶段使用错误 -lxxx 的问题。
  2. Rust target 推断覆盖不足
    原有 target 检测逻辑对部分 RISC-V fallback 和 Cortex-M 变体覆盖不足。例如 Cortex-M0、Cortex-M0+、Cortex-M23、Cortex-M85 等目标可能被推断为不合适的 Rust target,导致 Cargo target 与实际 MCU 架构不匹配。
  3. Rust core 构建开关与 SConscript 行为不完全一致
    RT_RUST_CORE 是 Rust core library 的配置开关,但原有 components/rust/core/SConscript 对该开关的约束不完整。在 Rust core staticlib 未成功生成、Rust toolchain 不存在、Rust target 未安装或 target 检测失败时,仍可能把 rust_cmd.c 加入 SCons group,导致后续链接阶段进入无效链接路径或产生符号风险。
  4. Rust example 的 Kconfig 门控与实际构建入口不一致
    部分 example 的 SConscript 或 build helper 没有严格遵守 Kconfig 语义。例如 module example 的子开关未被正确用于选择模块示例;component example 的总开关 RT_RUST_BUILD_COMPONENTS 曾被 RUST_LOG_COMPONENT 子开关错误门控;RT_RUST_BUILD_ALL_EXAMPLES 可能绕过部分 example 所需的底层依赖检查;loadlib example 依赖 RT_USING_MODULE,但原有 build-all 路径可能仍尝试构建;fs example 会引用 POSIX 文件 API,但原有逻辑未确认 DFS/POSIX 文件层是否可用。
  5. Cargo feature 传递不够精确
    部分 Rust component/application 的 feature 传递逻辑不完整。例如 RUST_LOG_COMPONENT 对应的 enable-log feature 需要正确传递给 component registry;fs application 使用日志组件时需要补齐对应 feature;component 构建时不应把 manifest 未声明的 feature 传给 Cargo,否则 Cargo 会因为 unknown feature 失败。
  6. Cargo 失败诊断不够清晰
    原有脚本在部分 Cargo 失败或 Cargo 不存在的场景下,错误输出不够完整,不利于用户定位 Rust toolchain、Rust target 或 Cargo manifest 问题。
    综上,本 PR 主要修复 Rust 构建系统中的普通软件缺陷,使 Rust core、Rust application example、Rust component example 和 Rust module example 的构建行为更加符合 Kconfig 配置语义,并提高 Cargo 构建、artifact 查找和 SCons 链接过程的一致性与可诊断性。

你的解决方案是什么 (what is your solution)

本 PR 对 Rust 构建脚本做了以下修复:

  1. 根据 Cargo 实际产物反推链接库名
    在 Rust core、component registry、user application 等构建路径中,根据实际生成的 staticlib artifact 反推链接库名,避免固定假设 libxxx.a 名称导致 SCons LIBS 与真实 Cargo 产物不一致。
  2. 完善 Rust target 检测
    改进 RISC-V fallback 逻辑,根据 -march / -mabi 等编译参数推断更合适的 Rust target。同时补充 Cortex-M 系列目标映射,包括:
    • Cortex-M0 / Cortex-M0+ -> thumbv6m-none-eabi
    • Cortex-M23 -> thumbv8m.base-none-eabi
    • Cortex-M85 -> thumbv8m.main-none-eabi
      这样可以避免部分 Cortex-M 目标被错误映射到不合适的 Rust target。
  3. 修正 Rust core 构建门控
    components/rust/core/SConscript 现在会正确遵守 RT_RUST_CORE 配置。当 Rust toolchain 不存在、target 检测失败、target 未安装或 Rust core staticlib 未成功生成时,不再把 rust_cmd.c 加入 SCons group,避免在 Rust core artifact 不存在时继续进入无效链接路径。
  4. 修正 Rust module example 的构建选择
    module example 的构建选择现在会遵守对应 Kconfig 子开关,例如 RT_RUST_MODULE_SIMPLE_MODULE。这样启用 Rust module example 总开关时,不会忽略具体 module example 的子配置。
  5. 修正 Rust component example 的入口门控
    RT_RUST_BUILD_COMPONENTS 是 component example 的总开关,RUST_LOG_COMPONENT 只是 log component 自动初始化相关子开关。修复后,RT_RUST_BUILD_COMPONENTS=y 时,即使 RUST_LOG_COMPONENT=n,component example 仍可正常进入构建流程;RUST_LOG_COMPONENT 只继续影响对应 Cargo feature,而不再错误地控制整个 component example 是否构建。
  6. 修正 build-all 路径下的 example 依赖检查
    对需要底层系统能力的 Rust application example 增加构建前依赖判断:
    • loadlib 依赖 RT_USING_MODULE。未启用 module 支持时,即使 RT_RUST_BUILD_ALL_EXAMPLES=y,也不会构建 loadlib
    • fs 依赖 POSIX 文件 API。只有满足以下任一条件时才构建:
      • RT_USING_POSIX_FS=y
      • RT_USING_DFS=y && DFS_USING_POSIX=y
      • RT_USING_DFS=y && RT_USING_DFS_V2=y
        这样可以避免 Rust application staticlib 通过 --whole-archive 被强制链接后引用缺失的底层 C 符号。
  7. 改进 Cargo feature 处理
    本 PR 对 component/application 的 Cargo feature 处理进行了收敛和校验:
    • 为 component registry 补充 RUST_LOG_COMPONENT -> enable-log 的 feature 映射;
    • 对 component manifest 未声明的 feature 进行过滤,避免向 Cargo 传入 unknown feature;
    • fs application 自动补齐日志相关 feature,保证其源码、Cargo manifest 和构建命令保持一致。
  8. 修正 module example 的 rt-rust 依赖路径
    修复 components/rust/examples/modules/example_lib/Cargo.tomlrt-rust 依赖路径,使其指向实际的 Rust core crate。
  9. 改进 Cargo 失败输出
    在 Cargo 执行失败或 Cargo 不存在时,输出更明确的错误信息和 Cargo stdout/stderr,方便用户定位 toolchain、target 或 manifest 问题。
    整体上,本 PR 只修改 Rust 构建脚本、Rust example 构建入口和一个 Rust module example 的 Cargo 依赖路径,不修改 BSP、驱动、内核调度或运行时业务逻辑。

请提供验证的bsp和config (provide the config and bsp)

  • BSP:
    本 PR 修改范围为 Rust 构建脚本和 Rust example 构建配置处理,不针对特定 BSP 修改运行时代码,因此没有限定单一验证 BSP。
    本地验证主要覆盖以下 Rust 构建路径:
components/rust/core/SConscript
components/rust/examples/application/SConscript
components/rust/examples/component/SConscript
components/rust/examples/modules/SConscript
components/rust/tools/build_support.py
components/rust/tools/build_component.py
components/rust/tools/build_usrapp.py

验证方式为本地构建脚本回归验证和静态编译检查,覆盖 Rust core、application example、component example、module example 的配置组合、Cargo 命令生成、artifact 查找、LIBS/LIBPATH/LINKFLAGS 生成和失败路径处理。

  • .config:
    本 PR 未要求固定某一个 BSP 的 .config。验证时覆盖了以下 Rust 相关配置组合:
RT_USING_RUST=y
RT_RUST_CORE=y
RT_USING_RUST_EXAMPLES=y
RT_RUST_BUILD_APPLICATIONS=y
RT_RUST_BUILD_COMPONENTS=y
RT_RUST_BUILD_MODULES=y
RT_RUST_BUILD_ALL_EXAMPLES=y
RUST_LOG_COMPONENT=y / n
RT_RUST_MODULE_SIMPLE_MODULE=y / n
RT_USING_MODULE=y / n
RT_USING_DFS=y / n
DFS_USING_POSIX=y / n
RT_USING_DFS_V2=y / n
RT_USING_POSIX_FS=y / n

重点验证场景包括:

1. RT_RUST_CORE=n 时,Rust core 不应进入构建。
2. Rust toolchain 缺失、target 缺失、Cargo 构建失败时,不应把 rust_cmd.c 加入无效链接路径。
3. RT_RUST_BUILD_COMPONENTS=y 且 RUST_LOG_COMPONENT=n 时,component example 仍应构建。
4. RUST_LOG_COMPONENT=y 时,component registry 应获得 enable-log feature。
5. component manifest 未声明 smp feature 时,不应向 Cargo 传入 smp feature。
6. RT_RUST_BUILD_ALL_EXAMPLES=y 但 RT_USING_MODULE=n 时,不应构建 loadlib example。
7. RT_RUST_BUILD_ALL_EXAMPLES=y 但 DFS/POSIX 文件 API 不可用时,不应构建 fs example。
8. DFS/POSIX 文件 API 可用时,fs example 应正常构建。
9. RT_RUST_MODULE_SIMPLE_MODULE=n 时,simple module example 不应被构建。
10. RISC-V 和 Cortex-M target 推断应匹配对应架构配置。

本地验证结果如下:

Local Rust build-script regression checks:
Ran 101 tests
Result: OK
Python syntax checks for Rust build helper scripts:
components\rust\tools\build_support.py
components\rust\tools\build_usrapp.py
components\rust\tools\build_component.py

Result: OK

说明:上述回归验证用于检查 Rust 构建脚本行为,包括 SConscript mock、fake Cargo 命令捕获、Cargo manifest 检查、link flag 生成检查和失败路径检查。PR 中未提交额外大型测试文件,避免引入与本次修复无关的测试体积。

  • action:
    GitHub Actions 已通过。当前 PR 检查结果为 103 successful checks,2 skipped checks,页面状态显示 All checks have passed。
    ]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  • 将目标分支设置为 \ Set the target branch to:bugfix-rust-build-target-artifact
  • 设置PR number为 \ Set the PR number to:11584
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 bugfix-rust-build-target-artifact 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the bugfix-rust-build-target-artifact branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: @Maihuanyi

Changed Files (Click to expand)
  • components/rust/core/SConscript
  • components/rust/examples/application/SConscript
  • components/rust/examples/component/SConscript
  • components/rust/examples/modules/SConscript
  • components/rust/examples/modules/example_lib/Cargo.toml
  • components/rust/tools/build_component.py
  • components/rust/tools/build_support.py
  • components/rust/tools/build_usrapp.py

📊 Current Review Status (Last Updated: 2026-07-07 19:22 CST)


📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@CYFS3

CYFS3 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

这个有ci可以看护起来吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants