cmake.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. """
  2. * Copyright (c) 2006-2025 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-05-24 klivelinux first version
  9. * 2021-04-19 liukangcc add c++ support and libpath
  10. * 2021-06-25 Guozhanxin fix path issue
  11. * 2021-06-30 Guozhanxin add scons --target=cmake-armclang
  12. * 2022-03-16 liukangcc 通过 SCons生成 CMakefile.txt 使用相对路径
  13. * 2022-04-12 mysterywolf rtconfig.CROSS_TOOL->rtconfig.PLATFORM
  14. * 2022-04-29 SunJun8 默认开启生成编译数据库
  15. * 2024-03-18 wirano fix the issue of the missing link flags added in Sconscript
  16. * 2024-07-04 kaidegit Let cmake generator get more param from `rtconfig.py`
  17. * 2024-08-07 imi415 Updated CMake generator handles private macros, using OBJECT and INTERFACE libraries.
  18. * 2024-11-18 kaidegit fix processing groups with similar name
  19. * 2025-02-22 kaidegit fix missing some flags added in Sconscript
  20. * 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.
  22. """
  23. import os
  24. import sys
  25. import re
  26. import utils
  27. import rtconfig
  28. from utils import _make_path_relative
  29. from collections import defaultdict, Counter
  30. def GenerateCFiles(env, project, project_name):
  31. """
  32. Generate CMakeLists.txt files
  33. """
  34. PROJECT_NAME = project_name if project_name != "project" else "rtthread"
  35. tool_path_conv = defaultdict(lambda : {"name":"", "path": ""})
  36. tool_path_conv_helper = lambda tool: {"name": tool, "path": os.path.join(rtconfig.EXEC_PATH, tool).replace('\\', "/")}
  37. tool_path_conv["CMAKE_C_COMPILER"] = tool_path_conv_helper(rtconfig.CC)
  38. if 'CXX' in dir(rtconfig):
  39. tool_path_conv["CMAKE_CXX_COMPILER"] = tool_path_conv_helper(rtconfig.CXX)
  40. tool_path_conv["CMAKE_ASM_COMPILER"] = tool_path_conv_helper(rtconfig.AS)
  41. tool_path_conv["CMAKE_AR"] = tool_path_conv_helper(rtconfig.AR)
  42. tool_path_conv["CMAKE_LINKER"] = tool_path_conv_helper(rtconfig.LINK)
  43. if rtconfig.PLATFORM in ['gcc']:
  44. tool_path_conv["CMAKE_SIZE"] = tool_path_conv_helper(rtconfig.SIZE)
  45. tool_path_conv["CMAKE_OBJDUMP"] = tool_path_conv_helper(rtconfig.OBJDUMP)
  46. tool_path_conv["CMAKE_OBJCOPY"] = tool_path_conv_helper(rtconfig.OBJCPY)
  47. elif rtconfig.PLATFORM in ['armcc', 'armclang']:
  48. tool_path_conv["CMAKE_FROMELF"] = tool_path_conv_helper(rtconfig.FROMELF)
  49. CC = tool_path_conv["CMAKE_C_COMPILER"]["path"]
  50. CXX = tool_path_conv["CMAKE_CXX_COMPILER"]["path"]
  51. AS = tool_path_conv["CMAKE_ASM_COMPILER"]["path"]
  52. AR = tool_path_conv["CMAKE_AR"]["path"]
  53. LINK = tool_path_conv["CMAKE_LINKER"]["path"]
  54. SIZE = tool_path_conv["CMAKE_SIZE"]["path"]
  55. OBJDUMP = tool_path_conv["CMAKE_OBJDUMP"]["path"]
  56. OBJCOPY = tool_path_conv["CMAKE_OBJCOPY"]["path"]
  57. FROMELF = tool_path_conv["CMAKE_FROMELF"]["path"]
  58. CFLAGS = "".join(env['CFLAGS'])
  59. CFLAGS = CFLAGS.replace('\\', "/").replace('\"', "\\\"")
  60. if 'CXXFLAGS' in dir(rtconfig):
  61. cflag_str=''.join(env['CXXFLAGS'])
  62. CXXFLAGS = cflag_str.replace('\\', "/").replace('\"', "\\\"")
  63. else:
  64. CXXFLAGS = CFLAGS
  65. AFLAGS = env['ASFLAGS'].replace('\\', "/").replace('\"', "\\\"")
  66. LFLAGS = env['LINKFLAGS'].replace('\\', "/").replace('\"', "\\\"")
  67. POST_ACTION = rtconfig.POST_ACTION
  68. # replace the tool name with the cmake variable
  69. for cmake_var, each_tool in tool_path_conv.items():
  70. tool_name = each_tool['name']
  71. if tool_name == "": continue
  72. if "win32" in sys.platform:
  73. while f"{tool_name}.exe" in POST_ACTION: # find the tool with `.exe` suffix first
  74. POST_ACTION = POST_ACTION.replace(tool_name, "string_to_replace")
  75. while tool_name in POST_ACTION:
  76. POST_ACTION = POST_ACTION.replace(tool_name, "string_to_replace")
  77. while "string_to_replace" in POST_ACTION:
  78. POST_ACTION = POST_ACTION.replace("string_to_replace", f"${{{cmake_var}}}")
  79. # replace the `$TARGET` with `${CMAKE_PROJECT_NAME}.elf`
  80. while "$TARGET" in POST_ACTION:
  81. POST_ACTION = POST_ACTION.replace("$TARGET", "${CMAKE_PROJECT_NAME}.elf")
  82. # add COMMAAND before each command
  83. POST_ACTION = POST_ACTION.split('\n')
  84. POST_ACTION = [each_line.strip() for each_line in POST_ACTION]
  85. POST_ACTION = [f"\tCOMMAND {each_line}" for each_line in POST_ACTION if each_line != '']
  86. POST_ACTION = "\n".join(POST_ACTION)
  87. if "win32" in sys.platform:
  88. CC += ".exe"
  89. if CXX != '':
  90. CXX += ".exe"
  91. AS += ".exe"
  92. AR += ".exe"
  93. LINK += ".exe"
  94. if rtconfig.PLATFORM in ['gcc']:
  95. SIZE += ".exe"
  96. OBJDUMP += ".exe"
  97. OBJCOPY += ".exe"
  98. elif rtconfig.PLATFORM in ['armcc', 'armclang']:
  99. FROMELF += ".exe"
  100. if not os.path.exists(CC) or not os.path.exists(AS) or not os.path.exists(AR) or not os.path.exists(LINK):
  101. print("'Cannot found toolchain directory, please check RTT_CC and RTT_EXEC_PATH'")
  102. sys.exit(-1)
  103. with open("CMakeLists.txt", "w") as cm_file:
  104. cm_file.write("CMAKE_MINIMUM_REQUIRED(VERSION 3.10)\n\n")
  105. cm_file.write("SET(CMAKE_SYSTEM_NAME Generic)\n")
  106. cm_file.write("SET(CMAKE_SYSTEM_PROCESSOR " + rtconfig.CPU +")\n")
  107. cm_file.write("#SET(CMAKE_VERBOSE_MAKEFILE ON)\n\n")
  108. cm_file.write("SET(CMAKE_EXPORT_COMPILE_COMMANDS ON)\n\n")
  109. cm_file.write("SET(CMAKE_C_COMPILER \""+ CC + "\")\n")
  110. cm_file.write("SET(CMAKE_ASM_COMPILER \""+ AS + "\")\n")
  111. cm_file.write("SET(CMAKE_C_FLAGS \""+ CFLAGS + "\")\n")
  112. cm_file.write("SET(CMAKE_ASM_FLAGS \""+ AFLAGS + "\")\n")
  113. cm_file.write("SET(CMAKE_C_COMPILER_WORKS TRUE)\n\n")
  114. if CXX != '':
  115. cm_file.write("SET(CMAKE_CXX_COMPILER \""+ CXX + "\")\n")
  116. cm_file.write("SET(CMAKE_CXX_FLAGS \""+ CXXFLAGS + "\")\n")
  117. cm_file.write("SET(CMAKE_CXX_COMPILER_WORKS TRUE)\n\n")
  118. if rtconfig.PLATFORM in ['gcc']:
  119. cm_file.write("SET(CMAKE_OBJCOPY \""+ OBJCOPY + "\")\n")
  120. cm_file.write("SET(CMAKE_SIZE \""+ SIZE + "\")\n\n")
  121. elif rtconfig.PLATFORM in ['armcc', 'armclang']:
  122. cm_file.write("SET(CMAKE_FROMELF \""+ FROMELF + "\")\n\n")
  123. LINKER_FLAGS = ''
  124. LINKER_LIBS = ''
  125. if rtconfig.PLATFORM in ['gcc']:
  126. LINKER_FLAGS += '-T'
  127. elif rtconfig.PLATFORM in ['armcc', 'armclang']:
  128. LINKER_FLAGS += '--scatter'
  129. for group in project:
  130. if 'LIBPATH' in group.keys():
  131. for f in group['LIBPATH']:
  132. LINKER_LIBS += ' --userlibpath ' + f.replace("\\", "/")
  133. for group in project:
  134. if 'LIBS' in group.keys():
  135. for f in group['LIBS']:
  136. LINKER_LIBS += ' ' + f.replace("\\", "/") + '.lib'
  137. cm_file.write("SET(CMAKE_EXE_LINKER_FLAGS \""+ re.sub(LINKER_FLAGS + r'(\s*)', LINKER_FLAGS + r' ${CMAKE_SOURCE_DIR}/', LFLAGS) + LINKER_LIBS + "\")\n\n")
  138. # get the c/cpp standard version from compilation flags
  139. # not support the version with alphabet in `-std` param yet
  140. pattern = re.compile(r'-std=[\w+]+')
  141. c_standard = 11
  142. if '-std=' in CFLAGS:
  143. c_standard = re.search(pattern, CFLAGS).group(0)
  144. c_standard = "".join([each for each in c_standard if each.isdigit()])
  145. else:
  146. print(f"Cannot find the param of the c standard in build flag, set to default {c_standard}")
  147. cm_file.write(f"SET(CMAKE_C_STANDARD {c_standard})\n")
  148. if CXX != '':
  149. cpp_standard = 17
  150. if '-std=' in CXXFLAGS:
  151. cpp_standard = re.search(pattern, CXXFLAGS).group(0)
  152. cpp_standard = "".join([each for each in cpp_standard if each.isdigit()])
  153. else:
  154. print(f"Cannot find the param of the cpp standard in build flag, set to default {cpp_standard}")
  155. cm_file.write(f"SET(CMAKE_CXX_STANDARD {cpp_standard})\n")
  156. cm_file.write('\n')
  157. cm_file.write(f"PROJECT({PROJECT_NAME} C {'CXX' if CXX != '' else ''} ASM)\n")
  158. cm_file.write('\n')
  159. cm_file.write("INCLUDE_DIRECTORIES(\n")
  160. for i in env['CPPPATH']:
  161. # use relative path
  162. path = _make_path_relative(os.getcwd(), i)
  163. cm_file.write( "\t" + path.replace("\\", "/") + "\n")
  164. cm_file.write(")\n\n")
  165. cm_file.write("ADD_DEFINITIONS(\n")
  166. for i in env['CPPDEFINES']:
  167. # Handle CPPDEFINES from SCons (str / tuple)
  168. if isinstance(i, tuple):
  169. # e.g. ('STM32F407xx',)
  170. if len(i) == 1:
  171. cm_file.write("\t-D" + str(i[0]) + "\n")
  172. # e.g. ('FOO', None)
  173. elif len(i) == 2:
  174. if i[1] is None:
  175. cm_file.write("\t-D" + str(i[0]) + "\n")
  176. # e.g. ('FOO', 1)
  177. else:
  178. cm_file.write("\t-D{}={}\n".format(i[0], i[1]))
  179. else:
  180. # unexpected form, fallback to name only
  181. cm_file.write("\t-D" + str(i[0]) + "\n")
  182. else:
  183. # generic macro (commonly a string), ensure robust string conversion
  184. cm_file.write("\t-D" + str(i) + "\n")
  185. cm_file.write(")\n\n")
  186. libgroups = []
  187. interfacelibgroups = []
  188. for group in project:
  189. if group['name'] == 'Applications':
  190. continue
  191. # When a group is provided without sources, add it to the <INTERFACE> library list
  192. if len(group['src']) == 0:
  193. interfacelibgroups.append(group)
  194. else:
  195. libgroups.append(group)
  196. # Process groups whose names differ only in capitalization.
  197. # (Groups have same name should be merged into one before)
  198. for group in libgroups:
  199. group['alias'] = group['name'].lower()
  200. names = [group['alias'] for group in libgroups]
  201. counter = Counter(names)
  202. names = [name for name in names if counter[name] > 1]
  203. for group in libgroups:
  204. if group['alias'] in names:
  205. counter[group['alias']] -= 1
  206. group['alias'] = f"{group['name']}_{counter[group['alias']]}"
  207. print(f"Renamed {group['name']} to {group['alias']}")
  208. group['name'] = group['alias']
  209. cm_file.write("# Library source files\n")
  210. for group in project:
  211. cm_file.write("SET(RT_{:s}_SOURCES\n".format(group['name'].upper()))
  212. for f in group['src']:
  213. # use relative path
  214. path = _make_path_relative(os.getcwd(), os.path.normpath(f.rfile().abspath))
  215. cm_file.write( "\t" + path.replace("\\", "/") + "\n" )
  216. cm_file.write(")\n\n")
  217. cm_file.write("# Library search paths\n")
  218. for group in libgroups + interfacelibgroups:
  219. if not 'LIBPATH' in group.keys():
  220. continue
  221. if len(group['LIBPATH']) == 0:
  222. continue
  223. cm_file.write("SET(RT_{:s}_LINK_DIRS\n".format(group['name'].upper()))
  224. for f in group['LIBPATH']:
  225. cm_file.write("\t"+ f.replace("\\", "/") + "\n" )
  226. cm_file.write(")\n\n")
  227. cm_file.write("# Library local macro definitions\n")
  228. for group in libgroups:
  229. if not 'LOCAL_CPPDEFINES' in group.keys():
  230. continue
  231. if len(group['LOCAL_CPPDEFINES']) == 0:
  232. continue
  233. cm_file.write("SET(RT_{:s}_DEFINES\n".format(group['name'].upper()))
  234. for f in group['LOCAL_CPPDEFINES']:
  235. cm_file.write("\t"+ f.replace("\\", "/") + "\n" )
  236. cm_file.write(")\n\n")
  237. cm_file.write("# Library dependencies\n")
  238. for group in libgroups + interfacelibgroups:
  239. if not 'LIBS' in group.keys():
  240. continue
  241. if len(group['LIBS']) == 0:
  242. continue
  243. cm_file.write("SET(RT_{:s}_LIBS\n".format(group['name'].upper()))
  244. for f in group['LIBS']:
  245. cm_file.write("\t"+ "{}\n".format(f.replace("\\", "/")))
  246. cm_file.write(")\n\n")
  247. cm_file.write("# Libraries\n")
  248. for group in libgroups:
  249. cm_file.write("ADD_LIBRARY(rtt_{:s} OBJECT ${{RT_{:s}_SOURCES}})\n"
  250. .format(group['name'], group['name'].upper()))
  251. cm_file.write("\n")
  252. cm_file.write("# Interface libraries\n")
  253. for group in interfacelibgroups:
  254. cm_file.write("ADD_LIBRARY(rtt_{:s} INTERFACE)\n".format(group['name']))
  255. cm_file.write("\n")
  256. cm_file.write("# Private macros\n")
  257. for group in libgroups:
  258. if not 'LOCAL_CPPDEFINES' in group.keys():
  259. continue
  260. if len(group['LOCAL_CPPDEFINES']) == 0:
  261. continue
  262. cm_file.write("TARGET_COMPILE_DEFINITIONS(rtt_{:s} PRIVATE ${{RT_{:s}_DEFINES}})\n"
  263. .format(group['name'], group['name'].upper()))
  264. cm_file.write("\n")
  265. cm_file.write("# Interface library search paths\n")
  266. if rtconfig.PLATFORM in ['gcc']:
  267. for group in libgroups:
  268. if not 'LIBPATH' in group.keys():
  269. continue
  270. if len(group['LIBPATH']) == 0:
  271. continue
  272. cm_file.write("TARGET_LINK_DIRECTORIES(rtt_{:s} INTERFACE ${{RT_{:s}_LINK_DIRS}})\n"
  273. .format(group['name'], group['name'].upper()))
  274. for group in libgroups:
  275. if not 'LIBS' in group.keys():
  276. continue
  277. if len(group['LIBS']) == 0:
  278. continue
  279. cm_file.write("TARGET_LINK_LIBRARIES(rtt_{:s} INTERFACE ${{RT_{:s}_LIBS}})\n"
  280. .format(group['name'], group['name'].upper()))
  281. cm_file.write("\n")
  282. cm_file.write("ADD_EXECUTABLE(${CMAKE_PROJECT_NAME}.elf ${RT_APPLICATIONS_SOURCES})\n")
  283. cm_file.write("TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME}.elf\n")
  284. for group in libgroups + interfacelibgroups:
  285. cm_file.write("\trtt_{:s}\n".format(group['name']))
  286. cm_file.write(")\n\n")
  287. cm_file.write("ADD_CUSTOM_COMMAND(TARGET ${CMAKE_PROJECT_NAME}.elf POST_BUILD \n" + POST_ACTION + '\n)\n')
  288. # auto inclue `custom.cmake` for user custom settings
  289. custom_cmake = \
  290. '''
  291. # if custom.cmake is exist, add it
  292. if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/custom.cmake)
  293. include(${CMAKE_CURRENT_SOURCE_DIR}/custom.cmake)
  294. endif()
  295. '''
  296. custom_cmake = custom_cmake.split('\n')
  297. custom_cmake = [each.strip() for each in custom_cmake]
  298. custom_cmake = "\n".join(custom_cmake)
  299. cm_file.write(custom_cmake)
  300. return
  301. def CMakeProject(env, project, project_name):
  302. print('Update setting files for CMakeLists.txt...')
  303. GenerateCFiles(env, project, project_name)
  304. print('Done!')
  305. return