1# Copyright (c) 2021 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14import("//build/config/clang/clang.gni") 15import("//build/config/python.gni") 16import("//build/ohos/notice/notice.gni") 17import("//build/templates/common/collect_target.gni") 18import("//build/templates/metadata/module_info.gni") 19 20template("ohos_copy") { 21 assert(defined(invoker.sources), 22 "sources must be defined for ${target_name}.") 23 assert(defined(invoker.outputs), 24 "outputs must be defined for ${target_name}.") 25 26 _is_test_target = defined(invoker.testonly) && invoker.testonly 27 _is_prebuilt = defined(invoker.prebuilt) && invoker.prebuilt 28 assert(_is_prebuilt != "") # Mark as used 29 30 # module_info generation is bypassed for prebuilt static library 31 _bypass_module_info_generation = 32 defined(invoker.bypass_module_info_generation) && 33 invoker.bypass_module_info_generation 34 _main_target_name = target_name 35 _target_label = 36 get_label_info(":${_main_target_name}", "label_with_toolchain") 37 assert(_target_label != "") # Mark as used 38 39 if (defined(invoker.subsystem_name) && defined(invoker.part_name)) { 40 _subsystem_name = invoker.subsystem_name 41 _part_name = invoker.part_name 42 } else if (defined(invoker.part_name)) { 43 _part_name = invoker.part_name 44 _part_subsystem_info_file = 45 "$root_build_dir/build_configs/parts_info/part_subsystem.json" 46 _arguments = [ 47 "--part-name", 48 _part_name, 49 "--part-subsystem-info-file", 50 rebase_path(_part_subsystem_info_file, root_build_dir), 51 ] 52 get_subsystem_script = "//build/templates/common/get_subsystem_name.py" 53 _subsystem_name = 54 exec_script(get_subsystem_script, _arguments, "trim string") 55 } else if (defined(invoker.subsystem_name)) { 56 _subsystem_name = invoker.subsystem_name 57 _part_name = _subsystem_name 58 } else { 59 _subsystem_name = "build" 60 _part_name = "build_framework" 61 } 62 assert(_subsystem_name != "") # Mark as used 63 assert(_part_name != "") # Mark as used 64 65 _deps = [] 66 if (defined(invoker.deps)) { 67 _deps += invoker.deps 68 } 69 70 if (!_is_test_target) { 71 module_label = get_label_info(":${target_name}", "label_with_toolchain") 72 _collect_target = "${target_name}__collect" 73 collect_module_target(_collect_target) { 74 forward_variables_from(invoker, [ "install_images" ]) 75 } 76 _notice_target = "${_main_target_name}__notice" 77 78 # Prebuilt target has some lexing error character 79 _notice_target = string_replace(_notice_target, "@", "_") 80 _notice_target = string_replace(_notice_target, "+", "_") 81 collect_notice(_notice_target) { 82 forward_variables_from(invoker, 83 [ 84 "testonly", 85 "license_as_sources", 86 "license_file", 87 "sources", 88 ]) 89 source_list = sources 90 module_name = _main_target_name 91 module_source_dir = get_label_info(":${_main_target_name}", "dir") 92 } 93 _deps += [ 94 ":$_notice_target", 95 ":${_collect_target}", 96 ] 97 } 98 99 if (!_bypass_module_info_generation) { 100 _module_info_target = "${_main_target_name}_info" 101 generate_module_info(_module_info_target) { 102 forward_variables_from(invoker, 103 [ 104 "module_install_dir", 105 "relative_install_dir", 106 "module_source_dir", 107 "module_install_name", 108 "module_type", 109 "install_enable", 110 ]) 111 module_name = _main_target_name 112 if (!defined(module_type)) { 113 module_type = "unknown" 114 } 115 if (!defined(module_source_dir)) { 116 module_source_dir = "${target_out_dir}" 117 } 118 119 if (_is_prebuilt) { 120 _outputs = invoker.outputs 121 module_source = string_replace(_outputs[0], "${target_out_dir}/", "", 1) 122 } 123 prebuilt = _is_prebuilt 124 125 if (!defined(install_enable)) { 126 install_enable = false 127 } 128 129 part_name = _part_name 130 subsystem_name = _subsystem_name 131 132 if (defined(invoker.symlink_path)) { 133 symlink_path = invoker.symlink_path 134 } 135 136 if (defined(invoker.softlink_create_path)) { 137 softlink_create_path = invoker.softlink_create_path 138 } 139 140 module_install_images = [ "system" ] 141 if (defined(invoker.install_images)) { 142 module_install_images = [] 143 module_install_images += invoker.install_images 144 } 145 146 if (defined(invoker.symlink_target_name)) { 147 symlink_target_name = invoker.symlink_target_name 148 } 149 150 if (defined(invoker.innerapi_tags)) { 151 innerapi_tags = invoker.innerapi_tags 152 } 153 154 if (defined(invoker.symlink_ext)) { 155 symlink_ext = invoker.symlink_ext 156 } 157 158 notice = "$target_out_dir/$_main_target_name.notice.txt" 159 } 160 if (!skip_gen_module_info) { 161 _deps += [ ":$_module_info_target" ] 162 } 163 } 164 165 if ((defined(invoker.enable_strip) && invoker.enable_strip) || 166 (defined(invoker.mini_debug) && invoker.mini_debug)) { 167 action_with_pydeps(target_name) { 168 forward_variables_from(invoker, 169 [ 170 "testonly", 171 "visibility", 172 "external_deps", 173 "public_external_deps", 174 "public_configs", 175 "sources", 176 "outputs", 177 ]) 178 deps = _deps 179 script = "//build/templates/common/delete_symbol.py" 180 mini_debug = "false" 181 if (defined(invoker.mini_debug) && invoker.mini_debug) { 182 mini_debug = "true" 183 } 184 args = [ 185 "--strip", 186 rebase_path("${clang_base_path}/bin/llvm-strip", root_build_dir), 187 "--input", 188 rebase_path(sources[0], root_build_dir), 189 "--output", 190 rebase_path(outputs[0], root_build_dir), 191 "--mini-debug", 192 mini_debug, 193 ] 194 195 if (defined(visibility) && visibility != []) { 196 visibility += [ "//build/*" ] 197 if (defined(build_ext_path)) { 198 visibility += [ "${build_ext_path}/*" ] 199 } 200 } 201 202 if (!_bypass_module_info_generation) { 203 _install_module_info = { 204 module_def = _target_label 205 module_info_file = 206 rebase_path(get_label_info(_target_label, "target_out_dir"), 207 root_build_dir) + 208 "/${_main_target_name}_module_info.json" 209 subsystem_name = _subsystem_name 210 part_name = _part_name 211 toolchain = current_toolchain 212 toolchain_out_dir = rebase_path(root_out_dir, root_build_dir) 213 } 214 215 metadata = { 216 install_modules = [ _install_module_info ] 217 } 218 } 219 } 220 } else { 221 copy(target_name) { 222 forward_variables_from(invoker, 223 [ 224 "testonly", 225 "visibility", 226 "public_deps", 227 "external_deps", 228 "public_external_deps", 229 "public_configs", 230 "sources", 231 "outputs", 232 "copy_linkable_file", 233 "rust_crate_name", 234 "rust_crate_type", 235 ]) 236 deps = _deps 237 238 if (defined(visibility) && visibility != []) { 239 visibility += [ "//build/*" ] 240 if (defined(build_ext_path)) { 241 visibility += [ "${build_ext_path}/*" ] 242 } 243 } 244 245 if (!_bypass_module_info_generation) { 246 _install_module_info = { 247 module_def = _target_label 248 module_info_file = 249 rebase_path(get_label_info(_target_label, "target_out_dir"), 250 root_build_dir) + 251 "/${_main_target_name}_module_info.json" 252 subsystem_name = _subsystem_name 253 part_name = _part_name 254 toolchain = current_toolchain 255 toolchain_out_dir = rebase_path(root_out_dir, root_build_dir) 256 } 257 258 metadata = { 259 install_modules = [ _install_module_info ] 260 } 261 } 262 } 263 } 264} 265