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 19import json 20from utils import get_json, get_ninja_args, get_gn_args, is_enable_ccache, print_ccache_stats, clean_ccache_info 21 22sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 23 24 25def _run_cmd(cmd: list): 26 process = subprocess.Popen(cmd, 27 stdout=subprocess.PIPE, 28 stderr=subprocess.STDOUT, 29 encoding='utf-8') 30 for line in iter(process.stdout.readline, ''): 31 print(line, end='') 32 process_status = process.poll() 33 if process_status: 34 sys.exit(process_status) 35 36 37def _get_args(): 38 parser = argparse.ArgumentParser(add_help=True) 39 parser.add_argument("-v", "--variants", default=r".", type=str, help="variants of build target") 40 parser.add_argument("-rp", "--root_path", default=r".", type=str, help="Path of root") 41 parser.add_argument( 42 "-out", 43 "--out_dir", 44 default="src", 45 type=str, 46 help="the independent build out storage dir. default src , choices: src src_test or test", 47 ) 48 parser.add_argument( 49 "-t", "--test", 50 default=1, type=int, 51 help="whether the target contains test type. default 0 , choices: 0 or 1 2", 52 ) 53 args = parser.parse_args() 54 return args 55 56 57def _get_all_features_info(root_path, variants) -> list: 58 _features_info_path = os.path.join(root_path, 'out', 'preloader', variants, 'features.json') 59 args_list = [] 60 try: 61 _features_json = get_json(_features_info_path) 62 for key, value in _features_json.get('features').items(): 63 if isinstance(value, bool): 64 args_list.append('{}={}'.format(key, str(value).lower())) 65 66 elif isinstance(value, str): 67 args_list.append('{}="{}"'.format(key, value)) 68 69 elif isinstance(value, int): 70 args_list.append('{}={}'.format(key, value)) 71 72 elif isinstance(value, list): 73 args_list.append('{}="{}"'.format(key, "&&".join(value))) 74 75 except Exception as e: 76 print('--_get_all_features_info json error--') 77 78 return args_list 79 80 81def _gn_cmd(root_path, variants, out_dir, test_filter): 82 _features_info = _get_all_features_info(root_path, variants) 83 _args_list = [f"ohos_indep_compiler_enable=true", f"product_name=\"{variants}\""] 84 _args_list.extend(_features_info) 85 if is_enable_ccache(): 86 _args_list.append(f"ohos_build_enable_ccache=true") 87 88 # Add 'use_thin_lto=false' to _args_list if test_filter equals 2 89 if test_filter in (1, 2): 90 _args_list.append('use_thin_lto=false') 91 92 input_args = get_gn_args() 93 _args_list.extend(input_args) 94 95 _cmd_list = [f'{root_path}/prebuilts/build-tools/linux-x86/bin/gn', 'gen', 96 '--args={}'.format(' '.join(_args_list)), 97 '-C', f'out/{variants}/{out_dir}'] 98 99 print('Excuting gn command: {} {} --args="{}" {}'.format( 100 f'{root_path}/prebuilts/build-tools/linux-x86/bin/gn', 'gen', 101 ' '.join(_args_list).replace('"', "\\\""), 102 ' '.join(_cmd_list[3:])), 103 'info') 104 return _cmd_list 105 106 107def _ninja_cmd(root_path, variants, out_dir): 108 _cmd_list = [f'{root_path}/prebuilts/build-tools/linux-x86/bin/ninja', '-w', 'dupbuild=warn', '-C', 109 f'out/{variants}/{out_dir}'] 110 input_args = get_ninja_args() 111 _cmd_list.extend(input_args) 112 return _cmd_list 113 114 115def _exec_cmd(root_path, variants, out_dir, test_filter): 116 gn_cmd = _gn_cmd(root_path, variants, out_dir, test_filter) 117 _run_cmd(gn_cmd) 118 ninja_cmd = _ninja_cmd(root_path, variants, out_dir) 119 clean_ccache_info() 120 _run_cmd(ninja_cmd) 121 print_ccache_stats() 122 123 124def main(): 125 args = _get_args() 126 variants = args.variants 127 root_path = args.root_path 128 out_dir = args.out_dir 129 test_filter = args.test 130 _exec_cmd(root_path, variants, out_dir, test_filter) 131 132 return 0 133 134 135if __name__ == '__main__': 136 sys.exit(main()) 137