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