build_mcu.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import os
  2. import logging
  3. from pathlib import Path
  4. from typing import Optional, List
  5. from utils import run_command_with_retry
  6. logger = logging.getLogger(__name__)
  7. class BuildMCUError(Exception):
  8. """MCU 编译异常"""
  9. pass
  10. def compile_mcu(mcu_sdk_dir: str, profile_mode: str, soc_project: str):
  11. """
  12. 执行 MCU 编译。
  13. 对应原 Shell 的 compile_mcu() 函数。
  14. Args:
  15. mcu_sdk_dir: MCU SDK 根目录 (例如 .../mcu/mcu_sdk)
  16. profile_mode: 'Release' 或 'Debug'
  17. soc_project: SOC 项目名,用于判断是否启用 DV 模式
  18. """
  19. build_dir = os.path.join(mcu_sdk_dir, 'build')
  20. if not os.path.isdir(build_dir):
  21. raise BuildMCUError(f"MCU build directory not found: {build_dir}")
  22. # 清理输出目录
  23. output_dir = os.path.join(mcu_sdk_dir, 'output')
  24. if os.path.isdir(output_dir):
  25. import shutil
  26. shutil.rmtree(output_dir)
  27. logger.info("Cleaned MCU output directory: %s", output_dir)
  28. # 构造额外参数,与原 Shell 逻辑一致
  29. extra_args = _build_extra_args(profile_mode, soc_project)
  30. # 完整构建命令
  31. cmd_parts = [
  32. 'python3', './build_autosar.py',
  33. 'lite', 'evm',
  34. ] + extra_args + ['j6b']
  35. logger.info("Compiling MCU with command: %s", ' '.join(cmd_parts))
  36. # 执行命令(带 license 重试)
  37. try:
  38. run_command_with_retry(
  39. cmd_parts,
  40. cwd=build_dir,
  41. retry_count=5,
  42. retry_pattern=r'No licenses available for toolchain',
  43. log_file=os.path.join(os.environ.get('WORKSPACE', '/tmp'), 'command_log.txt')
  44. )
  45. except Exception as e:
  46. raise BuildMCUError(f"MCU compilation failed: {e}") from e
  47. def prepare_mcu(mcu_sdk_dir: str, soc_dir: str):
  48. """
  49. 编译前的准备工作(当前为空函数,与原 Shell 一致)。
  50. 如需未来扩展配置生成代码等,可在此添加。
  51. """
  52. # 原脚本 prepare_for_compile_mcu 为空
  53. pass
  54. def _build_extra_args(profile_mode: str, soc_project: str) -> List[str]:
  55. """
  56. 构建额外参数列表。
  57. 对应原 Shell 中的 extra_para 变量拼接逻辑:
  58. - 若 SOC_PROJECT 包含 'DV',则加 'dv',否则加 'plus'
  59. - 若 PROFILE_MODE 为 'Release',则加 'release',否则加 'debug'
  60. """
  61. extra = []
  62. if 'DV' in soc_project.upper():
  63. extra.append('dv')
  64. else:
  65. extra.append('plus')
  66. if profile_mode == 'Release':
  67. extra.append('release')
  68. else:
  69. extra.append('debug')
  70. return extra