auto_compile.py 3.6 KB

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