refactor(bazel): use configured Rust toolchain root
This commit is contained in:
2
MODULE.bazel.lock
generated
2
MODULE.bazel.lock
generated
@@ -93,7 +93,7 @@
|
||||
"moduleExtensions": {
|
||||
"//bazel:rust.bzl%rust": {
|
||||
"general": {
|
||||
"bzlTransitiveDigest": "0r4u+74QPphTHEzrdQIFSn4SfNM4AAJNlPyNGE8JwRs=",
|
||||
"bzlTransitiveDigest": "4aaA6vAtrGES73j6RWZFmf/31/0PAazCSNi2ccK0w0E=",
|
||||
"usagesDigest": "jJoM2Hp1NbshT//puE4xIUfXdA0K6BRx1WTlsZIKFm4=",
|
||||
"recordedFileInputs": {},
|
||||
"recordedDirentsInputs": {},
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
exports_files([
|
||||
"rust.BUILD.tpl",
|
||||
"rust.bzl",
|
||||
"utils.bzl",
|
||||
])
|
||||
|
||||
117
bazel/rust.BUILD.tpl
Normal file
117
bazel/rust.BUILD.tpl
Normal file
@@ -0,0 +1,117 @@
|
||||
filegroup(
|
||||
name = "rustc",
|
||||
srcs = ["toolchain/bin/rustc"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "rustdoc",
|
||||
srcs = ["toolchain/bin/rustdoc"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "cargo",
|
||||
srcs = ["toolchain/bin/cargo"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "cargo_clippy",
|
||||
srcs = ["toolchain/bin/cargo-clippy"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "clippy_driver",
|
||||
srcs = ["toolchain/bin/clippy-driver"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "rustfmt",
|
||||
srcs = ["toolchain/bin/rustfmt"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "cc",
|
||||
srcs = ["toolchain/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 = "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_impl",
|
||||
toolchain_type = "@rules_rust//rust:toolchain_type",
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
153
bazel/rust.bzl
153
bazel/rust.bzl
@@ -1,14 +1,9 @@
|
||||
load("//bazel:utils.bzl", "execute", "require")
|
||||
load("//bazel:utils.bzl", "execute", "require_env")
|
||||
|
||||
|
||||
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")
|
||||
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"])
|
||||
@@ -26,148 +21,24 @@ def _rust_repository_impl(repository_ctx):
|
||||
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(root, "toolchain")
|
||||
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 = "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_impl",
|
||||
toolchain_type = "@rules_rust//rust:toolchain_type",
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
""".format(
|
||||
host_triple = host_triple,
|
||||
release = release,
|
||||
repository_ctx.template(
|
||||
"BUILD.bazel",
|
||||
Label("//bazel:rust.BUILD.tpl"),
|
||||
{
|
||||
"{host_triple}": host_triple,
|
||||
"{release}": release,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
rust_repository = repository_rule(
|
||||
implementation = _rust_repository_impl,
|
||||
local = True,
|
||||
environ = ["PATH"],
|
||||
environ = ["RUST_TOOLCHAIN_ROOT"],
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
def require(repository_ctx, executable):
|
||||
path = repository_ctx.which(executable)
|
||||
if path == None:
|
||||
fail("{} must be available in PATH.".format(executable))
|
||||
return path
|
||||
def require_env(repository_ctx, variable):
|
||||
value = repository_ctx.os.environ.get(variable)
|
||||
if not value:
|
||||
fail("{} must be set.".format(variable))
|
||||
return value
|
||||
|
||||
|
||||
def execute(repository_ctx, arguments):
|
||||
|
||||
@@ -8,18 +8,28 @@
|
||||
let
|
||||
cfg = config.modules.shell.rust;
|
||||
user = config.user.name;
|
||||
toolchain = pkgs.symlinkJoin {
|
||||
name = "rust-toolchain";
|
||||
paths = with pkgs; [
|
||||
cargo
|
||||
clippy
|
||||
gcc
|
||||
rustc
|
||||
rustfmt
|
||||
];
|
||||
};
|
||||
in
|
||||
{
|
||||
options.modules.shell.rust.enable = lib.mkEnableOption "Rust tools for the primary user's shell environment";
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
home-manager.users.${user}.home.packages = with pkgs; [
|
||||
cargo
|
||||
clippy
|
||||
gcc
|
||||
rust-analyzer
|
||||
rustc
|
||||
rustfmt
|
||||
];
|
||||
home-manager.users.${user} = {
|
||||
home.packages = [
|
||||
toolchain
|
||||
pkgs.rust-analyzer
|
||||
];
|
||||
|
||||
home.sessionVariables.RUST_TOOLCHAIN_ROOT = "${toolchain}";
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user