mkdist.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import os
  2. import subprocess
  3. import shutil
  4. from shutil import ignore_patterns
  5. from SCons.Script import *
  6. def do_copy_file(src, dst):
  7. # check source file
  8. if not os.path.exists(src):
  9. return
  10. path = os.path.dirname(dst)
  11. # mkdir if path not exist
  12. if not os.path.exists(path):
  13. os.makedirs(path)
  14. shutil.copy2(src, dst)
  15. def do_copy_folder(src_dir, dst_dir, ignore=None):
  16. # print(src_dir, dst_dir)
  17. import shutil
  18. # check source directory
  19. if not os.path.exists(src_dir):
  20. return
  21. try:
  22. if os.path.exists(dst_dir):
  23. shutil.rmtree(dst_dir)
  24. except:
  25. print('Deletes folder: %s failed.' % dst_dir)
  26. return
  27. shutil.copytree(src_dir, dst_dir, ignore = ignore)
  28. source_ext = ['c', 'h', 's', 'S', 'cpp', 'cxx', 'cc', 'xpm']
  29. source_list = []
  30. def walk_children(child):
  31. global source_list
  32. global source_ext
  33. # print child
  34. full_path = child.rfile().abspath
  35. file_type = full_path.rsplit('.',1)[1]
  36. #print file_type
  37. if file_type in source_ext:
  38. if full_path not in source_list:
  39. source_list.append(full_path)
  40. children = child.all_children()
  41. if children != []:
  42. for item in children:
  43. walk_children(item)
  44. def walk_kconfig(RTT_ROOT, source_list):
  45. for parent, dirnames, filenames in os.walk(RTT_ROOT):
  46. if 'bsp' in parent:
  47. continue
  48. if '.git' in parent:
  49. continue
  50. if 'tools' in parent:
  51. continue
  52. if 'Kconfig' in filenames:
  53. pathfile = os.path.join(parent, 'Kconfig')
  54. source_list.append(pathfile)
  55. if 'KConfig' in filenames:
  56. pathfile = os.path.join(parent, 'KConfig')
  57. source_list.append(pathfile)
  58. def bsp_update_kconfig(dist_dir):
  59. # change RTT_ROOT in Kconfig
  60. if not os.path.isfile(os.path.join(dist_dir, 'Kconfig')):
  61. return
  62. with open(os.path.join(dist_dir, 'Kconfig'), 'r') as f:
  63. data = f.readlines()
  64. with open(os.path.join(dist_dir, 'Kconfig'), 'w') as f:
  65. found = 0
  66. for line in data:
  67. if line.find('ROOT_SDK') != -1:
  68. line = "orsource 'rt-thread/Kconfig'\n"
  69. f.write(line)
  70. def bsp_copy_files(bsp_root, dist_dir):
  71. # copy BSP files
  72. do_copy_folder(os.path.join(bsp_root), dist_dir,
  73. ignore_patterns('build', 'dist', '*.pyc', '*.old', '*.map', 'rtthread.bin', '.sconsign.dblite', '*.elf', '*.axf', 'cconfig.h', '.pycache', 'cic.json'))
  74. def MkDist(program, BSP_ROOT, RTT_ROOT, Env, project_name, project_path):
  75. print('make app distribution....')
  76. print(project_path)
  77. if project_path == None:
  78. dist_dir = os.path.join(BSP_ROOT, 'dist', project_name)
  79. else:
  80. dist_dir = project_path
  81. print(dist_dir)
  82. rtt_dir_path = os.path.join(dist_dir, '..', 'rt-thread')
  83. # copy BSP files
  84. print('=> %s' % os.path.basename(BSP_ROOT))
  85. bsp_copy_files(BSP_ROOT, dist_dir)
  86. # copy .settings .cproject .project directory
  87. print('=> .settings')
  88. if Env['chip'] == 'rk3506':
  89. if "amp" in os.path.basename(BSP_ROOT):
  90. do_copy_folder(os.path.join(BSP_ROOT, '..', '..', '.ide', '.settings-amp'), os.path.join(dist_dir, '.settings'))
  91. else:
  92. do_copy_folder(os.path.join(BSP_ROOT, '..', '..', '.ide', '.settings-smp'), os.path.join(dist_dir, '.settings'))
  93. else:
  94. do_copy_folder(os.path.join(BSP_ROOT, '..', '..', '.ide', '.settings-amp'), os.path.join(dist_dir, '.settings'))
  95. do_copy_file(os.path.join(BSP_ROOT, '..', '..', '.ide', '.cproject'), os.path.join(dist_dir, '.cproject'))
  96. do_copy_file(os.path.join(BSP_ROOT, '..', '..', '.ide', '.project'), os.path.join(dist_dir, '.project'))
  97. # do bsp special dist handle
  98. if 'dist_handle' in Env:
  99. print("=> start dist handle")
  100. dist_handle = Env['dist_handle']
  101. dist_handle(BSP_ROOT, dist_dir)
  102. if not os.path.exists(rtt_dir_path):
  103. print('=> platform')
  104. do_copy_folder(os.path.join(BSP_ROOT, '..', '..', 'platform'), os.path.join(dist_dir, '..', 'platform'))
  105. # copy tools directory
  106. print('=> components')
  107. do_copy_folder(os.path.join(RTT_ROOT, 'components'), os.path.join(rtt_dir_path, 'components'))
  108. # copy include directory
  109. print('=> drivers')
  110. do_copy_folder(os.path.join(RTT_ROOT, 'drivers'), os.path.join(rtt_dir_path, 'drivers'))
  111. # skip documentation directory
  112. # skip examples
  113. # copy include directory
  114. print('=> include')
  115. do_copy_folder(os.path.join(RTT_ROOT, 'include'), os.path.join(rtt_dir_path, 'include'))
  116. # copy packages directory
  117. # print('=> packages')
  118. # do_copy_folder(os.path.join(RTT_ROOT, 'packages'), os.path.join(rtt_dir_path, 'packages'))
  119. # copy libs directory
  120. print('=> libs')
  121. do_copy_folder(os.path.join(RTT_ROOT, 'libs'), os.path.join(rtt_dir_path, 'libs'))
  122. # copy scripts directory
  123. print('=> scripts')
  124. do_copy_folder(os.path.join(RTT_ROOT, 'scripts'), os.path.join(rtt_dir_path, 'scripts'), ignore_patterns('*.pyc', '.pycache', 'cic.json'))
  125. # copy src directory
  126. print('=> src')
  127. do_copy_folder(os.path.join(RTT_ROOT, 'src'), os.path.join(rtt_dir_path, 'src'))
  128. do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(rtt_dir_path, 'Kconfig'))
  129. do_copy_file(os.path.join(RTT_ROOT, 'SConscript'), os.path.join(rtt_dir_path, 'SConscript'))
  130. bsp_update_kconfig(dist_dir)
  131. print('dist project successfully!')