181 lines
4.7 KiB
Python
181 lines
4.7 KiB
Python
load("//bazel:utils.bzl", _execute = "execute", _require = "require")
|
|
|
|
|
|
def _rust_repository_impl(repository_ctx):
|
|
rustc = _require(repository_ctx, "rustc")
|
|
rustdoc = _require(repository_ctx, "rustdoc")
|
|
cargo = _require(repository_ctx, "cargo")
|
|
cargo_clippy = _require(repository_ctx, "cargo-clippy")
|
|
clippy_driver = _require(repository_ctx, "clippy-driver")
|
|
rustfmt = _require(repository_ctx, "rustfmt")
|
|
cc = _require(repository_ctx, "cc")
|
|
|
|
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(rustc, "bin/rustc")
|
|
repository_ctx.symlink(rustdoc, "bin/rustdoc")
|
|
repository_ctx.symlink(cargo, "bin/cargo")
|
|
repository_ctx.symlink(cargo_clippy, "bin/cargo-clippy")
|
|
repository_ctx.symlink(clippy_driver, "bin/clippy-driver")
|
|
repository_ctx.symlink(rustfmt, "bin/rustfmt")
|
|
repository_ctx.symlink(cc, "bin/cc")
|
|
repository_ctx.symlink(sysroot, "rust")
|
|
|
|
repository_ctx.file("WORKSPACE.bazel", "")
|
|
repository_ctx.file("BUILD.bazel", _build_file(host_triple, release))
|
|
|
|
|
|
def _build_file(host_triple, release):
|
|
return """
|
|
filegroup(
|
|
name = "rustc",
|
|
srcs = ["bin/rustc"],
|
|
visibility = ["//visibility:public"],
|
|
)
|
|
|
|
filegroup(
|
|
name = "rustdoc",
|
|
srcs = ["bin/rustdoc"],
|
|
visibility = ["//visibility:public"],
|
|
)
|
|
|
|
filegroup(
|
|
name = "cargo",
|
|
srcs = ["bin/cargo"],
|
|
visibility = ["//visibility:public"],
|
|
)
|
|
|
|
filegroup(
|
|
name = "cargo_clippy",
|
|
srcs = ["bin/cargo-clippy"],
|
|
visibility = ["//visibility:public"],
|
|
)
|
|
|
|
filegroup(
|
|
name = "clippy_driver",
|
|
srcs = ["bin/clippy-driver"],
|
|
visibility = ["//visibility:public"],
|
|
)
|
|
|
|
filegroup(
|
|
name = "rustfmt",
|
|
srcs = ["bin/rustfmt"],
|
|
visibility = ["//visibility:public"],
|
|
)
|
|
|
|
filegroup(
|
|
name = "cc",
|
|
srcs = ["bin/cc"],
|
|
visibility = ["//visibility:public"],
|
|
)
|
|
|
|
filegroup(
|
|
name = "rustc_lib",
|
|
srcs = glob(
|
|
[
|
|
"rust/lib/*.so*",
|
|
"rust/lib/rustlib/{host_triple}/codegen-backends/*.so",
|
|
"rust/lib/rustlib/{host_triple}/lib/*.so*",
|
|
"rust/lib/rustlib/{host_triple}/lib/*.rmeta",
|
|
],
|
|
allow_empty = True,
|
|
),
|
|
visibility = ["//visibility:public"],
|
|
)
|
|
|
|
filegroup(
|
|
name = "rust_objcopy",
|
|
srcs = glob(
|
|
["rust/lib/rustlib/{host_triple}/bin/rust-objcopy"],
|
|
allow_empty = True,
|
|
),
|
|
visibility = ["//visibility:public"],
|
|
)
|
|
|
|
load("@rules_rust//rust:toolchain.bzl", "rust_stdlib_filegroup")
|
|
|
|
rust_stdlib_filegroup(
|
|
name = "rust_std",
|
|
srcs = glob(
|
|
[
|
|
"rust/lib/rustlib/{host_triple}/lib/*.rlib",
|
|
"rust/lib/rustlib/{host_triple}/lib/*.rmeta",
|
|
"rust/lib/rustlib/{host_triple}/lib/*.so*",
|
|
"rust/lib/rustlib/{host_triple}/lib/*.a",
|
|
"rust/lib/rustlib/{host_triple}/lib/self-contained/**",
|
|
],
|
|
allow_empty = True,
|
|
),
|
|
visibility = ["//visibility:public"],
|
|
)
|
|
|
|
load("@rules_rust//rust:toolchain.bzl", "rust_toolchain")
|
|
|
|
rust_toolchain(
|
|
name = "rust_toolchain_impl",
|
|
rust_doc = ":rustdoc",
|
|
rust_std = ":rust_std",
|
|
rustc = ":rustc",
|
|
linker = ":cc",
|
|
linker_type = "indirect",
|
|
rust_objcopy = ":rust_objcopy",
|
|
rustfmt = ":rustfmt",
|
|
cargo = ":cargo",
|
|
clippy_driver = ":clippy_driver",
|
|
cargo_clippy = ":cargo_clippy",
|
|
rustc_lib = ":rustc_lib",
|
|
allocator_library = None,
|
|
global_allocator_library = None,
|
|
binary_ext = "",
|
|
staticlib_ext = ".a",
|
|
dylib_ext = ".so",
|
|
stdlib_linkflags = ["-ldl", "-lpthread"],
|
|
default_edition = "2024",
|
|
exec_triple = "{host_triple}",
|
|
target_triple = "{host_triple}",
|
|
version = "{release}",
|
|
channel = "stable",
|
|
visibility = ["//visibility:public"],
|
|
)
|
|
|
|
toolchain(
|
|
name = "rust_toolchain",
|
|
toolchain = ":rust_toolchain_impl",
|
|
toolchain_type = "@rules_rust//rust:toolchain_type",
|
|
visibility = ["//visibility:public"],
|
|
)
|
|
""".format(
|
|
host_triple = host_triple,
|
|
release = release,
|
|
)
|
|
|
|
|
|
rust_repository = repository_rule(
|
|
implementation = _rust_repository_impl,
|
|
local = True,
|
|
environ = ["PATH"],
|
|
)
|
|
|
|
|
|
def _rust_impl(module_ctx):
|
|
rust_repository(name = "rust_toolchain")
|
|
|
|
|
|
rust = module_extension(
|
|
implementation = _rust_impl,
|
|
)
|