7c1a30d5创建于 2025年2月28日历史提交
# Copyright (c) 2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build/rust/rustc_toolchain.gni")
import("//build/templates/cxx/cxx.gni")
import("//build/templates/rust/ohos_rust_library.gni")

allowAllLints = [
  "--cap-lints",
  "allow",
]
rustcOhosLints = [
  "-A",
  "deprecated",
  "-D",
  "missing-docs",
  "-D",
  "warnings",
]
rustcVendorLints = [
  "-A",
  "deprecated",
  "-D",
  "warnings",
]
rustcAllWarningLints = [
  "-W",
  "non_ascii_idents",
  "-W",
  "ambiguous_associated_items",
  "-W",
  "arithmetic_overflow",
  "-W",
  "bindings_with_variant_name",
  "-W",
  "cenum_impl_drop_cast",
  "-W",
  "conflicting_repr_hints",
  "-W",
  "deprecated_cfg_attr_crate_type_name",
  "-W",
  "enum_intrinsics_non_enums",
  "-W",
  "ill_formed_attribute_input",
  "-W",
  "implied_bounds_entailment",
  "-W",
  "incomplete_include",
  "-W",
  "ineffective_unstable_trait_impl",
  "-W",
  "invalid_atomic_ordering",
  "-W",
  "invalid_type_param_default",
  "-W",
  "let_underscore_lock",
  "-W",
  "macro_expanded_macro_exports_accessed_by_absolute_paths",
  "-W",
  "missing_fragment_specifier",
  "-W",
  "mutable_transmutes",
  "-W",
  "named_asm_labels",
  "-W",
  "no_mangle_const_items",
  "-W",
  "order_dependent_trait_objects",
  "-W",
  "overflowing_literals",
  "-W",
  "patterns_in_fns_without_body",
  "-W",
  "proc_macro_back_compat",
  "-W",
  "proc_macro_derive_resolution_fallback",
  "-W",
  "pub_use_of_private_extern_crate",
  "-W",
  "soft_unstable",
  "-W",
  "test_unstable_lint",
  "-W",
  "text_direction_codepoint_in_comment",
  "-W",
  "text_direction_codepoint_in_literal",
  "-W",
  "unconditional_panic",
  "-W",
  "unknown_crate_types",
  "-W",
  "useless_deprecated",
]
clippyOhosLints = [
  "-A",
  "clippy::type-complexity",
  "-A",
  "clippy::unnecessary-wraps",
  "-A",
  "clippy::unusual-byte-groupings",
  "-A",
  "clippy::upper-case-acronyms",
  "-A",
  "clippy::let_and_return",
  "-A",
  "clippy::unnecessary-cast",
]
clippyVendorLints = [
  "-A",
  "clippy::complexity",
  "-A",
  "clippy::perf",
  "-A",
  "clippy::style",
]
clippyAllWarningLints = [
  "-W",
  "clippy::all",
  "-W",
  "clippy::pedantic",
  "-W",
  "clippy::restriction",
]

