mkdist.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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/drivers/Kconfig') != -1:
  143. position = line.find('../libraries/HAL_Drivers/drivers/Kconfig')
  144. line = line[0:position] + 'libraries/HAL_Drivers/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(program, BSP_ROOT, RTT_ROOT, Env, project_name, project_path):
  158. print('make distribution....')
  159. if project_path == None:
  160. dist_dir = os.path.join(BSP_ROOT, 'dist', project_name)
  161. else:
  162. dist_dir = project_path
  163. rtt_dir_path = os.path.join(dist_dir, 'rt-thread')
  164. # copy BSP files
  165. print('=> %s' % os.path.basename(BSP_ROOT))
  166. bsp_copy_files(BSP_ROOT, dist_dir)
  167. # do bsp special dist handle
  168. if 'dist_handle' in Env:
  169. print("=> start dist handle")
  170. dist_handle = Env['dist_handle']
  171. dist_handle(BSP_ROOT, dist_dir)
  172. # copy tools directory
  173. print('=> components')
  174. do_copy_folder(os.path.join(RTT_ROOT, 'components'), os.path.join(rtt_dir_path, 'components'))
  175. # skip documentation directory
  176. # skip examples
  177. # copy include directory
  178. print('=> include')
  179. do_copy_folder(os.path.join(RTT_ROOT, 'include'), os.path.join(rtt_dir_path, 'include'))
  180. # copy all libcpu/ARCH directory
  181. print('=> libcpu')
  182. import rtconfig
  183. do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH), os.path.join(rtt_dir_path, 'libcpu', rtconfig.ARCH))
  184. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'Kconfig'), os.path.join(rtt_dir_path, 'libcpu', 'Kconfig'))
  185. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'SConscript'), os.path.join(rtt_dir_path, 'libcpu', 'SConscript'))
  186. # copy src directory
  187. print('=> src')
  188. do_copy_folder(os.path.join(RTT_ROOT, 'src'), os.path.join(rtt_dir_path, 'src'))
  189. # copy tools directory
  190. print('=> tools')
  191. do_copy_folder(os.path.join(RTT_ROOT, 'tools'), os.path.join(rtt_dir_path, 'tools'), ignore_patterns('*.pyc'))
  192. do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(rtt_dir_path, 'Kconfig'))
  193. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(rtt_dir_path, 'AUTHORS'))
  194. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(rtt_dir_path, 'COPYING'))
  195. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(rtt_dir_path, 'README.md'))
  196. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(rtt_dir_path, 'README_zh.md'))
  197. print('Update configuration files...')
  198. # change RTT_ROOT in SConstruct
  199. bsp_update_sconstruct(dist_dir)
  200. # change RTT_ROOT in Kconfig
  201. bsp_update_kconfig(dist_dir)
  202. bsp_update_kconfig_library(dist_dir)
  203. # delete testcases in Kconfig
  204. bsp_update_kconfig_testcases(dist_dir)
  205. target_project_type = GetOption('target')
  206. if target_project_type:
  207. child = subprocess.Popen('scons --target={} --project-name="{}"'.format(target_project_type, project_name), cwd=dist_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  208. stdout, stderr = child.communicate()
  209. if child.returncode == 0:
  210. print(stdout)
  211. else:
  212. print(stderr)
  213. else:
  214. print('suggest to use command scons --dist [--target=xxx] [--project-name="xxx"] [--project-path="xxx"]')
  215. # make zip package
  216. if project_path == None:
  217. zip_dist(dist_dir, project_name)
  218. print('dist project successfully!')