• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2022 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/rust/rustc_toolchain.gni")
15import("//build/templates/cxx/cxx.gni")
16import("//build/templates/rust/ohos_rust_library.gni")
17
18allowAllLints = [
19  "--cap-lints",
20  "allow",
21]
22rustcOhosLints = [
23  "-A",
24  "deprecated",
25  "-D",
26  "missing-docs",
27  "-D",
28  "warnings",
29]
30rustcVendorLints = [
31  "-A",
32  "deprecated",
33  "-D",
34  "warnings",
35]
36rustcAllWarningLints = [
37  "-W",
38  "non_ascii_idents",
39  "-W",
40  "ambiguous_associated_items",
41  "-W",
42  "arithmetic_overflow",
43  "-W",
44  "bindings_with_variant_name",
45  "-W",
46  "cenum_impl_drop_cast",
47  "-W",
48  "conflicting_repr_hints",
49  "-W",
50  "deprecated_cfg_attr_crate_type_name",
51  "-W",
52  "enum_intrinsics_non_enums",
53  "-W",
54  "ill_formed_attribute_input",
55  "-W",
56  "implied_bounds_entailment",
57  "-W",
58  "incomplete_include",
59  "-W",
60  "ineffective_unstable_trait_impl",
61  "-W",
62  "invalid_atomic_ordering",
63  "-W",
64  "invalid_type_param_default",
65  "-W",
66  "let_underscore_lock",
67  "-W",
68  "macro_expanded_macro_exports_accessed_by_absolute_paths",
69  "-W",
70  "missing_fragment_specifier",
71  "-W",
72  "mutable_transmutes",
73  "-W",
74  "named_asm_labels",
75  "-W",
76  "no_mangle_const_items",
77  "-W",
78  "order_dependent_trait_objects",
79  "-W",
80  "overflowing_literals",
81  "-W",
82  "patterns_in_fns_without_body",
83  "-W",
84  "proc_macro_back_compat",
85  "-W",
86  "proc_macro_derive_resolution_fallback",
87  "-W",
88  "pub_use_of_private_extern_crate",
89  "-W",
90  "soft_unstable",
91  "-W",
92  "test_unstable_lint",
93  "-W",
94  "text_direction_codepoint_in_comment",
95  "-W",
96  "text_direction_codepoint_in_literal",
97  "-W",
98  "unconditional_panic",
99  "-W",
100  "unknown_crate_types",
101  "-W",
102  "useless_deprecated",
103]
104clippyOhosLints = [
105  "-A",
106  "clippy::type-complexity",
107  "-A",
108  "clippy::unnecessary-wraps",
109  "-A",
110  "clippy::unusual-byte-groupings",
111  "-A",
112  "clippy::upper-case-acronyms",
113  "-A",
114  "clippy::let_and_return",
115  "-A",
116  "clippy::unnecessary-cast",
117]
118clippyVendorLints = [
119  "-A",
120  "clippy::complexity",
121  "-A",
122  "clippy::perf",
123  "-A",
124  "clippy::style",
125]
126clippyAllWarningLints = [
127  "-W",
128  "clippy::all",
129  "-W",
130  "clippy::pedantic",
131  "-W",
132  "clippy::restriction",
133]
134
135template("rust_target") {
136  assert(!defined(invoker.crate_root) ||
137         !(defined(invoker.generate_crate_root) && invoker.generate_crate_root))
138
139  _crate_name = target_name
140  if (defined(invoker.crate_name)) {
141    _crate_name = invoker.crate_name
142  }
143  _crate_type = ""
144  if (defined(invoker.crate_type)) {
145    _crate_type = invoker.crate_type
146  }
147  _deps = []
148  if (defined(invoker.deps)) {
149    _deps += invoker.deps
150  }
151
152  _rustflags = []
153  if (defined(invoker.rustflags)) {
154    _rustflags += invoker.rustflags
155  }
156
157  _public_deps = []
158  if (defined(invoker.public_deps)) {
159    _public_deps += invoker.public_deps
160  }
161
162  if (defined(invoker.output_dir) && invoker.output_dir != "") {
163    _out_dir = invoker.output_dir
164  } else {
165    _out_dir = target_out_dir
166  }
167
168  if (defined(invoker.features)) {
169    foreach(i, invoker.features) {
170      _rustflags += [ "--cfg=feature=\"${i}\"" ]
171    }
172  }
173  _rustenv = [ "OUT_DIR=" + rebase_path(_out_dir) ]
174  if (defined(invoker.rustenv)) {
175    _rustenv += invoker.rustenv
176  }
177
178  assert(defined(invoker.sources), "sources must be listed")
179
180  _rust_deps = _deps
181  _rust_public_deps = _public_deps
182
183  _edition = rust_default_edition
184  if (defined(invoker.edition)) {
185    _edition = invoker.edition
186  }
187  _rustflags += [ string_join("",
188                              [
189                                "--edition=",
190                                _edition,
191                              ]) ]
192  if (invoker.target_type == "rust_proc_macro") {
193    _rustflags += [
194      "--extern",
195      "proc_macro",
196    ]
197  }
198
199  if (is_asan) {
200    if (use_hwasan) {
201      _rustflags += [
202        "-Clink-arg=-fsanitize=hwaddress",
203        "-Clink-arg=-shared-libasan",
204      ]
205    } else {
206      _rustflags += [
207        "-Clink-arg=-fsanitize=address",
208        "-Clink-arg=-shared-libasan",
209      ]
210    }
211  }
212  target(invoker.target_type, "${target_name}") {
213    forward_variables_from(invoker,
214                           "*",
215                           [
216                             "features",
217                             "deps",
218                             "public_deps",
219                             "rustflags",
220                             "rustenv",
221
222                             # "configs",
223                             "output_dir",
224                             "crate_type",
225                           ])
226    crate_name = _crate_name
227
228    deps = _rust_deps
229    public_deps = _rust_public_deps
230    rustflags = _rustflags
231    rustenv = _rustenv
232    crate_type = _crate_type
233    if (target_type == "rust_proc_macro") {
234      output_dir = _out_dir
235    }
236    if (!defined(output_name) || output_name == "") {
237      output_name = crate_name
238    }
239  }
240}
241
242template("ohos_rust_executable") {
243  _target_name = target_name
244  _rustflags = []
245  rust_target("$_target_name") {
246    target_type = "ohos_executable"
247    forward_variables_from(invoker, "*")
248    if (!defined(invoker.crate_name)) {
249      crate_name = _target_name
250    }
251    crate_type = "bin"
252    if (defined(invoker.crate_type)) {
253      assert(invoker.crate_type == crate_type,
254             "crate_type should be $crate_type or use default value.")
255    }
256    configs = []
257    if (!defined(deps)) {
258      deps = []
259    }
260    if (defined(invoker.rustc_lints)) {
261      rustc_lints = invoker.rustc_lints
262    }
263    if (defined(invoker.clippy_lints)) {
264      clippy_lints = invoker.clippy_lints
265    }
266
267    if (defined(rustc_codecheck) && rustc_codecheck) {
268      rustc_lints = "allWarning"
269      clippy_lints = "allWarning"
270    }
271
272    if (!defined(rustc_lints) && !defined(clippy_lints)) {
273      file_path =
274          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
275      file_path_split = string_split(file_path[0], "/")
276      source_dir_begin = file_path_split[2]
277
278      if (source_dir_begin == "third_party") {
279        _rustflags += allowAllLints
280      } else if (source_dir_begin == "prebuilts") {
281        _rustflags += allowAllLints
282      } else if (source_dir_begin == "vendor" &&
283                 file_path_split[3] == "open_source") {
284        _rustflags += allowAllLints
285      } else if (source_dir_begin == "vendor") {
286        _rustflags += rustcVendorLints
287        _rustflags += clippyVendorLints
288      } else if (source_dir_begin == "device") {
289        _rustflags += rustcVendorLints
290        _rustflags += clippyVendorLints
291      } else {
292        _rustflags += rustcOhosLints
293        _rustflags += clippyOhosLints
294      }
295    }
296
297    if (defined(rustc_lints)) {
298      if (rustc_lints == "openharmony") {
299        _rustflags += rustcOhosLints
300      } else if (rustc_lints == "vendor") {
301        _rustflags += rustcVendorLints
302      } else if (rustc_lints == "none") {
303        _rustflags += allowAllLints
304      } else if (rustc_lints == "allWarning") {
305        _rustflags += rustcAllWarningLints
306      }
307    }
308    if (defined(clippy_lints)) {
309      if (clippy_lints == "openharmony") {
310        _rustflags += clippyOhosLints
311      } else if (clippy_lints == "vendor") {
312        _rustflags += clippyVendorLints
313      } else if (clippy_lints == "none") {
314        _rustflags += allowAllLints
315      } else if (clippy_lints == "allWarning") {
316        _rustflags += clippyAllWarningLints
317      }
318    }
319    if (!defined(rustflags)) {
320      rustflags = _rustflags
321    } else {
322      rustflags += _rustflags
323    }
324    if (!defined(rust_static_link) || !rust_static_link) {
325      rustflags += [ "-Cprefer-dynamic" ]
326    }
327  }
328}
329
330template("ohos_rust_shared_library") {
331  _target_name = target_name
332  _rustflags = []
333  rust_target("$_target_name") {
334    target_type = "ohos_rust_library"
335    forward_variables_from(invoker, "*")
336    if (!defined(invoker.crate_name)) {
337      crate_name = _target_name
338    }
339    crate_type = "dylib"
340    if (defined(invoker.crate_type)) {
341      assert(invoker.crate_type == crate_type,
342             "crate_type should be $crate_type or use default value.")
343    }
344
345    if (defined(invoker.output_extension)) {
346      module_output_extension = "." + invoker.output_extension
347    } else {
348      module_output_extension = dylib_extension
349    }
350
351    if (defined(invoker.rustc_lints)) {
352      rustc_lints = invoker.rustc_lints
353    }
354    if (defined(invoker.clippy_lints)) {
355      clippy_lints = invoker.clippy_lints
356    }
357
358    if (defined(rustc_codecheck) && rustc_codecheck) {
359      rustc_lints = "allWarning"
360      clippy_lints = "allWarning"
361    }
362
363    if (!defined(rustc_lints) && !defined(clippy_lints)) {
364      file_path =
365          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
366      file_path_split = string_split(file_path[0], "/")
367      source_dir_begin = file_path_split[2]
368
369      if (source_dir_begin == "third_party") {
370        _rustflags += allowAllLints
371      } else if (source_dir_begin == "prebuilts") {
372        _rustflags += allowAllLints
373      } else if (source_dir_begin == "vendor" &&
374                 file_path_split[3] == "open_source") {
375        _rustflags += allowAllLints
376      } else if (source_dir_begin == "vendor") {
377        _rustflags += rustcVendorLints
378        _rustflags += clippyVendorLints
379      } else if (source_dir_begin == "device") {
380        _rustflags += rustcVendorLints
381        _rustflags += clippyVendorLints
382      } else {
383        _rustflags += rustcOhosLints
384        _rustflags += clippyOhosLints
385      }
386    }
387
388    if (defined(rustc_lints)) {
389      if (rustc_lints == "openharmony") {
390        _rustflags += rustcOhosLints
391      } else if (rustc_lints == "vendor") {
392        _rustflags += rustcVendorLints
393      } else if (rustc_lints == "none") {
394        _rustflags += allowAllLints
395      } else if (rustc_lints == "allWarning") {
396        _rustflags += rustcAllWarningLints
397      }
398    }
399    if (defined(clippy_lints)) {
400      if (clippy_lints == "openharmony") {
401        _rustflags += clippyOhosLints
402      } else if (clippy_lints == "vendor") {
403        _rustflags += clippyVendorLints
404      } else if (clippy_lints == "none") {
405        _rustflags += allowAllLints
406      } else if (clippy_lints == "allWarning") {
407        _rustflags += clippyAllWarningLints
408      }
409    }
410    if (!defined(rustflags)) {
411      rustflags = _rustflags
412    } else {
413      rustflags += _rustflags
414    }
415  }
416}
417
418template("ohos_rust_static_library") {
419  _target_name = target_name
420  _rustflags = []
421  rust_target("$_target_name") {
422    target_type = "ohos_rust_library"
423    forward_variables_from(invoker, "*")
424    if (!defined(invoker.crate_name)) {
425      crate_name = _target_name
426    }
427    crate_type = "rlib"
428    if (defined(invoker.crate_type)) {
429      assert(invoker.crate_type == crate_type,
430             "crate_type should be $crate_type or use default value.")
431    }
432    module_output_extension = rlib_extension
433    install_enable = false
434
435    if (defined(invoker.rustc_lints)) {
436      rustc_lints = invoker.rustc_lints
437    }
438    if (defined(invoker.clippy_lints)) {
439      clippy_lints = invoker.clippy_lints
440    }
441
442    if (defined(rustc_codecheck) && rustc_codecheck) {
443      rustc_lints = "allWarning"
444      clippy_lints = "allWarning"
445    }
446
447    if (!defined(rustc_lints) && !defined(clippy_lints)) {
448      file_path =
449          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
450      file_path_split = string_split(file_path[0], "/")
451      source_dir_begin = file_path_split[2]
452
453      if (source_dir_begin == "third_party") {
454        _rustflags += allowAllLints
455      } else if (source_dir_begin == "prebuilts") {
456        _rustflags += allowAllLints
457      } else if (source_dir_begin == "vendor" &&
458                 file_path_split[3] == "open_source") {
459        _rustflags += allowAllLints
460      } else if (source_dir_begin == "vendor") {
461        _rustflags += rustcVendorLints
462        _rustflags += clippyVendorLints
463      } else if (source_dir_begin == "device") {
464        _rustflags += rustcVendorLints
465        _rustflags += clippyVendorLints
466      } else {
467        _rustflags += rustcOhosLints
468        _rustflags += clippyOhosLints
469      }
470    }
471
472    if (defined(rustc_lints)) {
473      if (rustc_lints == "openharmony") {
474        _rustflags += rustcOhosLints
475      } else if (rustc_lints == "vendor") {
476        _rustflags += rustcVendorLints
477      } else if (rustc_lints == "none") {
478        _rustflags += allowAllLints
479      } else if (rustc_lints == "allWarning") {
480        _rustflags += rustcAllWarningLints
481      }
482    }
483    if (defined(clippy_lints)) {
484      if (clippy_lints == "openharmony") {
485        _rustflags += clippyOhosLints
486      } else if (clippy_lints == "vendor") {
487        _rustflags += clippyVendorLints
488      } else if (clippy_lints == "none") {
489        _rustflags += allowAllLints
490      } else if (clippy_lints == "allWarning") {
491        _rustflags += clippyAllWarningLints
492      }
493    }
494    if (!defined(rustflags)) {
495      rustflags = _rustflags
496    } else {
497      rustflags += _rustflags
498    }
499  }
500}
501
502template("ohos_rust_shared_ffi") {
503  _target_name = target_name
504  _rustflags = []
505  rust_target("$_target_name") {
506    target_type = "ohos_shared_library"
507    forward_variables_from(invoker, "*")
508    if (!defined(invoker.crate_name)) {
509      crate_name = _target_name
510    }
511    crate_type = "cdylib"
512    if (defined(invoker.crate_type)) {
513      assert(invoker.crate_type == crate_type,
514             "crate_type should be $crate_type or use default value.")
515    }
516
517    if (!defined(deps)) {
518      deps = []
519    }
520
521    if (defined(invoker.rustc_lints)) {
522      rustc_lints = invoker.rustc_lints
523    }
524    if (defined(invoker.clippy_lints)) {
525      clippy_lints = invoker.clippy_lints
526    }
527
528    if (defined(rustc_codecheck) && rustc_codecheck) {
529      rustc_lints = "allWarning"
530      clippy_lints = "allWarning"
531    }
532
533    if (!defined(rustc_lints) && !defined(clippy_lints)) {
534      file_path =
535          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
536      file_path_split = string_split(file_path[0], "/")
537      source_dir_begin = file_path_split[2]
538
539      if (source_dir_begin == "third_party") {
540        _rustflags += allowAllLints
541      } else if (source_dir_begin == "prebuilts") {
542        _rustflags += allowAllLints
543      } else if (source_dir_begin == "vendor" &&
544                 file_path_split[3] == "open_source") {
545        _rustflags += allowAllLints
546      } else if (source_dir_begin == "vendor") {
547        _rustflags += rustcVendorLints
548        _rustflags += clippyVendorLints
549      } else if (source_dir_begin == "device") {
550        _rustflags += rustcVendorLints
551        _rustflags += clippyVendorLints
552      } else {
553        _rustflags += rustcOhosLints
554        _rustflags += clippyOhosLints
555      }
556    }
557
558    if (defined(rustc_lints)) {
559      if (rustc_lints == "openharmony") {
560        _rustflags += rustcOhosLints
561      } else if (rustc_lints == "vendor") {
562        _rustflags += rustcVendorLints
563      } else if (rustc_lints == "none") {
564        _rustflags += allowAllLints
565      } else if (rustc_lints == "allWarning") {
566        _rustflags += rustcAllWarningLints
567      }
568    }
569    if (defined(clippy_lints)) {
570      if (clippy_lints == "openharmony") {
571        _rustflags += clippyOhosLints
572      } else if (clippy_lints == "vendor") {
573        _rustflags += clippyVendorLints
574      } else if (clippy_lints == "none") {
575        _rustflags += allowAllLints
576      } else if (clippy_lints == "allWarning") {
577        _rustflags += clippyAllWarningLints
578      }
579    }
580    if (!defined(rustflags)) {
581      rustflags = _rustflags
582    } else {
583      rustflags += _rustflags
584    }
585  }
586}
587
588template("ohos_rust_static_ffi") {
589  _target_name = target_name
590  _rustflags = []
591  rust_target("$_target_name") {
592    target_type = "ohos_static_library"
593    forward_variables_from(invoker, "*")
594    if (!defined(invoker.crate_name)) {
595      crate_name = _target_name
596    }
597    crate_type = "staticlib"
598    if (defined(invoker.crate_type)) {
599      assert(invoker.crate_type == crate_type,
600             "crate_type should be $crate_type or use default value.")
601    }
602    if (!defined(deps)) {
603      deps = []
604    }
605    if (defined(invoker.rustc_lints)) {
606      rustc_lints = invoker.rustc_lints
607    }
608    if (defined(invoker.clippy_lints)) {
609      clippy_lints = invoker.clippy_lints
610    }
611
612    if (defined(rustc_codecheck) && rustc_codecheck) {
613      rustc_lints = "allWarning"
614      clippy_lints = "allWarning"
615    }
616
617    if (!defined(rustc_lints) && !defined(clippy_lints)) {
618      file_path =
619          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
620      file_path_split = string_split(file_path[0], "/")
621      source_dir_begin = file_path_split[2]
622
623      if (source_dir_begin == "third_party") {
624        _rustflags += allowAllLints
625      } else if (source_dir_begin == "prebuilts") {
626        _rustflags += allowAllLints
627      } else if (source_dir_begin == "vendor" &&
628                 file_path_split[3] == "open_source") {
629        _rustflags += allowAllLints
630      } else if (source_dir_begin == "vendor") {
631        _rustflags += rustcVendorLints
632        _rustflags += clippyVendorLints
633      } else if (source_dir_begin == "device") {
634        _rustflags += rustcVendorLints
635        _rustflags += clippyVendorLints
636      } else {
637        _rustflags += rustcOhosLints
638        _rustflags += clippyOhosLints
639      }
640    }
641
642    if (defined(rustc_lints)) {
643      if (rustc_lints == "openharmony") {
644        _rustflags += rustcOhosLints
645      } else if (rustc_lints == "vendor") {
646        _rustflags += rustcVendorLints
647      } else if (rustc_lints == "none") {
648        _rustflags += allowAllLints
649      } else if (rustc_lints == "allWarning") {
650        _rustflags += rustcAllWarningLints
651      }
652    }
653    if (defined(clippy_lints)) {
654      if (clippy_lints == "openharmony") {
655        _rustflags += clippyOhosLints
656      } else if (clippy_lints == "vendor") {
657        _rustflags += clippyVendorLints
658      } else if (clippy_lints == "none") {
659        _rustflags += allowAllLints
660      } else if (clippy_lints == "allWarning") {
661        _rustflags += clippyAllWarningLints
662      }
663    }
664    if (!defined(rustflags)) {
665      rustflags = _rustflags
666    } else {
667      rustflags += _rustflags
668    }
669  }
670}
671
672template("ohos_rust_proc_macro") {
673  assert(!defined(invoker.output_dir),
674         "output_dir is not allowed to be defined.")
675  _test_target = defined(invoker.testonly) && invoker.testonly
676  if (defined(invoker.subsystem_name) && defined(invoker.part_name)) {
677    subsystem_name = invoker.subsystem_name
678    part_name = invoker.part_name
679  } else if (defined(invoker.part_name)) {
680    part_name = invoker.part_name
681    _part_subsystem_info_file =
682        "$root_build_dir/build_configs/parts_info/part_subsystem.json"
683    _arguments = [
684      "--part-name",
685      part_name,
686      "--part-subsystem-info-file",
687      rebase_path(_part_subsystem_info_file, root_build_dir),
688    ]
689    get_subsystem_script = "//build/templates/common/get_subsystem_name.py"
690    subsystem_name =
691        exec_script(get_subsystem_script, _arguments, "trim string")
692    if (is_use_check_deps && !_test_target) {
693      skip_check_subsystem = true
694    }
695  } else if (defined(invoker.subsystem_name)) {
696    subsystem_name = invoker.subsystem_name
697    part_name = subsystem_name
698  } else {
699    subsystem_name = "build"
700    part_name = "build_framework"
701  }
702  assert(subsystem_name != "")
703  assert(part_name != "")
704  if (is_use_check_deps && !_test_target) {
705    _check_target = "${target_name}__check"
706    target_path = get_label_info(":${target_name}", "label_no_toolchain")
707    check_target(_check_target) {
708      module_deps = []
709      if (defined(invoker.deps)) {
710        module_deps += invoker.deps
711      }
712      if (defined(invoker.public_deps)) {
713        module_deps += invoker.public_deps
714      }
715      if (defined(invoker.external_deps)) {
716        module_ex_deps = invoker.external_deps
717      }
718    }
719  }
720  if (check_deps) {
721    deps_data = {
722    }
723    module_label = get_label_info(":${target_name}", "label_with_toolchain")
724    module_deps = []
725    if (defined(invoker.deps)) {
726      foreach(dep, invoker.deps) {
727        module_deps += [ get_label_info(dep, "label_no_toolchain") ]
728      }
729    }
730    module_ex_deps = []
731    if (defined(invoker.external_deps) && invoker.external_deps != []) {
732      module_ex_deps = invoker.external_deps
733    }
734    deps_data = {
735      part_name = part_name
736      module_label = module_label
737      deps = module_deps
738      external_deps = module_ex_deps
739    }
740    write_file("${root_out_dir}/deps_files/${part_name}__${target_name}.json",
741               deps_data,
742               "json")
743  }
744
745  if (is_standard_system) {
746    output_dir = "${root_out_dir}/${subsystem_name}/${part_name}"
747  } else {
748    output_dir = "${root_out_dir}"
749  }
750
751  if (!_test_target) {
752    module_label = get_label_info(":${target_name}", "label_with_toolchain")
753    _collect_target = "${target_name}__collect"
754    collect_module_target(_collect_target) {
755      forward_variables_from(invoker, [ "install_images" ])
756    }
757
758    _notice_target = "${target_name}__notice"
759    _main_target_name = target_name
760    collect_notice(_notice_target) {
761      forward_variables_from(invoker,
762                             [
763                               "testonly",
764                               "license_as_sources",
765                               "license_file",
766                             ])
767
768      module_name = _main_target_name
769      module_source_dir = get_label_info(":${_main_target_name}", "dir")
770    }
771  }
772
773  target_label = get_label_info(":${target_name}", "label_with_toolchain")
774  target_toolchain = get_label_info(target_label, "toolchain")
775
776  if (target_toolchain == "${current_toolchain}") {
777    ohos_module_name = target_name
778    _module_info_target = "${target_name}_info"
779    generate_module_info(_module_info_target) {
780      module_name = ohos_module_name
781      module_type = "lib"
782      module_source_dir = "$root_out_dir"
783      if (defined(output_dir)) {
784        module_source_dir = output_dir
785      }
786
787      module_install_name = ohos_module_name
788      if (defined(invoker.output_name)) {
789        module_install_name = invoker.output_name
790      }
791
792      module_install_images = [ "system" ]
793      if (defined(invoker.install_images)) {
794        module_install_images = []
795        module_install_images += invoker.install_images
796      }
797
798      module_output_extension = shlib_extension
799      if (defined(invoker.output_extension)) {
800        module_output_extension = "." + invoker.output_extension
801      }
802
803      install_enable = true
804      if (defined(invoker.install_enable)) {
805        install_enable = invoker.install_enable
806      }
807
808      if (defined(invoker.module_install_dir)) {
809        module_install_dir = invoker.module_install_dir
810      }
811
812      if (defined(invoker.relative_install_dir)) {
813        relative_install_dir = invoker.relative_install_dir
814      }
815
816      if (defined(invoker.symlink_target_name)) {
817        symlink_target_name = invoker.symlink_target_name
818      }
819
820      if (defined(invoker.output_prefix_override)) {
821        output_prefix_override = invoker.output_prefix_override
822      }
823      notice = "$target_out_dir/$ohos_module_name.notice.txt"
824    }
825  }
826
827  _rustflags = []
828  rust_target(target_name) {
829    target_type = "rust_proc_macro"
830    forward_variables_from(invoker,
831                           "*",
832                           [
833                             "configs",
834                             "remove_configs",
835                             "no_default_deps",
836                             "install_images",
837                             "module_install_dir",
838                             "relative_install_dir",
839                             "symlink_target_name",
840                             "output_dir",
841                             "install_enable",
842                             "version_script",
843                             "license_file",
844                             "license_as_sources",
845                             "use_exceptions",
846                             "stl",
847
848                             # Sanitizer variables
849                             "sanitize",
850                           ])
851    if (!defined(invoker.crate_name)) {
852      crate_name = _target_name
853    }
854    crate_type = "proc-macro"
855    if (defined(invoker.crate_type)) {
856      assert(invoker.crate_type == crate_type,
857             "crate_type should be $crate_type or use default value.")
858    }
859
860    output_dir = output_dir
861
862    if (!defined(inputs)) {
863      inputs = []
864    }
865
866    if (!defined(ldflags)) {
867      ldflags = []
868    }
869
870    if (defined(invoker.configs)) {
871      configs += invoker.configs
872    }
873    if (defined(invoker.remove_configs)) {
874      configs -= invoker.remove_configs
875    }
876
877    if (!defined(output_name)) {
878      output_name = target_name
879    }
880
881    if (defined(invoker.no_default_deps)) {
882      no_default_deps = invoker.no_default_deps
883    }
884
885    if (!defined(libs)) {
886      libs = []
887    }
888    if (!defined(cflags_cc)) {
889      cflags_cc = []
890    }
891    if (!defined(deps)) {
892      deps = []
893    }
894    if (is_use_check_deps && !_test_target) {
895      deps += [ ":$_check_target" ]
896    }
897    if (target_toolchain == "${current_toolchain}" && !skip_gen_module_info) {
898      deps += [ ":$_module_info_target" ]
899    }
900
901    if (!_test_target) {
902      deps += [
903        ":$_notice_target",
904        ":${_collect_target}",
905      ]
906    }
907    if (!defined(include_dirs)) {
908      include_dirs = []
909    }
910
911    install_module_info = {
912      module_def = target_label
913      module_info_file =
914          rebase_path(get_label_info(module_def, "target_out_dir"),
915                      root_build_dir) + "/${target_name}_module_info.json"
916      subsystem_name = subsystem_name
917      part_name = part_name
918      toolchain = current_toolchain
919      toolchain_out_dir = rebase_path(root_out_dir, root_build_dir)
920    }
921    metadata = {
922      install_modules = [ install_module_info ]
923    }
924    if (defined(is_debug) && !is_debug && enable_debug_components != "") {
925      foreach(component_name, debug_components) {
926        if (part_name == component_name) {
927          configs -= default_opt_configs
928          configs += debug_level_configs
929        }
930      }
931    }
932
933    if (defined(invoker.rustc_lints)) {
934      rustc_lints = invoker.rustc_lints
935    }
936    if (defined(invoker.clippy_lints)) {
937      clippy_lints = invoker.clippy_lints
938    }
939
940    if (defined(rustc_codecheck) && rustc_codecheck) {
941      rustc_lints = "allWarning"
942      clippy_lints = "allWarning"
943    }
944
945    if (!defined(rustc_lints) && !defined(clippy_lints)) {
946      file_path =
947          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
948      file_path_split = string_split(file_path[0], "/")
949      source_dir_begin = file_path_split[2]
950
951      if (source_dir_begin == "third_party") {
952        _rustflags += allowAllLints
953      } else if (source_dir_begin == "prebuilts") {
954        _rustflags += allowAllLints
955      } else if (source_dir_begin == "vendor" &&
956                 file_path_split[3] == "open_source") {
957        _rustflags += allowAllLints
958      } else if (source_dir_begin == "vendor") {
959        _rustflags += rustcVendorLints
960        _rustflags += clippyVendorLints
961      } else if (source_dir_begin == "device") {
962        _rustflags += rustcVendorLints
963        _rustflags += clippyVendorLints
964      } else {
965        _rustflags += rustcOhosLints
966        _rustflags += clippyOhosLints
967      }
968    }
969
970    if (defined(rustc_lints)) {
971      if (rustc_lints == "openharmony") {
972        _rustflags += rustcOhosLints
973      } else if (rustc_lints == "vendor") {
974        _rustflags += rustcVendorLints
975      } else if (rustc_lints == "none") {
976        _rustflags += allowAllLints
977      } else if (rustc_lints == "allWarning") {
978        _rustflags += rustcAllWarningLints
979      }
980    }
981    if (defined(clippy_lints)) {
982      if (clippy_lints == "openharmony") {
983        _rustflags += clippyOhosLints
984      } else if (clippy_lints == "vendor") {
985        _rustflags += clippyVendorLints
986      } else if (clippy_lints == "none") {
987        _rustflags += allowAllLints
988      } else if (clippy_lints == "allWarning") {
989        _rustflags += clippyAllWarningLints
990      }
991    }
992    if (!defined(rustflags)) {
993      rustflags = _rustflags
994    } else {
995      rustflags += _rustflags
996    }
997  }
998}
999