template("rust_target") {
  assert(!defined(invoker.crate_root) ||
         !(defined(invoker.generate_crate_root) && invoker.generate_crate_root))

  _crate_name = target_name
  if (defined(invoker.crate_name)) {
    _crate_name = invoker.crate_name
  }
  _crate_type = ""
  if (defined(invoker.crate_type)) {
    _crate_type = invoker.crate_type
  }
  _deps = []
  if (defined(invoker.deps)) {
    _deps += invoker.deps
  }

  _rustflags = [ "-Zstack-protector=all" ]

  if (defined(invoker.rustflags)) {
    _rustflags += invoker.rustflags
  }

  _public_deps = []
  if (defined(invoker.public_deps)) {
    _public_deps += invoker.public_deps
  }

  if (defined(invoker.output_dir) && invoker.output_dir != "") {
    _out_dir = invoker.output_dir
  } else {
    _out_dir = target_out_dir
  }

  if (defined(invoker.features)) {
    foreach(i, invoker.features) {
      _rustflags += [ "--cfg=feature=\"${i}\"" ]
    }
  }
  _rustenv = [ "OUT_DIR=" + rebase_path(_out_dir) ]
  if (defined(invoker.rustenv)) {
    _rustenv += invoker.rustenv
  }

  assert(defined(invoker.sources), "sources must be listed")

  _rust_deps = _deps
  _rust_public_deps = _public_deps

  _edition = rust_default_edition
  if (defined(invoker.edition)) {
    _edition = invoker.edition
  }
  _rustflags += [ string_join("",
                              [
                                "--edition=",
                                _edition,
                              ]) ]
  if (invoker.target_type == "rust_proc_macro") {
    _rustflags += [
      "--extern",
      "proc_macro",
    ]
  }

  if (is_asan) {
    if (use_hwasan) {
      _rustflags += [
        "-Clink-arg=-fsanitize=hwaddress",
        "-Clink-arg=-shared-libasan",
      ]
    } else {
      _rustflags += [
        "-Clink-arg=-fsanitize=address",
        "-Clink-arg=-shared-libasan",
      ]
    }
  }

  if (is_tsan && !is_mingw && !is_win) {
    _rustflags += [
      "-Clink-arg=-fsanitize=thread",
      "-Clink-arg=-shared-libasan",
    ]
  }

  target(invoker.target_type, "${target_name}") {
    forward_variables_from(invoker,
                           "*",
                           [
                             "features",
                             "deps",
                             "public_deps",
                             "rustflags",
                             "rustenv",

                             # "configs",
                             "output_dir",
                             "crate_type",
                           ])
    crate_name = _crate_name

    deps = _rust_deps
    public_deps = _rust_public_deps
    rustflags = _rustflags
    rustenv = _rustenv
    crate_type = _crate_type
    if (target_type == "rust_proc_macro") {
      output_dir = _out_dir
    }
    if (!defined(output_name) || output_name == "") {
      output_name = crate_name
    }
    if (use_clang_coverage == true) {
      if (!defined(ldflags)) {
        ldflags = []
      }
      ldflags += [ "-Wl,--undefined-version" ]
      rustflags += [
        "-Cinstrument-coverage",
        "-Clink-dead-code",
        "-Zprofile",
        "-Ccodegen-units=1",
      ]
    }
  }
}

template("ohos_rust_executable") {
  _target_name = target_name
  _rustflags = []
  rust_target("$_target_name") {
    target_type = "ohos_executable"
    forward_variables_from(invoker, "*")
    if (!defined(invoker.crate_name)) {
      crate_name = _target_name
    }
    crate_type = "bin"
    if (defined(invoker.crate_type)) {
      assert(invoker.crate_type == crate_type,
             "crate_type should be $crate_type or use default value.")
    }
    configs = []
    if (!defined(deps)) {
      deps = []
    }
    if (defined(invoker.rustc_lints)) {
      rustc_lints = invoker.rustc_lints
    }
    if (defined(invoker.clippy_lints)) {
      clippy_lints = invoker.clippy_lints
    }

    if (defined(rustc_codecheck) && rustc_codecheck) {
      rustc_lints = "allWarning"
      clippy_lints = "allWarning"
    }

    if (!defined(rustc_lints) && !defined(clippy_lints)) {
      file_path =
          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
      file_path_split = string_split(file_path[0], "/")
      source_dir_begin = file_path_split[2]

      if (defined(invoker.install_images)) {
        install_images = []
        install_images = invoker.install_images
      }

      if (source_dir_begin == "third_party") {
        _rustflags += allowAllLints
      } else if (source_dir_begin == "prebuilts") {
        _rustflags += allowAllLints
      } else if (source_dir_begin == "vendor" &&
                 file_path_split[3] == "open_source") {
        _rustflags += allowAllLints
      } else if (source_dir_begin == "vendor") {
        _rustflags += rustcVendorLints
        _rustflags += clippyVendorLints
      } else if (source_dir_begin == "device") {
        _rustflags += rustcVendorLints
        _rustflags += clippyVendorLints
      } else {
        _rustflags += rustcOhosLints
        _rustflags += clippyOhosLints
      }
    }

    if (defined(rustc_lints)) {
      if (rustc_lints == "openharmony") {
        _rustflags += rustcOhosLints
      } else if (rustc_lints == "vendor") {
        _rustflags += rustcVendorLints
      } else if (rustc_lints == "none") {
        _rustflags += allowAllLints
      } else if (rustc_lints == "allWarning") {
        _rustflags += rustcAllWarningLints
      }
    }
    if (defined(clippy_lints)) {
      if (clippy_lints == "openharmony") {
        _rustflags += clippyOhosLints
      } else if (clippy_lints == "vendor") {
        _rustflags += clippyVendorLints
      } else if (clippy_lints == "none") {
        _rustflags += allowAllLints
      } else if (clippy_lints == "allWarning") {
        _rustflags += clippyAllWarningLints
      }
    }
    if (!defined(rustflags)) {
      rustflags = _rustflags
    } else {
      rustflags += _rustflags
    }
    if (!defined(rust_static_link) || !rust_static_link) {
      rustflags += [ "-Cprefer-dynamic" ]
    }
  }
}

