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 pathlib import Path 20 21from vmb.tool import ToolBase, VmbToolExecError 22from vmb.unit import BenchUnit 23from vmb.shell import ShellResult 24from vmb.result import BuildResult, BUStatus 25 26 27class Tool(ToolBase): 28 29 def __init__(self, *args): 30 super().__init__(*args) 31 self.panda_root = self.ensure_dir( 32 os.environ.get('PANDA_BUILD', ''), 33 err='Please set $PANDA_BUILD env var' 34 ) 35 self.opts = '--gen-stdlib=false --extension=sts --opt-level=2 ' \ 36 f'--arktsconfig={self.panda_root}' \ 37 f'/tools/es2panda/generated/arktsconfig.json {self.custom}' 38 self.es2panda = self.ensure_file(self.panda_root, 'bin', 'es2panda') 39 40 @property 41 def name(self) -> str: 42 return 'ES to Panda compiler (ArkTS mode)' 43 44 def exec(self, bu: BenchUnit) -> None: 45 for lib in bu.libs('.ts', '.sts'): 46 abc = lib.with_suffix('.abc') 47 if abc.is_file(): 48 continue 49 opts = '--ets-module ' + self.opts 50 self.run_es2panda(lib, abc, opts, bu) 51 src = bu.src('.ts', '.sts') 52 abc = src.with_suffix('.abc') 53 res = self.run_es2panda(src, abc, self.opts, bu) 54 abc_size = self.sh.get_filesize(abc) 55 bu.result.build.append( 56 BuildResult('es2panda', abc_size, res.tm, res.rss)) 57 bu.binaries.append(abc) 58 59 def run_es2panda(self, 60 src: Path, 61 abc: Path, 62 opts: str, 63 bu: BenchUnit) -> ShellResult: 64 res = self.sh.run( 65 f'LD_LIBRARY_PATH={self.panda_root}/lib ' 66 f'{self.es2panda} {opts} ' 67 f'--output={abc} {src}', 68 measure_time=True) 69 if res.ret != 0 or not abc.is_file(): 70 bu.status = BUStatus.COMPILATION_FAILED 71 raise VmbToolExecError(f'{self.name} failed: {src}', res) 72 return res 73