Skip to content

Commit 97e1f01

Browse files
xym-eeRbb666
authored andcommitted
cmake: make CPPDEFINES handling robust for SCons-generated values
[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.
1 parent 5d1f199 commit 97e1f01

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

tools/targets/cmake.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* 2024-11-18 kaidegit fix processing groups with similar name
1919
* 2025-02-22 kaidegit fix missing some flags added in Sconscript
2020
* 2025-02-24 kaidegit remove some code that is unnecessary but takes time, get them from env
21+
* 2026-01-22 xym-ee Fix handling of tuple-based CPPDEFINES from SCons in CMake project generation.
2122
"""
2223

2324
import os
@@ -187,7 +188,24 @@ def GenerateCFiles(env, project, project_name):
187188

188189
cm_file.write("ADD_DEFINITIONS(\n")
189190
for i in env['CPPDEFINES']:
190-
cm_file.write("\t-D" + i + "\n")
191+
# Handle CPPDEFINES from SCons (str / tuple)
192+
if isinstance(i, tuple):
193+
# e.g. ('STM32F407xx',)
194+
if len(i) == 1:
195+
cm_file.write("\t-D" + str(i[0]) + "\n")
196+
# e.g. ('FOO', None)
197+
elif len(i) == 2:
198+
if i[1] is None:
199+
cm_file.write("\t-D" + str(i[0]) + "\n")
200+
# e.g. ('FOO', 1)
201+
else:
202+
cm_file.write("\t-D{}={}\n".format(i[0], i[1]))
203+
else:
204+
# unexpected form, fallback to name only
205+
cm_file.write("\t-D" + str(i[0]) + "\n")
206+
else:
207+
# generic macro (commonly a string), ensure robust string conversion
208+
cm_file.write("\t-D" + str(i) + "\n")
191209
cm_file.write(")\n\n")
192210

193211
libgroups = []

0 commit comments

Comments
 (0)