1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2024 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15import subprocess 16import sys 17import argparse 18import os 19from utils import get_json 20 21sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 22 23 24def _run_cmd(cmd: list): 25 process = subprocess.Popen(cmd, 26 stdout=subprocess.PIPE, 27 stderr=subprocess.STDOUT, 28 encoding='utf-8') 29 for line in iter(process.stdout.readline, ''): 30 print(line, end='') 31 process_status = process.poll() 32 if process_status: 33 sys.exit(process_status) 34 35 36def _get_args(): 37 parser = argparse.ArgumentParser(add_help=True) 38 parser.add_argument("-v", "--variants", default=r".", type=str, help="variants of build target") 39 parser.add_argument("-rp", "--root_path", default=r".", type=str, help="Path of root") 40 args = parser.parse_args() 41 return args 42 43 44def _get_all_features_info(root_path, variants) -> list: 45 _features_info_path = os.path.join(root_path, 'out', 'preloader', variants, 'features.json') 46 args_list = [] 47 try: 48 _features_json = get_json(_features_info_path) 49 for key, value in _features_json.get('features').items(): 50 if isinstance(value, bool): 51 args_list.append('{}={}'.format(key, str(value).lower())) 52 53 elif isinstance(value, str): 54 args_list.append('{}="{}"'.format(key, value)) 55 56 elif isinstance(value, int): 57 args_list.append('{}={}'.format(key, value)) 58 59 elif isinstance(value, list): 60 args_list.append('{}="{}"'.format(key, "&&".join(value))) 61 62 except Exception as e: 63 print('--_get_all_features_info json error--') 64 65 return args_list 66 67 68def _gn_cmd(root_path, variants): 69 _features_info = _get_all_features_info(root_path, variants) 70 _args_list = [f"ohos_indep_compiler_enable=true", f"product_name=\"{variants}\""] 71 _args_list.extend(_features_info) 72 73 _cmd_list = [f'{root_path}/prebuilts/build-tools/linux-x86/bin/gn', 'gen', 74 '--args={}'.format(' '.join(_args_list)), 75 '-C', f'out/{variants}'] 76 77 print('Excuting gn command: {} {} --args="{}" {}'.format( 78 f'{root_path}/prebuilts/build-tools/linux-x86/bin/gn', 'gen', 79 ' '.join(_args_list).replace('"', "\\\""), 80 ' '.join(_cmd_list[3:])), 81 'info') 82 return _cmd_list 83 84 85def _ninja_cmd(root_path, variants): 86 _cmd_list = [f'{root_path}/prebuilts/build-tools/linux-x86/bin/ninja', '-w', 'dupbuild=warn', '-C', 87 f'out/{variants}'] 88 return _cmd_list 89 90 91def _exec_cmd(root_path, variants): 92 gn_cmd = _gn_cmd(root_path, variants) 93 _run_cmd(gn_cmd) 94 ninja_cmd = _ninja_cmd(root_path, variants) 95 _run_cmd(ninja_cmd) 96 97 98def main(): 99 args = _get_args() 100 variants = args.variants 101 root_path = args.root_path 102 _exec_cmd(root_path, variants) 103 104 return 0 105 106 107if __name__ == '__main__': 108 sys.exit(main()) 109