• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2023 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
16
17import os
18import sys
19import shutil
20
21from resources.global_var import CURRENT_OHOS_ROOT
22from util.log_util import LogUtil
23from resources.config import Config
24
25# Import jinja2 from third_party/jinja2
26sys.path.insert(1, os.path.join(CURRENT_OHOS_ROOT, 'third_party'))
27from jinja2 import Template  # noqa: E402
28
29sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
30from scripts.util.file_utils import write_file  # noqa: E402
31
32PARTS_LIST_GNI_TEMPLATE = """
33parts_list = [
34  {}
35]
36"""
37
38INNER_KITS_GNI_TEMPLATE = """
39inner_kits_list = [
40  {}
41]
42"""
43
44SYSTEM_KITS_GNI_TEMPLATE = """
45system_kits_list = [
46  {}
47]
48"""
49
50PARTS_TEST_GNI_TEMPLATE = """
51parts_test_list = [
52  {}
53]
54"""
55
56PHONY_TARGET_LIST_TEMPLATE = """
57group("part_phony_targets") {{
58  deps = [
59    {}
60  ]
61}}"""
62
63PHONY_GROUP_TEMPLATE = """
64group("{}_phony") {{
65  deps = [ "{}" ]
66}}"""
67
68gn_file_template = os.path.join(os.path.dirname(os.path.abspath(__file__)),
69                                'build_gn.template')
70
71
72def gen_targets_gn(parts_targets, config_output_dir):
73    parts_list = []
74    inner_kits_list = []
75    system_kits_list = []
76    parts_test_list = []
77    phony_target_list = []
78    for part_labels in parts_targets.values():
79        parts_list.append(part_labels.get('part'))
80        if 'phony' in part_labels:
81            phony_target_list.append(part_labels.get('phony'))
82        if 'inner_kits' in part_labels:
83            inner_kits_list.append(part_labels.get('inner_kits'))
84        if 'system_kits' in part_labels:
85            system_kits_list.append(part_labels.get('system_kits'))
86        if 'test' in part_labels:
87            parts_test_list.append(part_labels.get('test'))
88    parts_list_gni_file = os.path.join(config_output_dir, 'parts_list.gni')
89    parts_list_content = '"{}",'.format('",\n  "'.join(parts_list))
90    write_file(parts_list_gni_file,
91               PARTS_LIST_GNI_TEMPLATE.format(parts_list_content))
92    LogUtil.hb_info(
93        "generate part list gni file to '{}/parts_list.gni'".format(config_output_dir), mode=Config.log_mode)
94
95    inner_kits_gni_file = os.path.join(config_output_dir,
96                                       'inner_kits_list.gni')
97    if inner_kits_list:
98        inner_kits_content = '"{}",'.format('",\n  "'.join(inner_kits_list))
99    else:
100        inner_kits_content = ''
101    write_file(inner_kits_gni_file,
102               INNER_KITS_GNI_TEMPLATE.format(inner_kits_content))
103    LogUtil.hb_info(
104        "generate inner kits gni file to '{}/inner_kits_list.gni'".format(
105          config_output_dir), mode=Config.log_mode)
106
107    system_list_gni_file = os.path.join(config_output_dir,
108                                        'system_kits_list.gni')
109    if system_kits_list:
110        system_kits_content = '"{}",'.format('",\n  "'.join(system_kits_list))
111    else:
112        system_kits_content = ''
113    write_file(system_list_gni_file,
114               SYSTEM_KITS_GNI_TEMPLATE.format(system_kits_content))
115    LogUtil.hb_info(
116        "generate system list gni file to '{}/system_kits_list.gni'".format(
117          config_output_dir), mode=Config.log_mode)
118
119    parts_test_gni_file = os.path.join(config_output_dir,
120                                       'parts_test_list.gni')
121    if parts_test_list:
122        test_list_content = '"{}",'.format('",\n  "'.join(parts_test_list))
123    else:
124        test_list_content = ''
125    write_file(parts_test_gni_file,
126               PARTS_TEST_GNI_TEMPLATE.format(test_list_content))
127    LogUtil.hb_info(
128        "generate parts test gni file to '{}/parts_test_list.gni'".format(
129          config_output_dir), mode=Config.log_mode)
130
131    build_gn_file = os.path.join(config_output_dir, 'BUILD.gn')
132    shutil.copyfile(gn_file_template, build_gn_file)
133    LogUtil.hb_info(
134        "generate build gn file to '{}/BUILD.gn'".format(config_output_dir), mode=Config.log_mode)
135
136
137def gen_phony_targets(variant_phony_targets, config_output_dir):
138    phony_target_list = []
139    phony_group_list = []
140    for part_name, part_label in variant_phony_targets.items():
141        phony_target_list.append('{}_phony'.format(part_name))
142        phony_group_list.append(
143            PHONY_GROUP_TEMPLATE.format(part_name, part_label))
144
145    phony_list_content = ''
146    if phony_target_list:
147        phony_list_content = '":{}",'.format(
148            '",\n  ":'.join(phony_target_list))
149    phony_build_content = []
150    phony_build_content.append(
151        PHONY_TARGET_LIST_TEMPLATE.format(phony_list_content))
152    phony_build_content.extend(phony_group_list)
153
154    phony_build_file = os.path.join(config_output_dir, 'phony_targets',
155                                    'BUILD.gn')
156    write_file(phony_build_file, '\n'.join(phony_build_content))
157    LogUtil.hb_info(
158        "generate phony target build file to '{}/phony_targets/BUILD.gn'".format(
159          config_output_dir), mode=Config.log_mode)
160
161
162def gen_stub_targets(parts_kits_info, platform_stubs, config_output_dir):
163    template = Template("""
164    # AUTO generated gn file, DONOT try to modify it.
165    import("//build/config/ohos/rules.gni")
166    import("//build/ohos/kits/kits_package.gni")
167    {% if combined_jar_deps %}
168      ohos_combine_jars("{{ platform }}_stub_kits_combine_java") {
169        deps = [
170          {{ combined_jar_deps }}
171        ]
172      }
173    {% endif %}
174
175    stub_jar("{{ platform }}_zframework_stub_java")  {
176      deps = [
177        "//third_party/openjdk_stubs:rt_java",
178        "//build/ohos/kits/system_api:phone_systemsdk_base_java($default_toolchain)",
179      ]
180      {% if platform != "phone" %}
181        deps += [
182          "//build/ohos/kits/system_api:{{ platform }}_systemsdk_platform_java($default_toolchain)" # noqa: E501
183        ]
184      {% endif %}
185
186      {% if sources_list_files %}
187        sources_list_file = [ {{ sources_list_files }} ]
188      {% endif %}
189
190      {% if combined_jar_deps %}
191        sources_jar_deps = [":{{ platform }}_stub_kits_combine_java"]
192      {% endif %}
193    }
194    """,
195                        trim_blocks=True,
196                        lstrip_blocks=True)
197
198    for platform, stubs in platform_stubs.items():
199        gn_file = os.path.join(config_output_dir,
200                               '{}-stub/BUILD.gn'.format(platform))
201        gni_file = os.path.join(config_output_dir,
202                                '{}-stub/zframework_stub_exists.gni'.format(platform))
203        gni_contents = []
204        stub_kit_targets = []
205        dist_stub = []
206        parts = stubs.get('src')
207        for part in parts:
208            stub_kit_targets.extend(parts_kits_info.get(part))
209        if stubs.get('dist'):
210            dist_stub = stubs.get('dist')
211        if stub_kit_targets or dist_stub:
212            gni_contents.append('zframework_stub_exists = true')
213            gn_contents = template.render(
214                platform=platform,
215                combined_jar_deps=',\n'.join(stub_kit_targets),
216                sources_list_files=',\n'.join(dist_stub))
217            write_file(gn_file, gn_contents)
218            LogUtil.hb_info(
219                "generated platform stub to '{}/{}-stub/BUILD.gn'".format(
220                  config_output_dir, platform), mode=Config.log_mode)
221
222        else:
223            gni_contents.append('zframework_stub_exists = false')
224
225        write_file(gni_file, '\n'.join(gni_contents))
226        LogUtil.hb_info(
227            "generated platform zframework stub to '{}/subsystem_info/{}-stub/zframework_stub_exists.gni'".format(
228                config_output_dir, platform), mode=Config.log_mode)
229