mkdist.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_update_sconstruct(dist_dir):
  78. with open(os.path.join(dist_dir, 'SConstruct'), "r") as f:
  79. data = f.readlines()
  80. with open(os.path.join(dist_dir, 'SConstruct'), "w") as f:
  81. for line in data:
  82. if line.find('RTT_ROOT') != -1:
  83. if line.find('sys.path') != -1:
  84. f.write('# set RTT_ROOT\n')
  85. f.write("if not os.getenv('RTT_ROOT'): \n RTT_ROOT='rt-thread'\n\n")
  86. f.write(line)
  87. def bsp_update_kconfig(dist_dir):
  88. # change RTT_ROOT in Kconfig
  89. with open(os.path.join(dist_dir, 'Kconfig'), "r") as f:
  90. data = f.readlines()
  91. with open(os.path.join(dist_dir, 'Kconfig'), "w") as f:
  92. found = 0
  93. for line in data:
  94. if line.find('RTT_ROOT') != -1:
  95. found = 1
  96. if line.find('default') != -1 and found:
  97. position = line.find('default')
  98. line = line[0:position] + 'default: "rt-thread"\n'
  99. found = 0
  100. f.write(line)
  101. def bs_update_ide_project(bsp_root, rtt_root):
  102. import subprocess
  103. # default update the projects which have template file
  104. tgt_dict = {'mdk4':('keil', 'armcc'),
  105. 'mdk5':('keil', 'armcc'),
  106. 'iar':('iar', 'iar'),
  107. 'vs':('msvc', 'cl'),
  108. 'vs2012':('msvc', 'cl'),
  109. 'cdk':('gcc', 'gcc')}
  110. scons_env = os.environ.copy()
  111. scons_env["RTT_ROOT"] = rtt_root
  112. for item in tgt_dict:
  113. child = subprocess.Popen('scons --target=' + item, cwd=bsp_root, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  114. stdout, stderr = child.communicate()
  115. if child.returncode == 0:
  116. print("update %s project" % item)
  117. def zip_dist(bsp_root, dist_dir, dist_name):
  118. import zipfile
  119. zip_filename = os.path.join(bsp_root, 'dist', dist_name)
  120. zip = zipfile.ZipFile(zip_filename + ".zip", 'w')
  121. pre_len = len(os.path.dirname(dist_dir))
  122. for parent, dirnames, filenames in os.walk(dist_dir):
  123. for filename in filenames:
  124. pathfile = os.path.join(parent, filename)
  125. arcname = pathfile[pre_len:].strip(os.path.sep)
  126. zip.write(pathfile, arcname)
  127. zip.close()
  128. def MkDist(program, BSP_ROOT, RTT_ROOT, Env):
  129. print("make distribution....")
  130. dist_name = os.path.basename(BSP_ROOT)
  131. dist_dir = os.path.join(BSP_ROOT, 'dist', dist_name)
  132. target_path = os.path.join(dist_dir, 'rt-thread')
  133. # copy BSP files
  134. print("=> %s" % os.path.basename(BSP_ROOT))
  135. do_copy_folder(os.path.join(BSP_ROOT), dist_dir,
  136. ignore_patterns('build', 'dist', '*.pyc', '*.old', '*.map', 'rtthread.bin', '.sconsign.dblite', '*.elf', '*.axf', 'cconfig.h'))
  137. # copy tools directory
  138. print("=> components")
  139. do_copy_folder(os.path.join(RTT_ROOT, "components"), os.path.join(target_path, "components"))
  140. # skip documentation directory
  141. # skip examples
  142. # copy include directory
  143. print("=> include")
  144. do_copy_folder(os.path.join(RTT_ROOT, "include"), os.path.join(target_path, "include"))
  145. # copy all libcpu/ARCH directory
  146. print('=> libcpu')
  147. import rtconfig
  148. do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH), os.path.join(target_path, 'libcpu', rtconfig.ARCH))
  149. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'Kconfig'), os.path.join(target_path, 'libcpu', 'Kconfig'))
  150. do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'SConscript'), os.path.join(target_path, 'libcpu', 'SConscript'))
  151. # copy src directory
  152. print("=> src")
  153. do_copy_folder(os.path.join(RTT_ROOT, "src"), os.path.join(target_path, "src"))
  154. # copy tools directory
  155. print("=> tools")
  156. do_copy_folder(os.path.join(RTT_ROOT, "tools"), os.path.join(target_path, "tools"), ignore_patterns('*.pyc'))
  157. do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(target_path, 'Kconfig'))
  158. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(target_path, 'AUTHORS'))
  159. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(target_path, 'COPYING'))
  160. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(target_path, 'README.md'))
  161. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(target_path, 'README_zh.md'))
  162. # change RTT_ROOT in SConstruct
  163. bsp_update_sconstruct(dist_dir)
  164. # change RTT_ROOT in Kconfig
  165. bsp_update_kconfig(dist_dir)
  166. # update all project files
  167. bs_update_ide_project(dist_dir, target_path)
  168. # make zip package
  169. zip_dist(BSP_ROOT, dist_dir, dist_name)
  170. print('done!')