blob: 64bafd6928a242fb736e78dba4b376363bec0523 [file]
# Copyright 2026 The Fuchsia Authors
#
# Use of this source code is governed by a MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT
import("//build/rust/build.gni")
import("//build/rust/rustc_library.gni")
# Define a Rust crate meant specifically to go into the kernel.
#
# This is the same as rustc_library(), but does a little extra for the kernel.
# If a Rust crate just provides Rust APIs for other Rust code, it should just
# use rustc_library() instead. Use kernel_rust_crate() for a rustc_library()
# that defines `unsafe extern "C" pub` symbols to be used by the C++ code in
# the kernel. If the kernel_rust_crate() target appears in the deps graph of a
# kernel_executable() then it will be linked into the kernel to provide its
# `extern "C"` symbols to the C++ code. A plain rustc_library() crate that is
# not used by other Rust code will not get linked in (and should a warning).
#
# Any kernel_rust_crate() target is just a plain rustc_library() when built
# outside the kernel environment.
#
# Parameters
#
# * edition
# - Disallowed: This cannot be specified. It's always "2024".
#
# * name, output_name
# - Disallowed: Use $crate_name instead.
#
# See rustc_library() for other parameters.
#
template("kernel_rust_crate") {
rustc_library(target_name) {
edition = "2024"
forward_variables_from(invoker,
"*",
[
"edition",
"metadata",
"name",
"output_name",
])
if (!defined(crate_name)) {
crate_name = target_name
}
crate_name = string_replace(crate_name, "-", "_")
metadata = {
kernel_rust_crate_decls = []
kernel_rust_crate_flags = []
if (defined(invoker.metadata)) {
forward_variables_from(invoker.metadata, "*")
}
if (is_kernel) {
# See kernel_executable().
kernel_rust_crate_decls += [
# A label starts with // and so is already a Rust comment!
get_label_info(":$target_name", "label_with_toolchain"),
"#[allow(unused_extern_crates)]",
"extern crate $crate_name;",
]
_rlib_path = target_out_dir
if (rust_one_rlib_per_dir) {
_rlib_path += "/$target_name.actual"
}
_rlib_path += "/lib$crate_name.rlib"
kernel_rust_crate_flags += [ "--extern=$crate_name=" +
rebase_path(_rlib_path, root_build_dir) ]
}
}
}
}