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