mkdist.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. #
  2. # File : mkdir.py
  3. # This file is part of RT-Thread RTOS
  4. # COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. #
  20. # Change Logs:
  21. # Date Author Notes
  22. # 2017-10-04 Bernard The first version
  23. import os
  24. import subprocess
  25. import shutil
  26. from shutil import ignore_patterns
  27. from SCons.Script import *
  28. import time
  29. def do_copy_file(src, dst):
  30. # check source file
  31. if not os.path.exists(src):
  32. return
  33. path = os.path.dirname(dst)
  34. # mkdir if path not exist
  35. if not os.path.exists(path):
  36. os.makedirs(path)
  37. shutil.copy2(src, dst)
  38. def do_copy_folder(src_dir, dst_dir, ignore=None):
  39. # check source directory
  40. if not os.path.exists(src_dir):
  41. return
  42. try:
  43. if os.path.exists(dst_dir):
  44. shutil.rmtree(dst_dir)
  45. except:
  46. print('Deletes folder: %s failed.' % dst_dir)
  47. return
  48. shutil.copytree(src_dir, dst_dir, ignore = ignore)
  49. source_ext = ['c', 'h', 's', 'S', 'cpp', 'cxx', 'cc', 'xpm']
  50. source_list = []
  51. def walk_children(child):
  52. global source_list
  53. global source_ext
  54. # print child
  55. full_path = child.rfile().abspath
  56. file_type = full_path.rsplit('.',1)[1]
  57. #print file_type
  58. if file_type in source_ext:
  59. if full_path not in source_list:
  60. source_list.append(full_path)
  61. children = child.all_children()
  62. if children != []:
  63. for item in children:
  64. walk_children(item)
  65. def walk_kconfig(RTT_ROOT, source_list):
  66. for parent, dirnames, filenames in os.walk(RTT_ROOT):
  67. if 'bsp' in parent:
  68. continue
  69. if '.git' in parent:
  70. continue
  71. if 'tools' in parent:
  72. continue
  73. if 'Kconfig' in filenames:
  74. pathfile = os.path.join(parent, 'Kconfig')
  75. source_list.append(pathfile)
  76. if 'KConfig' in filenames:
  77. pathfile = os.path.join(parent, 'KConfig')
  78. source_list.append(pathfile)
  79. def bsp_copy_files(bsp_root, dist_dir):
  80. # copy BSP files
  81. do_copy_folder(os.path.join(bsp_root), dist_dir,
  82. ignore_patterns('build', 'dist', '*.pyc', '*.old', '*.map', 'rtthread.bin', '.sconsign.dblite', '*.elf', '*.axf', 'cconfig.h'))
  83. def bsp_update_sconstruct(dist_dir):
  84. with open(os.path.join(dist_dir, 'SConstruct'), 'r') as f:
  85. data = f.readlines()
  86. with open(os.path.join(dist_dir, 'SConstruct'), 'w') as f:
  87. for line in data:
  88. if line.find('RTT_ROOT') != -1:
  89. if line.find('sys.path') != -1:
  90. f.write('# set RTT_ROOT\n')
  91. f.write('if not os.getenv("RTT_ROOT"): \n RTT_ROOT="rt-thread"\n\n')
  92. f.write(line)
  93. def bsp_update_kconfig_testcases(dist_dir):
  94. # delete testcases in rt-thread/Kconfig
  95. if not os.path.isfile(os.path.join(dist_dir, 'rt-thread/Kconfig')):
  96. return
  97. with open(os.path.join(dist_dir, 'rt-thread/Kconfig'), 'r') as f:
  98. data = f.readlines()
  99. with open(os.path.join(dist_dir, 'rt-thread/Kconfig'), 'w') as f:
  100. for line in data:
  101. if line.find('examples/utest/testcases/Kconfig') == -1:
  102. f.write(line)
  103. def bsp_update_kconfig(dist_dir):
  104. # change RTT_ROOT in Kconfig
  105. if not os.path.isfile(os.path.join(dist_dir, 'Kconfig')):
  106. return
  107. with open(os.path.join(dist_dir, 'Kconfig'), 'r') as f:
  108. data = f.readlines()
  109. with open(os.path.join(dist_dir, 'Kconfig'), 'w') as f:
  110. for line in data:
  111. if line.find('RTT_DIR') != -1 and line.find(':=') != -1:
  112. line = 'RTT_DIR := rt-thread\n'
  113. f.write(line)
  114. def bsp_update_kconfig_library(dist_dir):
  115. # change RTT_ROOT in Kconfig
  116. if not os.path.isfile(os.path.join(dist_dir, 'Kconfig')):
  117. return
  118. with open(os.path.join(dist_dir, 'Kconfig'), 'r') as f:
  119. data = f.readlines()
  120. with open(os.path.join(dist_dir, 'Kconfig'), 'w') as f:
  121. for line in data:
  122. if line.find('source') != -1 and line.find('../libraries') != -1:
  123. line = line.replace('../libraries', 'libraries')
  124. f.write(line)
  125. # change board/kconfig path
  126. if not os.path.isfile(os.path.join(dist_dir, 'board/Kconfig')):
  127. return
  128. with open(os.path.join(dist_dir, 'board/Kconfig'), 'r') as f:
  129. data = f.readlines()
  130. with open(os.path.join(dist_dir, 'board/Kconfig'), 'w') as f:
  131. for line in data:
  132. if line.find('source') != -1 and line.find('../libraries') != -1:
  133. line = line.replace('../libraries', 'libraries')
  134. f.write(line)
  135. def zip_dist(dist_dir, dist_name):
  136. import zipfile
  137. zip_filename = os.path.join(dist_dir)
  138. zip = zipfile.ZipFile(zip_filename + '.zip', 'w')
  139. pre_len = len(os.path.dirname(dist_dir))
  140. for parent, dirnames, filenames in os.walk(dist_dir):
  141. for filename in filenames:
  142. pathfile = os.path.join(parent, filename)
  143. arcname = pathfile[pre_len:].strip(os.path.sep)
  144. zip.write(pathfile, arcname)
  145. zip.close()
  146. def get_system_features():
  147. """获取系统内置特性列表"""
  148. return {
  149. # 内核特性
  150. 'components_init',
  151. 'console',
  152. 'cpu_usage_tracer',
  153. 'heap',
  154. 'slab',
  155. 'mempool',
  156. 'memtrace',
  157. 'timer_soft',
  158. 'event',
  159. 'mailbox',
  160. 'messagequeue',
  161. 'mutex',
  162. 'semaphore',
  163. 'signals',
  164. 'hook',
  165. 'idle_hook',
  166. 'thread',
  167. 'cache',
  168. 'debug',
  169. 'device_ops',
  170. 'overflow_check',
  171. 'slab_as_heap',
  172. 'user_main',
  173. 'stdc_atomic',
  174. }
  175. def parse_components_from_config(config_file):
  176. """从 .config 文件解析启用的组件"""
  177. enabled_components = set()
  178. if not os.path.exists(config_file):
  179. print(f"Error: {config_file} does not exist")
  180. return enabled_components
  181. with open(config_file, 'r') as f:
  182. for line in f:
  183. line = line.strip()
  184. if line.startswith('CONFIG_'):
  185. if '=' in line:
  186. config = line.split('=')[0][7:] # 去掉 CONFIG_ 前缀
  187. if config.startswith('RT_USING_'):
  188. component = config[9:].lower() # 去掉 RT_USING_ 前缀
  189. enabled_components.add(component)
  190. return enabled_components
  191. def scan_components_dir(RTT_ROOT):
  192. """扫描组件目录结构,生成组件映射表"""
  193. components_map = {}
  194. components_root = os.path.join(RTT_ROOT, 'components')
  195. def parse_kconfig(kconfig_file):
  196. """解析 Kconfig 文件中的配置选项"""
  197. components = set()
  198. try:
  199. with open(kconfig_file, 'r') as f:
  200. content = f.read()
  201. # 查找 config RT_USING_XXX 形式的配置
  202. import re
  203. matches = re.finditer(r'config\s+RT_USING_(\w+)', content)
  204. for match in matches:
  205. component_name = match.group(1).lower()
  206. components.add(component_name)
  207. except Exception as e:
  208. print(f"Warning: Failed to parse {kconfig_file}: {str(e)}")
  209. return components
  210. def get_relative_path(full_path):
  211. """获取相对于 RTT_ROOT 的路径"""
  212. return os.path.relpath(os.path.dirname(full_path), RTT_ROOT)
  213. # 扫描所有组件目录
  214. for root, dirs, files in os.walk(components_root):
  215. if 'Kconfig' in files:
  216. kconfig_path = os.path.join(root, 'Kconfig')
  217. component_configs = parse_kconfig(kconfig_path)
  218. rel_path = get_relative_path(kconfig_path)
  219. # 将组件名称与路径关联
  220. for comp_name in component_configs:
  221. components_map[comp_name] = rel_path
  222. return components_map
  223. def get_component_path(component_name, RTT_ROOT):
  224. """获取组件的实际路径"""
  225. # 获取动态组件映射
  226. dynamic_map = scan_components_dir(RTT_ROOT)
  227. return dynamic_map.get(component_name)
  228. def generate_dist_doc(dist_dir, enabled_components, project_name, BSP_ROOT, RTT_ROOT):
  229. """Generate distribution package documentation"""
  230. doc_lines = [] # Store document content in a list
  231. # Basic information
  232. doc_lines.extend([
  233. "# RT-Thread Distribution Package\n",
  234. "\n## Basic Information\n\n",
  235. f"- Project Name: {project_name}\n",
  236. f"- Generation Time: {time.strftime('%Y-%m-%d %H:%M:%S')}\n",
  237. f"- BSP: {os.path.basename(BSP_ROOT)}\n",
  238. "\n## Components\n\n",
  239. "### Included Components:\n\n"
  240. ])
  241. # Add component information
  242. for comp in sorted(enabled_components):
  243. path = get_component_path(comp, RTT_ROOT)
  244. if path:
  245. doc_lines.append(f"- {comp}\n - Path: {path}\n")
  246. # Add configuration information
  247. doc_lines.extend(["\n## Configuration\n\n"])
  248. config_file = os.path.join(BSP_ROOT, '.config')
  249. if os.path.exists(config_file):
  250. doc_lines.extend([
  251. "### Main Configuration Items:\n\n```\n"
  252. ])
  253. with open(config_file, 'r') as f:
  254. for line in f:
  255. if line.startswith('CONFIG_'):
  256. doc_lines.append(line)
  257. doc_lines.append("```\n")
  258. # Add simplified directory structure
  259. doc_lines.extend(["\n## Directory Structure\n\n```\n"])
  260. # Show only top-level directories
  261. items = os.listdir(dist_dir)
  262. items.sort()
  263. for item in items:
  264. if item.startswith('.') or item == 'dist':
  265. continue
  266. path = os.path.join(dist_dir, item)
  267. if os.path.isdir(path):
  268. doc_lines.append(f"├── {item}/\n")
  269. else:
  270. doc_lines.append(f"├── {item}\n")
  271. doc_lines.append("```\n")
  272. # Add build instructions
  273. doc_lines.extend(["""
  274. ## Build Instructions
  275. 1. Requirements:
  276. - Python 3.x
  277. - SCons build tool
  278. - Appropriate cross-compiler toolchain
  279. 2. Build Steps:
  280. ```bash
  281. scons
  282. ```
  283. 3. Clean Build:
  284. ```bash
  285. scons -c
  286. ```
  287. ## Notes
  288. 1. Make sure the toolchain environment variables are properly set
  289. 2. To modify configuration, use menuconfig:
  290. ```bash
  291. scons --menuconfig
  292. ```
  293. ## License
  294. See `COPYING` file for details.
  295. """])
  296. # Write documentation
  297. doc_file = os.path.join(dist_dir, 'dist_readme.md')
  298. with open(doc_file, 'w', encoding='utf-8') as f:
  299. f.writelines(doc_lines)
  300. print(f"=> Generated distribution documentation: {doc_file}")
  301. def components_copy_files(RTT_ROOT, rtt_dir_path, config_file):
  302. """根据配置复制组件"""
  303. print('=> components (selective copy)')
  304. # 获取启用的组件
  305. enabled_components = parse_components_from_config(config_file)
  306. if not enabled_components:
  307. print("Warning: No components found in config file")
  308. return enabled_components
  309. # 复制每个启用的组件
  310. for comp_name in enabled_components:
  311. comp_path = get_component_path(comp_name, RTT_ROOT)
  312. if comp_path:
  313. src_path = os.path.join(RTT_ROOT, comp_path)
  314. dst_path = os.path.join(rtt_dir_path, comp_path)
  315. if os.path.exists(src_path):
  316. print(f' => copying {comp_name} from {comp_path}')
  317. do_copy_folder(src_path, dst_path)
  318. else:
  319. print(f"Warning: Component path not found: {src_path}")
  320. else:
  321. print(f"Note: Skipping system feature: {comp_name}")
  322. return enabled_components
  323. def MkDist(program, BSP_ROOT, RTT_ROOT, Env, project_name, project_path):
  324. print('make distribution....')
  325. if project_path == None:
  326. dist_dir = os.path.join(BSP_ROOT, 'dist', project_name)
  327. else:
  328. dist_dir = project_path
  329. rtt_dir_path = os.path.join(dist_dir, 'rt-thread')
  330. # copy BSP files
  331. print('=> %s' % os.path.basename(BSP_ROOT))
  332. bsp_copy_files(BSP_ROOT, dist_dir)
  333. # do bsp special dist handle
  334. if 'dist_handle' in Env:
  335. print("=> start dist handle")
  336. dist_handle = Env['dist_handle']
  337. dist_handle(BSP_ROOT, dist_dir)
  338. # 使用新的组件复制函数并获取启用的组件列表
  339. config_file = os.path.join(BSP_ROOT, '.config')
  340. enabled_components = components_copy_files(RTT_ROOT, rtt_dir_path, config_file)
  341. # skip documentation directory
  342. # skip examples
  343. # copy include directory
  344. print('=> include')
  345. do_copy_folder(os.path.join(RTT_ROOT, 'include'), os.path.join(rtt_dir_path, 'include'))
  346. # copy all libcpu/ARCH directory
  347. print('=> libcpu')
  348. import rtconfig
  349. do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH), os.path.join(rtt_dir_path, 'libcpu', rtconfig.ARCH))
  350. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'Kconfig'), os.path.join(rtt_dir_path, 'libcpu', 'Kconfig'))
  351. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'SConscript'), os.path.join(rtt_dir_path, 'libcpu', 'SConscript'))
  352. # copy src directory
  353. print('=> src')
  354. do_copy_folder(os.path.join(RTT_ROOT, 'src'), os.path.join(rtt_dir_path, 'src'))
  355. # copy tools directory
  356. print('=> tools')
  357. do_copy_folder(os.path.join(RTT_ROOT, 'tools'), os.path.join(rtt_dir_path, 'tools'), ignore_patterns('*.pyc'))
  358. do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(rtt_dir_path, 'Kconfig'))
  359. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(rtt_dir_path, 'AUTHORS'))
  360. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(rtt_dir_path, 'COPYING'))
  361. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(rtt_dir_path, 'README.md'))
  362. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(rtt_dir_path, 'README_zh.md'))
  363. print('Update configuration files...')
  364. # change RTT_ROOT in SConstruct
  365. bsp_update_sconstruct(dist_dir)
  366. # change RTT_ROOT in Kconfig
  367. bsp_update_kconfig(dist_dir)
  368. bsp_update_kconfig_library(dist_dir)
  369. # delete testcases in Kconfig
  370. bsp_update_kconfig_testcases(dist_dir)
  371. target_project_type = GetOption('target')
  372. if target_project_type:
  373. child = subprocess.Popen('scons --target={} --project-name="{}"'.format(target_project_type, project_name), cwd=dist_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  374. stdout, stderr = child.communicate()
  375. if child.returncode == 0:
  376. print(stdout)
  377. else:
  378. print(stderr)
  379. else:
  380. print('suggest to use command scons --dist [--target=xxx] [--project-name="xxx"] [--project-path="xxx"]')
  381. # make zip package
  382. if project_path == None:
  383. zip_dist(dist_dir, project_name)
  384. # 生成说明文档
  385. generate_dist_doc(dist_dir, enabled_components, project_name+'-dist', BSP_ROOT, RTT_ROOT)
  386. print('dist project successfully!')