mkdist.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. for src in source_list:
  151. if src.lower().startswith(BSP_ROOT.lower()):
  152. continue
  153. # skip libc and libcpu dir
  154. if src.lower().startswith(libcpu_dir):
  155. continue
  156. if src.lower().startswith(libc_dir):
  157. continue
  158. if src.lower().startswith(RTT_ROOT.lower()):
  159. target_list.append(src)
  160. source_list = target_list
  161. # get source directory
  162. src_dir = []
  163. for src in source_list:
  164. src = src.replace(RTT_ROOT, '')
  165. if src[0] == os.sep or src[0] == '/':
  166. src = src[1:]
  167. path = os.path.dirname(src)
  168. sub_path = path.split(os.sep)
  169. full_path = RTT_ROOT
  170. for item in sub_path:
  171. full_path = os.path.join(full_path, item)
  172. if full_path not in src_dir:
  173. src_dir.append(full_path)
  174. # add all of SConscript files
  175. for item in src_dir:
  176. source_list.append(os.path.join(item, 'SConscript'))
  177. # add all of Kconfig files
  178. walk_kconfig(RTT_ROOT, source_list)
  179. # copy all files to target directory
  180. source_list.sort()
  181. for src in source_list:
  182. dst = src.replace(RTT_ROOT, '')
  183. if dst[0] == os.sep or dst[0] == '/':
  184. dst = dst[1:]
  185. print('=> %s' % dst)
  186. dst = os.path.join(target_path, dst)
  187. do_copy_file(src, dst)
  188. # copy tools directory
  189. print('=> tools')
  190. do_copy_folder(os.path.join(RTT_ROOT, 'tools'), os.path.join(target_path, 'tools'), ignore_patterns('*.pyc'))
  191. do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(target_path, 'Kconfig'))
  192. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(target_path, 'AUTHORS'))
  193. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(target_path, 'COPYING'))
  194. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(target_path, 'README.md'))
  195. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(target_path, 'README_zh.md'))
  196. print('=> %s' % os.path.join('components', 'libc', 'compilers'))
  197. do_copy_folder(os.path.join(RTT_ROOT, 'components', 'libc', 'compilers'), os.path.join(target_path, 'components', 'libc', 'compilers'))
  198. # copy all libcpu/ARCH directory
  199. print('=> libcpu')
  200. import rtconfig
  201. do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH, rtconfig.CPU), os.path.join(target_path, 'libcpu', rtconfig.ARCH, rtconfig.CPU))
  202. if os.path.exists(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH, 'common')):
  203. do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH, 'common'), os.path.join(target_path, 'libcpu', rtconfig.ARCH, 'common'))
  204. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'Kconfig'), os.path.join(target_path, 'libcpu', 'Kconfig'))
  205. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'SConscript'), os.path.join(target_path, 'libcpu', 'SConscript'))
  206. # change RTT_ROOT in SConstruct
  207. bsp_update_sconstruct(dist_dir)
  208. # change RTT_ROOT in Kconfig
  209. bsp_update_kconfig(dist_dir)
  210. # update all project files
  211. bs_update_ide_project(dist_dir, target_path)
  212. # make zip package
  213. zip_dist(dist_dir, dist_name)
  214. print('done!')
  215. def MkDist(program, BSP_ROOT, RTT_ROOT, Env):
  216. print('make distribution....')
  217. dist_name = os.path.basename(BSP_ROOT)
  218. dist_dir = os.path.join(BSP_ROOT, 'dist', dist_name)
  219. target_path = os.path.join(dist_dir, 'rt-thread')
  220. # copy BSP files
  221. print('=> %s' % os.path.basename(BSP_ROOT))
  222. bsp_copy_files(BSP_ROOT, dist_dir)
  223. # copy tools directory
  224. print('=> components')
  225. do_copy_folder(os.path.join(RTT_ROOT, 'components'), os.path.join(target_path, 'components'))
  226. # skip documentation directory
  227. # skip examples
  228. # copy include directory
  229. print('=> include')
  230. do_copy_folder(os.path.join(RTT_ROOT, 'include'), os.path.join(target_path, 'include'))
  231. # copy all libcpu/ARCH directory
  232. print('=> libcpu')
  233. import rtconfig
  234. do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH), os.path.join(target_path, 'libcpu', rtconfig.ARCH))
  235. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'Kconfig'), os.path.join(target_path, 'libcpu', 'Kconfig'))
  236. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'SConscript'), os.path.join(target_path, 'libcpu', 'SConscript'))
  237. # copy src directory
  238. print('=> src')
  239. do_copy_folder(os.path.join(RTT_ROOT, 'src'), os.path.join(target_path, 'src'))
  240. # copy tools directory
  241. print('=> tools')
  242. do_copy_folder(os.path.join(RTT_ROOT, 'tools'), os.path.join(target_path, 'tools'), ignore_patterns('*.pyc'))
  243. do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(target_path, 'Kconfig'))
  244. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(target_path, 'AUTHORS'))
  245. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(target_path, 'COPYING'))
  246. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(target_path, 'README.md'))
  247. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(target_path, 'README_zh.md'))
  248. # change RTT_ROOT in SConstruct
  249. bsp_update_sconstruct(dist_dir)
  250. # change RTT_ROOT in Kconfig
  251. bsp_update_kconfig(dist_dir)
  252. # update all project files
  253. bs_update_ide_project(dist_dir, target_path)
  254. # make zip package
  255. zip_dist(dist_dir, dist_name)
  256. print('done!')