rt-thread/tools/ci/bsp_buildings.py

163 lines
3.9 KiB
Python
Raw Normal View History

2023-12-24 22:43:40 +08:00
import os
import re
import glob
import shutil
import multiprocessing
def add_summary(text):
"""
add summary to github action.
"""
os.system(f'echo "{text}" >> $GITHUB_STEP_SUMMARY ;')
2023-12-25 14:01:07 +08:00
def run_cmd(cmd, output_info=True):
2023-12-24 22:43:40 +08:00
"""
run command and return output and result.
"""
print('\033[1;32m' + cmd + '\033[0m')
2023-12-25 14:01:07 +08:00
output_str_list = []
if output_info:
res = os.system(cmd + " > output.txt 2>&1")
else:
res = os.system(cmd + " > /dev/null 2>output.txt")
2023-12-24 22:43:40 +08:00
with open("output.txt", "r") as file:
2023-12-25 14:01:07 +08:00
output_str_list = file.readlines()
for line in output_str_list:
2023-12-24 22:43:40 +08:00
print(line, end='')
2023-12-25 14:01:07 +08:00
2023-12-24 22:43:40 +08:00
os.remove("output.txt")
2023-12-25 14:01:07 +08:00
return output_str_list, res
2023-12-24 22:43:40 +08:00
def build_bsp(bsp):
"""
build bsp.
cd {rtt_root}
2023-12-25 14:01:07 +08:00
scons -C bsp/{bsp} --pyconfig-silent > /dev/null
2023-12-24 22:43:40 +08:00
cd {rtt_root}/bsp/{bsp}
pkgs --update > /dev/null
2023-12-24 22:43:40 +08:00
pkgs --list
cd {rtt_root}
scons -C bsp/{bsp} -j{nproc}
2023-12-24 22:43:40 +08:00
cd {rtt_root}/bsp/{bsp}
2023-12-25 14:01:07 +08:00
scons -c > /dev/null
2023-12-24 22:43:40 +08:00
rm -rf packages
"""
success = True
os.chdir(rtt_root)
if os.path.exists(f"{rtt_root}/bsp/{bsp}/Kconfig"):
os.chdir(rtt_root)
2023-12-25 14:01:07 +08:00
run_cmd(f'scons -C bsp/{bsp} --pyconfig-silent', output_info=False)
2023-12-24 22:43:40 +08:00
os.chdir(f'{rtt_root}/bsp/{bsp}')
run_cmd('pkgs --update', output_info=False)
2023-12-24 22:43:40 +08:00
run_cmd('pkgs --list')
nproc = multiprocessing.cpu_count()
os.chdir(rtt_root)
__, res = run_cmd(f'scons -C bsp/{bsp} -j{nproc}', output_info=False)
2023-12-25 11:24:33 +08:00
2023-12-24 22:43:40 +08:00
if res != 0:
success = False
os.chdir(f'{rtt_root}/bsp/{bsp}')
2023-12-25 14:01:07 +08:00
run_cmd('scons -c', output_info=False)
2023-12-24 22:43:40 +08:00
pkg_dir = os.path.join(rtt_root, 'bsp', bsp, 'packages')
shutil.rmtree(pkg_dir, ignore_errors=True)
2023-12-25 11:24:33 +08:00
return success
2023-12-24 22:43:40 +08:00
def append_file(source_file, destination_file):
"""
append file to another file.
"""
with open(source_file, 'r') as source:
with open(destination_file, 'a') as destination:
for line in source:
destination.write(line)
def build_bsp_attachconfig(bsp, attach_file):
"""
build bsp with attach config.
cp bsp/{bsp}/.config bsp/{bsp}/.config.origin
cat .ci/attachconfig/{attach_file} >> bsp/{bsp}/.config
build_bsp()
cp bsp/{bsp}/.config.origin bsp/{bsp}/.config
rm bsp/{bsp}/.config.origin
"""
config_file = os.path.join(rtt_root, 'bsp', bsp, '.config')
config_bacakup = config_file+'.origin'
shutil.copyfile(config_file, config_bacakup)
append_file(attach_file, config_file)
2023-12-25 11:24:33 +08:00
res = build_bsp(bsp)
2023-12-24 22:43:40 +08:00
shutil.copyfile(config_bacakup, config_file)
os.remove(config_bacakup)
2023-12-25 11:24:33 +08:00
return res
2023-12-24 22:43:40 +08:00
if __name__ == "__main__":
"""
build all bsp and attach config.
1. build all bsp.
2. build all bsp with attach config.
"""
failed = 0
count = 0
rtt_root = os.getcwd()
srtt_bsp = os.getenv('SRTT_BSP').split(',')
for bsp in srtt_bsp:
count += 1
print(f"::group::Compiling BSP: =={count}=== {bsp} ====")
2023-12-25 11:24:33 +08:00
res = build_bsp(bsp)
2023-12-24 22:43:40 +08:00
if not res:
print(f"::error::build {bsp} failed")
2023-12-25 11:24:33 +08:00
add_summary(f"- ❌ build {bsp} failed.")
2023-12-24 22:43:40 +08:00
failed += 1
else:
2023-12-25 11:24:33 +08:00
add_summary(f'- ✅ build {bsp} success.')
2023-12-24 22:43:40 +08:00
print("::endgroup::")
attach_dir = os.path.join(rtt_root, 'bsp', bsp, '.ci/attachconfig')
attach_files = glob.glob(os.path.join(attach_dir, '*.attach'))
for attach_file in attach_files:
count += 1
attach = os.path.basename(attach_file)
print(f"::group::\tCompiling BSP: =={count}=== {bsp} {attach}===")
2023-12-25 11:24:33 +08:00
res = build_bsp_attachconfig(bsp, attach_file)
2023-12-24 22:43:40 +08:00
if not res:
print(f"::error::build {bsp} {attach} failed.")
2023-12-25 11:24:33 +08:00
add_summary(f'\t- ❌ build {attach} failed.')
2023-12-24 22:43:40 +08:00
failed += 1
else:
2023-12-25 11:24:33 +08:00
add_summary(f'\t- ✅ build {attach} success.')
2023-12-24 22:43:40 +08:00
print("::endgroup::")
exit(failed)