• 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 argparse
17import json
18import os
19import time
20import shutil
21import stat
22import utils
23
24
25def _get_args():
26    parser = argparse.ArgumentParser(add_help=True)
27    parser.add_argument(
28        "-sp",
29        "--source_code_path",
30        default=r".",
31        type=str,
32        help="Path of source code",
33    )
34    parser.add_argument(
35        "-hp",
36        "--hpmcache_path",
37        default=r".",
38        type=str,
39        help="Path of .hpmcache",
40    )
41    parser.add_argument(
42        "-v",
43        "--variants",
44        default=r".",
45        type=str,
46        help="variants of build target",
47    )
48    parser.add_argument(
49        "-rp",
50        "--root_path",
51        default=r".",
52        type=str,
53        help="Path of root",
54    )
55    args = parser.parse_args()
56    return args
57
58
59def _get_dependence_json(_path) -> dict:
60    dependences_path = os.path.join(_path, 'dependences.json')
61    _json = utils.get_json(dependences_path)
62    return _json
63
64
65def _get_bundle_path(hpm_cache_path, dependences_json, part_name):
66    bundle_path = (hpm_cache_path +
67                   dependences_json[part_name]['installPath'] + os.sep + 'bundle.json')
68    return bundle_path
69
70
71def _get_src_bundle_path(source_code_path):
72    bundle_paths = list()
73    for root, dirs, files in os.walk(source_code_path):
74        for file in files:
75            if file.endswith("bundle.json"):
76                bundle_paths.append(os.path.join(root, file))
77    return bundle_paths
78
79
80def _symlink_src2dest(src_dir, dest_dir):
81    if os.path.exists(dest_dir) and os.path.islink(dest_dir):
82        os.unlink(dest_dir)
83    if os.path.exists(dest_dir) and dest_dir != src_dir:
84        if os.path.isdir(dest_dir):
85            shutil.rmtree(dest_dir)
86        else:
87            os.remove(dest_dir)
88    os.symlink(src_dir, dest_dir)
89
90
91def _symlink_binarys(hpm_cache_path, bundle_json, dependences_json, part_name):
92    path = bundle_json["segment"]["destPath"]
93    link_path = os.path.join("binarys", path)
94    if not os.path.isdir(link_path):
95        try:
96            os.remove(link_path)
97        except FileNotFoundError:
98            pass
99        os.makedirs(link_path, exist_ok=True)
100    real_path = hpm_cache_path + dependences_json[part_name]['installPath']
101    _symlink_src2dest(real_path, link_path)
102
103
104def _link_kernel_binarys(variants, hpm_cache_path, dependences_json):
105    musl_real_path = hpm_cache_path + dependences_json["musl"]['installPath']
106    musl_include_link_path = os.path.join("out", variants, "obj/binarys/third_party/musl/usr/include/arm-linux-ohos")
107    musl_lib_link_path = os.path.join("out", variants, "obj/binarys/third_party/musl/usr/lib/arm-linux-ohos")
108    os.makedirs(musl_include_link_path, exist_ok=True)
109    os.makedirs(musl_lib_link_path, exist_ok=True)
110    _symlink_src2dest(musl_real_path + os.sep + "innerapis/includes", musl_include_link_path)
111    _symlink_src2dest(musl_real_path + os.sep + "innerapis/libs", musl_lib_link_path)
112
113    kernel_real_path = hpm_cache_path + dependences_json["linux"]['installPath']
114    kernel_link_path = os.path.join("kernel/linux/patches")
115    if not os.path.isdir(kernel_link_path):
116        try:
117            os.remove(kernel_link_path)
118        except FileNotFoundError:
119            pass
120        os.makedirs(kernel_link_path, exist_ok=True)
121    os.makedirs(kernel_link_path, exist_ok=True)
122    _symlink_src2dest(kernel_real_path + os.sep + "innerapis/patches", kernel_link_path)
123
124
125def _gen_components_info(components_json, bundle_json, part_name, src_build_name_list):
126    subsystem = bundle_json["component"]["subsystem"]
127    path = bundle_json["segment"]["destPath"]
128    try:
129        component = bundle_json["component"]["build"]["inner_kits"]
130    except KeyError:
131        if bundle_json["component"]["build"] == []:
132            bundle_json["component"]["build"] = {}
133        if "inner_api" not in bundle_json["component"]["build"].keys():
134            bundle_json["component"]["build"]["inner_api"] = []
135        component = bundle_json["component"]["build"]["inner_api"]
136
137    innerapi_value_list = list()
138    for i in component:
139        innerapi_name = i["name"].split(':')[-1]
140        if part_name == 'musl':
141            innerapi_label = os.path.join("//binarys", path) + ":" + innerapi_name
142        elif part_name in src_build_name_list:
143            innerapi_label = i['name']
144        else:
145            innerapi_label = os.path.join("//binarys", path, "innerapis", innerapi_name) + ":" + innerapi_name
146        innerapi_value_list.append({"name": innerapi_name, "label": innerapi_label})
147    if part_name == 'cjson':
148        part_name = 'cJSON'
149    if part_name == 'f2fs_tools':
150        part_name = 'f2fs-tools'
151    if part_name == 'fsverity_utils':
152        part_name = 'fsverity-utils'
153    one_component_dict = {part_name: {
154        "innerapis": innerapi_value_list,
155        "path": path,
156        "subsystem": subsystem
157    }}
158    components_json.update(one_component_dict)
159
160    return components_json
161
162
163def _get_src_part_name(src_bundle_paths):
164    _name = ''
165    _path = ''
166    for src_bundle_path in src_bundle_paths:
167        src_bundle_json = utils.get_json(src_bundle_path)
168        part_name = src_bundle_json['component']['name']
169        if part_name.endswith('_lite'):
170            pass
171        else:
172            _name = part_name
173            _path = src_bundle_path
174    return _name, _path
175
176
177def _components_info_handler(part_name_list, source_code_path, hpm_cache_path, root_path, dependences_json):
178    components_json = dict()
179    src_bundle_paths = _get_src_bundle_path(source_code_path)
180    src_part_name, src_bundle_path = _get_src_part_name(src_bundle_paths)
181    src_build_name_list = [src_part_name, 'build_framework']
182    components_json = _gen_components_info(components_json, utils.get_json(src_bundle_path), src_part_name,
183                                           src_build_name_list)
184    components_json = _gen_components_info(components_json,
185                                           utils.get_json(os.path.join(root_path, "build", "bundle.json")),
186                                           "build_framework", src_build_name_list)
187    for part_name in part_name_list:
188        if part_name:
189            bundle_path = _get_bundle_path(hpm_cache_path, dependences_json, part_name)
190            bundle_json = utils.get_json(bundle_path)
191            components_json = _gen_components_info(components_json, bundle_json, part_name, src_build_name_list)
192            _symlink_binarys(hpm_cache_path, bundle_json, dependences_json, part_name)
193
194    return components_json
195
196
197def _out_components_json(components_json, output_path):
198    file_name = os.path.join(output_path, "components.json")
199    flags = os.O_WRONLY | os.O_CREAT
200    modes = stat.S_IWUSR | stat.S_IRUSR
201    with os.fdopen(os.open(file_name, flags, modes), 'w') as f:
202        json.dump(components_json, f, indent=4)
203
204
205def _generate_platforms_list(output_path):
206    platforms_list_gni_file = os.path.join(output_path, "platforms_list.gni")
207    platforms_list = ['phone']
208    platforms_list_strings = ' "," '.join(platforms_list)
209    gni_file_content = [f'target_platform_list = [ "{platforms_list_strings}" ]',
210                        f'kits_platform_list  = [ "{platforms_list_strings}" ]']
211    flags = os.O_WRONLY | os.O_CREAT
212    modes = stat.S_IWUSR | stat.S_IRUSR
213    with os.fdopen(os.open(platforms_list_gni_file, flags, modes), 'w') as f:
214        f.write('\n'.join(gni_file_content))
215
216
217def main():
218    args = _get_args()
219    source_code_path = args.source_code_path
220    hpm_cache_path = args.hpmcache_path
221    variants = args.variants
222    root_path = args.root_path
223    project_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
224    output_part_path = os.path.join(project_path, 'out', variants, 'build_configs', 'parts_info')
225    output_config_path = os.path.join(project_path, 'out', variants, 'build_configs')
226    dependences_json = _get_dependence_json(hpm_cache_path)
227    part_name_list = dependences_json.keys()
228
229    components_json = _components_info_handler(part_name_list, source_code_path,
230                                               hpm_cache_path, root_path, dependences_json)
231    _out_components_json(components_json, output_part_path)
232    _generate_platforms_list(output_config_path)
233    _link_kernel_binarys(variants, hpm_cache_path, dependences_json)
234
235
236if __name__ == '__main__':
237    main()
238