bsp_buildings.py 3.7 KB

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