15f9996aaSopenharmony_ci#!/usr/bin/env python
25f9996aaSopenharmony_ci# -*- coding: utf-8 -*-
35f9996aaSopenharmony_ci# Copyright (c) 2023 Huawei Device Co., Ltd.
45f9996aaSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
55f9996aaSopenharmony_ci# you may not use this file except in compliance with the License.
65f9996aaSopenharmony_ci# You may obtain a copy of the License at
75f9996aaSopenharmony_ci#
85f9996aaSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
95f9996aaSopenharmony_ci#
105f9996aaSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
115f9996aaSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
125f9996aaSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135f9996aaSopenharmony_ci# See the License for the specific language governing permissions and
145f9996aaSopenharmony_ci# limitations under the License.
155f9996aaSopenharmony_ci
165f9996aaSopenharmony_ci
175f9996aaSopenharmony_ciimport os
185f9996aaSopenharmony_ciimport sys
195f9996aaSopenharmony_ciimport shutil
205f9996aaSopenharmony_ci
215f9996aaSopenharmony_cifrom resources.global_var import CURRENT_OHOS_ROOT
225f9996aaSopenharmony_cifrom util.log_util import LogUtil
235f9996aaSopenharmony_cifrom resources.config import Config
245f9996aaSopenharmony_ci
255f9996aaSopenharmony_ci# Import jinja2 from third_party/jinja2
265f9996aaSopenharmony_cisys.path.insert(1, os.path.join(CURRENT_OHOS_ROOT, 'third_party'))
275f9996aaSopenharmony_cifrom jinja2 import Template  # noqa: E402
285f9996aaSopenharmony_ci
295f9996aaSopenharmony_cisys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
305f9996aaSopenharmony_cifrom scripts.util.file_utils import write_file  # noqa: E402
315f9996aaSopenharmony_ci
325f9996aaSopenharmony_ciPARTS_LIST_GNI_TEMPLATE = """
335f9996aaSopenharmony_ciparts_list = [
345f9996aaSopenharmony_ci  {}
355f9996aaSopenharmony_ci]
365f9996aaSopenharmony_ci"""
375f9996aaSopenharmony_ci
385f9996aaSopenharmony_ciINNER_KITS_GNI_TEMPLATE = """
395f9996aaSopenharmony_ciinner_kits_list = [
405f9996aaSopenharmony_ci  {}
415f9996aaSopenharmony_ci]
425f9996aaSopenharmony_ci"""
435f9996aaSopenharmony_ci
445f9996aaSopenharmony_ciSYSTEM_KITS_GNI_TEMPLATE = """
455f9996aaSopenharmony_cisystem_kits_list = [
465f9996aaSopenharmony_ci  {}
475f9996aaSopenharmony_ci]
485f9996aaSopenharmony_ci"""
495f9996aaSopenharmony_ci
505f9996aaSopenharmony_ciPARTS_TEST_GNI_TEMPLATE = """
515f9996aaSopenharmony_ciparts_test_list = [
525f9996aaSopenharmony_ci  {}
535f9996aaSopenharmony_ci]
545f9996aaSopenharmony_ci"""
555f9996aaSopenharmony_ci
565f9996aaSopenharmony_ciPHONY_TARGET_LIST_TEMPLATE = """
575f9996aaSopenharmony_cigroup("part_phony_targets") {{
585f9996aaSopenharmony_ci  deps = [
595f9996aaSopenharmony_ci    {}
605f9996aaSopenharmony_ci  ]
615f9996aaSopenharmony_ci}}"""
625f9996aaSopenharmony_ci
635f9996aaSopenharmony_ciPHONY_GROUP_TEMPLATE = """
645f9996aaSopenharmony_cigroup("{}_phony") {{
655f9996aaSopenharmony_ci  deps = [ "{}" ]
665f9996aaSopenharmony_ci}}"""
675f9996aaSopenharmony_ci
685f9996aaSopenharmony_cign_file_template = os.path.join(os.path.dirname(os.path.abspath(__file__)),
695f9996aaSopenharmony_ci                                'build_gn.template')
705f9996aaSopenharmony_ci
715f9996aaSopenharmony_ci
725f9996aaSopenharmony_cidef gen_targets_gn(parts_targets, config_output_dir):
735f9996aaSopenharmony_ci    parts_list = []
745f9996aaSopenharmony_ci    inner_kits_list = []
755f9996aaSopenharmony_ci    system_kits_list = []
765f9996aaSopenharmony_ci    parts_test_list = []
775f9996aaSopenharmony_ci    phony_target_list = []
785f9996aaSopenharmony_ci    for part_labels in parts_targets.values():
795f9996aaSopenharmony_ci        parts_list.append(part_labels.get('part'))
805f9996aaSopenharmony_ci        if 'phony' in part_labels:
815f9996aaSopenharmony_ci            phony_target_list.append(part_labels.get('phony'))
825f9996aaSopenharmony_ci        if 'inner_kits' in part_labels:
835f9996aaSopenharmony_ci            inner_kits_list.append(part_labels.get('inner_kits'))
845f9996aaSopenharmony_ci        if 'system_kits' in part_labels:
855f9996aaSopenharmony_ci            system_kits_list.append(part_labels.get('system_kits'))
865f9996aaSopenharmony_ci        if 'test' in part_labels:
875f9996aaSopenharmony_ci            parts_test_list.append(part_labels.get('test'))
885f9996aaSopenharmony_ci    parts_list_gni_file = os.path.join(config_output_dir, 'parts_list.gni')
895f9996aaSopenharmony_ci    parts_list_content = '"{}",'.format('",\n  "'.join(parts_list))
905f9996aaSopenharmony_ci    write_file(parts_list_gni_file,
915f9996aaSopenharmony_ci               PARTS_LIST_GNI_TEMPLATE.format(parts_list_content))
925f9996aaSopenharmony_ci    LogUtil.hb_info(
935f9996aaSopenharmony_ci        "generate part list gni file to '{}/parts_list.gni'".format(config_output_dir), mode=Config.log_mode)
945f9996aaSopenharmony_ci
955f9996aaSopenharmony_ci    inner_kits_gni_file = os.path.join(config_output_dir,
965f9996aaSopenharmony_ci                                       'inner_kits_list.gni')
975f9996aaSopenharmony_ci    if inner_kits_list:
985f9996aaSopenharmony_ci        inner_kits_content = '"{}",'.format('",\n  "'.join(inner_kits_list))
995f9996aaSopenharmony_ci    else:
1005f9996aaSopenharmony_ci        inner_kits_content = ''
1015f9996aaSopenharmony_ci    write_file(inner_kits_gni_file,
1025f9996aaSopenharmony_ci               INNER_KITS_GNI_TEMPLATE.format(inner_kits_content))
1035f9996aaSopenharmony_ci    LogUtil.hb_info(
1045f9996aaSopenharmony_ci        "generate inner kits gni file to '{}/inner_kits_list.gni'".format(
1055f9996aaSopenharmony_ci          config_output_dir), mode=Config.log_mode)
1065f9996aaSopenharmony_ci
1075f9996aaSopenharmony_ci    system_list_gni_file = os.path.join(config_output_dir,
1085f9996aaSopenharmony_ci                                        'system_kits_list.gni')
1095f9996aaSopenharmony_ci    if system_kits_list:
1105f9996aaSopenharmony_ci        system_kits_content = '"{}",'.format('",\n  "'.join(system_kits_list))
1115f9996aaSopenharmony_ci    else:
1125f9996aaSopenharmony_ci        system_kits_content = ''
1135f9996aaSopenharmony_ci    write_file(system_list_gni_file,
1145f9996aaSopenharmony_ci               SYSTEM_KITS_GNI_TEMPLATE.format(system_kits_content))
1155f9996aaSopenharmony_ci    LogUtil.hb_info(
1165f9996aaSopenharmony_ci        "generate system list gni file to '{}/system_kits_list.gni'".format(
1175f9996aaSopenharmony_ci          config_output_dir), mode=Config.log_mode)
1185f9996aaSopenharmony_ci
1195f9996aaSopenharmony_ci    parts_test_gni_file = os.path.join(config_output_dir,
1205f9996aaSopenharmony_ci                                       'parts_test_list.gni')
1215f9996aaSopenharmony_ci    if parts_test_list:
1225f9996aaSopenharmony_ci        test_list_content = '"{}",'.format('",\n  "'.join(parts_test_list))
1235f9996aaSopenharmony_ci    else:
1245f9996aaSopenharmony_ci        test_list_content = ''
1255f9996aaSopenharmony_ci    write_file(parts_test_gni_file,
1265f9996aaSopenharmony_ci               PARTS_TEST_GNI_TEMPLATE.format(test_list_content))
1275f9996aaSopenharmony_ci    LogUtil.hb_info(
1285f9996aaSopenharmony_ci        "generate parts test gni file to '{}/parts_test_list.gni'".format(
1295f9996aaSopenharmony_ci          config_output_dir), mode=Config.log_mode)
1305f9996aaSopenharmony_ci
1315f9996aaSopenharmony_ci    build_gn_file = os.path.join(config_output_dir, 'BUILD.gn')
1325f9996aaSopenharmony_ci    shutil.copyfile(gn_file_template, build_gn_file)
1335f9996aaSopenharmony_ci    LogUtil.hb_info(
1345f9996aaSopenharmony_ci        "generate build gn file to '{}/BUILD.gn'".format(config_output_dir), mode=Config.log_mode)
1355f9996aaSopenharmony_ci
1365f9996aaSopenharmony_ci
1375f9996aaSopenharmony_cidef gen_phony_targets(variant_phony_targets, config_output_dir):
1385f9996aaSopenharmony_ci    phony_target_list = []
1395f9996aaSopenharmony_ci    phony_group_list = []
1405f9996aaSopenharmony_ci    for part_name, part_label in variant_phony_targets.items():
1415f9996aaSopenharmony_ci        phony_target_list.append('{}_phony'.format(part_name))
1425f9996aaSopenharmony_ci        phony_group_list.append(
1435f9996aaSopenharmony_ci            PHONY_GROUP_TEMPLATE.format(part_name, part_label))
1445f9996aaSopenharmony_ci
1455f9996aaSopenharmony_ci    phony_list_content = ''
1465f9996aaSopenharmony_ci    if phony_target_list:
1475f9996aaSopenharmony_ci        phony_list_content = '":{}",'.format(
1485f9996aaSopenharmony_ci            '",\n  ":'.join(phony_target_list))
1495f9996aaSopenharmony_ci    phony_build_content = []
1505f9996aaSopenharmony_ci    phony_build_content.append(
1515f9996aaSopenharmony_ci        PHONY_TARGET_LIST_TEMPLATE.format(phony_list_content))
1525f9996aaSopenharmony_ci    phony_build_content.extend(phony_group_list)
1535f9996aaSopenharmony_ci
1545f9996aaSopenharmony_ci    phony_build_file = os.path.join(config_output_dir, 'phony_targets',
1555f9996aaSopenharmony_ci                                    'BUILD.gn')
1565f9996aaSopenharmony_ci    write_file(phony_build_file, '\n'.join(phony_build_content))
1575f9996aaSopenharmony_ci    LogUtil.hb_info(
1585f9996aaSopenharmony_ci        "generate phony target build file to '{}/phony_targets/BUILD.gn'".format(
1595f9996aaSopenharmony_ci          config_output_dir), mode=Config.log_mode)
1605f9996aaSopenharmony_ci
1615f9996aaSopenharmony_ci
1625f9996aaSopenharmony_cidef gen_stub_targets(parts_kits_info, platform_stubs, config_output_dir):
1635f9996aaSopenharmony_ci    template = Template("""
1645f9996aaSopenharmony_ci    # AUTO generated gn file, DONOT try to modify it.
1655f9996aaSopenharmony_ci    import("//build/config/ohos/rules.gni")
1665f9996aaSopenharmony_ci    import("//build/ohos/kits/kits_package.gni")
1675f9996aaSopenharmony_ci    {% if combined_jar_deps %}
1685f9996aaSopenharmony_ci      ohos_combine_jars("{{ platform }}_stub_kits_combine_java") {
1695f9996aaSopenharmony_ci        deps = [
1705f9996aaSopenharmony_ci          {{ combined_jar_deps }}
1715f9996aaSopenharmony_ci        ]
1725f9996aaSopenharmony_ci      }
1735f9996aaSopenharmony_ci    {% endif %}
1745f9996aaSopenharmony_ci
1755f9996aaSopenharmony_ci    stub_jar("{{ platform }}_zframework_stub_java")  {
1765f9996aaSopenharmony_ci      deps = [
1775f9996aaSopenharmony_ci        "//third_party/openjdk_stubs:rt_java",
1785f9996aaSopenharmony_ci        "//build/ohos/kits/system_api:phone_systemsdk_base_java($default_toolchain)",
1795f9996aaSopenharmony_ci      ]
1805f9996aaSopenharmony_ci      {% if platform != "phone" %}
1815f9996aaSopenharmony_ci        deps += [
1825f9996aaSopenharmony_ci          "//build/ohos/kits/system_api:{{ platform }}_systemsdk_platform_java($default_toolchain)" # noqa: E501
1835f9996aaSopenharmony_ci        ]
1845f9996aaSopenharmony_ci      {% endif %}
1855f9996aaSopenharmony_ci
1865f9996aaSopenharmony_ci      {% if sources_list_files %}
1875f9996aaSopenharmony_ci        sources_list_file = [ {{ sources_list_files }} ]
1885f9996aaSopenharmony_ci      {% endif %}
1895f9996aaSopenharmony_ci
1905f9996aaSopenharmony_ci      {% if combined_jar_deps %}
1915f9996aaSopenharmony_ci        sources_jar_deps = [":{{ platform }}_stub_kits_combine_java"]
1925f9996aaSopenharmony_ci      {% endif %}
1935f9996aaSopenharmony_ci    }
1945f9996aaSopenharmony_ci    """,
1955f9996aaSopenharmony_ci                        trim_blocks=True,
1965f9996aaSopenharmony_ci                        lstrip_blocks=True)
1975f9996aaSopenharmony_ci
1985f9996aaSopenharmony_ci    for platform, stubs in platform_stubs.items():
1995f9996aaSopenharmony_ci        gn_file = os.path.join(config_output_dir,
2005f9996aaSopenharmony_ci                               '{}-stub/BUILD.gn'.format(platform))
2015f9996aaSopenharmony_ci        gni_file = os.path.join(config_output_dir,
2025f9996aaSopenharmony_ci                                '{}-stub/zframework_stub_exists.gni'.format(platform))
2035f9996aaSopenharmony_ci        gni_contents = []
2045f9996aaSopenharmony_ci        stub_kit_targets = []
2055f9996aaSopenharmony_ci        dist_stub = []
2065f9996aaSopenharmony_ci        parts = stubs.get('src')
2075f9996aaSopenharmony_ci        for part in parts:
2085f9996aaSopenharmony_ci            stub_kit_targets.extend(parts_kits_info.get(part))
2095f9996aaSopenharmony_ci        if stubs.get('dist'):
2105f9996aaSopenharmony_ci            dist_stub = stubs.get('dist')
2115f9996aaSopenharmony_ci        if stub_kit_targets or dist_stub:
2125f9996aaSopenharmony_ci            gni_contents.append('zframework_stub_exists = true')
2135f9996aaSopenharmony_ci            gn_contents = template.render(
2145f9996aaSopenharmony_ci                platform=platform,
2155f9996aaSopenharmony_ci                combined_jar_deps=',\n'.join(stub_kit_targets),
2165f9996aaSopenharmony_ci                sources_list_files=',\n'.join(dist_stub))
2175f9996aaSopenharmony_ci            write_file(gn_file, gn_contents)
2185f9996aaSopenharmony_ci            LogUtil.hb_info(
2195f9996aaSopenharmony_ci                "generated platform stub to '{}/{}-stub/BUILD.gn'".format(
2205f9996aaSopenharmony_ci                  config_output_dir, platform), mode=Config.log_mode)
2215f9996aaSopenharmony_ci
2225f9996aaSopenharmony_ci        else:
2235f9996aaSopenharmony_ci            gni_contents.append('zframework_stub_exists = false')
2245f9996aaSopenharmony_ci
2255f9996aaSopenharmony_ci        write_file(gni_file, '\n'.join(gni_contents))
2265f9996aaSopenharmony_ci        LogUtil.hb_info(
2275f9996aaSopenharmony_ci            "generated platform zframework stub to '{}/subsystem_info/{}-stub/zframework_stub_exists.gni'".format(
2285f9996aaSopenharmony_ci                config_output_dir, platform), mode=Config.log_mode)
229