mkdist.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 MakeCopy(program, BSP_ROOT, RTT_ROOT, Env):
  78. global source_list
  79. target_path = os.path.join(BSP_ROOT, 'rt-thread')
  80. if target_path.startswith(RTT_ROOT):
  81. print('please use scons --copy to copy rt-thread to local bsp')
  82. return
  83. for item in program:
  84. walk_children(item)
  85. source_list.sort()
  86. # fill source file in RT-Thread
  87. target_list = []
  88. for src in source_list:
  89. if Env['PLATFORM'] == 'win32':
  90. src = src.lower()
  91. if src.startswith(RTT_ROOT):
  92. target_list.append(src)
  93. source_list = target_list
  94. # get source directory
  95. src_dir = []
  96. for src in source_list:
  97. src = src.replace(RTT_ROOT, '')
  98. if src[0] == os.sep or src[0] == '/':
  99. src = src[1:]
  100. path = os.path.dirname(src)
  101. sub_path = path.split(os.sep)
  102. full_path = RTT_ROOT
  103. for item in sub_path:
  104. full_path = os.path.join(full_path, item)
  105. if full_path not in src_dir:
  106. src_dir.append(full_path)
  107. for item in src_dir:
  108. source_list.append(os.path.join(item, 'SConscript'))
  109. walk_kconfig(RTT_ROOT, source_list)
  110. for src in source_list:
  111. dst = src.replace(RTT_ROOT, '')
  112. if dst[0] == os.sep or dst[0] == '/':
  113. dst = dst[1:]
  114. print '=> ', dst
  115. dst = os.path.join(target_path, dst)
  116. do_copy_file(src, dst)
  117. # copy tools directory
  118. print("=> tools")
  119. do_copy_folder(os.path.join(RTT_ROOT, "tools"), os.path.join(target_path, "tools"), ignore_patterns('*.pyc'))
  120. do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(target_path, 'Kconfig'))
  121. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(target_path, 'AUTHORS'))
  122. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(target_path, 'COPYING'))
  123. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(target_path, 'README.md'))
  124. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(target_path, 'README_zh.md'))
  125. print('=> libc')
  126. do_copy_folder(os.path.join(RTT_ROOT, "components", 'libc', 'compilers'), os.path.join(target_path, "components", 'libc', 'compilers'))
  127. print('done!')
  128. def MakeCopyHeader(program, BSP_ROOT, RTT_ROOT, Env):
  129. global source_list
  130. global source_ext
  131. source_ext = []
  132. source_ext = ["h", "xpm"]
  133. target_path = os.path.join(BSP_ROOT, 'rt-thread')
  134. if target_path.startswith(RTT_ROOT):
  135. print('please use scons --copy-header to copy header files only')
  136. return
  137. for item in program:
  138. walk_children(item)
  139. source_list.sort()
  140. # fill source file in RT-Thread
  141. target_list = []
  142. for src in source_list:
  143. if Env['PLATFORM'] == 'win32':
  144. src = src.lower()
  145. if src.startswith(RTT_ROOT):
  146. target_list.append(src)
  147. source_list = target_list
  148. for src in source_list:
  149. dst = src.replace(RTT_ROOT, '')
  150. if dst[0] == os.sep or dst[0] == '/':
  151. dst = dst[1:]
  152. print '=> ', dst
  153. dst = os.path.join(target_path, dst)
  154. do_copy_file(src, dst)
  155. # copy tools directory
  156. print "=> tools"
  157. do_copy_folder(os.path.join(RTT_ROOT, "tools"), os.path.join(target_path, "tools"), ignore_patterns('*.pyc'))
  158. do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(target_path, 'Kconfig'))
  159. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(target_path, 'AUTHORS'))
  160. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(target_path, 'COPYING'))
  161. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(target_path, 'README.md'))
  162. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(target_path, 'README_zh.md'))
  163. print('done!')
  164. def MkDist(program, BSP_ROOT, RTT_ROOT, Env):
  165. print("make distribution....")
  166. dist_name = os.path.basename(BSP_ROOT)
  167. dist_dir = os.path.join(BSP_ROOT, 'dist', dist_name)
  168. # copy BSP files
  169. do_copy_folder(os.path.join(BSP_ROOT), dist_dir,
  170. ignore_patterns('build', 'dist', '*.pyc', '*.old', '*.map', 'rtthread.bin', '.sconsign.dblite', '*.elf', '*.axf', 'cconfig.h'))
  171. global source_list
  172. target_path = os.path.join(dist_dir, 'rt-thread')
  173. for item in program:
  174. walk_children(item)
  175. source_list.sort()
  176. # copy the source files in RT-Thread
  177. target_list = []
  178. for src in source_list:
  179. if src.lower().startswith(BSP_ROOT.lower()):
  180. continue
  181. if src.lower().startswith(RTT_ROOT.lower()):
  182. target_list.append(src)
  183. source_list = target_list
  184. # get source directory
  185. src_dir = []
  186. for src in source_list:
  187. src = src.replace(RTT_ROOT, '')
  188. if src[0] == os.sep or src[0] == '/':
  189. src = src[1:]
  190. path = os.path.dirname(src)
  191. sub_path = path.split(os.sep)
  192. full_path = RTT_ROOT
  193. for item in sub_path:
  194. full_path = os.path.join(full_path, item)
  195. if full_path not in src_dir:
  196. src_dir.append(full_path)
  197. for item in src_dir:
  198. source_list.append(os.path.join(item, 'SConscript'))
  199. # add all of Kconfig files
  200. walk_kconfig(RTT_ROOT, source_list)
  201. source_list.sort()
  202. for src in source_list:
  203. dst = src.replace(RTT_ROOT, '')
  204. if dst[0] == os.sep or dst[0] == '/':
  205. dst = dst[1:]
  206. print('=> %s' % dst)
  207. dst = os.path.join(target_path, dst)
  208. do_copy_file(src, dst)
  209. # copy tools directory
  210. print("=> tools")
  211. do_copy_folder(os.path.join(RTT_ROOT, "tools"), os.path.join(target_path, "tools"), ignore_patterns('*.pyc'))
  212. do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(target_path, 'Kconfig'))
  213. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(target_path, 'AUTHORS'))
  214. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(target_path, 'COPYING'))
  215. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(target_path, 'README.md'))
  216. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(target_path, 'README_zh.md'))
  217. print('=> libc')
  218. do_copy_folder(os.path.join(RTT_ROOT, "components", 'libc', 'compilers'), os.path.join(target_path, "components", 'libc', 'compilers'))
  219. # change RTT_ROOT in SConstruct
  220. try:
  221. sconstruct = file(os.path.join(BSP_ROOT, 'SConstruct'))
  222. out = file(os.path.join(dist_dir, 'SConstruct'), 'w')
  223. for line in sconstruct:
  224. if line.find('RTT_ROOT') != -1:
  225. if line.find('sys.path') != -1:
  226. out.write('# set RTT_ROOT\n')
  227. out.write("if not os.getenv('RTT_ROOT'): \n RTT_ROOT='rt-thread'\n\n")
  228. out.write(line)
  229. except :
  230. print('')
  231. # change RTT_ROOT in Kconfig
  232. try:
  233. if os.path.exists(os.path.join(BSP_ROOT, 'Kconfig')):
  234. Kconfig = file(os.path.join(BSP_ROOT, 'Kconfig'))
  235. out = file(os.path.join(dist_dir, 'Kconfig'), 'w')
  236. found = 0
  237. for line in Kconfig:
  238. if line.find('RTT_ROOT') != -1:
  239. found = 1
  240. if line.find('default') != -1 and found:
  241. position = line.find('default')
  242. line = line[0:position] + 'default: "rt-thread"\n'
  243. found = 0
  244. out.write(line)
  245. out.close()
  246. except :
  247. print('')
  248. # make zip package
  249. import zipfile
  250. zip_filename = os.path.join(BSP_ROOT, 'dist', dist_name)
  251. zip = zipfile.ZipFile(zip_filename + ".zip", 'w')
  252. pre_len = len(os.path.dirname(dist_dir))
  253. for parent, dirnames, filenames in os.walk(dist_dir):
  254. for filename in filenames:
  255. pathfile = os.path.join(parent, filename)
  256. arcname = pathfile[pre_len:].strip(os.path.sep)
  257. zip.write(pathfile, arcname)
  258. zip.close()
  259. print('done!')