load("@rules_rust//rust:toolchain.bzl", "rust_stdlib_filegroup", "rust_toolchain")
load("@bazel_skylib//rules:write_file.bzl", "write_file")

# Similar to the one automatically generated by @rust, but with the correct
# hardware platform configured.
toolchain(
    name = "rust-toolchain-roborio",
    exec_compatible_with = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
    ],
    target_compatible_with = [
        "@platforms//os:linux",
        "//tools/platforms/hardware:roborio",
    ],
    toolchain = "@rust//:toolchain_for_arm-unknown-linux-gnueabi_impl",
    toolchain_type = "@rules_rust//rust:toolchain",
)

# The remainder of this file exists to create a NOOP toolchain for Rust on
# platforms that don't support Rust. We can probably get rid of this once
# https://github.com/bazelbuild/bazel/issues/12897 is fixed.

write_file(
    name = "noop_error_exit",
    out = "noop_error_exit.sh",
    content = [
        "#!/bin/bash",
        "echo 'This should never be executed. Something went wrong.' >&2",
        "echo 'This NOOP Rust toolchain should never be executed. Something went wrong.' >&2",
        "echo 'Check that your target has `target_compatible_with` set to a platform that supports Rust.' >&2",
        "exit 1",
    ],
    is_executable = True,
)

rust_stdlib_filegroup(
    name = "empty_stdlib",
    srcs = [":noop_error_exit"],
)

rust_toolchain(
    name = "noop_rust_toolchain_impl",
    binary_ext = "",
    cargo = ":noop_error_exit",
    clippy_driver = ":noop_error_exit",
    default_edition = "2021",
    dylib_ext = ".so",
    exec_triple = "none",
    os = "none",
    rust_doc = ":noop_error_exit",
    rust_lib = ":empty_stdlib",
    rustc = ":noop_error_exit",
    rustc_lib = ":noop_error_exit",
    rustc_srcs = None,
    rustfmt = ":noop_error_exit",
    staticlib_ext = ".a",
    stdlib_linkflags = [],
    tags = ["manual"],
    target_triple = "none",
)

toolchain(
    name = "noop_rust_toolchain",
    exec_compatible_with = [
        "@platforms//os:linux",
    ],
    target_compatible_with = [
        "//tools/platforms/rust:lacks_support",
    ],
    toolchain = ":noop_rust_toolchain_impl",
    toolchain_type = "@rules_rust//rust:toolchain",
)