template("ohos_rust_shared_library") {
  _target_name = target_name
  _rustflags = []
  rust_target("$_target_name") {
    target_type = "ohos_rust_library"
    forward_variables_from(invoker, "*")
    if (!defined(invoker.crate_name)) {
      crate_name = _target_name
    }
    crate_type = "dylib"
    if (defined(invoker.crate_type)) {
      assert(invoker.crate_type == crate_type,
             "crate_type should be $crate_type or use default value.")
    }

    if (defined(invoker.output_extension)) {
      module_output_extension = "." + invoker.output_extension
    } else {
      module_output_extension = dylib_extension
    }

    if (defined(invoker.rustc_lints)) {
      rustc_lints = invoker.rustc_lints
    }
    if (defined(invoker.clippy_lints)) {
      clippy_lints = invoker.clippy_lints
    }

    if (defined(rustc_codecheck) && rustc_codecheck) {
      rustc_lints = "allWarning"
      clippy_lints = "allWarning"
    }

    if (!defined(rustc_lints) && !defined(clippy_lints)) {
      file_path =
          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
      file_path_split = string_split(file_path[0], "/")
      source_dir_begin = file_path_split[2]

      if (source_dir_begin == "third_party") {
        _rustflags += allowAllLints
      } else if (source_dir_begin == "prebuilts") {
        _rustflags += allowAllLints
      } else if (source_dir_begin == "vendor" &&
                 file_path_split[3] == "open_source") {
        _rustflags += allowAllLints
      } else if (source_dir_begin == "vendor") {
        _rustflags += rustcVendorLints
        _rustflags += clippyVendorLints
      } else if (source_dir_begin == "device") {
        _rustflags += rustcVendorLints
        _rustflags += clippyVendorLints
      } else {
        _rustflags += rustcOhosLints
        _rustflags += clippyOhosLints
      }
    }

    if (defined(invoker.install_images)) {
      install_images = []
      install_images = invoker.install_images
    }

    if (defined(rustc_lints)) {
      if (rustc_lints == "openharmony") {
        _rustflags += rustcOhosLints
      } else if (rustc_lints == "vendor") {
        _rustflags += rustcVendorLints
      } else if (rustc_lints == "none") {
        _rustflags += allowAllLints
      } else if (rustc_lints == "allWarning") {
        _rustflags += rustcAllWarningLints
      }
    }
    if (defined(clippy_lints)) {
      if (clippy_lints == "openharmony") {
        _rustflags += clippyOhosLints
      } else if (clippy_lints == "vendor") {
        _rustflags += clippyVendorLints
      } else if (clippy_lints == "none") {
        _rustflags += allowAllLints
      } else if (clippy_lints == "allWarning") {
        _rustflags += clippyAllWarningLints
      }
    }
    if (!defined(rustflags)) {
      rustflags = _rustflags
    } else {
      rustflags += _rustflags
    }
  }
}

