• 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.
15
16import json
17import os
18import subprocess
19
20
21def get_json(file_path):
22    data = {}
23    try:
24        with open(file_path, 'r') as f:
25            data = json.load(f)
26    except FileNotFoundError:
27        print(f"can not find file: {file_path}.")
28    except Exception as e:
29        print(f"{file_path}: \n {e}")
30    return data
31
32
33def get_indep_args():
34    """
35    读取独立构建参数的JSON文件,并返回解析后的数据。
36    :return: 包含独立构建参数的字典(成功时)或 None(失败时)
37    """
38    src_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
39    file_path = os.path.join(src_root, "out/hb_args/indepbuildargs.json")
40    try:
41        # 以只读模式打开文件,并使用json模块加载内容
42        with open(file_path, 'r') as file:
43            data = json.load(file)
44            return data
45    except FileNotFoundError:
46        # 如果文件不存在
47        print(f"file not found:{file_path}")
48        return None
49    except Exception as e:
50        print(f"something wrong with file:{e}")
51        return None
52
53
54def get_ninja_args():
55    """
56    从独立构建参数中获取Ninja构建系统的参数。
57    :return: Ninja参数列表
58    """
59    input_ninja_args = []
60    data = get_indep_args()
61    # 如果配置中设置了保持Ninja运行,则添加相应的参数
62    if data["keep_ninja_going"]["argDefault"]:
63        input_ninja_args.append("-k10000")
64    return input_ninja_args
65
66
67def get_gn_args():
68    """
69    从独立构建参数中获取GN构建系统的参数。
70    :return: GN参数列表
71    """
72    input_gn_args = []
73    data = get_indep_args()
74    # 如果配置中设置了GN参数,则扩展到参数列表中
75    if data["gn_args"]["argDefault"]:
76        input_gn_args.extend(data["gn_args"]["argDefault"])
77
78    return input_gn_args
79
80
81def get_build_target():
82    """
83    从独立构建参数中获取编译目标。
84    :return: 编译目标
85    """
86    input_build_target = []
87    data = get_indep_args()
88    if data["build_target"]["argDefault"]:
89        input_build_target.extend(data["build_target"]["argDefault"])
90    return input_build_target
91
92
93def is_enable_ccache():
94    """
95    检查是否启用了ccache。
96    :return: 如果启用了ccache,则返回True;否则返回False。
97    """
98    # Read the independent build parameters JSON file and get the value of the "ccache" key's "argDefault"
99    return get_indep_args()["ccache"]["argDefault"]
100
101
102
103def print_ccache_stats():
104    """
105    打印ccache的统计信息。
106    如果ccache已启用,则执行ccache -s命令并打印输出。
107    如果ccache命令未找到或执行失败,则打印相应的错误消息。
108    """
109    # Check if ccache is enabled
110    if is_enable_ccache():
111        try:
112            # Execute the 'ccache -s' command and capture the output
113            output = subprocess.check_output(["ccache", "-s"], text=True)
114            # Print the header for ccache hit statistics
115            print("ccache hit statistics:")
116            # Print the output of the 'ccache -s' command
117            print(output)
118        except FileNotFoundError:
119            # Print an error message if the 'ccache' command is not found
120            print("Error: ccache command not found")
121        except subprocess.CalledProcessError as e:
122            # Print an error message if the 'ccache -s' command fails
123            print(f"Failed to execute ccache command: {e}")
124
125
126def clean_ccache_info():
127    """
128    清除ccache的统计信息。
129    如果ccache已启用,则执行ccache -z命令以重置ccache的统计信息。
130    如果ccache命令未找到或执行失败,则打印相应的错误消息。
131    """
132    # Check if ccache is enabled
133    if is_enable_ccache():
134        try:
135            # Execute the 'ccache -z' command to reset the ccache statistics
136            subprocess.check_output(["ccache", "-z"], text=True)
137        except FileNotFoundError:
138            # Print an error message if the 'ccache' command is not found
139            print("Error: ccache command not found")
140        except subprocess.CalledProcessError as e:
141            # Print an error message if the 'ccache -z' command fails
142            print(f"Failed to execute ccache command: {e}")
143