bsp_buildings.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import os
  2. import shutil
  3. import re
  4. import multiprocessing
  5. import yaml
  6. def add_summary(text):
  7. """
  8. add summary to github action.
  9. """
  10. os.system(f'echo "{text}" >> $GITHUB_STEP_SUMMARY ;')
  11. def run_cmd(cmd, output_info=True):
  12. """
  13. run command and return output and result.
  14. """
  15. print('\033[1;32m' + cmd + '\033[0m')
  16. output_str_list = []
  17. res = 0
  18. if output_info:
  19. res = os.system(cmd + " > output.txt 2>&1")
  20. else:
  21. res = os.system(cmd + " > /dev/null 2>output.txt")
  22. with open("output.txt", "r") as file:
  23. output_str_list = file.readlines()
  24. for line in output_str_list:
  25. print(line, end='')
  26. os.remove("output.txt")
  27. return output_str_list, res
  28. def build_bsp(bsp, scons_args=''):
  29. """
  30. build bsp.
  31. cd {rtt_root}
  32. scons -C bsp/{bsp} --pyconfig-silent > /dev/null
  33. cd {rtt_root}/bsp/{bsp}
  34. pkgs --update > /dev/null
  35. pkgs --list
  36. cd {rtt_root}
  37. scons -C bsp/{bsp} -j{nproc} {scons_args}
  38. cd {rtt_root}/bsp/{bsp}
  39. scons -c > /dev/null
  40. rm -rf packages
  41. """
  42. success = True
  43. os.chdir(rtt_root)
  44. if os.path.exists(f"{rtt_root}/bsp/{bsp}/Kconfig"):
  45. os.chdir(rtt_root)
  46. run_cmd(f'scons -C bsp/{bsp} --pyconfig-silent', output_info=False)
  47. os.chdir(f'{rtt_root}/bsp/{bsp}')
  48. run_cmd('pkgs --update-force', output_info=False)
  49. run_cmd('pkgs --list')
  50. nproc = multiprocessing.cpu_count()
  51. os.chdir(rtt_root)
  52. cmd = f'scons -C bsp/{bsp} -j{nproc} {scons_args} --debug=time'
  53. __, res = run_cmd(cmd, output_info=True)
  54. if res != 0:
  55. success = False
  56. os.chdir(f'{rtt_root}/bsp/{bsp}')
  57. run_cmd('scons -c', output_info=False)
  58. #pkg_dir = os.path.join(rtt_root, 'bsp', bsp, 'packages')
  59. #shutil.rmtree(pkg_dir, ignore_errors=True)
  60. return success
  61. def append_file(source_file, destination_file):
  62. """
  63. append file to another file.
  64. """
  65. with open(source_file, 'r') as source:
  66. with open(destination_file, 'a') as destination:
  67. for line in source:
  68. destination.write(line)
  69. def check_scons_args(file_path):
  70. args = []
  71. with open(file_path, 'r') as file:
  72. for line in file:
  73. match = re.search(r'#\s*scons:\s*(.*)', line)
  74. if match:
  75. args.append(match.group(1).strip())
  76. return ' '.join(args)
  77. def get_details_and_dependencies(details, projects, seen=None):
  78. if seen is None:
  79. seen = set()
  80. detail_list = []
  81. if details is not None:
  82. for dep in details:
  83. if dep not in seen:
  84. dep_details=projects.get(dep)
  85. seen.add(dep)
  86. if dep_details is not None:
  87. if dep_details.get('depends') is not None:
  88. detail_temp=get_details_and_dependencies(dep_details.get('depends'), projects, seen)
  89. for line in detail_temp:
  90. detail_list.append(line)
  91. if dep_details.get('kconfig') is not None:
  92. for line in dep_details.get('kconfig'):
  93. detail_list.append(line)
  94. else:
  95. print(f"::error::There are some problems with attachconfig depend: {dep}");
  96. return detail_list
  97. def build_bsp_attachconfig(bsp, attach_file):
  98. """
  99. build bsp with attach config.
  100. cp bsp/{bsp}/.config bsp/{bsp}/.config.origin
  101. cat .ci/attachconfig/{attach_file} >> bsp/{bsp}/.config
  102. build_bsp()
  103. cp bsp/{bsp}/.config.origin bsp/{bsp}/.config
  104. rm bsp/{bsp}/.config.origin
  105. """
  106. config_file = os.path.join(rtt_root, 'bsp', bsp, '.config')
  107. config_bacakup = config_file+'.origin'
  108. shutil.copyfile(config_file, config_bacakup)
  109. attachconfig_dir = os.path.join(rtt_root, 'bsp', bsp, '.ci/attachconfig')
  110. attach_path = os.path.join(attachconfig_dir, attach_file)
  111. append_file(attach_path, config_file)
  112. scons_args = check_scons_args(attach_path)
  113. res = build_bsp(bsp, scons_args)
  114. shutil.copyfile(config_bacakup, config_file)
  115. os.remove(config_bacakup)
  116. return res
  117. if __name__ == "__main__":
  118. """
  119. build all bsp and attach config.
  120. 1. build all bsp.
  121. 2. build all bsp with attach config.
  122. """
  123. failed = 0
  124. count = 0
  125. rtt_root = os.getcwd()
  126. srtt_bsp = os.getenv('SRTT_BSP').split(',')
  127. for bsp in srtt_bsp:
  128. count += 1
  129. print(f"::group::Compiling BSP: =={count}=== {bsp} ====")
  130. res = build_bsp(bsp)
  131. if not res:
  132. print(f"::error::build {bsp} failed")
  133. add_summary(f"- ❌ build {bsp} failed.")
  134. failed += 1
  135. else:
  136. add_summary(f'- ✅ build {bsp} success.')
  137. print("::endgroup::")
  138. yml_files_content = []
  139. directory = os.path.join(rtt_root, 'bsp', bsp, '.ci/attachconfig')
  140. if os.path.exists(directory):
  141. for root, dirs, files in os.walk(directory):
  142. for filename in files:
  143. if filename.endswith('attachconfig.yml'):
  144. file_path = os.path.join(root, filename)
  145. if os.path.exists(file_path):
  146. try:
  147. with open(file_path, 'r') as file:
  148. content = yaml.safe_load(file)
  149. if content is None:
  150. continue
  151. yml_files_content.append(content)
  152. except yaml.YAMLError as e:
  153. print(f"::error::Error parsing YAML file: {e}")
  154. continue
  155. except Exception as e:
  156. print(f"::error::Error reading file: {e}")
  157. continue
  158. config_file = os.path.join(rtt_root, 'bsp', bsp, '.config')
  159. for projects in yml_files_content:
  160. for name, details in projects.items():
  161. count += 1
  162. config_bacakup = config_file+'.origin'
  163. shutil.copyfile(config_file, config_bacakup)
  164. with open(config_file, 'a') as destination:
  165. if details.get("kconfig") is None:
  166. continue
  167. if(projects.get(name) is not None):
  168. detail_list=get_details_and_dependencies([name],projects)
  169. for line in detail_list:
  170. destination.write(line + '\n')
  171. scons_arg=[]
  172. if details.get('scons_arg') is not None:
  173. for line in details.get('scons_arg'):
  174. scons_arg.append(line)
  175. scons_arg_str=' '.join(scons_arg) if scons_arg else ' '
  176. print(f"::group::\tCompiling yml project: =={count}==={name}=scons_arg={scons_arg_str}==")
  177. res = build_bsp(bsp, scons_arg_str)
  178. if not res:
  179. print(f"::error::build {bsp} {name} failed.")
  180. add_summary(f'\t- ❌ build {bsp} {name} failed.')
  181. failed += 1
  182. else:
  183. add_summary(f'\t- ✅ build {bsp} {name} success.')
  184. print("::endgroup::")
  185. shutil.copyfile(config_bacakup, config_file)
  186. os.remove(config_bacakup)
  187. attach_dir = os.path.join(rtt_root, 'bsp', bsp, '.ci/attachconfig')
  188. attach_list = []
  189. for root, dirs, files in os.walk(attach_dir):
  190. for file in files:
  191. if file.endswith('attach'):
  192. file_path = os.path.join(root, file)
  193. relative_path = os.path.relpath(file_path, attach_dir)
  194. attach_list.append(relative_path)
  195. for attach_file in attach_list:
  196. count += 1
  197. print(f"::group::\tCompiling BSP: =={count}=== {bsp} {attach_file}===")
  198. res = build_bsp_attachconfig(bsp, attach_file)
  199. if not res:
  200. print(f"::error::build {bsp} {attach_file} failed.")
  201. add_summary(f'\t- ❌ build {attach_file} failed.')
  202. failed += 1
  203. else:
  204. add_summary(f'\t- ✅ build {attach_file} success.')
  205. print("::endgroup::")
  206. exit(failed)