Skip to content

Fix Rust build example configuration handling#11584

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

Fix Rust build example configuration handling#11584
yanhu7150-tech wants to merge 3 commits 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: bsp_rt-spark

Reviewers: @Rbb666 @kurisaW

Changed Files (Click to expand)
  • bsp/stm32/stm32f407-rt-spark/.ci/attachconfig/ci.attachconfig.yml

🏷️ Tag: bsp_stm32

Reviewers: @Liang1795 @hamburger-os @wdfk-prog

Changed Files (Click to expand)
  • bsp/stm32/stm32f407-rt-spark/.ci/attachconfig/ci.attachconfig.yml

🏷️ Tag: components

Reviewers: @Maihuanyi

Changed Files (Click to expand)
  • components/rust/core/SConscript
  • components/rust/core/src/api/queue.rs
  • components/rust/core/src/bindings/mod.rs
  • components/rust/core/src/lib.rs
  • 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-09 22:57 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.

@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

hpmicro-hpm5301evklite

  • ILM: .fast +32 B (+0.1%, 38,096 B / 131,072 B, total: 29% used)
  • XPI0: .fast +32 B, .text +56 B (+0.1%, 124,880 B / 1,048,576 B, total: 12% used)

nuvoton-m487

@CYFS3

CYFS3 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

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

@yanhu7150-tech

Copy link
Copy Markdown
Author

这个有ci可以看吗?
可以的,当前 PR 的 GitHub Checks 已经通过,页面状态显示 All checks have passed。
CI链接:https://github.com/RT-Thread/rt-thread/pull/11584/checks
当前结果:

  • 103 successful checks
  • 2 skipped checks
  • No failed checks

@Rbb666

Rbb666 commented Jul 9, 2026

Copy link
Copy Markdown
Member

这个有ci可以看吗?
可以的,当前 PR 的 GitHub Checks 已经通过,页面状态显示 All checks have passed。
CI链接:https://github.com/RT-Thread/rt-thread/pull/11584/checks
当前结果:

  • 103 successful checks
  • 2 skipped checks
  • No failed checks

这个要给具体的BSP添加ci-attach功能,让gihub的ci检测到然后把rust组件添加到构建系统中进行编译看护

@github-actions github-actions Bot added BSP: STM32 BSP related with ST/STM32 BSP action github action yml imporve labels Jul 9, 2026
@yanhu7150-tech yanhu7150-tech force-pushed the bugfix-rust-build-target-artifact branch from d3bd2a3 to 22fbb3f Compare July 9, 2026 03:59
@yanhu7150-tech

yanhu7150-tech commented Jul 9, 2026

Copy link
Copy Markdown
Author

这个有ci可以看吗?
可以的,当前 PR 的 GitHub 检查已经通过,页面状态显示所有检查都已通过。
CI链接:https://github.com/RT-Thread/rt-thread/pull/11584/checks
当前结果:

  • 103 次检查成功
  • 跳过 2 次检查
  • 无检查失败

这个要给具体的BSP添加ci-attach功能,让gihub的ci检测到然后把rust添加组件到构建系统中进行编译看护

已经按照您的建议补充 Rust CI 编译看护。
本次更新内容:

  1. bsp/stm32/stm32f407-rt-spark 增加 Rust ci-attach 配置,启用:
    • CONFIG_RT_USING_RUST=y
    • CONFIG_RT_RUST_CORE=y
    • CONFIG_RT_USING_RUST_EXAMPLES=y
    • CONFIG_RT_RUST_BUILD_COMPONENTS=y
    • CONFIG_RUST_LOG_COMPONENT=y
  2. 在 BSP static build workflow 中增加 Rust toolchain 安装步骤,并安装 thumbv7em-none-eabihf target,使该 BSP 的 CI 能实际编译 Rust core 和 component example,而不是只触发 SConscript gate 后跳过。
    已重新 push,等待 GitHub CI 重新运行。

@yanhu7150-tech

Copy link
Copy Markdown
Author

这个有ci可以看吗?
可以的,当前 PR 的 GitHub 检查已经通过,页面状态显示所有检查都已通过。
CI链接:https://github.com/RT-Thread/rt-thread/pull/11584/checks
当前结果:

  • 103 次检查成功
  • 跳过 2 次检查
  • 无检查失败

