load("//bazel:utils.bzl", "execute", "require_env") def _rust_repository_impl(repository_ctx): root = repository_ctx.path(require_env(repository_ctx, "RUST_TOOLCHAIN_ROOT")) rustc = root.get_child("bin/rustc") sysroot = execute(repository_ctx, [str(rustc), "--print", "sysroot"]) verbose_version = execute(repository_ctx, [str(rustc), "-vV"]) host_triple = None release = None for line in verbose_version.splitlines(): if line.startswith("host: "): host_triple = line.removeprefix("host: ") if line.startswith("release: "): release = line.removeprefix("release: ") if host_triple == None: fail("rustc -vV did not report a host triple") if release == None: fail("rustc -vV did not report a release") repository_ctx.symlink(root, "toolchain") repository_ctx.symlink(sysroot, "rust") repository_ctx.file("WORKSPACE.bazel", "") repository_ctx.template( "BUILD.bazel", Label("//bazel:rust.BUILD.template"), { "{host_triple}": host_triple, "{release}": release, }, ) rust_repository = repository_rule( implementation = _rust_repository_impl, local = True, environ = ["RUST_TOOLCHAIN_ROOT"], ) def _rust_impl(module_ctx): rust_repository(name = "rust") rust = module_extension( implementation = _rust_impl, )