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/metadata/module_info.gni") 18 19template("ohos_copy") { 20 assert(defined(invoker.sources), 21 "sources must be defined for ${target_name}.") 22 assert(defined(invoker.outputs), 23 "outputs must be defined for ${target_name}.") 24 25 _is_test_target = defined(invoker.testonly) && invoker.testonly 26 _is_prebuilt = defined(invoker.prebuilt) && invoker.prebuilt 27 assert(_is_prebuilt != "") # Mark as used 28 29 # module_info generation is bypassed for prebuilt static library 30 _bypass_module_info_generation = 31 defined(invoker.bypass_module_info_generation) && 32 invoker.bypass_module_info_generation 33 _main_target_name = target_name 34 _target_label = 35 get_label_info(":${_main_target_name}", "label_with_toolchain") 36 assert(_target_label != "") # Mark as used 37 38 if (defined(invoker.subsystem_name) && defined(invoker.part_name)) { 39 _subsystem_name = invoker.subsystem_name 40 _part_name = invoker.part_name 41 } else if (defined(invoker.part_name)) { 42 _part_name = invoker.part_name 43 _part_subsystem_info_file = 44 "$root_build_dir/build_configs/parts_info/part_subsystem.json" 45 _arguments = [ 46 "--part-name", 47 _part_name, 48 "--part-subsystem-info-file", 49 rebase_path(_part_subsystem_info_file, root_build_dir), 50 ] 51 get_subsystem_script = "//build/templates/common/get_subsystem_name.py" 52 _subsystem_name = 53 exec_script(get_subsystem_script, _arguments, "trim string") 54 } else if (defined(invoker.subsystem_name)) { 55 _subsystem_name = invoker.subsystem_name 56 _part_name = _subsystem_name 57 } else { 58 _subsystem_name = "common" 59 _part_name = _subsystem_name 60 } 61 assert(_subsystem_name != "") # Mark as used 62 assert(_part_name != "") # Mark as used 63 64 _deps = [] 65 if (defined(invoker.deps)) { 66 _deps += invoker.deps 67 } 68 69 if (!_is_test_target) { 70 _notice_target = "${_main_target_name}__notice" 71 72 # Prebuilt target has some lexing error character 73 _notice_target = string_replace(_notice_target, "@", "_") 74 _notice_target = string_replace(_notice_target, "+", "_") 75 collect_notice(_notice_target) { 76 forward_variables_from(invoker, 77 [ 78 "testonly", 79 "license_as_sources", 80 "license_file", 81 ]) 82 module_name = _main_target_name 83 module_source_dir = get_label_info(":${_main_target_name}", "dir") 84 } 85 _deps += [ ":$_notice_target" ] 86 } 87 88 if (defined(invoker.enable_strip) && invoker.enable_strip) { 89 action_with_pydeps(target_name) { 90 forward_variables_from(invoker, 91 [ 92 "testonly", 93 "visibility", 94 "public_configs", 95 "sources", 96 "outputs", 97 ]) 98 deps = _deps 99 script = "//build/templates/common/delete_symbol.py" 100 args = [ 101 "--strip", 102 rebase_path("${clang_base_path}/bin/llvm-strip", root_build_dir), 103 "--input", 104 rebase_path(sources[0], root_build_dir), 105 "--output", 106 rebase_path(outputs[0], root_build_dir), 107 ] 108 109 if (!_bypass_module_info_generation) { 110 _install_module_info = { 111 module_def = _target_label 112 module_info_file = 113 rebase_path(get_label_info(_target_label, "target_out_dir"), 114 root_build_dir) + 115 "/${_main_target_name}_module_info.json" 116 subsystem_name = _subsystem_name 117 part_name = _part_name 118 toolchain = current_toolchain 119 toolchain_out_dir = rebase_path(root_out_dir, root_build_dir) 120 } 121 122 metadata = { 123 install_modules = [ _install_module_info ] 124 } 125 } 126 } 127 } else { 128 copy(target_name) { 129 forward_variables_from(invoker, 130 [ 131 "testonly", 132 "visibility", 133 "public_configs", 134 "sources", 135 "outputs", 136 ]) 137 deps = _deps 138 139 if (!_bypass_module_info_generation) { 140 _install_module_info = { 141 module_def = _target_label 142 module_info_file = 143 rebase_path(get_label_info(_target_label, "target_out_dir"), 144 root_build_dir) + 145 "/${_main_target_name}_module_info.json" 146 subsystem_name = _subsystem_name 147 part_name = _part_name 148 toolchain = current_toolchain 149 toolchain_out_dir = rebase_path(root_out_dir, root_build_dir) 150 } 151 152 metadata = { 153 install_modules = [ _install_module_info ] 154 } 155 } 156 } 157 } 158 if (skip_gen_module_info) { 159 not_needed(invoker, "*") 160 } else if (!_bypass_module_info_generation) { 161 generate_module_info("${_main_target_name}_info") { 162 forward_variables_from(invoker, 163 [ 164 "module_install_dir", 165 "relative_install_dir", 166 "module_source_dir", 167 "module_install_name", 168 "module_type", 169 "install_enable", 170 ]) 171 module_name = _main_target_name 172 if (!defined(module_type)) { 173 module_type = "unknown" 174 } 175 if (!defined(module_source_dir)) { 176 module_source_dir = "${target_out_dir}" 177 } 178 179 if (_is_prebuilt) { 180 _outputs = invoker.outputs 181 module_source = string_replace(_outputs[0], "${target_out_dir}/", "", 1) 182 } 183 prebuilt = _is_prebuilt 184 185 if (!defined(install_enable)) { 186 install_enable = false 187 } 188 189 module_install_images = [ "system" ] 190 if (defined(invoker.install_images)) { 191 module_install_images = [] 192 module_install_images += invoker.install_images 193 } 194 195 if (defined(invoker.symlink_target_name)) { 196 symlink_target_name = invoker.symlink_target_name 197 } 198 199 notice = "$target_out_dir/$_main_target_name.notice.txt" 200 } 201 } 202} 203