bsp_buildings.py 4.2 KB

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