mkdist.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. def do_copy_file(src, dst):
  29. # check source file
  30. if not os.path.exists(src):
  31. return
  32. path = os.path.dirname(dst)
  33. # mkdir if path not exist
  34. if not os.path.exists(path):
  35. os.makedirs(path)
  36. shutil.copy2(src, dst)
  37. def do_copy_folder(src_dir, dst_dir, ignore=None):
  38. import shutil
  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', '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. found = 0
  111. for line in data:
  112. if line.find('RTT_ROOT') != -1:
  113. found = 1
  114. if line.find('default') != -1 and found:
  115. position = line.find('default')
  116. line = line[0:position] + 'default "rt-thread"\n'
  117. found = 0
  118. f.write(line)
  119. def bsp_update_kconfig_library(dist_dir):
  120. # change RTT_ROOT in Kconfig
  121. if not os.path.isfile(os.path.join(dist_dir, 'Kconfig')):
  122. return
  123. with open(os.path.join(dist_dir, 'Kconfig'), 'r') as f:
  124. data = f.readlines()
  125. with open(os.path.join(dist_dir, 'Kconfig'), 'w') as f:
  126. found = 0
  127. for line in data:
  128. if line.find('RTT_ROOT') != -1:
  129. found = 1
  130. if line.find('../libraries') != -1 and found:
  131. position = line.find('../libraries')
  132. line = line[0:position] + 'libraries/Kconfig"\n'
  133. found = 0
  134. f.write(line)
  135. # change board/kconfig path
  136. if not os.path.isfile(os.path.join(dist_dir, 'board/Kconfig')):
  137. return
  138. with open(os.path.join(dist_dir, 'board/Kconfig'), 'r') as f:
  139. data = f.readlines()
  140. with open(os.path.join(dist_dir, 'board/Kconfig'), 'w') as f:
  141. for line in data:
  142. if line.find('../libraries/HAL_Drivers/Kconfig') != -1:
  143. position = line.find('../libraries/HAL_Drivers/Kconfig')
  144. line = line[0:position] + 'libraries/HAL_Drivers/Kconfig"\n'
  145. f.write(line)
  146. def zip_dist(dist_dir, dist_name):
  147. import zipfile
  148. zip_filename = os.path.join(dist_dir)
  149. zip = zipfile.ZipFile(zip_filename + '.zip', 'w')
  150. pre_len = len(os.path.dirname(dist_dir))
  151. for parent, dirnames, filenames in os.walk(dist_dir):
  152. for filename in filenames:
  153. pathfile = os.path.join(parent, filename)
  154. arcname = pathfile[pre_len:].strip(os.path.sep)
  155. zip.write(pathfile, arcname)
  156. zip.close()
  157. def MkDist_Strip(program, BSP_ROOT, RTT_ROOT, Env):
  158. global source_list
  159. print('make distribution and strip useless files....')
  160. dist_name = os.path.basename(BSP_ROOT)
  161. dist_dir = os.path.join(BSP_ROOT, 'dist-strip', dist_name)
  162. rtt_dir_path = os.path.join(dist_dir, 'rt-thread')
  163. print('=> %s' % os.path.basename(BSP_ROOT))
  164. bsp_copy_files(BSP_ROOT, dist_dir)
  165. # copy stm32 bsp libiary files
  166. if os.path.basename(os.path.dirname(BSP_ROOT)) == 'stm32':
  167. print("=> copy stm32 bsp library")
  168. library_path = os.path.join(os.path.dirname(BSP_ROOT), 'libraries')
  169. library_dir = os.path.join(dist_dir, 'libraries')
  170. bsp_copy_files(os.path.join(library_path, 'HAL_Drivers'), os.path.join(library_dir, 'HAL_Drivers'))
  171. bsp_copy_files(os.path.join(library_path, Env['bsp_lib_type']), os.path.join(library_dir, Env['bsp_lib_type']))
  172. shutil.copyfile(os.path.join(library_path, 'Kconfig'), os.path.join(library_dir, 'Kconfig'))
  173. # do bsp special dist handle
  174. if 'dist_handle' in Env:
  175. print("=> start dist handle")
  176. dist_handle = Env['dist_handle']
  177. dist_handle(BSP_ROOT, dist_dir)
  178. # get all source files from program
  179. for item in program:
  180. walk_children(item)
  181. source_list.sort()
  182. # copy the source files without libcpu and components/libc in RT-Thread
  183. target_list = []
  184. libcpu_dir = os.path.join(RTT_ROOT, 'libcpu').lower()
  185. libc_dir = os.path.join(RTT_ROOT, 'components', 'libc', 'compilers').lower()
  186. sal_dir = os.path.join(RTT_ROOT, 'components', 'net', 'sal_socket').lower()
  187. sources_include_sal = False
  188. for src in source_list:
  189. if src.lower().startswith(BSP_ROOT.lower()):
  190. continue
  191. # skip libc and libcpu dir
  192. if src.lower().startswith(libcpu_dir):
  193. continue
  194. if src.lower().startswith(libc_dir):
  195. continue
  196. if src.lower().startswith(sal_dir):
  197. sources_include_sal = True
  198. continue
  199. if src.lower().startswith(RTT_ROOT.lower()):
  200. target_list.append(src)
  201. source_list = target_list
  202. # get source directory
  203. src_dir = []
  204. for src in source_list:
  205. src = src.replace(RTT_ROOT, '')
  206. if src[0] == os.sep or src[0] == '/':
  207. src = src[1:]
  208. path = os.path.dirname(src)
  209. sub_path = path.split(os.sep)
  210. full_path = RTT_ROOT
  211. for item in sub_path:
  212. full_path = os.path.join(full_path, item)
  213. if full_path not in src_dir:
  214. src_dir.append(full_path)
  215. # add all of SConscript files
  216. for item in src_dir:
  217. source_list.append(os.path.join(item, 'SConscript'))
  218. # add all of Kconfig files
  219. walk_kconfig(RTT_ROOT, source_list)
  220. # copy all files to target directory
  221. source_list.sort()
  222. for src in source_list:
  223. dst = src.replace(RTT_ROOT, '')
  224. if dst[0] == os.sep or dst[0] == '/':
  225. dst = dst[1:]
  226. print('=> %s' % dst)
  227. dst = os.path.join(rtt_dir_path, dst)
  228. do_copy_file(src, dst)
  229. # copy tools directory
  230. print('=> tools')
  231. do_copy_folder(os.path.join(RTT_ROOT, 'tools'), os.path.join(rtt_dir_path, 'tools'), ignore_patterns('*.pyc'))
  232. do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(rtt_dir_path, 'Kconfig'))
  233. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(rtt_dir_path, 'AUTHORS'))
  234. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(rtt_dir_path, 'COPYING'))
  235. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(rtt_dir_path, 'README.md'))
  236. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(rtt_dir_path, 'README_zh.md'))
  237. print('=> %s' % os.path.join('components', 'libc', 'compilers'))
  238. do_copy_folder(os.path.join(RTT_ROOT, 'components', 'libc', 'compilers'), os.path.join(rtt_dir_path, 'components', 'libc', 'compilers'))
  239. if sources_include_sal:
  240. print('=> %s' % os.path.join('components', 'net', 'sal_socket'))
  241. do_copy_folder(os.path.join(RTT_ROOT, 'components', 'net', 'sal_socket'), os.path.join(rtt_dir_path, 'components', 'net', 'sal_socket'))
  242. # copy all libcpu/ARCH directory
  243. import rtconfig
  244. print('=> %s' % (os.path.join('libcpu', rtconfig.ARCH, rtconfig.CPU)))
  245. do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH, rtconfig.CPU), os.path.join(rtt_dir_path, 'libcpu', rtconfig.ARCH, rtconfig.CPU))
  246. if os.path.exists(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH, 'common')):
  247. print('=> %s' % (os.path.join('libcpu', rtconfig.ARCH, 'common')))
  248. do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH, 'common'), os.path.join(rtt_dir_path, 'libcpu', rtconfig.ARCH, 'common'))
  249. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'Kconfig'), os.path.join(rtt_dir_path, 'libcpu', 'Kconfig'))
  250. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'SConscript'), os.path.join(rtt_dir_path, 'libcpu', 'SConscript'))
  251. print('Update configuration files...')
  252. # change RTT_ROOT in SConstruct
  253. bsp_update_sconstruct(dist_dir)
  254. # change RTT_ROOT in Kconfig
  255. bsp_update_kconfig(dist_dir)
  256. bsp_update_kconfig_library(dist_dir)
  257. # delete testcases in Kconfig
  258. bsp_update_kconfig_testcases(dist_dir)
  259. # make zip package
  260. zip_dist(dist_dir, dist_name)
  261. print('done!')
  262. def MkDist(program, BSP_ROOT, RTT_ROOT, Env, project_name, project_path):
  263. print('make distribution....')
  264. if project_path == None:
  265. dist_dir = os.path.join(BSP_ROOT, 'dist', project_name)
  266. else:
  267. dist_dir = project_path
  268. rtt_dir_path = os.path.join(dist_dir, 'rt-thread')
  269. # copy BSP files
  270. print('=> %s' % os.path.basename(BSP_ROOT))
  271. bsp_copy_files(BSP_ROOT, dist_dir)
  272. # do bsp special dist handle
  273. if 'dist_handle' in Env:
  274. print("=> start dist handle")
  275. dist_handle = Env['dist_handle']
  276. dist_handle(BSP_ROOT, dist_dir)
  277. # copy tools directory
  278. print('=> components')
  279. do_copy_folder(os.path.join(RTT_ROOT, 'components'), os.path.join(rtt_dir_path, 'components'))
  280. # skip documentation directory
  281. # skip examples
  282. # copy include directory
  283. print('=> include')
  284. do_copy_folder(os.path.join(RTT_ROOT, 'include'), os.path.join(rtt_dir_path, 'include'))
  285. # copy all libcpu/ARCH directory
  286. print('=> libcpu')
  287. import rtconfig
  288. do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH), os.path.join(rtt_dir_path, 'libcpu', rtconfig.ARCH))
  289. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'Kconfig'), os.path.join(rtt_dir_path, 'libcpu', 'Kconfig'))
  290. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'SConscript'), os.path.join(rtt_dir_path, 'libcpu', 'SConscript'))
  291. # copy src directory
  292. print('=> src')
  293. do_copy_folder(os.path.join(RTT_ROOT, 'src'), os.path.join(rtt_dir_path, 'src'))
  294. # copy tools directory
  295. print('=> tools')
  296. do_copy_folder(os.path.join(RTT_ROOT, 'tools'), os.path.join(rtt_dir_path, 'tools'), ignore_patterns('*.pyc'))
  297. do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(rtt_dir_path, 'Kconfig'))
  298. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(rtt_dir_path, 'AUTHORS'))
  299. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(rtt_dir_path, 'COPYING'))
  300. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(rtt_dir_path, 'README.md'))
  301. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(rtt_dir_path, 'README_zh.md'))
  302. print('Update configuration files...')
  303. # change RTT_ROOT in SConstruct
  304. bsp_update_sconstruct(dist_dir)
  305. # change RTT_ROOT in Kconfig
  306. bsp_update_kconfig(dist_dir)
  307. bsp_update_kconfig_library(dist_dir)
  308. # delete testcases in Kconfig
  309. bsp_update_kconfig_testcases(dist_dir)
  310. # make zip package
  311. if project_path == None:
  312. zip_dist(dist_dir, project_name)
  313. target_project_type = GetOption('target')
  314. if target_project_type:
  315. child = subprocess.Popen('scons --target={} --project-name={}'.format(target_project_type, project_name), cwd=dist_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  316. stdout, stderr = child.communicate()
  317. if child.returncode == 0:
  318. print(stdout)
  319. else:
  320. print(stderr)
  321. else:
  322. print('suggest to use command scons --dist [--target=xxx] [--project-name=xxx] [--project-path=xxx]')
  323. print('dist project successfully!')