template("ohos_rust_static_library") {
  _target_name = target_name
  _rustflags = []
  rust_target("$_target_name") {
    target_type = "ohos_rust_library"
    forward_variables_from(invoker, "*")
    if (!defined(invoker.crate_name)) {
      crate_name = _target_name
    }
    crate_type = "rlib"
    if (defined(invoker.crate_type)) {
      assert(invoker.crate_type == crate_type,
             "crate_type should be $crate_type or use default value.")
    }
    module_output_extension = rlib_extension
    install_enable = false

    if (defined(invoker.rustc_lints)) {
      rustc_lints = invoker.rustc_lints
    }
    if (defined(invoker.clippy_lints)) {
      clippy_lints = invoker.clippy_lints
    }

    if (defined(rustc_codecheck) && rustc_codecheck) {
      rustc_lints = "allWarning"
      clippy_lints = "allWarning"
    }

    if (!defined(rustc_lints) && !defined(clippy_lints)) {
      file_path =
          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
      file_path_split = string_split(file_path[0], "/")
      source_dir_begin = file_path_split[2]

      if (source_dir_begin == "third_party") {
        _rustflags += allowAllLints
      } else if (source_dir_begin == "prebuilts") {
        _rustflags += allowAllLints
      } else if (source_dir_begin == "vendor" &&
                 file_path_split[3] == "open_source") {
        _rustflags += allowAllLints
      } else if (source_dir_begin == "vendor") {
        _rustflags += rustcVendorLints
        _rustflags += clippyVendorLints
      } else if (source_dir_begin == "device") {
        _rustflags += rustcVendorLints
        _rustflags += clippyVendorLints
      } else {
        _rustflags += rustcOhosLints
        _rustflags += clippyOhosLints
      }
    }

    if (defined(invoker.install_images)) {
      install_images = []
      install_images = invoker.install_images
    }

    if (defined(rustc_lints)) {
      if (rustc_lints == "openharmony") {
        _rustflags += rustcOhosLints
      } else if (rustc_lints == "vendor") {
        _rustflags += rustcVendorLints
      } else if (rustc_lints == "none") {
        _rustflags += allowAllLints
      } else if (rustc_lints == "allWarning") {
        _rustflags += rustcAllWarningLints
      }
    }
    if (defined(clippy_lints)) {
      if (clippy_lints == "openharmony") {
        _rustflags += clippyOhosLints
      } else if (clippy_lints == "vendor") {
        _rustflags += clippyVendorLints
      } else if (clippy_lints == "none") {
        _rustflags += allowAllLints
      } else if (clippy_lints == "allWarning") {
        _rustflags += clippyAllWarningLints
      }
    }
    if (!defined(rustflags)) {
      rustflags = _rustflags
    } else {
      rustflags += _rustflags
    }
  }
}

template("ohos_rust_shared_ffi") {
  _target_name = target_name
  _rustflags = []
  rust_target("$_target_name") {
    target_type = "ohos_shared_library"
    forward_variables_from(invoker, "*")
    if (!defined(invoker.crate_name)) {
      crate_name = _target_name
    }
    crate_type = "cdylib"
    if (defined(invoker.crate_type)) {
      assert(invoker.crate_type == crate_type,
             "crate_type should be $crate_type or use default value.")
    }

    if (!defined(deps)) {
      deps = []
    }

    if (defined(invoker.rustc_lints)) {
      rustc_lints = invoker.rustc_lints
    }
    if (defined(invoker.clippy_lints)) {
      clippy_lints = invoker.clippy_lints
    }

    if (defined(rustc_codecheck) && rustc_codecheck) {
      rustc_lints = "allWarning"
      clippy_lints = "allWarning"
    }

    if (!defined(rustc_lints) && !defined(clippy_lints)) {
      file_path =
          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
      file_path_split = string_split(file_path[0], "/")
      source_dir_begin = file_path_split[2]

      if (source_dir_begin == "third_party") {
        _rustflags += allowAllLints
      } else if (source_dir_begin == "prebuilts") {
        _rustflags += allowAllLints
      } else if (source_dir_begin == "vendor" &&
                 file_path_split[3] == "open_source") {
        _rustflags += allowAllLints
      } else if (source_dir_begin == "vendor") {
        _rustflags += rustcVendorLints
        _rustflags += clippyVendorLints
      } else if (source_dir_begin == "device") {
        _rustflags += rustcVendorLints
        _rustflags += clippyVendorLints
      } else {
        _rustflags += rustcOhosLints
        _rustflags += clippyOhosLints
      }
    }

    if (defined(invoker.install_images)) {
      install_images = []
      install_images = invoker.install_images
    }

    if (defined(rustc_lints)) {
      if (rustc_lints == "openharmony") {
        _rustflags += rustcOhosLints
      } else if (rustc_lints == "vendor") {
        _rustflags += rustcVendorLints
      } else if (rustc_lints == "none") {
        _rustflags += allowAllLints
      } else if (rustc_lints == "allWarning") {
        _rustflags += rustcAllWarningLints
      }
    }
    if (defined(clippy_lints)) {
      if (clippy_lints == "openharmony") {
        _rustflags += clippyOhosLints
      } else if (clippy_lints == "vendor") {
        _rustflags += clippyVendorLints
      } else if (clippy_lints == "none") {
        _rustflags += allowAllLints
      } else if (clippy_lints == "allWarning") {
        _rustflags += clippyAllWarningLints
      }
    }
    if (!defined(rustflags)) {
      rustflags = _rustflags
    } else {
      rustflags += _rustflags
    }
  }
}

