• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2023 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/ohos/config.gni")
16import("//build/ohos/notice/notice.gni")
17import("//build/ohos_var.gni")
18import("//build/templates/common/check_target.gni")
19import("//build/templates/common/collect_target.gni")
20import("//build/templates/metadata/module_info.gni")
21
22template("ohos_rust_library") {
23  assert(!defined(invoker.output_dir),
24         "output_dir is not allowed to be defined.")
25  _test_target = defined(invoker.testonly) && invoker.testonly
26  if (defined(invoker.subsystem_name) && defined(invoker.part_name)) {
27    subsystem_name = invoker.subsystem_name
28    part_name = invoker.part_name
29  } else if (defined(invoker.part_name)) {
30    part_name = invoker.part_name
31    _part_subsystem_info_file =
32        "$root_build_dir/build_configs/parts_info/part_subsystem.json"
33    _arguments = [
34      "--part-name",
35      part_name,
36      "--part-subsystem-info-file",
37      rebase_path(_part_subsystem_info_file, root_build_dir),
38    ]
39    get_subsystem_script = "//build/templates/common/get_subsystem_name.py"
40    subsystem_name =
41        exec_script(get_subsystem_script, _arguments, "trim string")
42    if (is_use_check_deps && !_test_target) {
43      skip_check_subsystem = true
44    }
45  } else if (defined(invoker.subsystem_name)) {
46    subsystem_name = invoker.subsystem_name
47    part_name = subsystem_name
48  } else {
49    subsystem_name = "build"
50    part_name = "build_framework"
51  }
52  assert(subsystem_name != "")
53  assert(part_name != "")
54  if (is_use_check_deps && !_test_target) {
55    _check_target = "${target_name}__check"
56    target_path = get_label_info(":${target_name}", "label_no_toolchain")
57    check_target(_check_target) {
58      module_deps = []
59      module_ex_deps = []
60      if (defined(invoker.deps)) {
61        module_deps += invoker.deps
62      }
63      if (defined(invoker.public_deps)) {
64        module_deps += invoker.public_deps
65      }
66      if (defined(invoker.external_deps)) {
67        module_ex_deps += invoker.external_deps
68      }
69      if (defined(invoker.public_external_deps)) {
70        module_ex_deps += invoker.public_external_deps
71      }
72    }
73  }
74
75  if (check_deps) {
76    deps_data = {
77    }
78    module_label = get_label_info(":${target_name}", "label_with_toolchain")
79    module_deps = []
80    if (defined(invoker.deps)) {
81      foreach(dep, invoker.deps) {
82        module_deps += [ get_label_info(dep, "label_no_toolchain") ]
83      }
84    }
85    module_ex_deps = []
86    if (defined(invoker.external_deps) && invoker.external_deps != []) {
87      module_ex_deps = invoker.external_deps
88    }
89    deps_data = {
90      part_name = part_name
91      module_label = module_label
92      deps = module_deps
93      external_deps = module_ex_deps
94    }
95    write_file("${root_out_dir}/deps_files/${part_name}__${target_name}.json",
96               deps_data,
97               "json")
98  }
99
100  if (is_standard_system) {
101    output_dir = "${root_out_dir}/${subsystem_name}/${part_name}"
102  } else {
103    output_dir = "${root_out_dir}"
104  }
105
106  if (!_test_target) {
107    module_label = get_label_info(":${target_name}", "label_with_toolchain")
108    _collect_target = "${target_name}__collect"
109    collect_module_target(_collect_target) {
110      forward_variables_from(invoker, [ "install_images" ])
111    }
112
113    _notice_target = "${target_name}__notice"
114    _main_target_name = target_name
115    collect_notice(_notice_target) {
116      forward_variables_from(invoker,
117                             [
118                               "testonly",
119                               "license_as_sources",
120                               "license_file",
121                             ])
122      module_type = "rust_library"
123
124      module_name = _main_target_name
125      if (defined(invoker.crate_name)) {
126        module_name = invoker.crate_name
127      }
128      module_source_dir = get_label_info(":${_main_target_name}", "dir")
129    }
130  }
131
132  target_label = get_label_info(":${target_name}", "label_with_toolchain")
133  target_toolchain = get_label_info(target_label, "toolchain")
134
135  if (target_toolchain == "${current_toolchain}") {
136    ohos_module_name = target_name
137    _module_info_target = "${target_name}_info"
138    generate_module_info(_module_info_target) {
139      module_name = ohos_module_name
140      module_type = "lib"
141      module_source_dir = "$root_out_dir"
142      if (defined(output_dir)) {
143        module_source_dir = output_dir
144      }
145
146      module_install_name = ohos_module_name
147      if (defined(invoker.output_name)) {
148        module_install_name = invoker.output_name
149      }
150
151      module_install_images = [ "system" ]
152      if (defined(invoker.install_images)) {
153        module_install_images = []
154        module_install_images += invoker.install_images
155      }
156
157      module_output_extension = shlib_extension
158      if (defined(invoker.module_output_extension)) {
159        module_output_extension = invoker.module_output_extension
160      }
161
162      install_enable = true
163      if (defined(invoker.install_enable)) {
164        install_enable = invoker.install_enable
165      }
166
167      if (defined(invoker.module_install_dir)) {
168        module_install_dir = invoker.module_install_dir
169      }
170
171      if (defined(invoker.relative_install_dir)) {
172        relative_install_dir = invoker.relative_install_dir
173      }
174
175      if (defined(invoker.symlink_target_name)) {
176        symlink_target_name = invoker.symlink_target_name
177      }
178
179      if (defined(invoker.output_prefix_override)) {
180        output_prefix_override = invoker.output_prefix_override
181      }
182      notice = "$target_out_dir/$ohos_module_name.notice.txt"
183    }
184  }
185
186  rust_library("${target_name}") {
187    forward_variables_from(invoker,
188                           "*",
189                           [
190                             "configs",
191                             "remove_configs",
192                             "no_default_deps",
193                             "install_images",
194                             "module_install_dir",
195                             "relative_install_dir",
196                             "symlink_target_name",
197                             "output_dir",
198                             "install_enable",
199                             "version_script",
200                             "license_file",
201                             "license_as_sources",
202                             "use_exceptions",
203                             "stl",
204
205                             # Sanitizer variables
206                             "sanitize",
207                           ])
208    output_dir = output_dir
209
210    if (!defined(inputs)) {
211      inputs = []
212    }
213
214    if (!defined(ldflags)) {
215      ldflags = []
216    }
217
218    if (defined(invoker.configs)) {
219      configs += invoker.configs
220    }
221    if (defined(invoker.remove_configs)) {
222      configs -= invoker.remove_configs
223    }
224
225    if (!defined(output_name)) {
226      output_name = target_name
227    }
228
229    if (defined(invoker.no_default_deps)) {
230      no_default_deps = invoker.no_default_deps
231    }
232
233    if (!defined(ldflags)) {
234      ldflags = []
235    }
236    if (!defined(libs)) {
237      libs = []
238    }
239    if (!defined(cflags_cc)) {
240      cflags_cc = []
241    }
242    if (!defined(deps)) {
243      deps = []
244    }
245    if (is_use_check_deps && !_test_target) {
246      deps += [ ":$_check_target" ]
247    }
248    if (target_toolchain == "${current_toolchain}" && !skip_gen_module_info) {
249      deps += [ ":$_module_info_target" ]
250    }
251
252    if (is_ohos) {
253      if (defined(invoker.stl)) {
254        cflags_cc += [
255          "-nostdinc++",
256          "-I" + rebase_path(
257                  "${}toolchains_dir/${host_platform_dir}/llvm_ndk/include/c++/v1",
258                  root_build_dir),
259        ]
260        ldflags += [
261          "-nostdlib++",
262          "-L" + rebase_path("${clang_stl_path}/${abi_target}/c++",
263                             root_build_dir),
264        ]
265
266        libs += [ invoker.stl ]
267      } else {
268        if (current_cpu == "arm" || current_cpu == "arm64") {
269          libs += [ "unwind" ]
270        }
271        ldflags += [ "-L" + rebase_path("${clang_stl_path}/${abi_target}/c++",
272                                        root_build_dir) ]
273
274        libs += [ "c++" ]
275      }
276    }
277
278    if (defined(visibility) && visibility != []) {
279      visibility += [ "//build/*" ]
280      if (defined(build_ext_path)) {
281        visibility += [ "${build_ext_path}/*" ]
282      }
283    }
284
285    if (!_test_target) {
286      deps += [
287        ":$_notice_target",
288        ":${_collect_target}",
289      ]
290    }
291    if (!defined(include_dirs)) {
292      include_dirs = []
293    }
294
295    install_module_info = {
296      module_def = target_label
297      module_info_file =
298          rebase_path(get_label_info(module_def, "target_out_dir"),
299                      root_build_dir) + "/${target_name}_module_info.json"
300      subsystem_name = subsystem_name
301      part_name = part_name
302      toolchain = current_toolchain
303      toolchain_out_dir = rebase_path(root_out_dir, root_build_dir)
304    }
305    metadata = {
306      install_modules = [ install_module_info ]
307    }
308    if (defined(is_debug) && !is_debug && enable_debug_components != "") {
309      foreach(component_name, debug_components) {
310        if (part_name == component_name) {
311          configs -= default_opt_configs
312          configs += debug_level_configs
313        }
314      }
315    }
316  }
317}
318