1import subprocess 2import os 3 4# 每个 target_dir 对应一个配置:env 脚本 + 自定义的 first_commands 列表 5target_configs = { 6 "../../../aarch64": { 7 "env_script": "thread-env-aarch64.sh", 8 "first_commands": [ 9 "scons --attach=board.e2000d_demo_rtthread", 10 "scons --attach=board.e2000q_demo_rtthread", 11 "scons --attach=board.phytium_pi_rtthread", 12 "scons --attach=board.pd2408_test_a_rtthread", 13 "scons --attach=board.pd2408_test_b_rtthread", 14 ] 15 }, 16 "../../../aarch32": { 17 "env_script": "thread-env-aarch32.sh", 18 "first_commands": [ 19 "scons --attach=board.e2000d_demo_rtthread", 20 "scons --attach=board.e2000q_demo_rtthread", 21 "scons --attach=board.phytium_pi_rtthread", 22 ] 23 }, 24} 25 26# 固定命令(每组后执行) 27fixed_commands = [ 28 "make clean", 29 "scons -j8", 30 "make mv_auto_test_file", 31 "scons --attach=default", 32] 33 34# <<< 新增 >>> 指定要检查/清理的 ELF 文件输出目录 35build_output_path = "/home/zhugy/tftpboot/rtthread_elfs/" 36 37# <<< 新增 >>> 删除路径下的所有 ELF 文件 38def remove_elf_files_in_path(target_path): 39 abs_target_path = os.path.abspath(target_path) 40 print(f"\n====== 清理路径: {abs_target_path} 中的 ELF 文件 ======") 41 removed_any = False 42 43 for root, dirs, files in os.walk(abs_target_path): 44 for file in files: 45 if file.endswith(".elf"): 46 file_path = os.path.join(root, file) 47 try: 48 os.remove(file_path) 49 print(f"️ 删除: {file_path}") 50 removed_any = True 51 except Exception as e: 52 print(f"⚠️ 删除失败: {file_path}, 错误: {e}") 53 54 55# <<< 新增 >>> 执行前先清理 build_output_path 中的 ELF 文件 56remove_elf_files_in_path(build_output_path) 57 58# 执行命令组的函数 59def run_commands_in_directory(target_dir, env_script, first_commands): 60 abs_target_dir = os.path.abspath(target_dir) 61 script_dir = os.path.abspath(os.path.dirname(__file__)) 62 env_script_path = os.path.join(script_dir, env_script) 63 64 print(f"\n>>> 进入目录: {abs_target_dir}") 65 print(f">>> 使用环境脚本: {env_script_path}") 66 67 for i, first_cmd in enumerate(first_commands, start=1): 68 print(f"\n== 执行第 {i} 组命令 ==") 69 70 # 构造 bash 命令 71 full_command = ( 72 f"bash -c '" 73 f"source \"{env_script_path}\" && " 74 f"pushd \"{abs_target_dir}\" > /dev/null && " 75 f"{first_cmd} && " 76 f"{' && '.join(fixed_commands)} && " 77 f"popd > /dev/null'" 78 ) 79 80 subprocess.run(full_command, shell=True) 81 82# 遍历所有配置项并执行命令 83for target_dir, config in target_configs.items(): 84 run_commands_in_directory( 85 target_dir, 86 config["env_script"], 87 config["first_commands"] 88 ) 89 90# >>> 检查 build_output_path 下的所有 ELF 文件 <<< 91def find_elf_files_in_path(search_path): 92 abs_search_path = os.path.abspath(search_path) 93 print(f"\n====== 检查路径: {abs_search_path} 下的 ELF 文件 ======") 94 95 elf_files = [] 96 for root, dirs, files in os.walk(abs_search_path): 97 for file in files: 98 if file.endswith(".elf"): 99 elf_files.append(os.path.join(root, file)) 100 101 if elf_files: 102 for elf in elf_files: 103 print(f"✔️ 找到 ELF 文件: {elf}") 104 else: 105 print("⚠️ 未找到 ELF 文件") 106 107# 最后执行 ELF 文件检查 108find_elf_files_in_path(build_output_path) 109