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