[tools][cmake] fix type handling when generating CMake targets#11149
[tools][cmake] fix type handling when generating CMake targets#11149Rbb666 merged 1 commit intoRT-Thread:masterfrom
Conversation
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
There was a problem hiding this comment.
Pull request overview
This PR fixes a TypeError that occurs when generating CMake projects from SCons build files using scons --target=cmake. The issue affected multiple BSPs (stm32f407-atk-explorer, stm32h743-armfly-v7, imxrt1064-nxp-evk, gd32450z-eval, qemu-vexpress-a9) because the CMake generator assumed all CPPDEFINES were strings, but SCons can produce tuple-based macro definitions like ('STM32F407xx',).
Changes:
- Enhanced CPPDEFINES processing in CMake generator to handle both string and tuple types from SCons
- Added type checking to support single-element tuples (e.g.,
('FOO',)), two-element tuples with values (e.g.,('FOO', 1)), and two-element tuples with None (e.g.,('FOO', None)) - Maintained backward compatibility with existing string-based macro definitions
[tools][cmake] fix type handling when generating CMake targets [Root Cause] Unexpected macro definition types during `scons --target=cmake` could trigger a TypeError and abort CMakeLists.txt generation. [Solution] Add support for handling different macro definition types. [Affect Area] cmake.py [Test Suggestion] Run `scons --target=cmake` and verify CMakeLists.txt is generated successfully with different macro definition formats.
bd82300 to
dc8065d
Compare



拉取/合并请求描述:(PR description)
为什么提交这份PR (why to submit this PR)
在 ubuntu 使用
scons --target=cmake生成 CMake 工程时,当前 CMake 生成器在处理CPPDEFINES会抛出异常。试了几个 bsp,基本上都有这个问题
报错信息一致
你的解决方案是什么 (what is your solution)
CMake 生成脚本在拼接宏定义时,遇到了一个「tuple(元组)」类型的宏,而不是字符串,导致 Python 报错。
增加调试输出查看 i 的类型和值
得到输出
当前 CMake 生成器在处理
CPPDEFINES时默认假设宏定义为字符串类型,并直接进行字符串拼接生成-D参数。当遇到 SCons 规范化后的 tuple 形式宏定义(例如
('STM32F407xx',))时,由于未进行类型判断,导致在字符串拼接阶段触发 TypeError,从而中断 CMake 工程的生成流程。需要说明的是,该问题并非 BSP 或用户宏定义写法错误,而是 CMake 生成器对
CPPDEFINES的类型假设过于严格。本 PR 对 CMake 生成器中 CPPDEFINES 的处理逻辑进行了完善,使其能够正确兼容 SCons 中合法的宏定义表示形式:
("FOO",))按无值宏处理,生成-DFOO(name, None)形式的宏同样生成-Dname(name, value)形式的宏生成-Dname=value保持原有字符串形式宏定义的处理方式不变
该修改使 CMake 工程生成行为与 SCons 的宏定义语义保持一致,提高了工具链的健壮性和兼容性。
影响范围
请提供验证的bsp和config (provide the config and bsp)
在这两个 bsp 中验证,
执行
scons --target=cmake后,不再报异常,可以成功使用 cmake 构建工具。当前拉取/合并请求的状态 Intent for your PR
必须选择一项 Choose one (Mandatory):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up