mkdist.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 shutil
  25. from shutil import ignore_patterns
  26. def do_copy_file(src, dst):
  27. # check source file
  28. if not os.path.exists(src):
  29. return
  30. path = os.path.dirname(dst)
  31. # mkdir if path not exist
  32. if not os.path.exists(path):
  33. os.makedirs(path)
  34. shutil.copy2(src, dst)
  35. def do_copy_folder(src_dir, dst_dir, ignore=None):
  36. import shutil
  37. # check source directory
  38. if not os.path.exists(src_dir):
  39. return
  40. try:
  41. if os.path.exists(dst_dir):
  42. shutil.rmtree(dst_dir)
  43. except:
  44. print('Deletes folder: %s failed.' % dst_dir)
  45. return
  46. shutil.copytree(src_dir, dst_dir, ignore = ignore)
  47. source_ext = ['c', 'h', 's', 'S', 'cpp', 'xpm']
  48. source_list = []
  49. def walk_children(child):
  50. global source_list
  51. global source_ext
  52. # print child
  53. full_path = child.rfile().abspath
  54. file_type = full_path.rsplit('.',1)[1]
  55. #print file_type
  56. if file_type in source_ext:
  57. if full_path not in source_list:
  58. source_list.append(full_path)
  59. children = child.all_children()
  60. if children != []:
  61. for item in children:
  62. walk_children(item)
  63. def walk_kconfig(RTT_ROOT, source_list):
  64. for parent, dirnames, filenames in os.walk(RTT_ROOT):
  65. if 'bsp' in parent:
  66. continue
  67. if '.git' in parent:
  68. continue
  69. if 'tools' in parent:
  70. continue
  71. if 'Kconfig' in filenames:
  72. pathfile = os.path.join(parent, 'Kconfig')
  73. source_list.append(pathfile)
  74. if 'KConfig' in filenames:
  75. pathfile = os.path.join(parent, 'KConfig')
  76. source_list.append(pathfile)
  77. def bsp_copy_files(bsp_root, dist_dir):
  78. # copy BSP files
  79. do_copy_folder(os.path.join(bsp_root), dist_dir,
  80. ignore_patterns('build', 'dist', '*.pyc', '*.old', '*.map', 'rtthread.bin', '.sconsign.dblite', '*.elf', '*.axf', 'cconfig.h'))
  81. def bsp_update_sconstruct(dist_dir):
  82. with open(os.path.join(dist_dir, 'SConstruct'), 'r') as f:
  83. data = f.readlines()
  84. with open(os.path.join(dist_dir, 'SConstruct'), 'w') as f:
  85. for line in data:
  86. if line.find('RTT_ROOT') != -1:
  87. if line.find('sys.path') != -1:
  88. f.write('# set RTT_ROOT\n')
  89. f.write('if not os.getenv("RTT_ROOT"): \n RTT_ROOT="rt-thread"\n\n')
  90. f.write(line)
  91. def bsp_update_kconfig(dist_dir):
  92. # change RTT_ROOT in Kconfig
  93. if not os.path.isfile(os.path.join(dist_dir, 'Kconfig')):
  94. return
  95. with open(os.path.join(dist_dir, 'Kconfig'), 'r') as f:
  96. data = f.readlines()
  97. with open(os.path.join(dist_dir, 'Kconfig'), 'w') as f:
  98. found = 0
  99. for line in data:
  100. if line.find('RTT_ROOT') != -1:
  101. found = 1
  102. if line.find('default') != -1 and found:
  103. position = line.find('default')
  104. line = line[0:position] + 'default: "rt-thread"\n'
  105. found = 0
  106. f.write(line)
  107. def bsp_update_kconfig_library(dist_dir):
  108. # change RTT_ROOT in Kconfig
  109. if not os.path.isfile(os.path.join(dist_dir, 'Kconfig')):
  110. return
  111. with open(os.path.join(dist_dir, 'Kconfig'), 'r') as f:
  112. data = f.readlines()
  113. with open(os.path.join(dist_dir, 'Kconfig'), 'w') as f:
  114. found = 0
  115. for line in data:
  116. if line.find('RTT_ROOT') != -1:
  117. found = 1
  118. if line.find('../libraries') != -1 and found:
  119. position = line.find('../libraries')
  120. line = line[0:position] + 'libraries/Kconfig"\n'
  121. found = 0
  122. f.write(line)
  123. def bs_update_ide_project(bsp_root, rtt_root):
  124. import subprocess
  125. # default update the projects which have template file
  126. tgt_dict = {'mdk4':('keil', 'armcc'),
  127. 'mdk5':('keil', 'armcc'),
  128. 'iar':('iar', 'iar'),
  129. 'vs':('msvc', 'cl'),
  130. 'vs2012':('msvc', 'cl'),
  131. 'cdk':('gcc', 'gcc')}
  132. scons_env = os.environ.copy()
  133. scons_env['RTT_ROOT'] = rtt_root
  134. for item in tgt_dict:
  135. child = subprocess.Popen('scons --target=' + item, cwd=bsp_root, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  136. stdout, stderr = child.communicate()
  137. if child.returncode == 0:
  138. print('update %s project' % item)
  139. def zip_dist(dist_dir, dist_name):
  140. import zipfile
  141. zip_filename = os.path.join(dist_dir)
  142. zip = zipfile.ZipFile(zip_filename + '.zip', 'w')
  143. pre_len = len(os.path.dirname(dist_dir))
  144. for parent, dirnames, filenames in os.walk(dist_dir):
  145. for filename in filenames:
  146. pathfile = os.path.join(parent, filename)
  147. arcname = pathfile[pre_len:].strip(os.path.sep)
  148. zip.write(pathfile, arcname)
  149. zip.close()
  150. def MkDist_Strip(program, BSP_ROOT, RTT_ROOT, Env):
  151. global source_list
  152. print('make distribution and strip useless files....')
  153. dist_name = os.path.basename(BSP_ROOT)
  154. dist_dir = os.path.join(BSP_ROOT, 'dist-strip', dist_name)
  155. target_path = os.path.join(dist_dir, 'rt-thread')
  156. print('=> %s' % os.path.basename(BSP_ROOT))
  157. bsp_copy_files(BSP_ROOT, dist_dir)
  158. # copy stm32 bsp libiary files
  159. if os.path.basename(os.path.dirname(BSP_ROOT)) == 'stm32':
  160. print("=> copy stm32 bsp library")
  161. library_path = os.path.join(os.path.dirname(BSP_ROOT), 'libraries')
  162. library_dir = os.path.join(dist_dir, 'libraries')
  163. bsp_copy_files(os.path.join(library_path, 'HAL_Drivers'), os.path.join(library_dir, 'HAL_Drivers'))
  164. bsp_copy_files(os.path.join(library_path, Env['bsp_lib_type']), os.path.join(library_dir, Env['bsp_lib_type']))
  165. shutil.copyfile(os.path.join(library_path, 'Kconfig'), os.path.join(library_dir, 'Kconfig'))
  166. # do bsp special dist handle
  167. if 'dist_handle' in Env:
  168. print("=> start dist handle")
  169. dist_handle = Env['dist_handle']
  170. dist_handle(BSP_ROOT)
  171. # get all source files from program
  172. for item in program:
  173. walk_children(item)
  174. source_list.sort()
  175. # copy the source files without libcpu and components/libc in RT-Thread
  176. target_list = []
  177. libcpu_dir = os.path.join(RTT_ROOT, 'libcpu').lower()
  178. libc_dir = os.path.join(RTT_ROOT, 'components', 'libc', 'compilers').lower()
  179. sal_dir = os.path.join(RTT_ROOT, 'components', 'net', 'sal_socket').lower()
  180. sources_include_sal = False
  181. for src in source_list:
  182. if src.lower().startswith(BSP_ROOT.lower()):
  183. continue
  184. # skip libc and libcpu dir
  185. if src.lower().startswith(libcpu_dir):
  186. continue
  187. if src.lower().startswith(libc_dir):
  188. continue
  189. if src.lower().startswith(sal_dir):
  190. sources_include_sal = True
  191. continue
  192. if src.lower().startswith(RTT_ROOT.lower()):
  193. target_list.append(src)
  194. source_list = target_list
  195. # get source directory
  196. src_dir = []
  197. for src in source_list:
  198. src = src.replace(RTT_ROOT, '')
  199. if src[0] == os.sep or src[0] == '/':
  200. src = src[1:]
  201. path = os.path.dirname(src)
  202. sub_path = path.split(os.sep)
  203. full_path = RTT_ROOT
  204. for item in sub_path:
  205. full_path = os.path.join(full_path, item)
  206. if full_path not in src_dir:
  207. src_dir.append(full_path)
  208. # add all of SConscript files
  209. for item in src_dir:
  210. source_list.append(os.path.join(item, 'SConscript'))
  211. # add all of Kconfig files
  212. walk_kconfig(RTT_ROOT, source_list)
  213. # copy all files to target directory
  214. source_list.sort()
  215. for src in source_list:
  216. dst = src.replace(RTT_ROOT, '')
  217. if dst[0] == os.sep or dst[0] == '/':
  218. dst = dst[1:]
  219. print('=> %s' % dst)
  220. dst = os.path.join(target_path, dst)
  221. do_copy_file(src, dst)
  222. # copy tools directory
  223. print('=> tools')
  224. do_copy_folder(os.path.join(RTT_ROOT, 'tools'), os.path.join(target_path, 'tools'), ignore_patterns('*.pyc'))
  225. do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(target_path, 'Kconfig'))
  226. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(target_path, 'AUTHORS'))
  227. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(target_path, 'COPYING'))
  228. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(target_path, 'README.md'))
  229. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(target_path, 'README_zh.md'))
  230. print('=> %s' % os.path.join('components', 'libc', 'compilers'))
  231. do_copy_folder(os.path.join(RTT_ROOT, 'components', 'libc', 'compilers'), os.path.join(target_path, 'components', 'libc', 'compilers'))
  232. if sources_include_sal:
  233. print('=> %s' % os.path.join('components', 'net', 'sal_socket'))
  234. do_copy_folder(os.path.join(RTT_ROOT, 'components', 'net', 'sal_socket'), os.path.join(target_path, 'components', 'net', 'sal_socket'))
  235. # copy all libcpu/ARCH directory
  236. import rtconfig
  237. print('=> %s' % (os.path.join('libcpu', rtconfig.ARCH, rtconfig.CPU)))
  238. do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH, rtconfig.CPU), os.path.join(target_path, 'libcpu', rtconfig.ARCH, rtconfig.CPU))
  239. if os.path.exists(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH, 'common')):
  240. print('=> %s' % (os.path.join('libcpu', rtconfig.ARCH, 'common')))
  241. do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH, 'common'), os.path.join(target_path, 'libcpu', rtconfig.ARCH, 'common'))
  242. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'Kconfig'), os.path.join(target_path, 'libcpu', 'Kconfig'))
  243. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'SConscript'), os.path.join(target_path, 'libcpu', 'SConscript'))
  244. # change RTT_ROOT in SConstruct
  245. bsp_update_sconstruct(dist_dir)
  246. # change RTT_ROOT in Kconfig
  247. bsp_update_kconfig(dist_dir)
  248. bsp_update_kconfig_library(dist_dir)
  249. # update all project files
  250. bs_update_ide_project(dist_dir, target_path)
  251. # make zip package
  252. zip_dist(dist_dir, dist_name)
  253. print('done!')
  254. def MkDist(program, BSP_ROOT, RTT_ROOT, Env):
  255. print('make distribution....')
  256. dist_name = os.path.basename(BSP_ROOT)
  257. dist_dir = os.path.join(BSP_ROOT, 'dist', dist_name)
  258. target_path = os.path.join(dist_dir, 'rt-thread')
  259. # copy BSP files
  260. print('=> %s' % os.path.basename(BSP_ROOT))
  261. bsp_copy_files(BSP_ROOT, dist_dir)
  262. # copy stm32 bsp libiary files
  263. if os.path.basename(os.path.dirname(BSP_ROOT)) == 'stm32':
  264. print("=> copy stm32 bsp library")
  265. library_path = os.path.join(os.path.dirname(BSP_ROOT), 'libraries')
  266. library_dir = os.path.join(dist_dir, 'libraries')
  267. bsp_copy_files(os.path.join(library_path, 'HAL_Drivers'), os.path.join(library_dir, 'HAL_Drivers'))
  268. bsp_copy_files(os.path.join(library_path, Env['bsp_lib_type']), os.path.join(library_dir, Env['bsp_lib_type']))
  269. shutil.copyfile(os.path.join(library_path, 'Kconfig'), os.path.join(library_dir, 'Kconfig'))
  270. # do bsp special dist handle
  271. if 'dist_handle' in Env:
  272. print("=> start dist handle")
  273. dist_handle = Env['dist_handle']
  274. dist_handle(BSP_ROOT)
  275. # copy tools directory
  276. print('=> components')
  277. do_copy_folder(os.path.join(RTT_ROOT, 'components'), os.path.join(target_path, 'components'))
  278. # skip documentation directory
  279. # skip examples
  280. # copy include directory
  281. print('=> include')
  282. do_copy_folder(os.path.join(RTT_ROOT, 'include'), os.path.join(target_path, 'include'))
  283. # copy all libcpu/ARCH directory
  284. print('=> libcpu')
  285. import rtconfig
  286. do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH), os.path.join(target_path, 'libcpu', rtconfig.ARCH))
  287. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'Kconfig'), os.path.join(target_path, 'libcpu', 'Kconfig'))
  288. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'SConscript'), os.path.join(target_path, 'libcpu', 'SConscript'))
  289. # copy src directory
  290. print('=> src')
  291. do_copy_folder(os.path.join(RTT_ROOT, 'src'), os.path.join(target_path, 'src'))
  292. # copy tools directory
  293. print('=> tools')
  294. do_copy_folder(os.path.join(RTT_ROOT, 'tools'), os.path.join(target_path, 'tools'), ignore_patterns('*.pyc'))
  295. do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(target_path, 'Kconfig'))
  296. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(target_path, 'AUTHORS'))
  297. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(target_path, 'COPYING'))
  298. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(target_path, 'README.md'))
  299. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(target_path, 'README_zh.md'))
  300. # change RTT_ROOT in SConstruct
  301. bsp_update_sconstruct(dist_dir)
  302. # change RTT_ROOT in Kconfig
  303. bsp_update_kconfig(dist_dir)
  304. bsp_update_kconfig_library(dist_dir)
  305. # update all project files
  306. bs_update_ide_project(dist_dir, target_path)
  307. # make zip package
  308. zip_dist(dist_dir, dist_name)
  309. print('done!')