Skip to content

Commit

Permalink
Avoid downloading multiple resolvers (bazelbuild#694)
Browse files Browse the repository at this point in the history
Instead, download just the one that's needed for the host platform.
  • Loading branch information
illicitonion authored and yagehu committed Apr 23, 2021
1 parent fb66a4f commit 9bc3c8e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/crate_universe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ jobs:
cp ${{ github.workspace }}/crate_universe/private/defaults.bzl.template ${{ github.workspace }}/crate_universe/private/defaults.bzl
# Replace the url
url="$(echo $URL | sed 's|releases/tag/|releases/download/|')/{bin}"
url="$(echo $URL | sed 's|releases/tag/|releases/download/|')/crate_universe_resolver-{host_triple}{extension}"
sed -i "s|{DEFAULT_URL_TEMPLATE}|${url}|" ${{ github.workspace }}/crate_universe/private/defaults.bzl
# Populate all sha256 values
Expand Down
4 changes: 0 additions & 4 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ load("@rules_rust//rust:repositories.bzl", "rust_repositories")

rust_repositories()

load("@rules_rust//crate_universe:deps.bzl", "crate_universe_deps")

crate_universe_deps()

load("@rules_rust//proto:repositories.bzl", "rust_proto_repositories")

rust_proto_repositories()
Expand Down
46 changes: 19 additions & 27 deletions crate_universe/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""A module defining the `crate_universe` rule"""

load("//crate_universe/private:defaults.bzl", "DEFAULT_SHA256_CHECKSUMS", "DEFAULT_URL_TEMPLATE")
load("//rust:repositories.bzl", "DEFAULT_TOOLCHAIN_TRIPLES")
load(":deps.bzl", _crate_universe_deps = "crate_universe_deps")

DEFAULT_CRATE_REGISTRY_TEMPLATE = "https://crates.io/api/v1/crates/{crate}/{version}/download"

Expand Down Expand Up @@ -165,20 +165,17 @@ def _crate_universe_resolve_impl(repository_ctx):
if resolver_url.startswith("file://"):
sha256_result = repository_ctx.execute(["sha256sum", resolver_url[7:]])
resolver_sha = sha256_result.stdout[:64]

resolver_path = repository_ctx.path("resolver")
repository_ctx.download(
url = resolver_url,
sha256 = resolver_sha,
output = resolver_path,
executable = True,
)
else:
resolver_label = Label("@rules_rust_crate_universe__{}//file:resolver{}".format(
resolver_triple,
extension,
))
resolver_path = repository_ctx.path(resolver_label)
resolver_url = repository_ctx.attr.resolver_download_url_template.format(host_triple = resolver_triple, extension = extension)
resolver_sha = repository_ctx.attr.resolver_sha256s.get(resolver_triple, None)

resolver_path = repository_ctx.path("resolver")
repository_ctx.download(
url = resolver_url,
sha256 = resolver_sha,
output = resolver_path,
executable = True,
)

lockfile_path = None
if repository_ctx.attr.lockfile:
Expand Down Expand Up @@ -301,21 +298,19 @@ crate_universe(
doc = "",
allow_empty = True,
),
"resolver_download_url_template": attr.string(
doc = "URL template from which to download the resolver binary. {host_triple} and {extension} will be filled in according to the host platform.",
default = DEFAULT_URL_TEMPLATE,
),
"resolver_sha256s": attr.string_dict(
doc = "Dictionary of host_triple -> sha256 for resolver binary.",
default = DEFAULT_SHA256_CHECKSUMS,
),
"supported_targets": attr.string_list(
doc = "",
allow_empty = False,
default = DEFAULT_TOOLCHAIN_TRIPLES.keys(),
),
"_resolvers": attr.label_list(
doc = "A list of resolver binaries for various platforms",
default = [
"@rules_rust_crate_universe__aarch64-apple-darwin//:resolver",
"@rules_rust_crate_universe__aarch64-unknown-linux-gnu//:resolver",
"@rules_rust_crate_universe__x86_64-apple-darwin//:resolver",
"@rules_rust_crate_universe__x86_64-pc-windows-gnu//:resolver.exe",
"@rules_rust_crate_universe__x86_64-unknown-linux-gnu//:resolver",
],
),
},
environ = [
"RULES_RUST_CRATE_UNIVERSE_RESOLVER_URL_OVERRIDE",
Expand Down Expand Up @@ -369,6 +364,3 @@ crate = struct(
spec = _spec,
override = _override,
)

# Reexport the dependencies macro
crate_universe_deps = _crate_universe_deps
35 changes: 0 additions & 35 deletions crate_universe/deps.bzl

This file was deleted.

6 changes: 1 addition & 5 deletions examples/crate_universe/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ workspace(name = "rules_rust_examples_crate_universe")

local_repository(
name = "rules_rust",
path = "../../..",
path = "../..",
)

load("@rules_rust//rust:repositories.bzl", "rust_repositories")

rust_repositories()

load("@rules_rust//crate_universe:defs.bzl", "crate_universe_deps")

crate_universe_deps()

load("//basic:workspace.bzl", basic_deps = "deps")

basic_deps()
Expand Down

0 comments on commit 9bc3c8e

Please sign in to comment.