1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2024 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18import os 19from typing import Union 20from pathlib import Path 21 22from vmb.tool import ToolBase, VmbToolExecError 23from vmb.unit import BenchUnit 24from vmb.result import BuildResult, BUStatus 25from vmb.shell import ShellResult 26 27 28class Tool(ToolBase): 29 """Wrapper for es2abc.""" 30 31 def __init__(self, *args): 32 super().__init__(*args) 33 self.sdk = self.ensure_dir_env('OHOS_SDK') 34 self.opts = f'--module --merge-abc --extension=ts {self.custom}' 35 if not os.path.isdir(self.sdk): 36 raise RuntimeError('OHOS_SDK not found.') 37 self.es2abc = os.path.join( 38 self.sdk, 'out/x64.release/arkcompiler/ets_frontend/es2abc') 39 if not os.path.isfile(self.es2abc): 40 raise RuntimeError('es2abc not found.') 41 42 @property 43 def name(self) -> str: 44 return 'ES to ABC compiler' 45 46 def exec(self, bu: BenchUnit) -> None: 47 ts_files = [bu.src('.ts')] + list(bu.libs('.ts')) 48 for ts in ts_files: 49 if not ts.is_file(): 50 continue 51 abc = ts.with_suffix('.abc') 52 abc.unlink(missing_ok=True) 53 res = self.run_es2abc(ts, abc) 54 if res.ret != 0 or not abc.is_file(): 55 bu.status = BUStatus.COMPILATION_FAILED 56 raise VmbToolExecError(f'{self.name} failed: {ts}', res) 57 abc_size = self.sh.get_filesize(abc) 58 bu.result.build.append( 59 BuildResult('es2abc', abc_size, res.tm, res.rss)) 60 bu.binaries.append(abc) 61 62 def run_es2abc(self, 63 src: Union[str, Path], 64 out: Union[str, Path]) -> ShellResult: 65 return self.sh.run( 66 f'{self.es2abc} {self.opts} ' 67 f'--output={out} {src}', 68 measure_time=True) 69