Skip to content

Commit

Permalink
Update rules_rust to use Bazel's toolchains feature. (#52)
Browse files Browse the repository at this point in the history
This allows rust targets to only depend on (and thus download) the
toolchains that are used, dynamically chosen based on the current target
platform.

Fixes #14.
  • Loading branch information
katre committed Sep 27, 2017
1 parent 9c8b8d9 commit 7b1ba1f
Show file tree
Hide file tree
Showing 4 changed files with 325 additions and 227 deletions.
48 changes: 5 additions & 43 deletions rust/BUILD
Original file line number Diff line number Diff line change
@@ -1,45 +1,7 @@
package(default_visibility = ["//visibility:public"])

exports_files(["rust.bzl"])

config_setting(
name = "darwin",
values = {"host_cpu": "darwin"},
)

config_setting(
name = "k8",
values = {"host_cpu": "k8"},
)

filegroup(
name = "rustc",
srcs = select({
":darwin": ["@rust_darwin_x86_64//:rustc"],
":k8": ["@rust_linux_x86_64//:rustc"],
}),
)

filegroup(
name = "rustc_lib",
srcs = select({
":darwin": ["@rust_darwin_x86_64//:rustc_lib"],
":k8": ["@rust_linux_x86_64//:rustc_lib"],
}),
)

filegroup(
name = "rustdoc",
srcs = select({
":darwin": ["@rust_darwin_x86_64//:rustdoc"],
":k8": ["@rust_linux_x86_64//:rustdoc"],
}),
)

filegroup(
name = "rust_lib",
srcs = select({
":darwin": ["@rust_darwin_x86_64//:rust_lib"],
":k8": ["@rust_linux_x86_64//:rust_lib"],
}),
)
exports_files([
"rust.bzl",
"toolchain.bzl",
"repositories.bzl",
])
62 changes: 62 additions & 0 deletions rust/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,58 @@ filegroup(
)
"""

# This defines the default toolchain separately from the actual repositories, so that the remote
# repositories will only be downloaded if they are actually used.
DEFAULT_TOOLCHAINS = """
load("@io_bazel_rules_rust//rust:toolchain.bzl", "rust_toolchain")
toolchain(
name = "rust-linux-x86_64",
exec_compatible_with = [
"@bazel_tools//platforms:linux",
"@bazel_tools//platforms:x86_64",
],
target_compatible_with = [
"@bazel_tools//platforms:linux",
"@bazel_tools//platforms:x86_64",
],
toolchain = ":rust-linux-x86_64_impl",
toolchain_type = "@io_bazel_rules_rust//rust:toolchain",
)
rust_toolchain(
name = "rust-linux-x86_64_impl",
rust_doc = "@rust_linux_x86_64//:rustdoc",
rust_lib = ["@rust_linux_x86_64//:rust_lib"],
rustc = "@rust_linux_x86_64//:rustc",
rustc_lib = ["@rust_linux_x86_64//:rustc_lib"],
visibility = ["//visibility:public"],
)
toolchain(
name = "rust-darwin-x86_64",
exec_compatible_with = [
"@bazel_tools//platforms:osx",
"@bazel_tools//platforms:x86_64",
],
target_compatible_with = [
"@bazel_tools//platforms:osx",
"@bazel_tools//platforms:x86_64",
],
toolchain = ":rust-darwin-x86_64_impl",
toolchain_type = "@io_bazel_rules_rust//rust:toolchain",
)
rust_toolchain(
name = "rust-darwin-x86_64_impl",
rust_doc = "@rust_darwin_x86_64//:rustdoc",
rust_lib = ["@rust_darwin_x86_64//:rust_lib"],
rustc = "@rust_darwin_x86_64//:rustc",
rustc_lib = ["@rust_darwin_x86_64//:rustc_lib"],
visibility = ["//visibility:public"],
)
"""

# Eventually with better toolchain hosting options we could load only one of these, not both.
def rust_repositories():
native.new_http_archive(
Expand All @@ -81,3 +133,13 @@ def rust_repositories():
sha256 = "38606e464b31a778ffa7d25d490a9ac53b472102bad8445b52e125f63726ac64",
build_file_content = RUST_DARWIN_BUILD_FILE,
)

native.new_local_repository(
name = "rust_default_toolchains",
path = ".",
build_file_content = DEFAULT_TOOLCHAINS)

# Register toolchains
native.register_toolchains(
"@rust_default_toolchains//:rust-linux-x86_64",
"@rust_default_toolchains//:rust-darwin-x86_64")
Loading

0 comments on commit 7b1ba1f

Please sign in to comment.