这个要给具体的BSP添加ci-attach功能,让gihub的ci检测到然后把rust添加组件到构建系统中进行编译看护

这个失败看起来发生在 MemBrowse workflow 的环境依赖安装阶段:Install env tool and Python build deps
该步骤执行 sudo apt-get update、安装 Python/wget/build-essential,并下载 RT-Thread env 安装脚本,最后以 exit code 8 退出。
失败 job 是 renesas-ra2l1 的 MemBrowse 分析,还没有进入 toolchain 安装、Build 或 MemBrowse analysis 阶段;也不是本 PR 新增 Rust ci-attach 的 stm32f407-rt-spark 构建路径。因此目前看起来不像本 PR 的 Rust 构建改动导致。
麻烦维护者方便时帮忙 rerun 该 failed job,我也会继续观察 CI 结果。感谢您们~

@Rbb666 Rbb666 closed this Jul 9, 2026
@Rbb666 Rbb666 reopened this Jul 9, 2026
Add a rust attach config to the stm32f407-rt-spark BSP so CI enables the Rust core and component-log example on a Cortex-M4 target. The Rust toolchain setup is scoped to this attach configuration so unrelated BSP build jobs are not affected.
@yanhu7150-tech yanhu7150-tech force-pushed the bugfix-rust-build-target-artifact branch from 22fbb3f to f89e400 Compare July 9, 2026 09:24
@yanhu7150-tech

Copy link
Copy Markdown
Author

我已经通过最小改动修复了 RT-Thread Online Packages (STM32F407 RT-Spark) 中与 Rust attach 相关的失败,改动范围仅限于 Rust core 和 STM32F407 RT-Spark 的 rust attach 配置。
目前剩余的 failed jobs 看起来是与本 PR 无关的 BSP/vendor SDK 或 HAL/CMSIS 包问题,例如其他 BSP 组中缺少厂商头文件。为了避免将本 PR 的范围扩大到 Rust 构建改动之外,我没有把这些无关修复包含进来。
请问能否帮忙确认一下:这些剩余失败是否属于当前 CI 的 baseline 问题,还是仍然存在某个与本 PR 相关的 required check 需要我继续处理?

@yanhu7150-tech

Copy link
Copy Markdown
Author

@CYFS3 @Rbb666 @grissiom @rogerz 能请您们通过一下工作流吗?谢谢啦。我是一个新手,所以如果任然出现错误的话,可能还会麻烦您们。谢谢您们啦~

@Rbb666

Rbb666 commented Jul 9, 2026

Copy link
Copy Markdown
Member

@CYFS3 @Rbb666 @grissiom @rogerz 能请您们通过一下工作流吗?谢谢啦。我是一个新手,所以如果任然出现错误的话,可能还会麻烦您们。谢谢您们啦~

已开启,因为是第一次 pr 所以不会自动开,下一次就好啦

@yanhu7150-tech

Copy link
Copy Markdown
Author

@CYFS3 @Rbb666 @grissiom @rogerz能请你们通过一下工作流程吗?谢谢啦。我是新手,所以如果任然出现错误的话,可能还会麻烦你们。谢谢你们啦~

已开启,因为是第一次所以不会自动开,下一次就好啦

好的,谢谢啦~~

@yanhu7150-tech

yanhu7150-tech commented Jul 9, 2026

Copy link
Copy Markdown
Author

已根据 CI 结果进一步修复 Rust attach 相关构建问题。
本次补充修改范围保持最小,仅涉及 Rust core 和 bsp/stm32/stm32f407-rt-spark 的 rust attach 配置,主要处理:

  1. 删除不存在的 rt_safe_malloc / rt_safe_free 悬空 re-export;
  2. 修复 rt_mq_create 等 FFI 调用中的 rt_size_t 类型转换问题;
  3. 删除未使用的 alloc_error_handler feature gate;
  4. 由于 Rust core 仍使用 nightly-only 的 linkage feature,将该 rust attach 构建收窄为 nightly toolchain;
  5. 在该 rust attach block 内安装 Python toml 模块,避免影响其他 BSP 或共享 workflow。
    当前 CI 已重新通过。谢谢啦~

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

Labels

action github action yml imporve BSP: STM32 BSP related with ST/STM32 BSP Component

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants