| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- import os
- import logging
- from pathlib import Path
- from typing import Optional, List
- from utils import run_command_with_retry
- logger = logging.getLogger(__name__)
- class BuildMCUError(Exception):
- """MCU 编译异常"""
- pass
- def compile_mcu(mcu_sdk_dir: str, profile_mode: str, soc_project: str):
- """
- 执行 MCU 编译。
- 对应原 Shell 的 compile_mcu() 函数。
- Args:
- mcu_sdk_dir: MCU SDK 根目录 (例如 .../mcu/mcu_sdk)
- profile_mode: 'Release' 或 'Debug'
- soc_project: SOC 项目名,用于判断是否启用 DV 模式
- """
- build_dir = os.path.join(mcu_sdk_dir, 'build')
- if not os.path.isdir(build_dir):
- raise BuildMCUError(f"MCU build directory not found: {build_dir}")
- # 清理输出目录
- output_dir = os.path.join(mcu_sdk_dir, 'output')
- if os.path.isdir(output_dir):
- import shutil
- shutil.rmtree(output_dir)
- logger.info("Cleaned MCU output directory: %s", output_dir)
- # 构造额外参数,与原 Shell 逻辑一致
- extra_args = _build_extra_args(profile_mode, soc_project)
- # 完整构建命令
- cmd_parts = [
- 'python3', './build_autosar.py',
- 'lite', 'evm',
- ] + extra_args + ['j6b']
- logger.info("Compiling MCU with command: %s", ' '.join(cmd_parts))
- # 执行命令(带 license 重试)
- try:
- run_command_with_retry(
- cmd_parts,
- cwd=build_dir,
- retry_count=5,
- retry_pattern=r'No licenses available for toolchain',
- log_file=os.path.join(os.environ.get('WORKSPACE', '/tmp'), 'command_log.txt')
- )
- except Exception as e:
- raise BuildMCUError(f"MCU compilation failed: {e}") from e
- def prepare_mcu(mcu_sdk_dir: str, soc_dir: str):
- """
- 编译前的准备工作(当前为空函数,与原 Shell 一致)。
- 如需未来扩展配置生成代码等,可在此添加。
- """
- # 原脚本 prepare_for_compile_mcu 为空
- pass
- def _build_extra_args(profile_mode: str, soc_project: str) -> List[str]:
- """
- 构建额外参数列表。
- 对应原 Shell 中的 extra_para 变量拼接逻辑:
- - 若 SOC_PROJECT 包含 'DV',则加 'dv',否则加 'plus'
- - 若 PROFILE_MODE 为 'Release',则加 'release',否则加 'debug'
- """
- extra = []
- if 'DV' in soc_project.upper():
- extra.append('dv')
- else:
- extra.append('plus')
- if profile_mode == 'Release':
- extra.append('release')
- else:
- extra.append('debug')
- return extra
|