mkdist.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. # 2025-01-07 ZhaoCake components copy and gen doc
  24. import os
  25. import subprocess
  26. import shutil
  27. from shutil import ignore_patterns
  28. from SCons.Script import *
  29. import time
  30. def do_copy_file(src, dst):
  31. # check source file
  32. if not os.path.exists(src):
  33. return
  34. path = os.path.dirname(dst)
  35. # mkdir if path not exist
  36. if not os.path.exists(path):
  37. os.makedirs(path)
  38. shutil.copy2(src, dst)
  39. def do_copy_folder(src_dir, dst_dir, ignore=None):
  40. # check source directory
  41. if not os.path.exists(src_dir):
  42. return
  43. try:
  44. if os.path.exists(dst_dir):
  45. shutil.rmtree(dst_dir)
  46. except:
  47. print('Deletes folder: %s failed.' % dst_dir)
  48. return
  49. shutil.copytree(src_dir, dst_dir, ignore = ignore)
  50. source_ext = ['c', 'h', 's', 'S', 'cpp', 'cxx', 'cc', 'xpm']
  51. source_list = []
  52. def walk_children(child):
  53. global source_list
  54. global source_ext
  55. # print child
  56. full_path = child.rfile().abspath
  57. file_type = full_path.rsplit('.',1)[1]
  58. #print file_type
  59. if file_type in source_ext:
  60. if full_path not in source_list:
  61. source_list.append(full_path)
  62. children = child.all_children()
  63. if children != []:
  64. for item in children:
  65. walk_children(item)
  66. def walk_kconfig(RTT_ROOT, source_list):
  67. for parent, dirnames, filenames in os.walk(RTT_ROOT):
  68. if 'bsp' in parent:
  69. continue
  70. if '.git' in parent:
  71. continue
  72. if 'tools' in parent:
  73. continue
  74. if 'Kconfig' in filenames:
  75. pathfile = os.path.join(parent, 'Kconfig')
  76. source_list.append(pathfile)
  77. if 'KConfig' in filenames:
  78. pathfile = os.path.join(parent, 'KConfig')
  79. source_list.append(pathfile)
  80. def bsp_copy_files(bsp_root, dist_dir):
  81. # copy BSP files
  82. do_copy_folder(os.path.join(bsp_root), dist_dir,
  83. ignore_patterns('build','__pycache__','dist', '*.pyc', '*.old', '*.map', 'rtthread.bin', '.sconsign.dblite', '*.elf', '*.axf', 'cconfig.h'))
  84. def bsp_update_sconstruct(dist_dir):
  85. with open(os.path.join(dist_dir, 'SConstruct'), 'r') as f:
  86. data = f.readlines()
  87. with open(os.path.join(dist_dir, 'SConstruct'), 'w') as f:
  88. for line in data:
  89. if line.find('RTT_ROOT') != -1:
  90. if line.find('sys.path') != -1:
  91. f.write('# set RTT_ROOT\n')
  92. f.write('if not os.getenv("RTT_ROOT"): \n RTT_ROOT="rt-thread"\n\n')
  93. f.write(line)
  94. def bsp_update_kconfig_testcases(dist_dir):
  95. # delete testcases in rt-thread/Kconfig
  96. if not os.path.isfile(os.path.join(dist_dir, 'rt-thread/Kconfig')):
  97. return
  98. with open(os.path.join(dist_dir, 'rt-thread/Kconfig'), 'r') as f:
  99. data = f.readlines()
  100. with open(os.path.join(dist_dir, 'rt-thread/Kconfig'), 'w') as f:
  101. for line in data:
  102. if line.find('examples/utest/testcases/Kconfig') == -1:
  103. f.write(line)
  104. def bsp_update_kconfig(dist_dir):
  105. # change RTT_ROOT in Kconfig
  106. if not os.path.isfile(os.path.join(dist_dir, 'Kconfig')):
  107. return
  108. with open(os.path.join(dist_dir, 'Kconfig'), 'r') as f:
  109. data = f.readlines()
  110. with open(os.path.join(dist_dir, 'Kconfig'), 'w') as f:
  111. for line in data:
  112. if line.find('RTT_DIR') != -1 and line.find(':=') != -1:
  113. line = 'RTT_DIR := rt-thread\n'
  114. f.write(line)
  115. def bsp_update_kconfig_library(dist_dir):
  116. # change RTT_ROOT in Kconfig
  117. if not os.path.isfile(os.path.join(dist_dir, 'Kconfig')):
  118. return
  119. with open(os.path.join(dist_dir, 'Kconfig'), 'r') as f:
  120. data = f.readlines()
  121. with open(os.path.join(dist_dir, 'Kconfig'), 'w') as f:
  122. for line in data:
  123. if line.find('source') != -1 and line.find('../libraries') != -1:
  124. line = line.replace('../libraries', 'libraries')
  125. f.write(line)
  126. # change board/kconfig path
  127. if not os.path.isfile(os.path.join(dist_dir, 'board/Kconfig')):
  128. return
  129. with open(os.path.join(dist_dir, 'board/Kconfig'), 'r') as f:
  130. data = f.readlines()
  131. with open(os.path.join(dist_dir, 'board/Kconfig'), 'w') as f:
  132. for line in data:
  133. if line.find('source') != -1 and line.find('../libraries') != -1:
  134. line = line.replace('../libraries', 'libraries')
  135. f.write(line)
  136. def zip_dist(dist_dir, dist_name):
  137. import zipfile
  138. zip_filename = os.path.join(dist_dir)
  139. zip = zipfile.ZipFile(zip_filename + '.zip', 'w')
  140. pre_len = len(os.path.dirname(dist_dir))
  141. for parent, dirnames, filenames in os.walk(dist_dir):
  142. for filename in filenames:
  143. pathfile = os.path.join(parent, filename)
  144. arcname = pathfile[pre_len:].strip(os.path.sep)
  145. zip.write(pathfile, arcname)
  146. zip.close()
  147. def parse_components_from_config(config_file):
  148. """Parse enabled components from .config file"""
  149. enabled_components = set()
  150. if not os.path.exists(config_file):
  151. print(f"Error: {config_file} does not exist")
  152. return enabled_components
  153. with open(config_file, 'r') as f:
  154. for line in f:
  155. line = line.strip()
  156. # Skip empty lines and comments
  157. if not line or line.startswith('#'):
  158. continue
  159. if line.startswith('CONFIG_'):
  160. if '=' in line:
  161. config = line.split('=')[0][7:] # Remove CONFIG_ prefix
  162. if config.startswith('RT_USING_'):
  163. component = config[9:].lower() # Remove RT_USING_ prefix
  164. enabled_components.add(component)
  165. return enabled_components
  166. def scan_components_dir(RTT_ROOT):
  167. """Scan component directory structure and generate component mapping"""
  168. components_map = {}
  169. components_root = os.path.join(RTT_ROOT, 'components')
  170. def parse_kconfig(kconfig_file):
  171. """Parse configuration options from Kconfig file"""
  172. components = set()
  173. try:
  174. with open(kconfig_file, 'r') as f:
  175. content = f.read()
  176. # Find configurations in the form of config RT_USING_XXX
  177. import re
  178. matches = re.finditer(r'config\s+RT_USING_(\w+)', content)
  179. for match in matches:
  180. component_name = match.group(1).lower()
  181. components.add(component_name)
  182. except Exception as e:
  183. print(f"Warning: Failed to parse {kconfig_file}: {str(e)}")
  184. return components
  185. def get_relative_path(full_path):
  186. """Get path relative to RTT_ROOT"""
  187. rel_path = os.path.relpath(os.path.dirname(full_path), RTT_ROOT)
  188. # Skip if path is directly under components directory
  189. if rel_path == 'components' or rel_path == os.path.join('components', ''):
  190. return None
  191. return rel_path
  192. # Scan all component directories
  193. for root, dirs, files in os.walk(components_root):
  194. if 'Kconfig' in files:
  195. kconfig_path = os.path.join(root, 'Kconfig')
  196. component_configs = parse_kconfig(kconfig_path)
  197. rel_path = get_relative_path(kconfig_path)
  198. # Only add component if it has a valid sub-path
  199. if rel_path:
  200. for comp_name in component_configs:
  201. components_map[comp_name] = rel_path
  202. return components_map
  203. def get_component_path(component_name, RTT_ROOT):
  204. """Get actual path of component"""
  205. # Get dynamic component mapping
  206. dynamic_map = scan_components_dir(RTT_ROOT)
  207. return dynamic_map.get(component_name)
  208. def generate_dist_doc(dist_dir, enabled_components, project_name, BSP_ROOT, RTT_ROOT):
  209. """Generate distribution package documentation"""
  210. doc_lines = [] # Store document content in a list
  211. # Basic information
  212. doc_lines.extend([
  213. "# RT-Thread Distribution Package\n",
  214. "\n## Basic Information\n\n",
  215. f"- Project Name: {project_name}\n",
  216. f"- Generation Time: {time.strftime('%Y-%m-%d %H:%M:%S')}\n",
  217. f"- BSP: {os.path.basename(BSP_ROOT)}\n",
  218. "\n## Components\n\n",
  219. "### Included Components:\n\n"
  220. ])
  221. # Add component information
  222. for comp in sorted(enabled_components):
  223. path = get_component_path(comp, RTT_ROOT)
  224. if path:
  225. doc_lines.append(f"- {comp}\n - Path: {path}\n")
  226. # Add configuration information
  227. doc_lines.extend(["\n## Configuration\n\n"])
  228. config_file = os.path.join(BSP_ROOT, '.config')
  229. if os.path.exists(config_file):
  230. doc_lines.extend([
  231. "### Main Configuration Items:\n\n```\n"
  232. ])
  233. with open(config_file, 'r') as f:
  234. for line in f:
  235. if line.startswith('CONFIG_'):
  236. doc_lines.append(line)
  237. doc_lines.append("```\n")
  238. # Add simplified directory structure
  239. doc_lines.extend(["\n## Directory Structure\n\n```\n"])
  240. # Show only top-level directories
  241. items = os.listdir(dist_dir)
  242. items.sort()
  243. for item in items:
  244. if item.startswith('.') or item == 'dist':
  245. continue
  246. path = os.path.join(dist_dir, item)
  247. if os.path.isdir(path):
  248. doc_lines.append(f"├── {item}/\n")
  249. else:
  250. doc_lines.append(f"├── {item}\n")
  251. doc_lines.append("```\n")
  252. # Add build instructions
  253. doc_lines.extend(["""
  254. ## Build Instructions
  255. 1. Requirements:
  256. - Python 3.x
  257. - SCons build tool
  258. - Appropriate cross-compiler toolchain
  259. 2. Build Steps:
  260. ```bash
  261. scons
  262. ```
  263. 3. Clean Build:
  264. ```bash
  265. scons -c
  266. ```
  267. ## Notes
  268. 1. Make sure the toolchain environment variables are properly set
  269. 2. To modify configuration, use menuconfig:
  270. ```bash
  271. scons --menuconfig
  272. ```
  273. ## License
  274. See `COPYING` file for details.
  275. """])
  276. # Write documentation
  277. doc_file = os.path.join(dist_dir, 'dist_readme.md')
  278. with open(doc_file, 'w', encoding='utf-8') as f:
  279. f.writelines(doc_lines)
  280. print(f"=> Generated distribution documentation: {doc_file}")
  281. def is_text_file(filepath):
  282. """Check if a file is a text file"""
  283. text_extensions = {
  284. '.h', '.c', '.cpp', '.hpp', '.S', '.s', '.asm',
  285. '.txt', '.md', '.rst', '.ini', '.conf',
  286. 'Kconfig', 'SConscript', 'SConstruct',
  287. '.json', '.yml', '.yaml',
  288. '.cmake', 'CMakeLists.txt',
  289. '.py', '.sh', '.bat',
  290. 'README', 'LICENSE', 'Makefile'
  291. }
  292. # Check by extension
  293. ext = os.path.splitext(filepath)[1].lower()
  294. if ext in text_extensions or os.path.basename(filepath) in text_extensions:
  295. return True
  296. # Additional check for files without extension
  297. if '.' not in os.path.basename(filepath):
  298. try:
  299. with open(filepath, 'r', encoding='utf-8') as f:
  300. f.read(1024) # Try to read as text
  301. return True
  302. except:
  303. return False
  304. return False
  305. def copy_component_dependencies(src_path, dst_base, RTT_ROOT, copied_files=None):
  306. """Copy component dependencies (text files) from parent directories"""
  307. if copied_files is None:
  308. copied_files = set()
  309. # Get relative path from RTT_ROOT
  310. rel_path = os.path.relpath(src_path, RTT_ROOT)
  311. parent_path = os.path.dirname(rel_path)
  312. # Process all parent directories until RTT_ROOT
  313. while parent_path and parent_path != '.':
  314. src_dir = os.path.join(RTT_ROOT, parent_path)
  315. # Copy all text files in the directory (not recursively)
  316. for item in os.listdir(src_dir):
  317. src_file = os.path.join(src_dir, item)
  318. if os.path.isfile(src_file) and src_file not in copied_files:
  319. if is_text_file(src_file):
  320. dst_file = os.path.join(dst_base, parent_path, item)
  321. dst_dir = os.path.dirname(dst_file)
  322. if not os.path.exists(dst_dir):
  323. os.makedirs(dst_dir)
  324. do_copy_file(src_file, dst_file)
  325. copied_files.add(src_file)
  326. print(f' => copying {item} from {parent_path}')
  327. parent_path = os.path.dirname(parent_path)
  328. return copied_files
  329. def get_essential_paths():
  330. """Get essential paths that must be included"""
  331. return {
  332. 'components/libc/compilers', # Common compiler support
  333. 'components/drivers/include', # Driver headers
  334. 'components/drivers/core', # Driver core
  335. 'components/utilities', # Utility functions
  336. 'components/mm', # Memory management
  337. 'components/legacy/ipc', # IPC support, not always used, but have no config option for it
  338. }
  339. def copy_essential_paths(RTT_ROOT, rtt_dir_path, copied_files=None):
  340. """Copy essential paths and their build files"""
  341. if copied_files is None:
  342. copied_files = set()
  343. print('=> copying essential paths')
  344. for path in get_essential_paths():
  345. src_path = os.path.join(RTT_ROOT, path)
  346. if os.path.exists(src_path):
  347. dst_path = os.path.join(rtt_dir_path, path)
  348. print(f' => copying {path}')
  349. do_copy_folder(src_path, dst_path)
  350. # Copy build files for this path
  351. copied_files = copy_component_dependencies(src_path, rtt_dir_path, RTT_ROOT, copied_files)
  352. return copied_files
  353. def copy_components_kconfig(RTT_ROOT, rtt_dir_path):
  354. """Copy all Kconfig files under components directory"""
  355. components_dir = os.path.join(RTT_ROOT, 'components')
  356. print('=> copying components Kconfig files')
  357. # Walk through all directories under components
  358. for root, dirs, files in os.walk(components_dir):
  359. if 'Kconfig' in files:
  360. # Get relative path from components directory
  361. rel_path = os.path.relpath(root, RTT_ROOT)
  362. src_file = os.path.join(root, 'Kconfig')
  363. dst_file = os.path.join(rtt_dir_path, rel_path, 'Kconfig')
  364. # Create destination directory if not exists
  365. dst_dir = os.path.dirname(dst_file)
  366. if not os.path.exists(dst_dir):
  367. os.makedirs(dst_dir)
  368. do_copy_file(src_file, dst_file)
  369. print(f' => copying Kconfig from {rel_path}')
  370. def components_copy_files(RTT_ROOT, rtt_dir_path, config_file):
  371. """Copy components based on configuration"""
  372. print('=> components (selective copy)')
  373. # Copy all Kconfig files first
  374. copy_components_kconfig(RTT_ROOT, rtt_dir_path)
  375. # Track copied build files to avoid duplication
  376. copied_files = set()
  377. # Copy components/SConscript first
  378. components_sconscript = os.path.join(RTT_ROOT, 'components', 'SConscript')
  379. if os.path.exists(components_sconscript):
  380. dst_dir = os.path.join(rtt_dir_path, 'components')
  381. if not os.path.exists(dst_dir):
  382. os.makedirs(dst_dir)
  383. do_copy_file(components_sconscript, os.path.join(dst_dir, 'SConscript'))
  384. copied_files.add(components_sconscript)
  385. # Copy essential paths first
  386. copied_files = copy_essential_paths(RTT_ROOT, rtt_dir_path, copied_files)
  387. # Get enabled components
  388. enabled_components = parse_components_from_config(config_file)
  389. if not enabled_components:
  390. print("Warning: No components found in config file")
  391. return enabled_components
  392. # Copy each enabled component
  393. for comp_name in enabled_components:
  394. comp_path = get_component_path(comp_name, RTT_ROOT)
  395. if comp_path:
  396. src_path = os.path.join(RTT_ROOT, comp_path)
  397. dst_path = os.path.join(rtt_dir_path, comp_path)
  398. if os.path.exists(src_path):
  399. print(f' => copying {comp_name} from {comp_path}')
  400. do_copy_folder(src_path, dst_path)
  401. # Copy parent directory build files
  402. copied_files = copy_component_dependencies(src_path, rtt_dir_path, RTT_ROOT, copied_files)
  403. else:
  404. print(f"Warning: Component path not found: {src_path}")
  405. else:
  406. print(f"Note: Skipping system feature: {comp_name}")
  407. return enabled_components
  408. def MkDist(program, BSP_ROOT, RTT_ROOT, Env, project_name, project_path):
  409. print('make distribution....')
  410. if project_path == None:
  411. dist_dir = os.path.join(BSP_ROOT, 'dist', project_name)
  412. else:
  413. dist_dir = project_path
  414. rtt_dir_path = os.path.join(dist_dir, 'rt-thread')
  415. # Copy BSP files
  416. print('=> %s' % os.path.basename(BSP_ROOT))
  417. bsp_copy_files(BSP_ROOT, dist_dir)
  418. # Do BSP special dist handle
  419. if 'dist_handle' in Env:
  420. print("=> start dist handle")
  421. dist_handle = Env['dist_handle']
  422. dist_handle(BSP_ROOT, dist_dir)
  423. # Use new component copy function and get list of enabled components
  424. config_file = os.path.join(BSP_ROOT, '.config')
  425. enabled_components = components_copy_files(RTT_ROOT, rtt_dir_path, config_file)
  426. # Skip documentation directory
  427. # Skip examples
  428. # Copy include directory
  429. print('=> include')
  430. do_copy_folder(os.path.join(RTT_ROOT, 'include'), os.path.join(rtt_dir_path, 'include'))
  431. # Copy all libcpu/ARCH directory
  432. print('=> libcpu')
  433. import rtconfig
  434. do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH), os.path.join(rtt_dir_path, 'libcpu', rtconfig.ARCH))
  435. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'Kconfig'), os.path.join(rtt_dir_path, 'libcpu', 'Kconfig'))
  436. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'SConscript'), os.path.join(rtt_dir_path, 'libcpu', 'SConscript'))
  437. # Copy src directory
  438. print('=> src')
  439. do_copy_folder(os.path.join(RTT_ROOT, 'src'), os.path.join(rtt_dir_path, 'src'))
  440. # Copy tools directory
  441. print('=> tools')
  442. do_copy_folder(os.path.join(RTT_ROOT, 'tools'), os.path.join(rtt_dir_path, 'tools'), ignore_patterns('*.pyc'))
  443. # Copy necessary files
  444. do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(rtt_dir_path, 'Kconfig'))
  445. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(rtt_dir_path, 'AUTHORS'))
  446. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(rtt_dir_path, 'COPYING'))
  447. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(rtt_dir_path, 'README.md'))
  448. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(rtt_dir_path, 'README_zh.md'))
  449. print('Update configuration files...')
  450. bsp_update_sconstruct(dist_dir)
  451. bsp_update_kconfig(dist_dir)
  452. bsp_update_kconfig_library(dist_dir)
  453. bsp_update_kconfig_testcases(dist_dir)
  454. # Generate documentation
  455. generate_dist_doc(dist_dir, enabled_components, project_name+'-dist', BSP_ROOT, RTT_ROOT)
  456. target_project_type = GetOption('target')
  457. if target_project_type:
  458. child = subprocess.Popen('scons --target={} --project-name="{}"'.format(target_project_type, project_name), cwd=dist_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  459. stdout, stderr = child.communicate()
  460. if child.returncode == 0:
  461. print(stdout)
  462. else:
  463. print(stderr)
  464. else:
  465. print('suggest to use command scons --dist [--target=xxx] [--project-name="xxx"] [--project-path="xxx"]')
  466. # make zip package
  467. if project_path == None:
  468. zip_dist(dist_dir, project_name)
  469. print('dist project successfully!')