| # 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") | |
| import("kernel_test_deps.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. | |
| # | |
| # * test_deps | |
| # - Optional: Dependencies only used in the unit tests within the crate. | |
| # Note that unlike rustc_library(), none of these dependencies may be | |
| # testonly. (See `kernel_test_deps()`.) | |
| # - Type: list(label) | |
| # - Default: [] | |
| # | |
| # See rustc_library() for other parameters. | |
| # | |
| template("kernel_rust_crate") { | |
| main_target = target_name | |
| test_deps = [] | |
| if (defined(invoker.test_deps)) { | |
| test_deps_target = "kernel_rust_crate.${target_name}.test_deps" | |
| test_deps += [ ":$test_deps_target" ] | |
| kernel_test_deps(test_deps_target) { | |
| visibility = [ ":*" ] | |
| public_deps = invoker.test_deps | |
| } | |
| } | |
| rustc_library(main_target) { | |
| edition = "2024" | |
| forward_variables_from(invoker, | |
| "*", | |
| [ | |
| "edition", | |
| "name", | |
| "output_name", | |
| "test_deps", | |
| ]) | |
| if (!defined(crate_name)) { | |
| crate_name = target_name | |
| } | |
| crate_name = string_replace(crate_name, "-", "_") | |
| if (!defined(deps)) { | |
| deps = [] | |
| } | |
| deps += test_deps | |
| } | |
| } | |
| set_defaults("kernel_rust_crate") { | |
| configs = default_common_binary_configs | |
| } |