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