mkdist.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 bs_update_ide_project(bsp_root, rtt_root):
  108. import subprocess
  109. # default update the projects which have template file
  110. tgt_dict = {'mdk4':('keil', 'armcc'),
  111. 'mdk5':('keil', 'armcc'),
  112. 'iar':('iar', 'iar'),
  113. 'vs':('msvc', 'cl'),
  114. 'vs2012':('msvc', 'cl'),
  115. 'cdk':('gcc', 'gcc')}
  116. scons_env = os.environ.copy()
  117. scons_env['RTT_ROOT'] = rtt_root
  118. for item in tgt_dict:
  119. child = subprocess.Popen('scons --target=' + item, cwd=bsp_root, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  120. stdout, stderr = child.communicate()
  121. if child.returncode == 0:
  122. print('update %s project' % item)
  123. def zip_dist(dist_dir, dist_name):
  124. import zipfile
  125. zip_filename = os.path.join(dist_dir)
  126. zip = zipfile.ZipFile(zip_filename + '.zip', 'w')
  127. pre_len = len(os.path.dirname(dist_dir))
  128. for parent, dirnames, filenames in os.walk(dist_dir):
  129. for filename in filenames:
  130. pathfile = os.path.join(parent, filename)
  131. arcname = pathfile[pre_len:].strip(os.path.sep)
  132. zip.write(pathfile, arcname)
  133. zip.close()
  134. def MkDist_Strip(program, BSP_ROOT, RTT_ROOT, Env):
  135. global source_list
  136. print('make distribution and strip useless files....')
  137. dist_name = os.path.basename(BSP_ROOT)
  138. dist_dir = os.path.join(BSP_ROOT, 'dist-strip', dist_name)
  139. target_path = os.path.join(dist_dir, 'rt-thread')
  140. print('=> %s' % os.path.basename(BSP_ROOT))
  141. bsp_copy_files(BSP_ROOT, dist_dir)
  142. # get all source files from program
  143. for item in program:
  144. walk_children(item)
  145. source_list.sort()
  146. # copy the source files without libcpu and components/libc in RT-Thread
  147. target_list = []
  148. libcpu_dir = os.path.join(RTT_ROOT, 'libcpu').lower()
  149. libc_dir = os.path.join(RTT_ROOT, 'components', 'libc', 'compilers').lower()
  150. sal_dir = os.path.join(RTT_ROOT, 'components', 'net', 'sal_socket').lower()
  151. sources_include_sal = False
  152. for src in source_list:
  153. if src.lower().startswith(BSP_ROOT.lower()):
  154. continue
  155. # skip libc and libcpu dir
  156. if src.lower().startswith(libcpu_dir):
  157. continue
  158. if src.lower().startswith(libc_dir):
  159. continue
  160. if src.lower().startswith(sal_dir):
  161. sources_include_sal = True
  162. continue
  163. if src.lower().startswith(RTT_ROOT.lower()):
  164. target_list.append(src)
  165. source_list = target_list
  166. # get source directory
  167. src_dir = []
  168. for src in source_list:
  169. src = src.replace(RTT_ROOT, '')
  170. if src[0] == os.sep or src[0] == '/':
  171. src = src[1:]
  172. path = os.path.dirname(src)
  173. sub_path = path.split(os.sep)
  174. full_path = RTT_ROOT
  175. for item in sub_path:
  176. full_path = os.path.join(full_path, item)
  177. if full_path not in src_dir:
  178. src_dir.append(full_path)
  179. # add all of SConscript files
  180. for item in src_dir:
  181. source_list.append(os.path.join(item, 'SConscript'))
  182. # add all of Kconfig files
  183. walk_kconfig(RTT_ROOT, source_list)
  184. # copy all files to target directory
  185. source_list.sort()
  186. for src in source_list:
  187. dst = src.replace(RTT_ROOT, '')
  188. if dst[0] == os.sep or dst[0] == '/':
  189. dst = dst[1:]
  190. print('=> %s' % dst)
  191. dst = os.path.join(target_path, dst)
  192. do_copy_file(src, dst)
  193. # copy tools directory
  194. print('=> tools')
  195. do_copy_folder(os.path.join(RTT_ROOT, 'tools'), os.path.join(target_path, 'tools'), ignore_patterns('*.pyc'))
  196. do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(target_path, 'Kconfig'))
  197. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(target_path, 'AUTHORS'))
  198. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(target_path, 'COPYING'))
  199. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(target_path, 'README.md'))
  200. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(target_path, 'README_zh.md'))
  201. print('=> %s' % os.path.join('components', 'libc', 'compilers'))
  202. do_copy_folder(os.path.join(RTT_ROOT, 'components', 'libc', 'compilers'), os.path.join(target_path, 'components', 'libc', 'compilers'))
  203. if sources_include_sal:
  204. print('=> %s' % os.path.join('components', 'net', 'sal_socket'))
  205. do_copy_folder(os.path.join(RTT_ROOT, 'components', 'net', 'sal_socket'), os.path.join(target_path, 'components', 'net', 'sal_socket'))
  206. # copy all libcpu/ARCH directory
  207. import rtconfig
  208. print('=> %s' % (os.path.join('libcpu', rtconfig.ARCH, rtconfig.CPU)))
  209. do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH, rtconfig.CPU), os.path.join(target_path, 'libcpu', rtconfig.ARCH, rtconfig.CPU))
  210. if os.path.exists(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH, 'common')):
  211. print('=> %s' % (os.path.join('libcpu', rtconfig.ARCH, 'common')))
  212. do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH, 'common'), os.path.join(target_path, 'libcpu', rtconfig.ARCH, 'common'))
  213. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'Kconfig'), os.path.join(target_path, 'libcpu', 'Kconfig'))
  214. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'SConscript'), os.path.join(target_path, 'libcpu', 'SConscript'))
  215. # change RTT_ROOT in SConstruct
  216. bsp_update_sconstruct(dist_dir)
  217. # change RTT_ROOT in Kconfig
  218. bsp_update_kconfig(dist_dir)
  219. # update all project files
  220. bs_update_ide_project(dist_dir, target_path)
  221. # make zip package
  222. zip_dist(dist_dir, dist_name)
  223. print('done!')
  224. def MkDist(program, BSP_ROOT, RTT_ROOT, Env):
  225. print('make distribution....')
  226. dist_name = os.path.basename(BSP_ROOT)
  227. dist_dir = os.path.join(BSP_ROOT, 'dist', dist_name)
  228. target_path = os.path.join(dist_dir, 'rt-thread')
  229. # copy BSP files
  230. print('=> %s' % os.path.basename(BSP_ROOT))
  231. bsp_copy_files(BSP_ROOT, dist_dir)
  232. # copy tools directory
  233. print('=> components')
  234. do_copy_folder(os.path.join(RTT_ROOT, 'components'), os.path.join(target_path, 'components'))
  235. # skip documentation directory
  236. # skip examples
  237. # copy include directory
  238. print('=> include')
  239. do_copy_folder(os.path.join(RTT_ROOT, 'include'), os.path.join(target_path, 'include'))
  240. # copy all libcpu/ARCH directory
  241. print('=> libcpu')
  242. import rtconfig
  243. do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH), os.path.join(target_path, 'libcpu', rtconfig.ARCH))
  244. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'Kconfig'), os.path.join(target_path, 'libcpu', 'Kconfig'))
  245. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'SConscript'), os.path.join(target_path, 'libcpu', 'SConscript'))
  246. # copy src directory
  247. print('=> src')
  248. do_copy_folder(os.path.join(RTT_ROOT, 'src'), os.path.join(target_path, 'src'))
  249. # copy tools directory
  250. print('=> tools')
  251. do_copy_folder(os.path.join(RTT_ROOT, 'tools'), os.path.join(target_path, 'tools'), ignore_patterns('*.pyc'))
  252. do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(target_path, 'Kconfig'))
  253. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(target_path, 'AUTHORS'))
  254. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(target_path, 'COPYING'))
  255. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(target_path, 'README.md'))
  256. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(target_path, 'README_zh.md'))
  257. # change RTT_ROOT in SConstruct
  258. bsp_update_sconstruct(dist_dir)
  259. # change RTT_ROOT in Kconfig
  260. bsp_update_kconfig(dist_dir)
  261. # update all project files
  262. bs_update_ide_project(dist_dir, target_path)
  263. # make zip package
  264. zip_dist(dist_dir, dist_name)
  265. print('done!')