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)
31
32
33def _get_args():
34    parser = argparse.ArgumentParser(add_help=True)
35    parser.add_argument("-v", "--variants", default=r".", type=str, help="variants of build target")
36    parser.add_argument("-rp", "--root_path", default=r".", type=str, help="Path of root")
37    args = parser.parse_args()
38    return args
39
40
41def _get_all_features_info(root_path) -> dict:
42    _features_info_path = os.path.join(root_path, 'build', 'indep_configs', 'variants', 'default',
43                                       'features.json')
44    try:
45        _features_json = get_json(_features_info_path)
46    except Exception as e:
47        print('--_get_all_features_info json error--')
48    return _features_json.get('features')
49
50
51def _gn_cmd(root_path, variants):
52    _features_info = _get_all_features_info(root_path)
53    _args_list = [f"ohos_indep_compiler_enable=true", f"product_name=\"{variants}\""]
54    for k, v in _features_info.items():
55        _args_list.append(f'{k}={str(v).lower()}')
56
57    _args_info = ' '.join(_args_list)
58    _cmd_list = [f'{root_path}/prebuilts/build-tools/linux-x86/bin/gn', 'gen', f'--args={_args_info}']
59    _cmd_list += ['-C', f'out/{variants}']
60    return _cmd_list
61
62
63def _ninja_cmd(root_path, variants):
64    _cmd_list = [f'{root_path}/prebuilts/build-tools/linux-x86/bin/ninja', '-w', 'dupbuild=warn', '-C',
65                 f'out/{variants}']
66    return _cmd_list
67
68
69def _exec_cmd(root_path, variants):
70    gn_cmd = _gn_cmd(root_path, variants)
71    _run_cmd(gn_cmd)
72    ninja_cmd = _ninja_cmd(root_path, variants)
73    _run_cmd(ninja_cmd)
74
75
76def main():
77    args = _get_args()
78    variants = args.variants
79    root_path = args.root_path
80    _exec_cmd(root_path, variants)
81
82    return 0
83
84
85if __name__ == '__main__':
86    sys.exit(main())
87