Skip to content

fix dist-strip compile_commands path parsing#11551

Open
yanhu7150-tech wants to merge 1 commit into
RT-Thread:masterfrom
yanhu7150-tech:bugfix-dist-strip-compile-commands
Open

fix dist-strip compile_commands path parsing#11551
yanhu7150-tech wants to merge 1 commit into
RT-Thread:masterfrom
yanhu7150-tech:bugfix-dist-strip-compile-commands

Conversation

@yanhu7150-tech

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

Copy link
Copy Markdown

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

本 PR 用于修复 RT-Thread tools/--dist-strip 依赖 compile_commands.json 推导最小发布包保留路径时的路径解析问题,以及解析结果为空时仍继续执行源码清理的问题。

该问题属于普通构建/发布工具的软件逻辑缺陷

--dist-strip 的目标是根据 compile_commands.json 中记录的实际编译路径,生成只保留必要源码的最小发布包。当前实现对 compile database 的解析不完整,在部分标准输入格式和跨平台路径场景下,可能漏掉实际需要保留的源码目录或 include 目录,进而影响最小发布包的正确性。

主要问题如下:

  1. tools/compile_commands.py 只解析 command 字符串,未支持 compile database 标准允许的 arguments 数组;
  2. 原实现使用 command.split() 解析编译命令,无法正确处理 -I path 分离写法、带引号路径以及 Windows 反斜杠路径;
  3. 相对 file 路径和相对 include 路径没有基于 compile database entry 的 directory 字段进行归一化,导致标准格式的 compile_commands.json 中有效源码路径和 include 路径可能被漏掉;
  4. tools/mkdist.pyMkDist_Strip()compile_commands.json 缺失、解析失败或解析结果为空时,仍继续执行源码清理,可能删除 dist 包中实际需要保留的源码文件。

修复前,使用如下标准 compile database entry 时,get_minimal_dist_paths() 无法正确识别相对源码目录和 include 目录:

[
  {
    "directory": "/tmp/rt-thread",
    "arguments": [
      "gcc",
      "-I",
      "components/foo/include",
      "-c",
      "components/foo/foo.c"
    ],
    "file": "components/foo/foo.c"
  }
]

修复前实际行为:

returned_paths: []
has_src_dir: False
has_inc_dir: False

这会导致 --dist-strip 后续清理逻辑缺少可靠的保留路径依据。

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

本 PR 的解决方案主要包括以下几个部分:

  1. 增强 compile_commands.json entry 解析逻辑

    • 优先支持 compile database 标准中的 arguments 数组;
    • command 字符串使用 shlex.split(..., posix=False) 解析,避免 Windows 反斜杠路径被错误转义;
    • 支持常见 include 参数形式:
      • -Ifoo
      • -I foo
      • /Ifoo
      • /I foo
      • -isystem path
      • -iquote path
      • -idirafter path
    • 对非 POSIX shlex 解析后保留的外层引号进行必要剥离,保证带引号路径能够继续参与路径归一化。
  2. 增强路径归一化逻辑

    • 将相对 file 路径基于 compile database entry 的 directory 字段归一化;
    • 将相对 include 路径基于 entry 的 directory 字段归一化;
    • 使用规范化后的路径判断其是否位于 RT-Thread 根目录下,避免简单字符串前缀判断带来的误判;
    • 对异常路径场景进行保护,避免路径比较时抛出未处理异常。
  3. 修复 MkDist_Strip() 的空结果处理

    • compile_commands.json 缺失、解析失败或没有解析到任何使用路径时,跳过源码清理;
    • 避免在路径信息不足时删除可能仍然需要保留的源码文件;
    • 保持完整 dist 产物可用,避免生成被错误裁剪的最小发布包。
  4. 新增回归测试

    新增 tools/testcases/test_compile_commands_paths.py,覆盖以下场景:

    • arguments 数组和相对路径解析;
    • used_paths 为空时,MkDist_Strip() 不应删除源码;
    • Windows command 字符串中的反斜杠 include 路径应被正确保留。

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

  • BSP:

本 PR 修改的是 tools/ 下的构建/发布辅助脚本,不依赖具体 BSP 或真实开发板。

Host-side verification was used for this tools-only change.

  • .config:

未修改 .config

本 PR 不涉及 BSP 配置项变更。

  • action:

Fork branch:

https://github.com/yanhu7150-tech/rt-thread/tree/bugfix-dist-strip-compile-commands

Action page:

https://github.com/yanhu7150-tech/rt-thread/actions?query=branch%3Abugfix-dist-strip-compile-commands

本地验证环境:

OS: Windows
Python: 3.10.11
SCons: 4.10.1

本地验证命令:

python -m py_compile tools\compile_commands.py tools\mkdist.py tools\testcases\test_compile_commands_paths.py
python tools\testcases\test_compile_commands_paths.py
echo "test_exit_code=$LASTEXITCODE"

验证结果:

PASSED: arguments and relative paths are handled
PASSED: MkDist_Strip keeps sources when used_paths is empty
PASSED: Windows include path is preserved
test_exit_code=0

变更范围:

tools/compile_commands.py
tools/mkdist.py
tools/testcases/test_compile_commands_paths.py

提交分支:

bugfix-dist-strip-compile-commands

提交信息:

fix dist strip compile command path parsing

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

  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

  • 已经仔细查看过代码改动的对比 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 代码规范

@github-actions

github-actions Bot commented Jul 3, 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-dist-strip-compile-commands
  • 设置PR number为 \ Set the PR number to:11551
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 bugfix-dist-strip-compile-commands 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the bugfix-dist-strip-compile-commands 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 added the tools label Jul 3, 2026
@yanhu7150-tech yanhu7150-tech changed the title [tools] fix dist-strip compile_commands path parsing fix dist-strip compile_commands path parsing Jul 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant