bsp_buildings.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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', 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}'
  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 build_bsp_attachconfig(bsp, attach_file):
  78. """
  79. build bsp with attach config.
  80. cp bsp/{bsp}/.config bsp/{bsp}/.config.origin
  81. cat .ci/attachconfig/{attach_file} >> bsp/{bsp}/.config
  82. build_bsp()
  83. cp bsp/{bsp}/.config.origin bsp/{bsp}/.config
  84. rm bsp/{bsp}/.config.origin
  85. """
  86. config_file = os.path.join(rtt_root, 'bsp', bsp, '.config')
  87. config_bacakup = config_file+'.origin'
  88. shutil.copyfile(config_file, config_bacakup)
  89. attachconfig_dir = os.path.join(rtt_root, 'bsp', bsp, '.ci/attachconfig')
  90. attach_path = os.path.join(attachconfig_dir, attach_file)
  91. append_file(attach_path, config_file)
  92. scons_args = check_scons_args(attach_path)
  93. res = build_bsp(bsp, scons_args)
  94. shutil.copyfile(config_bacakup, config_file)
  95. os.remove(config_bacakup)
  96. return res
  97. if __name__ == "__main__":
  98. """
  99. build all bsp and attach config.
  100. 1. build all bsp.
  101. 2. build all bsp with attach config.
  102. """
  103. failed = 0
  104. count = 0
  105. rtt_root = os.getcwd()
  106. srtt_bsp = os.getenv('SRTT_BSP').split(',')
  107. for bsp in srtt_bsp:
  108. count += 1
  109. print(f"::group::Compiling BSP: =={count}=== {bsp} ====")
  110. res = build_bsp(bsp)
  111. if not res:
  112. print(f"::error::build {bsp} failed")
  113. add_summary(f"- ❌ build {bsp} failed.")
  114. failed += 1
  115. else:
  116. add_summary(f'- ✅ build {bsp} success.')
  117. print("::endgroup::")
  118. yml_files_content = []
  119. directory = os.path.join(rtt_root, 'bsp', bsp, '.ci/attachconfig')
  120. if os.path.exists(directory):
  121. for root, dirs, files in os.walk(directory):
  122. for filename in files:
  123. if filename.endswith('attachconfig.yml'):
  124. file_path = os.path.join(root, filename)
  125. if os.path.exists(file_path):
  126. with open(file_path, 'r') as file:
  127. content = yaml.safe_load(file)
  128. yml_files_content.append(content)
  129. config_file = os.path.join(rtt_root, 'bsp', bsp, '.config')
  130. for projects in yml_files_content:
  131. for name, details in projects.items():
  132. count += 1
  133. config_bacakup = config_file+'.origin'
  134. shutil.copyfile(config_file, config_bacakup)
  135. with open(config_file, 'a') as destination:
  136. for line in details.get('kconfig'):
  137. destination.write(line + '\n')
  138. scons_arg = details.get('scons_arg')
  139. scons_arg_str = scons_arg[0] if scons_arg else ' '
  140. print(f"::group::\tCompiling yml project: =={count}==={name}=scons_arg={scons_arg_str}==")
  141. res = build_bsp(bsp, scons_arg_str)
  142. if not res:
  143. print(f"::error::build {bsp} {name} failed.")
  144. add_summary(f'\t- ❌ build {bsp} {name} failed.')
  145. failed += 1
  146. else:
  147. add_summary(f'\t- ✅ build {bsp} {name} success.')
  148. print("::endgroup::")
  149. shutil.copyfile(config_bacakup, config_file)
  150. os.remove(config_bacakup)
  151. attach_dir = os.path.join(rtt_root, 'bsp', bsp, '.ci/attachconfig')
  152. attach_list = []
  153. for root, dirs, files in os.walk(attach_dir):
  154. for file in files:
  155. if file.endswith('attach'):
  156. file_path = os.path.join(root, file)
  157. relative_path = os.path.relpath(file_path, attach_dir)
  158. attach_list.append(relative_path)
  159. for attach_file in attach_list:
  160. count += 1
  161. print(f"::group::\tCompiling BSP: =={count}=== {bsp} {attach_file}===")
  162. res = build_bsp_attachconfig(bsp, attach_file)
  163. if not res:
  164. print(f"::error::build {bsp} {attach_file} failed.")
  165. add_summary(f'\t- ❌ build {attach_file} failed.')
  166. failed += 1
  167. else:
  168. add_summary(f'\t- ✅ build {attach_file} success.')
  169. print("::endgroup::")
  170. exit(failed)