• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (!_bypass_module_info_generation) {
89    _module_info_target = "${_main_target_name}_info"
90    generate_module_info(_module_info_target) {
91      forward_variables_from(invoker,
92                             [
93                               "module_install_dir",
94                               "relative_install_dir",
95                               "module_source_dir",
96                               "module_install_name",
97                               "module_type",
98                               "install_enable",
99                             ])
100      module_name = _main_target_name
101      if (!defined(module_type)) {
102        module_type = "unknown"
103      }
104      if (!defined(module_source_dir)) {
105        module_source_dir = "${target_out_dir}"
106      }
107
108      if (_is_prebuilt) {
109        _outputs = invoker.outputs
110        module_source = string_replace(_outputs[0], "${target_out_dir}/", "", 1)
111      }
112      prebuilt = _is_prebuilt
113
114      if (!defined(install_enable)) {
115        install_enable = false
116      }
117
118      part_name = _part_name
119      subsystem_name = _subsystem_name
120
121      module_install_images = [ "system" ]
122      if (defined(invoker.install_images)) {
123        module_install_images = []
124        module_install_images += invoker.install_images
125      }
126
127      if (defined(invoker.symlink_target_name)) {
128        symlink_target_name = invoker.symlink_target_name
129      }
130
131      if (defined(invoker.innerapi_tags)) {
132        innerapi_tags = invoker.innerapi_tags
133      }
134
135      notice = "$target_out_dir/$_main_target_name.notice.txt"
136    }
137    if (!skip_gen_module_info) {
138      _deps += [ ":$_module_info_target" ]
139    }
140  }
141
142  if (defined(invoker.enable_strip) && invoker.enable_strip) {
143    action_with_pydeps(target_name) {
144      forward_variables_from(invoker,
145                             [
146                               "testonly",
147                               "visibility",
148                               "public_configs",
149                               "sources",
150                               "outputs",
151                             ])
152      deps = _deps
153      script = "//build/templates/common/delete_symbol.py"
154      args = [
155        "--strip",
156        rebase_path("${clang_base_path}/bin/llvm-strip", root_build_dir),
157        "--input",
158        rebase_path(sources[0], root_build_dir),
159        "--output",
160        rebase_path(outputs[0], root_build_dir),
161      ]
162
163      if (!_bypass_module_info_generation) {
164        _install_module_info = {
165          module_def = _target_label
166          module_info_file =
167              rebase_path(get_label_info(_target_label, "target_out_dir"),
168                          root_build_dir) +
169              "/${_main_target_name}_module_info.json"
170          subsystem_name = _subsystem_name
171          part_name = _part_name
172          toolchain = current_toolchain
173          toolchain_out_dir = rebase_path(root_out_dir, root_build_dir)
174        }
175
176        metadata = {
177          install_modules = [ _install_module_info ]
178        }
179      }
180    }
181  } else {
182    copy(target_name) {
183      forward_variables_from(invoker,
184                             [
185                               "testonly",
186                               "visibility",
187                               "public_configs",
188                               "sources",
189                               "outputs",
190                             ])
191      deps = _deps
192
193      if (!_bypass_module_info_generation) {
194        _install_module_info = {
195          module_def = _target_label
196          module_info_file =
197              rebase_path(get_label_info(_target_label, "target_out_dir"),
198                          root_build_dir) +
199              "/${_main_target_name}_module_info.json"
200          subsystem_name = _subsystem_name
201          part_name = _part_name
202          toolchain = current_toolchain
203          toolchain_out_dir = rebase_path(root_out_dir, root_build_dir)
204        }
205
206        metadata = {
207          install_modules = [ _install_module_info ]
208        }
209      }
210    }
211  }
212}
213