98 lines
2.3 KiB
Python
98 lines
2.3 KiB
Python
load(
|
|
"@rules_rust//rust:defs.bzl",
|
|
_rules_rust_binary = "rust_binary",
|
|
_rules_rust_library = "rust_library",
|
|
_rules_rust_test = "rust_test",
|
|
)
|
|
load("//bazel:utils.bzl", "execute", "require_env")
|
|
|
|
|
|
def rust_module(name, src, deps = [], **kwargs):
|
|
native.filegroup(
|
|
name = name,
|
|
srcs = [src] + deps,
|
|
**kwargs
|
|
)
|
|
|
|
|
|
def rust_library(name, src, crate_name = None, modules = [], deps = [], **kwargs):
|
|
_rules_rust_library(
|
|
name = name,
|
|
crate_name = crate_name,
|
|
crate_root = src,
|
|
srcs = [src] + modules,
|
|
deps = deps,
|
|
**kwargs
|
|
)
|
|
|
|
|
|
def rust_binary(name, src, crate_name = None, deps = [], **kwargs):
|
|
_rules_rust_binary(
|
|
name = name,
|
|
crate_name = crate_name,
|
|
crate_root = src,
|
|
srcs = [src],
|
|
deps = deps,
|
|
**kwargs
|
|
)
|
|
|
|
|
|
def rust_test(name, src, deps = [], **kwargs):
|
|
_rules_rust_test(
|
|
name = name,
|
|
crate_root = src,
|
|
srcs = [src],
|
|
deps = deps,
|
|
**kwargs
|
|
)
|
|
|
|
|
|
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,
|
|
)
|