mkdist.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. for line in data:
  111. if line.find('RTT_DIR') != -1 and line.find(':=') != -1:
  112. line = 'RTT_DIR := rt-thread\n'
  113. f.write(line)
  114. def bsp_update_kconfig_library(dist_dir):
  115. # change RTT_ROOT in Kconfig
  116. if not os.path.isfile(os.path.join(dist_dir, 'Kconfig')):
  117. return
  118. with open(os.path.join(dist_dir, 'Kconfig'), 'r') as f:
  119. data = f.readlines()
  120. with open(os.path.join(dist_dir, 'Kconfig'), 'w') as f:
  121. for line in data:
  122. if line.find('source') != -1 and line.find('../libraries') != -1:
  123. line = line.replace('../libraries', 'libraries')
  124. f.write(line)
  125. # change board/kconfig path
  126. if not os.path.isfile(os.path.join(dist_dir, 'board/Kconfig')):
  127. return
  128. with open(os.path.join(dist_dir, 'board/Kconfig'), 'r') as f:
  129. data = f.readlines()
  130. with open(os.path.join(dist_dir, 'board/Kconfig'), 'w') as f:
  131. for line in data:
  132. if line.find('source') != -1 and line.find('../libraries') != -1:
  133. line = line.replace('../libraries', 'libraries')
  134. f.write(line)
  135. def zip_dist(dist_dir, dist_name):
  136. import zipfile
  137. zip_filename = os.path.join(dist_dir)
  138. zip = zipfile.ZipFile(zip_filename + '.zip', 'w')
  139. pre_len = len(os.path.dirname(dist_dir))
  140. for parent, dirnames, filenames in os.walk(dist_dir):
  141. for filename in filenames:
  142. pathfile = os.path.join(parent, filename)
  143. arcname = pathfile[pre_len:].strip(os.path.sep)
  144. zip.write(pathfile, arcname)
  145. zip.close()
  146. def MkDist(program, BSP_ROOT, RTT_ROOT, Env, project_name, project_path):
  147. print('make distribution....')
  148. if project_path == None:
  149. dist_dir = os.path.join(BSP_ROOT, 'dist', project_name)
  150. else:
  151. dist_dir = project_path
  152. rtt_dir_path = os.path.join(dist_dir, 'rt-thread')
  153. # copy BSP files
  154. print('=> %s' % os.path.basename(BSP_ROOT))
  155. bsp_copy_files(BSP_ROOT, dist_dir)
  156. # do bsp special dist handle
  157. if 'dist_handle' in Env:
  158. print("=> start dist handle")
  159. dist_handle = Env['dist_handle']
  160. dist_handle(BSP_ROOT, dist_dir)
  161. # copy tools directory
  162. print('=> components')
  163. do_copy_folder(os.path.join(RTT_ROOT, 'components'), os.path.join(rtt_dir_path, 'components'))
  164. # skip documentation directory
  165. # skip examples
  166. # copy include directory
  167. print('=> include')
  168. do_copy_folder(os.path.join(RTT_ROOT, 'include'), os.path.join(rtt_dir_path, 'include'))
  169. # copy all libcpu/ARCH directory
  170. print('=> libcpu')
  171. import rtconfig
  172. do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH), os.path.join(rtt_dir_path, 'libcpu', rtconfig.ARCH))
  173. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'Kconfig'), os.path.join(rtt_dir_path, 'libcpu', 'Kconfig'))
  174. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'SConscript'), os.path.join(rtt_dir_path, 'libcpu', 'SConscript'))
  175. # copy src directory
  176. print('=> src')
  177. do_copy_folder(os.path.join(RTT_ROOT, 'src'), os.path.join(rtt_dir_path, 'src'))
  178. # copy tools directory
  179. print('=> tools')
  180. do_copy_folder(os.path.join(RTT_ROOT, 'tools'), os.path.join(rtt_dir_path, 'tools'), ignore_patterns('*.pyc'))
  181. do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(rtt_dir_path, 'Kconfig'))
  182. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(rtt_dir_path, 'AUTHORS'))
  183. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(rtt_dir_path, 'COPYING'))
  184. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(rtt_dir_path, 'README.md'))
  185. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(rtt_dir_path, 'README_zh.md'))
  186. print('Update configuration files...')
  187. # change RTT_ROOT in SConstruct
  188. bsp_update_sconstruct(dist_dir)
  189. # change RTT_ROOT in Kconfig
  190. bsp_update_kconfig(dist_dir)
  191. bsp_update_kconfig_library(dist_dir)
  192. # delete testcases in Kconfig
  193. bsp_update_kconfig_testcases(dist_dir)
  194. target_project_type = GetOption('target')
  195. if target_project_type:
  196. child = subprocess.Popen('scons --target={} --project-name="{}"'.format(target_project_type, project_name), cwd=dist_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  197. stdout, stderr = child.communicate()
  198. if child.returncode == 0:
  199. print(stdout)
  200. else:
  201. print(stderr)
  202. else:
  203. print('suggest to use command scons --dist [--target=xxx] [--project-name="xxx"] [--project-path="xxx"]')
  204. # make zip package
  205. if project_path == None:
  206. zip_dist(dist_dir, project_name)
  207. print('dist project successfully!')