template("ohos_rust_static_ffi") {
  _target_name = target_name
  _rustflags = []
  rust_target("$_target_name") {
    target_type = "ohos_static_library"
    forward_variables_from(invoker, "*")
    if (!defined(invoker.crate_name)) {
      crate_name = _target_name
    }
    crate_type = "staticlib"
    if (defined(invoker.crate_type)) {
      assert(invoker.crate_type == crate_type,
             "crate_type should be $crate_type or use default value.")
    }
    if (!defined(deps)) {
      deps = []
    }
    if (defined(invoker.rustc_lints)) {
      rustc_lints = invoker.rustc_lints
    }
    if (defined(invoker.clippy_lints)) {
      clippy_lints = invoker.clippy_lints
    }

    if (defined(rustc_codecheck) && rustc_codecheck) {
      rustc_lints = "allWarning"
      clippy_lints = "allWarning"
    }

    if (!defined(rustc_lints) && !defined(clippy_lints)) {
      file_path =
          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
      file_path_split = string_split(file_path[0], "/")
      source_dir_begin = file_path_split[2]

      if (source_dir_begin == "third_party") {
        _rustflags += allowAllLints
      } else if (source_dir_begin == "prebuilts") {
        _rustflags += allowAllLints
      } else if (source_dir_begin == "vendor" &&
                 file_path_split[3] == "open_source") {
        _rustflags += allowAllLints
      } else if (source_dir_begin == "vendor") {
        _rustflags += rustcVendorLints
        _rustflags += clippyVendorLints
      } else if (source_dir_begin == "device") {
        _rustflags += rustcVendorLints
        _rustflags += clippyVendorLints
      } else {
        _rustflags += rustcOhosLints
        _rustflags += clippyOhosLints
      }
    }

    if (defined(invoker.install_images)) {
      install_images = []
      install_images = invoker.install_images
    }

    if (defined(rustc_lints)) {
      if (rustc_lints == "openharmony") {
        _rustflags += rustcOhosLints
      } else if (rustc_lints == "vendor") {
        _rustflags += rustcVendorLints
      } else if (rustc_lints == "none") {
        _rustflags += allowAllLints
      } else if (rustc_lints == "allWarning") {
        _rustflags += rustcAllWarningLints
      }
    }
    if (defined(clippy_lints)) {
      if (clippy_lints == "openharmony") {
        _rustflags += clippyOhosLints
      } else if (clippy_lints == "vendor") {
        _rustflags += clippyVendorLints
      } else if (clippy_lints == "none") {
        _rustflags += allowAllLints
      } else if (clippy_lints == "allWarning") {
        _rustflags += clippyAllWarningLints
      }
    }
    if (!defined(rustflags)) {
      rustflags = _rustflags
    } else {
      rustflags += _rustflags
    }
  }
}

