bsp_buildings.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import os
  2. import re
  3. import glob
  4. import shutil
  5. import multiprocessing
  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. 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):
  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}
  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. __, res = run_cmd(f'scons -C bsp/{bsp} -j{nproc}', output_info=False)
  52. if res != 0:
  53. success = False
  54. os.chdir(f'{rtt_root}/bsp/{bsp}')
  55. run_cmd('scons -c', output_info=False)
  56. pkg_dir = os.path.join(rtt_root, 'bsp', bsp, 'packages')
  57. shutil.rmtree(pkg_dir, ignore_errors=True)
  58. return success
  59. def append_file(source_file, destination_file):
  60. """
  61. append file to another file.
  62. """
  63. with open(source_file, 'r') as source:
  64. with open(destination_file, 'a') as destination:
  65. for line in source:
  66. destination.write(line)
  67. def build_bsp_attachconfig(bsp, attach_file):
  68. """
  69. build bsp with attach config.
  70. cp bsp/{bsp}/.config bsp/{bsp}/.config.origin
  71. cat .ci/attachconfig/{attach_file} >> bsp/{bsp}/.config
  72. build_bsp()
  73. cp bsp/{bsp}/.config.origin bsp/{bsp}/.config
  74. rm bsp/{bsp}/.config.origin
  75. """
  76. config_file = os.path.join(rtt_root, 'bsp', bsp, '.config')
  77. config_bacakup = config_file+'.origin'
  78. shutil.copyfile(config_file, config_bacakup)
  79. append_file(attach_file, 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_files = glob.glob(os.path.join(attach_dir, '*.attach'))
  107. for attach_file in attach_files:
  108. count += 1
  109. attach = os.path.basename(attach_file)
  110. print(f"::group::\tCompiling BSP: =={count}=== {bsp} {attach}===")
  111. res = build_bsp_attachconfig(bsp, attach_file)
  112. if not res:
  113. print(f"::error::build {bsp} {attach} failed.")
  114. add_summary(f'\t- ❌ build {attach} failed.')
  115. failed += 1
  116. else:
  117. add_summary(f'\t- ✅ build {attach} success.')
  118. print("::endgroup::")
  119. exit(failed)