Files
phs_v1.0.1.0/build/templates/rust/rust_template.gni

899 lines
26 KiB
Plaintext
Raw Permalink Normal View History

2024-09-27 19:16:49 +08:00
# 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",
]
clippyOhosLints = [
"-A",
"clippy::type-complexity",
"-A",
"clippy::unnecessary-wraps",
"-A",
"clippy::unusual-byte-groupings",
"-A",
"clippy::upper-case-acronyms",
"-A",
"clippy::let_and_return",
]
clippyVendorLints = [
"-A",
"clippy::complexity",
"-A",
"clippy::perf",
"-A",
"clippy::style",
]
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 = []
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",
]
}
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
}
}
}
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 = []
}
_external_deps = [
"common:libstd.dylib.so",
"common:libtest.dylib.so",
]
if (defined(external_deps)) {
external_deps += _external_deps
} else {
external_deps = _external_deps
}
if (defined(invoker.rustc_lints)) {
rustc_lints = invoker.rustc_lints
}
if (defined(invoker.clippy_lints)) {
clippy_lints = invoker.clippy_lints
}
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") {
_rustflags += rustcVendorLints
_rustflags += clippyVendorLints
} else if (source_dir_begin == "device") {
_rustflags += rustcVendorLints
_rustflags += clippyVendorLints
} else {
_rustflags += rustcOhosLints
_rustflags += clippyOhosLints
}
}
if (defined(rustc_lints)) {
if (invoker.rustc_lints == "openharmony") {
_rustflags += rustcOhosLints
} else if (rustc_lints == "vendor") {
_rustflags += rustcVendorLints
} else if (rustc_lints == "none") {
_rustflags += allowAllLints
}
}
if (defined(clippy_lints)) {
if (invoker.clippy_lints == "openharmony") {
_rustflags += clippyOhosLints
} else if (clippy_lints == "vendor") {
_rustflags += clippyVendorLints
} else if (clippy_lints == "none") {
_rustflags += allowAllLints
}
}
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_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") {
_rustflags += rustcVendorLints
_rustflags += clippyVendorLints
} else if (source_dir_begin == "device") {
_rustflags += rustcVendorLints
_rustflags += clippyVendorLints
} else {
_rustflags += rustcOhosLints
_rustflags += clippyOhosLints
}
}
if (defined(rustc_lints)) {
if (invoker.rustc_lints == "openharmony") {
_rustflags += rustcOhosLints
} else if (rustc_lints == "vendor") {
_rustflags += rustcVendorLints
} else if (rustc_lints == "none") {
_rustflags += allowAllLints
}
}
if (defined(clippy_lints)) {
if (invoker.clippy_lints == "openharmony") {
_rustflags += clippyOhosLints
} else if (clippy_lints == "vendor") {
_rustflags += clippyVendorLints
} else if (clippy_lints == "none") {
_rustflags += allowAllLints
}
}
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_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") {
_rustflags += rustcVendorLints
_rustflags += clippyVendorLints
} else if (source_dir_begin == "device") {
_rustflags += rustcVendorLints
_rustflags += clippyVendorLints
} else {
_rustflags += rustcOhosLints
_rustflags += clippyOhosLints
}
}
if (defined(rustc_lints)) {
if (invoker.rustc_lints == "openharmony") {
_rustflags += rustcOhosLints
} else if (rustc_lints == "vendor") {
_rustflags += rustcVendorLints
} else if (rustc_lints == "none") {
_rustflags += allowAllLints
}
}
if (defined(clippy_lints)) {
if (invoker.clippy_lints == "openharmony") {
_rustflags += clippyOhosLints
} else if (clippy_lints == "vendor") {
_rustflags += clippyVendorLints
} else if (clippy_lints == "none") {
_rustflags += allowAllLints
}
}
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 = []
}
_external_deps = [
"common:libstd.dylib.so",
"common:libtest.dylib.so",
]
if (defined(external_deps)) {
external_deps += _external_deps
} else {
external_deps = _external_deps
}
if (defined(invoker.rustc_lints)) {
rustc_lints = invoker.rustc_lints
}
if (defined(invoker.clippy_lints)) {
clippy_lints = invoker.clippy_lints
}
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") {
_rustflags += rustcVendorLints
_rustflags += clippyVendorLints
} else if (source_dir_begin == "device") {
_rustflags += rustcVendorLints
_rustflags += clippyVendorLints
} else {
_rustflags += rustcOhosLints
_rustflags += clippyOhosLints
}
}
if (defined(rustc_lints)) {
if (invoker.rustc_lints == "openharmony") {
_rustflags += rustcOhosLints
} else if (rustc_lints == "vendor") {
_rustflags += rustcVendorLints
} else if (rustc_lints == "none") {
_rustflags += allowAllLints
}
}
if (defined(clippy_lints)) {
if (invoker.clippy_lints == "openharmony") {
_rustflags += clippyOhosLints
} else if (clippy_lints == "vendor") {
_rustflags += clippyVendorLints
} else if (clippy_lints == "none") {
_rustflags += allowAllLints
}
}
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 = []
}
_external_deps = [
"common:libstd.dylib.so",
"common:libtest.dylib.so",
]
if (defined(external_deps)) {
external_deps += _external_deps
} else {
external_deps = _external_deps
}
if (defined(invoker.rustc_lints)) {
rustc_lints = invoker.rustc_lints
}
if (defined(invoker.clippy_lints)) {
clippy_lints = invoker.clippy_lints
}
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") {
_rustflags += rustcVendorLints
_rustflags += clippyVendorLints
} else if (source_dir_begin == "device") {
_rustflags += rustcVendorLints
_rustflags += clippyVendorLints
} else {
_rustflags += rustcOhosLints
_rustflags += clippyOhosLints
}
}
if (defined(rustc_lints)) {
if (invoker.rustc_lints == "openharmony") {
_rustflags += rustcOhosLints
} else if (rustc_lints == "vendor") {
_rustflags += rustcVendorLints
} else if (rustc_lints == "none") {
_rustflags += allowAllLints
}
}
if (defined(clippy_lints)) {
if (invoker.clippy_lints == "openharmony") {
_rustflags += clippyOhosLints
} else if (clippy_lints == "vendor") {
_rustflags += clippyVendorLints
} else if (clippy_lints == "none") {
_rustflags += allowAllLints
}
}
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.")
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) {
skip_check_subsystem = true
}
} else if (defined(invoker.subsystem_name)) {
subsystem_name = invoker.subsystem_name
part_name = subsystem_name
} else {
subsystem_name = "common"
part_name = subsystem_name
}
assert(subsystem_name != "")
assert(part_name != "")
if (is_use_check_deps) {
_check_target = "${target_name}__check"
target_path = get_label_info(":${target_name}", "label_no_toolchain")
check_target(_check_target) {
module_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 (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}"
}
_test_target = defined(invoker.testonly) && invoker.testonly
if (!_test_target) {
_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",
"external_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) {
deps += [ ":$_check_target" ]
}
if (target_toolchain == "${current_toolchain}" && !skip_gen_module_info) {
deps += [ ":$_module_info_target" ]
}
if (!_test_target) {
deps += [ ":$_notice_target" ]
}
if (!defined(include_dirs)) {
include_dirs = []
}
if (defined(invoker.external_deps)) {
component_override_map = rebase_path(
"${root_build_dir}/build_configs/component_override_map.json")
external_deps_script =
rebase_path("//build/templates/common/external_deps_handler.py")
external_deps_temp_file =
"$target_gen_dir/${part_name}__${target_name}_external_deps_temp.json"
arguments = [ "--external-deps" ]
arguments += invoker.external_deps
arguments += [
"--parts-src-flag-file",
rebase_path(parts_src_flag_file, root_build_dir),
"--external-deps-temp-file",
rebase_path(external_deps_temp_file, root_build_dir),
"--sdk-base-dir",
rebase_path("${innersdk_base_dir}", root_build_dir),
"--sdk-dir-name",
"${innersdk_dir_name}",
"--current-toolchain",
current_toolchain,
"--innerkits-adapter-info-file",
rebase_path("//build/ohos/inner_kits_adapter.json", root_build_dir),
"--component-override-map",
component_override_map,
]
if (is_use_sdk) {
arguments += [ "--use-sdk" ]
}
handler_result = exec_script(external_deps_script, arguments, "string")
if (handler_result != "") {
print(handler_result)
}
external_deps_info = read_file(external_deps_temp_file, "json")
if (defined(external_deps_info.deps)) {
deps += external_deps_info.deps
}
if (defined(external_deps_info.libs)) {
libs += external_deps_info.libs
}
if (defined(external_deps_info.include_dirs)) {
include_dirs += external_deps_info.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_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") {
_rustflags += rustcVendorLints
_rustflags += clippyVendorLints
} else if (source_dir_begin == "device") {
_rustflags += rustcVendorLints
_rustflags += clippyVendorLints
} else {
_rustflags += rustcOhosLints
_rustflags += clippyOhosLints
}
}
if (defined(rustc_lints)) {
if (invoker.rustc_lints == "openharmony") {
_rustflags += rustcOhosLints
} else if (rustc_lints == "vendor") {
_rustflags += rustcVendorLints
} else if (rustc_lints == "none") {
_rustflags += allowAllLints
}
}
if (defined(clippy_lints)) {
if (invoker.clippy_lints == "openharmony") {
_rustflags += clippyOhosLints
} else if (clippy_lints == "vendor") {
_rustflags += clippyVendorLints
} else if (clippy_lints == "none") {
_rustflags += allowAllLints
}
}
if (!defined(rustflags)) {
rustflags = _rustflags
} else {
rustflags += _rustflags
}
}
}