bsp_buildings.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import os
  2. import shutil
  3. import re
  4. import multiprocessing
  5. def add_summary(text):
  6. """
  7. add summary to github action.
  8. """
  9. os.system(f'echo "{text}" >> $GITHUB_STEP_SUMMARY ;')
  10. def run_cmd(cmd, output_info=True):
  11. """
  12. run command and return output and result.
  13. """
  14. print('\033[1;32m' + cmd + '\033[0m')
  15. output_str_list = []
  16. res = 0
  17. if output_info:
  18. res = os.system(cmd + " > output.txt 2>&1")
  19. else:
  20. res = os.system(cmd + " > /dev/null 2>output.txt")
  21. with open("output.txt", "r") as file:
  22. output_str_list = file.readlines()
  23. for line in output_str_list:
  24. print(line, end='')
  25. os.remove("output.txt")
  26. return output_str_list, res
  27. def build_bsp(bsp, scons_args=''):
  28. """
  29. build bsp.
  30. cd {rtt_root}
  31. scons -C bsp/{bsp} --pyconfig-silent > /dev/null
  32. cd {rtt_root}/bsp/{bsp}
  33. pkgs --update > /dev/null
  34. pkgs --list
  35. cd {rtt_root}
  36. scons -C bsp/{bsp} -j{nproc} {scons_args}
  37. cd {rtt_root}/bsp/{bsp}
  38. scons -c > /dev/null
  39. rm -rf packages
  40. """
  41. success = True
  42. os.chdir(rtt_root)
  43. if os.path.exists(f"{rtt_root}/bsp/{bsp}/Kconfig"):
  44. os.chdir(rtt_root)
  45. run_cmd(f'scons -C bsp/{bsp} --pyconfig-silent', output_info=False)
  46. os.chdir(f'{rtt_root}/bsp/{bsp}')
  47. run_cmd('pkgs --update', output_info=False)
  48. run_cmd('pkgs --list')
  49. nproc = multiprocessing.cpu_count()
  50. os.chdir(rtt_root)
  51. cmd = f'scons -C bsp/{bsp} -j{nproc} {scons_args}'
  52. __, res = run_cmd(cmd, output_info=False)
  53. if res != 0:
  54. success = False
  55. os.chdir(f'{rtt_root}/bsp/{bsp}')
  56. run_cmd('scons -c', output_info=False)
  57. pkg_dir = os.path.join(rtt_root, 'bsp', bsp, 'packages')
  58. shutil.rmtree(pkg_dir, ignore_errors=True)
  59. return success
  60. def append_file(source_file, destination_file):
  61. """
  62. append file to another file.
  63. """
  64. with open(source_file, 'r') as source:
  65. with open(destination_file, 'a') as destination:
  66. for line in source:
  67. destination.write(line)
  68. def check_scons_args(file_path):
  69. args = []
  70. with open(file_path, 'r') as file:
  71. for line in file:
  72. match = re.search(r'#\s*scons:\s*(.*)', line)
  73. if match:
  74. args.append(match.group(1).strip())
  75. return ' '.join(args)
  76. def build_bsp_attachconfig(bsp, attach_file):
  77. """
  78. build bsp with attach config.
  79. cp bsp/{bsp}/.config bsp/{bsp}/.config.origin
  80. cat .ci/attachconfig/{attach_file} >> bsp/{bsp}/.config
  81. build_bsp()
  82. cp bsp/{bsp}/.config.origin bsp/{bsp}/.config
  83. rm bsp/{bsp}/.config.origin
  84. """
  85. config_file = os.path.join(rtt_root, 'bsp', bsp, '.config')
  86. config_bacakup = config_file+'.origin'
  87. shutil.copyfile(config_file, config_bacakup)
  88. attachconfig_dir = os.path.join(rtt_root, 'bsp', bsp, '.ci/attachconfig')
  89. attach_path = os.path.join(attachconfig_dir, attach_file)
  90. append_file(attach_path, config_file)
  91. scons_args = check_scons_args(attach_path)
  92. res = build_bsp(bsp, scons_args)
  93. shutil.copyfile(config_bacakup, config_file)
  94. os.remove(config_bacakup)
  95. return res
  96. if __name__ == "__main__":
  97. """
  98. build all bsp and attach config.
  99. 1. build all bsp.
  100. 2. build all bsp with attach config.
  101. """
  102. failed = 0
  103. count = 0
  104. rtt_root = os.getcwd()
  105. srtt_bsp = os.getenv('SRTT_BSP').split(',')
  106. for bsp in srtt_bsp:
  107. count += 1
  108. print(f"::group::Compiling BSP: =={count}=== {bsp} ====")
  109. res = build_bsp(bsp)
  110. if not res:
  111. print(f"::error::build {bsp} failed")
  112. add_summary(f"- ❌ build {bsp} failed.")
  113. failed += 1
  114. else:
  115. add_summary(f'- ✅ build {bsp} success.')
  116. print("::endgroup::")
  117. attach_dir = os.path.join(rtt_root, 'bsp', bsp, '.ci/attachconfig')
  118. attach_list = []
  119. for root, dirs, files in os.walk(attach_dir):
  120. for file in files:
  121. file_path = os.path.join(root, file)
  122. relative_path = os.path.relpath(file_path, attach_dir)
  123. attach_list.append(relative_path)
  124. for attach_file in attach_list:
  125. count += 1
  126. print(f"::group::\tCompiling BSP: =={count}=== {bsp} {attach_file}===")
  127. res = build_bsp_attachconfig(bsp, attach_file)
  128. if not res:
  129. print(f"::error::build {bsp} {attach_file} failed.")
  130. add_summary(f'\t- ❌ build {attach_file} failed.')
  131. failed += 1
  132. else:
  133. add_summary(f'\t- ✅ build {attach_file} success.')
  134. print("::endgroup::")
  135. exit(failed)