template("ohos_rust_proc_macro") {
  assert(!defined(invoker.output_dir),
         "output_dir is not allowed to be defined.")
  _test_target = defined(invoker.testonly) && invoker.testonly
  if (defined(invoker.subsystem_name) && defined(invoker.part_name)) {
    subsystem_name = invoker.subsystem_name
    part_name = invoker.part_name
  } else if (defined(invoker.part_name)) {
    part_name = invoker.part_name
    _part_subsystem_info_file =
        "$root_build_dir/build_configs/parts_info/part_subsystem.json"
    _arguments = [
      "--part-name",
      part_name,
      "--part-subsystem-info-file",
      rebase_path(_part_subsystem_info_file, root_build_dir),
    ]
    get_subsystem_script = "//build/templates/common/get_subsystem_name.py"
    subsystem_name =
        exec_script(get_subsystem_script, _arguments, "trim string")
    if (is_use_check_deps && !_test_target) {
      skip_check_subsystem = true
    }
  } else if (defined(invoker.subsystem_name)) {
    subsystem_name = invoker.subsystem_name
    part_name = subsystem_name
  } else {
    subsystem_name = "build"
    part_name = "build_framework"
  }
  assert(subsystem_name != "")
  assert(part_name != "")
  if (is_use_check_deps && !_test_target) {
    _check_target = "${target_name}__check"
    target_path = get_label_info(":${target_name}", "label_no_toolchain")
    check_target(_check_target) {
      module_deps = []
      module_ex_deps = []
      if (defined(invoker.deps)) {
        module_deps += invoker.deps
      }
      if (defined(invoker.public_deps)) {
        module_deps += invoker.public_deps
      }
      if (defined(invoker.external_deps)) {
        module_ex_deps = invoker.external_deps
      }
      if (defined(invoker.external_deps)) {
        module_ex_deps += invoker.external_deps
      }
      if (defined(invoker.public_external_deps)) {
        module_ex_deps += invoker.public_external_deps
      }
    }
  }
  if (check_deps) {
    deps_data = {
    }
    module_label = get_label_info(":${target_name}", "label_with_toolchain")
    module_deps = []
    if (defined(invoker.deps)) {
      foreach(dep, invoker.deps) {
        module_deps += [ get_label_info(dep, "label_no_toolchain") ]
      }
    }
    module_ex_deps = []
    if (defined(invoker.external_deps) && invoker.external_deps != []) {
      module_ex_deps = invoker.external_deps
    }
    deps_data = {
      part_name = part_name
      module_label = module_label
      deps = module_deps
      external_deps = module_ex_deps
    }
    write_file("${root_out_dir}/deps_files/${part_name}__${target_name}.json",
               deps_data,
               "json")
  }

  if (is_standard_system) {
    output_dir = "${root_out_dir}/${subsystem_name}/${part_name}"
  } else {
    output_dir = "${root_out_dir}"
  }

  if (!_test_target) {
    module_label = get_label_info(":${target_name}", "label_with_toolchain")
    _collect_target = "${target_name}__collect"
    collect_module_target(_collect_target) {
      forward_variables_from(invoker, [ "install_images" ])
    }

    _notice_target = "${target_name}__notice"
    _main_target_name = target_name
    collect_notice(_notice_target) {
      forward_variables_from(invoker,
                             [
                               "testonly",
                               "license_as_sources",
                               "license_file",
                             ])

      module_name = _main_target_name
      module_source_dir = get_label_info(":${_main_target_name}", "dir")
    }
  }

  target_label = get_label_info(":${target_name}", "label_with_toolchain")
  target_toolchain = get_label_info(target_label, "toolchain")

  if (target_toolchain == "${current_toolchain}") {
    ohos_module_name = target_name
    _module_info_target = "${target_name}_info"
    generate_module_info(_module_info_target) {
      module_name = ohos_module_name
      module_type = "lib"
      module_source_dir = "$root_out_dir"
      if (defined(output_dir)) {
        module_source_dir = output_dir
      }

      module_install_name = ohos_module_name
      if (defined(invoker.output_name)) {
        module_install_name = invoker.output_name
      }

      module_install_images = [ "system" ]
      if (defined(invoker.install_images)) {
        module_install_images = []
        module_install_images += invoker.install_images
      }

      module_output_extension = shlib_extension
      if (defined(invoker.output_extension)) {
        module_output_extension = "." + invoker.output_extension
      }

      install_enable = true
      if (defined(invoker.install_enable)) {
        install_enable = invoker.install_enable
      }

      if (defined(invoker.module_install_dir)) {
        module_install_dir = invoker.module_install_dir
      }

      if (defined(invoker.relative_install_dir)) {
        relative_install_dir = invoker.relative_install_dir
      }

      if (defined(invoker.symlink_target_name)) {
        symlink_target_name = invoker.symlink_target_name
      }

      if (defined(invoker.output_prefix_override)) {
        output_prefix_override = invoker.output_prefix_override
      }
      notice = "$target_out_dir/$ohos_module_name.notice.txt"
    }
  }

  _rustflags = []
  rust_target(target_name) {
    target_type = "rust_proc_macro"
    forward_variables_from(invoker,
                           "*",
                           [
                             "configs",
                             "remove_configs",
                             "no_default_deps",
                             "install_images",
                             "module_install_dir",
                             "relative_install_dir",
                             "symlink_target_name",
                             "output_dir",
                             "install_enable",
                             "version_script",
                             "license_file",
                             "license_as_sources",
                             "use_exceptions",
                             "stl",

                             # Sanitizer variables
                             "sanitize",
                           ])
    if (!defined(invoker.crate_name)) {
      crate_name = _target_name
    }
    crate_type = "proc-macro"
    if (defined(invoker.crate_type)) {
      assert(invoker.crate_type == crate_type,
             "crate_type should be $crate_type or use default value.")
    }

    output_dir = output_dir

    if (!defined(inputs)) {
      inputs = []
    }

    if (!defined(ldflags)) {
      ldflags = []
    }

    if (defined(invoker.configs)) {
      configs += invoker.configs
    }
    if (defined(invoker.remove_configs)) {
      configs -= invoker.remove_configs
    }

    if (!defined(output_name)) {
      output_name = target_name
    }

    if (defined(invoker.no_default_deps)) {
      no_default_deps = invoker.no_default_deps
    }

    if (!defined(libs)) {
      libs = []
    }
    if (!defined(cflags_cc)) {
      cflags_cc = []
    }
    if (!defined(deps)) {
      deps = []
    }
    if (is_use_check_deps && !_test_target) {
      deps += [ ":$_check_target" ]
    }
    if (target_toolchain == "${current_toolchain}" && !skip_gen_module_info) {
      deps += [ ":$_module_info_target" ]
    }

    if (!_test_target) {
      deps += [
        ":$_notice_target",
        ":${_collect_target}",
      ]
    }
    if (!defined(include_dirs)) {
      include_dirs = []
    }

    install_module_info = {
      module_def = target_label
      module_info_file =
          rebase_path(get_label_info(module_def, "target_out_dir"),
                      root_build_dir) + "/${target_name}_module_info.json"
      subsystem_name = subsystem_name
      part_name = part_name
      toolchain = current_toolchain
      toolchain_out_dir = rebase_path(root_out_dir, root_build_dir)
    }
    metadata = {
      install_modules = [ install_module_info ]
    }
    if (defined(is_debug) && !is_debug && enable_debug_components != "") {
      foreach(component_name, debug_components) {
        if (part_name == component_name) {
          configs -= default_opt_configs
          configs += debug_level_configs
        }
      }
    }

    if (defined(invoker.rustc_lints)) {
      rustc_lints = invoker.rustc_lints
    }
    if (defined(invoker.clippy_lints)) {
      clippy_lints = invoker.clippy_lints
    }

    if (defined(rustc_codecheck) && rustc_codecheck) {
      rustc_lints = "allWarning"
      clippy_lints = "allWarning"
    }

    if (!defined(rustc_lints) && !defined(clippy_lints)) {
      file_path =
          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
      file_path_split = string_split(file_path[0], "/")
      source_dir_begin = file_path_split[2]

      if (source_dir_begin == "third_party") {
        _rustflags += allowAllLints
      } else if (source_dir_begin == "prebuilts") {
        _rustflags += allowAllLints
      } else if (source_dir_begin == "vendor" &&
                 file_path_split[3] == "open_source") {
        _rustflags += allowAllLints
      } else if (source_dir_begin == "vendor") {
        _rustflags += rustcVendorLints
        _rustflags += clippyVendorLints
      } else if (source_dir_begin == "device") {
        _rustflags += rustcVendorLints
        _rustflags += clippyVendorLints
      } else {
        _rustflags += rustcOhosLints
        _rustflags += clippyOhosLints
      }
    }

    if (defined(rustc_lints)) {
      if (rustc_lints == "openharmony") {
        _rustflags += rustcOhosLints
      } else if (rustc_lints == "vendor") {
        _rustflags += rustcVendorLints
      } else if (rustc_lints == "none") {
        _rustflags += allowAllLints
      } else if (rustc_lints == "allWarning") {
        _rustflags += rustcAllWarningLints
      }
    }
    if (defined(clippy_lints)) {
      if (clippy_lints == "openharmony") {
        _rustflags += clippyOhosLints
      } else if (clippy_lints == "vendor") {
        _rustflags += clippyVendorLints
      } else if (clippy_lints == "none") {
        _rustflags += allowAllLints
      } else if (clippy_lints == "allWarning") {
        _rustflags += clippyAllWarningLints
      }
    }
    if (!defined(rustflags)) {
      rustflags = _rustflags
    } else {
      rustflags += _rustflags
    }
  }
}