| # Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| import("//build/rust/rustc_binary.gni") | |
| import("//build/testing/boot_tests/zbi_test.gni") | |
| import("//build/zbi/kernel_cmdline.gni") | |
| # Build a Rust executable to run as a standalone test binary under Zircon. | |
| # | |
| # This wraps a rustc_binary() with the metadata appropriate to have it launched | |
| # by userboot as a standalone binary. This target should be included in deps of | |
| # a zbi_test() target, along with a kernel. An auxiliary metadata target | |
| # "$target_name.cmdline" is defined and referenced in data_deps. | |
| # | |
| # Parameters: (see rustc_binary for other parameters) | |
| # | |
| # userboot_entry_type (optional, defaults to "test") | |
| # [string] May be "test" or "boot". By setting this parameter, the | |
| # executable will be launched by userboot as a test program or as boot | |
| # program. | |
| # | |
| # userboot_next_is_test (optional, defaults to `false`) | |
| # [boolean] Whether the programs being launched constitute a single test. | |
| # Setting this flag will force userboot to wait for `userboot.next` to exit, | |
| # and use `userboot.next`'s result as the success criteria for the test. | |
| # | |
| # prefix (optional, defaults to "zbi-test") | |
| # [string] Same behavior as zbi_input() prefix. | |
| template("rust_zbi_test_executable") { | |
| if (defined(invoker.output_name)) { | |
| output_name = invoker.output_name | |
| } else { | |
| output_name = string_replace(target_name, "-", "_") | |
| } | |
| userboot_prefix = "zbi-test" | |
| if (defined(invoker.prefix)) { | |
| userboot_prefix = invoker.prefix | |
| } | |
| # Defined by 'executable' template (used by 'rustc_binary'). | |
| install_path = "bin/${output_name}" | |
| if (defined(invoker.userboot_entry_type)) { | |
| userboot_entry_type = invoker.userboot_entry_type | |
| } else { | |
| userboot_entry_type = "test" | |
| } | |
| if (defined(invoker.userboot_next_is_test)) { | |
| userboot_next_is_test = invoker.userboot_next_is_test | |
| } else { | |
| userboot_next_is_test = false | |
| } | |
| main_target = target_name | |
| zbi_input_target = "$target_name.zbi_input" | |
| test_target = "$target_name.bin" | |
| cmdline_target = "$target_name.cmdline" | |
| group(main_target) { | |
| testonly = true | |
| deps = [ ":$zbi_input_target" ] | |
| } | |
| static_remove_configs = [ | |
| "//build/config/fuchsia:fdio_config", | |
| "//build/config/fuchsia:dynamic_rust_standard_library", | |
| ] | |
| _fdio_stub_label = "//src/bringup/bin/userboot/tests:fdio_stub" | |
| _fdio_stub_dir = | |
| get_label_info(_fdio_stub_label, "target_out_dir") + "/fdio_stub" | |
| rustc_binary(test_target) { | |
| testonly = true | |
| edition = "2024" | |
| data_deps = [] | |
| deps = [] | |
| rustflags = [] | |
| forward_variables_from(invoker, | |
| "*", | |
| [ | |
| "output_name", | |
| "prefix", | |
| "userboot_entry_type", | |
| "userboot_next_is_test", | |
| ]) | |
| output_name = output_name | |
| configs += static_remove_configs | |
| configs -= static_remove_configs | |
| rustflags += [ "-Lnative=" + rebase_path(_fdio_stub_dir, root_build_dir) ] | |
| deps += [ | |
| "//sdk/lib/c/stubs", | |
| _fdio_stub_label, | |
| ] | |
| data_deps += [ ":$cmdline_target" ] | |
| exclude_toolchain_tags = [ "uses-shadow" ] | |
| } | |
| zbi_input(zbi_input_target) { | |
| testonly = true | |
| prefix = userboot_prefix | |
| deps = [ ":$test_target" ] | |
| } | |
| kernel_cmdline(cmdline_target) { | |
| testonly = true | |
| forward_variables_from(invoker, [ "visibility" ]) | |
| if (defined(visibility)) { | |
| visibility += [ | |
| ":$main_target", | |
| ":$test_target", | |
| ":${test_target}.actual", | |
| ] | |
| } | |
| if (userboot_entry_type == "test") { | |
| args = [ | |
| "kernel.select.userboot=userboot-test", | |
| "userboot.test.root=$userboot_prefix", | |
| "userboot.test.next=$install_path", | |
| ] | |
| } else { | |
| args = [ | |
| "userboot.root=$userboot_prefix", | |
| "userboot.next=$install_path", | |
| ] | |
| } | |
| if (userboot_next_is_test) { | |
| args += [ "userboot.next-is-test=true" ] | |
| if (userboot_entry_type != "test") { | |
| args += [ "kernel.select.userboot=userboot-test" ] | |
| } | |
| } | |
| } | |
| } |