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
213  if (is_tsan && !is_mingw && !is_win) {
214    _rustflags += [
215      "-Clink-arg=-fsanitize=thread",
216      "-Clink-arg=-shared-libasan",
217    ]
218  }
219
220  target(invoker.target_type, "${target_name}") {
221    forward_variables_from(invoker,
222                           "*",
223                           [
224                             "features",
225                             "deps",
226                             "public_deps",
227                             "rustflags",
228                             "rustenv",
229
230                             # "configs",
231                             "output_dir",
232                             "crate_type",
233                           ])
234    crate_name = _crate_name
235
236    deps = _rust_deps
237    public_deps = _rust_public_deps
238    rustflags = _rustflags
239    rustenv = _rustenv
240    crate_type = _crate_type
241    if (target_type == "rust_proc_macro") {
242      output_dir = _out_dir
243    }
244    if (!defined(output_name) || output_name == "") {
245      output_name = crate_name
246    }
247  }
248}
249
250template("ohos_rust_executable") {
251  _target_name = target_name
252  _rustflags = []
253  rust_target("$_target_name") {
254    target_type = "ohos_executable"
255    forward_variables_from(invoker, "*")
256    if (!defined(invoker.crate_name)) {
257      crate_name = _target_name
258    }
259    crate_type = "bin"
260    if (defined(invoker.crate_type)) {
261      assert(invoker.crate_type == crate_type,
262             "crate_type should be $crate_type or use default value.")
263    }
264    configs = []
265    if (!defined(deps)) {
266      deps = []
267    }
268    if (defined(invoker.rustc_lints)) {
269      rustc_lints = invoker.rustc_lints
270    }
271    if (defined(invoker.clippy_lints)) {
272      clippy_lints = invoker.clippy_lints
273    }
274
275    if (defined(rustc_codecheck) && rustc_codecheck) {
276      rustc_lints = "allWarning"
277      clippy_lints = "allWarning"
278    }
279
280    if (!defined(rustc_lints) && !defined(clippy_lints)) {
281      file_path =
282          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
283      file_path_split = string_split(file_path[0], "/")
284      source_dir_begin = file_path_split[2]
285
286      if (defined(invoker.install_images)) {
287        install_images = []
288        install_images = invoker.install_images
289      }
290
291      if (source_dir_begin == "third_party") {
292        _rustflags += allowAllLints
293      } else if (source_dir_begin == "prebuilts") {
294        _rustflags += allowAllLints
295      } else if (source_dir_begin == "vendor" &&
296                 file_path_split[3] == "open_source") {
297        _rustflags += allowAllLints
298      } else if (source_dir_begin == "vendor") {
299        _rustflags += rustcVendorLints
300        _rustflags += clippyVendorLints
301      } else if (source_dir_begin == "device") {
302        _rustflags += rustcVendorLints
303        _rustflags += clippyVendorLints
304      } else {
305        _rustflags += rustcOhosLints
306        _rustflags += clippyOhosLints
307      }
308    }
309
310    if (defined(rustc_lints)) {
311      if (rustc_lints == "openharmony") {
312        _rustflags += rustcOhosLints
313      } else if (rustc_lints == "vendor") {
314        _rustflags += rustcVendorLints
315      } else if (rustc_lints == "none") {
316        _rustflags += allowAllLints
317      } else if (rustc_lints == "allWarning") {
318        _rustflags += rustcAllWarningLints
319      }
320    }
321    if (defined(clippy_lints)) {
322      if (clippy_lints == "openharmony") {
323        _rustflags += clippyOhosLints
324      } else if (clippy_lints == "vendor") {
325        _rustflags += clippyVendorLints
326      } else if (clippy_lints == "none") {
327        _rustflags += allowAllLints
328      } else if (clippy_lints == "allWarning") {
329        _rustflags += clippyAllWarningLints
330      }
331    }
332    if (!defined(rustflags)) {
333      rustflags = _rustflags
334    } else {
335      rustflags += _rustflags
336    }
337    if (!defined(rust_static_link) || !rust_static_link) {
338      rustflags += [ "-Cprefer-dynamic" ]
339    }
340  }
341}
342
343template("ohos_rust_shared_library") {
344  _target_name = target_name
345  _rustflags = []
346  rust_target("$_target_name") {
347    target_type = "ohos_rust_library"
348    forward_variables_from(invoker, "*")
349    if (!defined(invoker.crate_name)) {
350      crate_name = _target_name
351    }
352    crate_type = "dylib"
353    if (defined(invoker.crate_type)) {
354      assert(invoker.crate_type == crate_type,
355             "crate_type should be $crate_type or use default value.")
356    }
357
358    if (defined(invoker.output_extension)) {
359      module_output_extension = "." + invoker.output_extension
360    } else {
361      module_output_extension = dylib_extension
362    }
363
364    if (defined(invoker.rustc_lints)) {
365      rustc_lints = invoker.rustc_lints
366    }
367    if (defined(invoker.clippy_lints)) {
368      clippy_lints = invoker.clippy_lints
369    }
370
371    if (defined(rustc_codecheck) && rustc_codecheck) {
372      rustc_lints = "allWarning"
373      clippy_lints = "allWarning"
374    }
375
376    if (!defined(rustc_lints) && !defined(clippy_lints)) {
377      file_path =
378          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
379      file_path_split = string_split(file_path[0], "/")
380      source_dir_begin = file_path_split[2]
381
382      if (source_dir_begin == "third_party") {
383        _rustflags += allowAllLints
384      } else if (source_dir_begin == "prebuilts") {
385        _rustflags += allowAllLints
386      } else if (source_dir_begin == "vendor" &&
387                 file_path_split[3] == "open_source") {
388        _rustflags += allowAllLints
389      } else if (source_dir_begin == "vendor") {
390        _rustflags += rustcVendorLints
391        _rustflags += clippyVendorLints
392      } else if (source_dir_begin == "device") {
393        _rustflags += rustcVendorLints
394        _rustflags += clippyVendorLints
395      } else {
396        _rustflags += rustcOhosLints
397        _rustflags += clippyOhosLints
398      }
399    }
400
401    if (defined(invoker.install_images)) {
402      install_images = []
403      install_images = invoker.install_images
404    }
405
406    if (defined(rustc_lints)) {
407      if (rustc_lints == "openharmony") {
408        _rustflags += rustcOhosLints
409      } else if (rustc_lints == "vendor") {
410        _rustflags += rustcVendorLints
411      } else if (rustc_lints == "none") {
412        _rustflags += allowAllLints
413      } else if (rustc_lints == "allWarning") {
414        _rustflags += rustcAllWarningLints
415      }
416    }
417    if (defined(clippy_lints)) {
418      if (clippy_lints == "openharmony") {
419        _rustflags += clippyOhosLints
420      } else if (clippy_lints == "vendor") {
421        _rustflags += clippyVendorLints
422      } else if (clippy_lints == "none") {
423        _rustflags += allowAllLints
424      } else if (clippy_lints == "allWarning") {
425        _rustflags += clippyAllWarningLints
426      }
427    }
428    if (!defined(rustflags)) {
429      rustflags = _rustflags
430    } else {
431      rustflags += _rustflags
432    }
433  }
434}
435
436template("ohos_rust_static_library") {
437  _target_name = target_name
438  _rustflags = []
439  rust_target("$_target_name") {
440    target_type = "ohos_rust_library"
441    forward_variables_from(invoker, "*")
442    if (!defined(invoker.crate_name)) {
443      crate_name = _target_name
444    }
445    crate_type = "rlib"
446    if (defined(invoker.crate_type)) {
447      assert(invoker.crate_type == crate_type,
448             "crate_type should be $crate_type or use default value.")
449    }
450    module_output_extension = rlib_extension
451    install_enable = false
452
453    if (defined(invoker.rustc_lints)) {
454      rustc_lints = invoker.rustc_lints
455    }
456    if (defined(invoker.clippy_lints)) {
457      clippy_lints = invoker.clippy_lints
458    }
459
460    if (defined(rustc_codecheck) && rustc_codecheck) {
461      rustc_lints = "allWarning"
462      clippy_lints = "allWarning"
463    }
464
465    if (!defined(rustc_lints) && !defined(clippy_lints)) {
466      file_path =
467          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
468      file_path_split = string_split(file_path[0], "/")
469      source_dir_begin = file_path_split[2]
470
471      if (source_dir_begin == "third_party") {
472        _rustflags += allowAllLints
473      } else if (source_dir_begin == "prebuilts") {
474        _rustflags += allowAllLints
475      } else if (source_dir_begin == "vendor" &&
476                 file_path_split[3] == "open_source") {
477        _rustflags += allowAllLints
478      } else if (source_dir_begin == "vendor") {
479        _rustflags += rustcVendorLints
480        _rustflags += clippyVendorLints
481      } else if (source_dir_begin == "device") {
482        _rustflags += rustcVendorLints
483        _rustflags += clippyVendorLints
484      } else {
485        _rustflags += rustcOhosLints
486        _rustflags += clippyOhosLints
487      }
488    }
489
490    if (defined(invoker.install_images)) {
491      install_images = []
492      install_images = invoker.install_images
493    }
494
495    if (defined(rustc_lints)) {
496      if (rustc_lints == "openharmony") {
497        _rustflags += rustcOhosLints
498      } else if (rustc_lints == "vendor") {
499        _rustflags += rustcVendorLints
500      } else if (rustc_lints == "none") {
501        _rustflags += allowAllLints
502      } else if (rustc_lints == "allWarning") {
503        _rustflags += rustcAllWarningLints
504      }
505    }
506    if (defined(clippy_lints)) {
507      if (clippy_lints == "openharmony") {
508        _rustflags += clippyOhosLints
509      } else if (clippy_lints == "vendor") {
510        _rustflags += clippyVendorLints
511      } else if (clippy_lints == "none") {
512        _rustflags += allowAllLints
513      } else if (clippy_lints == "allWarning") {
514        _rustflags += clippyAllWarningLints
515      }
516    }
517    if (!defined(rustflags)) {
518      rustflags = _rustflags
519    } else {
520      rustflags += _rustflags
521    }
522  }
523}
524
525template("ohos_rust_shared_ffi") {
526  _target_name = target_name
527  _rustflags = []
528  rust_target("$_target_name") {
529    target_type = "ohos_shared_library"
530    forward_variables_from(invoker, "*")
531    if (!defined(invoker.crate_name)) {
532      crate_name = _target_name
533    }
534    crate_type = "cdylib"
535    if (defined(invoker.crate_type)) {
536      assert(invoker.crate_type == crate_type,
537             "crate_type should be $crate_type or use default value.")
538    }
539
540    if (!defined(deps)) {
541      deps = []
542    }
543
544    if (defined(invoker.rustc_lints)) {
545      rustc_lints = invoker.rustc_lints
546    }
547    if (defined(invoker.clippy_lints)) {
548      clippy_lints = invoker.clippy_lints
549    }
550
551    if (defined(rustc_codecheck) && rustc_codecheck) {
552      rustc_lints = "allWarning"
553      clippy_lints = "allWarning"
554    }
555
556    if (!defined(rustc_lints) && !defined(clippy_lints)) {
557      file_path =
558          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
559      file_path_split = string_split(file_path[0], "/")
560      source_dir_begin = file_path_split[2]
561
562      if (source_dir_begin == "third_party") {
563        _rustflags += allowAllLints
564      } else if (source_dir_begin == "prebuilts") {
565        _rustflags += allowAllLints
566      } else if (source_dir_begin == "vendor" &&
567                 file_path_split[3] == "open_source") {
568        _rustflags += allowAllLints
569      } else if (source_dir_begin == "vendor") {
570        _rustflags += rustcVendorLints
571        _rustflags += clippyVendorLints
572      } else if (source_dir_begin == "device") {
573        _rustflags += rustcVendorLints
574        _rustflags += clippyVendorLints
575      } else {
576        _rustflags += rustcOhosLints
577        _rustflags += clippyOhosLints
578      }
579    }
580
581    if (defined(invoker.install_images)) {
582      install_images = []
583      install_images = invoker.install_images
584    }
585
586    if (defined(rustc_lints)) {
587      if (rustc_lints == "openharmony") {
588        _rustflags += rustcOhosLints
589      } else if (rustc_lints == "vendor") {
590        _rustflags += rustcVendorLints
591      } else if (rustc_lints == "none") {
592        _rustflags += allowAllLints
593      } else if (rustc_lints == "allWarning") {
594        _rustflags += rustcAllWarningLints
595      }
596    }
597    if (defined(clippy_lints)) {
598      if (clippy_lints == "openharmony") {
599        _rustflags += clippyOhosLints
600      } else if (clippy_lints == "vendor") {
601        _rustflags += clippyVendorLints
602      } else if (clippy_lints == "none") {
603        _rustflags += allowAllLints
604      } else if (clippy_lints == "allWarning") {
605        _rustflags += clippyAllWarningLints
606      }
607    }
608    if (!defined(rustflags)) {
609      rustflags = _rustflags
610    } else {
611      rustflags += _rustflags
612    }
613  }
614}
615
616template("ohos_rust_static_ffi") {
617  _target_name = target_name
618  _rustflags = []
619  rust_target("$_target_name") {
620    target_type = "ohos_static_library"
621    forward_variables_from(invoker, "*")
622    if (!defined(invoker.crate_name)) {
623      crate_name = _target_name
624    }
625    crate_type = "staticlib"
626    if (defined(invoker.crate_type)) {
627      assert(invoker.crate_type == crate_type,
628             "crate_type should be $crate_type or use default value.")
629    }
630    if (!defined(deps)) {
631      deps = []
632    }
633    if (defined(invoker.rustc_lints)) {
634      rustc_lints = invoker.rustc_lints
635    }
636    if (defined(invoker.clippy_lints)) {
637      clippy_lints = invoker.clippy_lints
638    }
639
640    if (defined(rustc_codecheck) && rustc_codecheck) {
641      rustc_lints = "allWarning"
642      clippy_lints = "allWarning"
643    }
644
645    if (!defined(rustc_lints) && !defined(clippy_lints)) {
646      file_path =
647          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
648      file_path_split = string_split(file_path[0], "/")
649      source_dir_begin = file_path_split[2]
650
651      if (source_dir_begin == "third_party") {
652        _rustflags += allowAllLints
653      } else if (source_dir_begin == "prebuilts") {
654        _rustflags += allowAllLints
655      } else if (source_dir_begin == "vendor" &&
656                 file_path_split[3] == "open_source") {
657        _rustflags += allowAllLints
658      } else if (source_dir_begin == "vendor") {
659        _rustflags += rustcVendorLints
660        _rustflags += clippyVendorLints
661      } else if (source_dir_begin == "device") {
662        _rustflags += rustcVendorLints
663        _rustflags += clippyVendorLints
664      } else {
665        _rustflags += rustcOhosLints
666        _rustflags += clippyOhosLints
667      }
668    }
669
670    if (defined(invoker.install_images)) {
671      install_images = []
672      install_images = invoker.install_images
673    }
674
675    if (defined(rustc_lints)) {
676      if (rustc_lints == "openharmony") {
677        _rustflags += rustcOhosLints
678      } else if (rustc_lints == "vendor") {
679        _rustflags += rustcVendorLints
680      } else if (rustc_lints == "none") {
681        _rustflags += allowAllLints
682      } else if (rustc_lints == "allWarning") {
683        _rustflags += rustcAllWarningLints
684      }
685    }
686    if (defined(clippy_lints)) {
687      if (clippy_lints == "openharmony") {
688        _rustflags += clippyOhosLints
689      } else if (clippy_lints == "vendor") {
690        _rustflags += clippyVendorLints
691      } else if (clippy_lints == "none") {
692        _rustflags += allowAllLints
693      } else if (clippy_lints == "allWarning") {
694        _rustflags += clippyAllWarningLints
695      }
696    }
697    if (!defined(rustflags)) {
698      rustflags = _rustflags
699    } else {
700      rustflags += _rustflags
701    }
702  }
703}
704
705template("ohos_rust_proc_macro") {
706  assert(!defined(invoker.output_dir),
707         "output_dir is not allowed to be defined.")
708  _test_target = defined(invoker.testonly) && invoker.testonly
709  if (defined(invoker.subsystem_name) && defined(invoker.part_name)) {
710    subsystem_name = invoker.subsystem_name
711    part_name = invoker.part_name
712  } else if (defined(invoker.part_name)) {
713    part_name = invoker.part_name
714    _part_subsystem_info_file =
715        "$root_build_dir/build_configs/parts_info/part_subsystem.json"
716    _arguments = [
717      "--part-name",
718      part_name,
719      "--part-subsystem-info-file",
720      rebase_path(_part_subsystem_info_file, root_build_dir),
721    ]
722    get_subsystem_script = "//build/templates/common/get_subsystem_name.py"
723    subsystem_name =
724        exec_script(get_subsystem_script, _arguments, "trim string")
725    if (is_use_check_deps && !_test_target) {
726      skip_check_subsystem = true
727    }
728  } else if (defined(invoker.subsystem_name)) {
729    subsystem_name = invoker.subsystem_name
730    part_name = subsystem_name
731  } else {
732    subsystem_name = "build"
733    part_name = "build_framework"
734  }
735  assert(subsystem_name != "")
736  assert(part_name != "")
737  if (is_use_check_deps && !_test_target) {
738    _check_target = "${target_name}__check"
739    target_path = get_label_info(":${target_name}", "label_no_toolchain")
740    check_target(_check_target) {
741      module_deps = []
742      if (defined(invoker.deps)) {
743        module_deps += invoker.deps
744      }
745      if (defined(invoker.public_deps)) {
746        module_deps += invoker.public_deps
747      }
748      if (defined(invoker.external_deps)) {
749        module_ex_deps = invoker.external_deps
750      }
751    }
752  }
753  if (check_deps) {
754    deps_data = {
755    }
756    module_label = get_label_info(":${target_name}", "label_with_toolchain")
757    module_deps = []
758    if (defined(invoker.deps)) {
759      foreach(dep, invoker.deps) {
760        module_deps += [ get_label_info(dep, "label_no_toolchain") ]
761      }
762    }
763    module_ex_deps = []
764    if (defined(invoker.external_deps) && invoker.external_deps != []) {
765      module_ex_deps = invoker.external_deps
766    }
767    deps_data = {
768      part_name = part_name
769      module_label = module_label
770      deps = module_deps
771      external_deps = module_ex_deps
772    }
773    write_file("${root_out_dir}/deps_files/${part_name}__${target_name}.json",
774               deps_data,
775               "json")
776  }
777
778  if (is_standard_system) {
779    output_dir = "${root_out_dir}/${subsystem_name}/${part_name}"
780  } else {
781    output_dir = "${root_out_dir}"
782  }
783
784  if (!_test_target) {
785    module_label = get_label_info(":${target_name}", "label_with_toolchain")
786    _collect_target = "${target_name}__collect"
787    collect_module_target(_collect_target) {
788      forward_variables_from(invoker, [ "install_images" ])
789    }
790
791    _notice_target = "${target_name}__notice"
792    _main_target_name = target_name
793    collect_notice(_notice_target) {
794      forward_variables_from(invoker,
795                             [
796                               "testonly",
797                               "license_as_sources",
798                               "license_file",
799                             ])
800
801      module_name = _main_target_name
802      module_source_dir = get_label_info(":${_main_target_name}", "dir")
803    }
804  }
805
806  target_label = get_label_info(":${target_name}", "label_with_toolchain")
807  target_toolchain = get_label_info(target_label, "toolchain")
808
809  if (target_toolchain == "${current_toolchain}") {
810    ohos_module_name = target_name
811    _module_info_target = "${target_name}_info"
812    generate_module_info(_module_info_target) {
813      module_name = ohos_module_name
814      module_type = "lib"
815      module_source_dir = "$root_out_dir"
816      if (defined(output_dir)) {
817        module_source_dir = output_dir
818      }
819
820      module_install_name = ohos_module_name
821      if (defined(invoker.output_name)) {
822        module_install_name = invoker.output_name
823      }
824
825      module_install_images = [ "system" ]
826      if (defined(invoker.install_images)) {
827        module_install_images = []
828        module_install_images += invoker.install_images
829      }
830
831      module_output_extension = shlib_extension
832      if (defined(invoker.output_extension)) {
833        module_output_extension = "." + invoker.output_extension
834      }
835
836      install_enable = true
837      if (defined(invoker.install_enable)) {
838        install_enable = invoker.install_enable
839      }
840
841      if (defined(invoker.module_install_dir)) {
842        module_install_dir = invoker.module_install_dir
843      }
844
845      if (defined(invoker.relative_install_dir)) {
846        relative_install_dir = invoker.relative_install_dir
847      }
848
849      if (defined(invoker.symlink_target_name)) {
850        symlink_target_name = invoker.symlink_target_name
851      }
852
853      if (defined(invoker.output_prefix_override)) {
854        output_prefix_override = invoker.output_prefix_override
855      }
856      notice = "$target_out_dir/$ohos_module_name.notice.txt"
857    }
858  }
859
860  _rustflags = []
861  rust_target(target_name) {
862    target_type = "rust_proc_macro"
863    forward_variables_from(invoker,
864                           "*",
865                           [
866                             "configs",
867                             "remove_configs",
868                             "no_default_deps",
869                             "install_images",
870                             "module_install_dir",
871                             "relative_install_dir",
872                             "symlink_target_name",
873                             "output_dir",
874                             "install_enable",
875                             "version_script",
876                             "license_file",
877                             "license_as_sources",
878                             "use_exceptions",
879                             "stl",
880
881                             # Sanitizer variables
882                             "sanitize",
883                           ])
884    if (!defined(invoker.crate_name)) {
885      crate_name = _target_name
886    }
887    crate_type = "proc-macro"
888    if (defined(invoker.crate_type)) {
889      assert(invoker.crate_type == crate_type,
890             "crate_type should be $crate_type or use default value.")
891    }
892
893    output_dir = output_dir
894
895    if (!defined(inputs)) {
896      inputs = []
897    }
898
899    if (!defined(ldflags)) {
900      ldflags = []
901    }
902
903    if (defined(invoker.configs)) {
904      configs += invoker.configs
905    }
906    if (defined(invoker.remove_configs)) {
907      configs -= invoker.remove_configs
908    }
909
910    if (!defined(output_name)) {
911      output_name = target_name
912    }
913
914    if (defined(invoker.no_default_deps)) {
915      no_default_deps = invoker.no_default_deps
916    }
917
918    if (!defined(libs)) {
919      libs = []
920    }
921    if (!defined(cflags_cc)) {
922      cflags_cc = []
923    }
924    if (!defined(deps)) {
925      deps = []
926    }
927    if (is_use_check_deps && !_test_target) {
928      deps += [ ":$_check_target" ]
929    }
930    if (target_toolchain == "${current_toolchain}" && !skip_gen_module_info) {
931      deps += [ ":$_module_info_target" ]
932    }
933
934    if (!_test_target) {
935      deps += [
936        ":$_notice_target",
937        ":${_collect_target}",
938      ]
939    }
940    if (!defined(include_dirs)) {
941      include_dirs = []
942    }
943
944    install_module_info = {
945      module_def = target_label
946      module_info_file =
947          rebase_path(get_label_info(module_def, "target_out_dir"),
948                      root_build_dir) + "/${target_name}_module_info.json"
949      subsystem_name = subsystem_name
950      part_name = part_name
951      toolchain = current_toolchain
952      toolchain_out_dir = rebase_path(root_out_dir, root_build_dir)
953    }
954    metadata = {
955      install_modules = [ install_module_info ]
956    }
957    if (defined(is_debug) && !is_debug && enable_debug_components != "") {
958      foreach(component_name, debug_components) {
959        if (part_name == component_name) {
960          configs -= default_opt_configs
961          configs += debug_level_configs
962        }
963      }
964    }
965
966    if (defined(invoker.rustc_lints)) {
967      rustc_lints = invoker.rustc_lints
968    }
969    if (defined(invoker.clippy_lints)) {
970      clippy_lints = invoker.clippy_lints
971    }
972
973    if (defined(rustc_codecheck) && rustc_codecheck) {
974      rustc_lints = "allWarning"
975      clippy_lints = "allWarning"
976    }
977
978    if (!defined(rustc_lints) && !defined(clippy_lints)) {
979      file_path =
980          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
981      file_path_split = string_split(file_path[0], "/")
982      source_dir_begin = file_path_split[2]
983
984      if (source_dir_begin == "third_party") {
985        _rustflags += allowAllLints
986      } else if (source_dir_begin == "prebuilts") {
987        _rustflags += allowAllLints
988      } else if (source_dir_begin == "vendor" &&
989                 file_path_split[3] == "open_source") {
990        _rustflags += allowAllLints
991      } else if (source_dir_begin == "vendor") {
992        _rustflags += rustcVendorLints
993        _rustflags += clippyVendorLints
994      } else if (source_dir_begin == "device") {
995        _rustflags += rustcVendorLints
996        _rustflags += clippyVendorLints
997      } else {
998        _rustflags += rustcOhosLints
999        _rustflags += clippyOhosLints
1000      }
1001    }
1002
1003    if (defined(rustc_lints)) {
1004      if (rustc_lints == "openharmony") {
1005        _rustflags += rustcOhosLints
1006      } else if (rustc_lints == "vendor") {
1007        _rustflags += rustcVendorLints
1008      } else if (rustc_lints == "none") {
1009        _rustflags += allowAllLints
1010      } else if (rustc_lints == "allWarning") {
1011        _rustflags += rustcAllWarningLints
1012      }
1013    }
1014    if (defined(clippy_lints)) {
1015      if (clippy_lints == "openharmony") {
1016        _rustflags += clippyOhosLints
1017      } else if (clippy_lints == "vendor") {
1018        _rustflags += clippyVendorLints
1019      } else if (clippy_lints == "none") {
1020        _rustflags += allowAllLints
1021      } else if (clippy_lints == "allWarning") {
1022        _rustflags += clippyAllWarningLints
1023      }
1024    }
1025    if (!defined(rustflags)) {
1026      rustflags = _rustflags
1027    } else {
1028      rustflags += _rustflags
1029    }
1030  }
1031}
1032