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      module_install_images = [ "system" ]
137      if (defined(invoker.install_images)) {
138        module_install_images = []
139        module_install_images += invoker.install_images
140      }
141
142      if (defined(invoker.symlink_target_name)) {
143        symlink_target_name = invoker.symlink_target_name
144      }
145
146      if (defined(invoker.innerapi_tags)) {
147        innerapi_tags = invoker.innerapi_tags
148      }
149
150      if (defined(invoker.symlink_ext)) {
151        symlink_ext = invoker.symlink_ext
152      }
153
154      notice = "$target_out_dir/$_main_target_name.notice.txt"
155    }
156    if (!skip_gen_module_info) {
157      _deps += [ ":$_module_info_target" ]
158    }
159  }
160
161  if ((defined(invoker.enable_strip) && invoker.enable_strip) ||
162      (defined(invoker.mini_debug) && invoker.mini_debug)) {
163    action_with_pydeps(target_name) {
164      forward_variables_from(invoker,
165                             [
166                               "testonly",
167                               "visibility",
168                               "external_deps",
169                               "public_external_deps",
170                               "public_configs",
171                               "sources",
172                               "outputs",
173                             ])
174      deps = _deps
175      script = "//build/templates/common/delete_symbol.py"
176      mini_debug = "false"
177      if (defined(invoker.mini_debug) && invoker.mini_debug) {
178        mini_debug = "true"
179      }
180      args = [
181        "--strip",
182        rebase_path("${clang_base_path}/bin/llvm-strip", root_build_dir),
183        "--input",
184        rebase_path(sources[0], root_build_dir),
185        "--output",
186        rebase_path(outputs[0], root_build_dir),
187        "--mini-debug",
188        mini_debug,
189      ]
190
191      if (defined(visibility) && visibility != []) {
192        visibility += [ "//build/*" ]
193        if (defined(build_ext_path)) {
194          visibility += [ "${build_ext_path}/*" ]
195        }
196      }
197
198      if (!_bypass_module_info_generation) {
199        _install_module_info = {
200          module_def = _target_label
201          module_info_file =
202              rebase_path(get_label_info(_target_label, "target_out_dir"),
203                          root_build_dir) +
204              "/${_main_target_name}_module_info.json"
205          subsystem_name = _subsystem_name
206          part_name = _part_name
207          toolchain = current_toolchain
208          toolchain_out_dir = rebase_path(root_out_dir, root_build_dir)
209        }
210
211        metadata = {
212          install_modules = [ _install_module_info ]
213        }
214      }
215    }
216  } else {
217    copy(target_name) {
218      forward_variables_from(invoker,
219                             [
220                               "testonly",
221                               "visibility",
222                               "external_deps",
223                               "public_external_deps",
224                               "public_configs",
225                               "sources",
226                               "outputs",
227                             ])
228      deps = _deps
229
230      if (defined(visibility) && visibility != []) {
231        visibility += [ "//build/*" ]
232        if (defined(build_ext_path)) {
233          visibility += [ "${build_ext_path}/*" ]
234        }
235      }
236
237      if (!_bypass_module_info_generation) {
238        _install_module_info = {
239          module_def = _target_label
240          module_info_file =
241              rebase_path(get_label_info(_target_label, "target_out_dir"),
242                          root_build_dir) +
243              "/${_main_target_name}_module_info.json"
244          subsystem_name = _subsystem_name
245          part_name = _part_name
246          toolchain = current_toolchain
247          toolchain_out_dir = rebase_path(root_out_dir, root_build_dir)
248        }
249
250        metadata = {
251          install_modules = [ _install_module_info ]
252        }
253      }
254    }
255  }
256}
257