xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/unit/toolchain/toolchain_test_utils.bzl (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1"""Helpers for testing `rust_toolchain.target_json`"""
2
3def _rules_rust_toolchain_test_target_json_repository_impl(repository_ctx):
4    target_json_path = repository_ctx.path(repository_ctx.attr.target_json)
5    target_json_content = repository_ctx.read(target_json_path)
6    target_json = json.decode(target_json_content)
7
8    repository_ctx.file("BUILD.bazel", """exports_files(["defs.bzl"])""")
9    repository_ctx.file("defs.bzl", "TARGET_JSON = {}".format(target_json))
10    repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
11        repository_ctx.name,
12    ))
13
14rules_rust_toolchain_test_target_json_repository = repository_rule(
15    doc = (
16        "A repository rule used for converting json files to starlark. This " +
17        "rule acts as an example of how users can use json files to represent " +
18        "custom rust platforms."
19    ),
20    implementation = _rules_rust_toolchain_test_target_json_repository_impl,
21    attrs = {
22        "target_json": attr.label(
23            doc = "A custom target specification. For more details see: https://doc.rust-lang.org/rustc/targets/custom.html",
24            mandatory = True,
25        ),
26    },
27)
28