build: add bazel rust workflow
This commit is contained in:
1
bazel/BUILD
Normal file
1
bazel/BUILD
Normal file
@@ -0,0 +1 @@
|
||||
exports_files(["rust_toolchain.bzl"])
|
||||
191
bazel/rust_toolchain.bzl
Normal file
191
bazel/rust_toolchain.bzl
Normal file
@@ -0,0 +1,191 @@
|
||||
def _required_path(repository_ctx, command):
|
||||
path = repository_ctx.which(command)
|
||||
if path == None:
|
||||
fail("{} must be available in PATH. Enter the rust dev shell first.".format(command))
|
||||
return path
|
||||
|
||||
|
||||
def _execute(repository_ctx, arguments):
|
||||
result = repository_ctx.execute(arguments)
|
||||
if result.return_code != 0:
|
||||
fail("command failed: {}\n{}".format(" ".join(arguments), result.stderr))
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def _local_rust_repository_impl(repository_ctx):
|
||||
rustc = _required_path(repository_ctx, "rustc")
|
||||
rustdoc = _required_path(repository_ctx, "rustdoc")
|
||||
cargo = _required_path(repository_ctx, "cargo")
|
||||
cargo_clippy = _required_path(repository_ctx, "cargo-clippy")
|
||||
clippy_driver = _required_path(repository_ctx, "clippy-driver")
|
||||
rustfmt = _required_path(repository_ctx, "rustfmt")
|
||||
cc = _required_path(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 = "local_rust_toolchain",
|
||||
toolchain = ":rust_toolchain_impl",
|
||||
toolchain_type = "@rules_rust//rust:toolchain_type",
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
""".format(
|
||||
host_triple = host_triple,
|
||||
release = release,
|
||||
)
|
||||
|
||||
|
||||
local_rust_repository = repository_rule(
|
||||
implementation = _local_rust_repository_impl,
|
||||
local = True,
|
||||
environ = ["PATH"],
|
||||
)
|
||||
|
||||
|
||||
def _local_rust_impl(module_ctx):
|
||||
local_rust_repository(name = "local_rust_toolchain")
|
||||
|
||||
|
||||
local_rust = module_extension(
|
||||
implementation = _local_rust_impl,
|
||||
)
|
||||
Reference in New Issue
Block a user