Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create_universe no longer assumes the name of rust_repository repos #805

Merged
merged 7 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions crate_universe/bootstrap.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""A module for declaraing a repository for bootstrapping crate_universe"""

load("//crate_universe/private:util.bzl", "get_host_info")
load("//crate_universe/private:util.bzl", "get_cargo_and_rustc", "get_host_triple")
load("//rust:repositories.bzl", "DEFAULT_RUST_VERSION")
load("//rust/platform:triple_mappings.bzl", "system_to_binary_ext", "triple_to_system")

BOOTSTRAP_ENV_VAR = "RULES_RUST_CRATE_UNIVERSE_BOOTSTRAP"

Expand Down Expand Up @@ -35,16 +37,15 @@ def _crate_universe_resolver_bootstrapping_impl(repository_ctx):
repository_ctx.file("BUILD.bazel")
return

resolver_triple, toolchain_repo, extension = get_host_info(repository_ctx)

cargo_path = repository_ctx.path(Label(toolchain_repo + "//:bin/cargo" + extension))
rustc_path = repository_ctx.path(Label(toolchain_repo + "//:bin/rustc" + extension))
host_triple, _ = get_host_triple(repository_ctx)
tools = get_cargo_and_rustc(repository_ctx, host_triple)
extension = system_to_binary_ext(triple_to_system(host_triple))

repository_dir = repository_ctx.path(".")
resolver_path = repository_ctx.path("release/crate_universe_resolver" + extension)

args = [
cargo_path,
tools.cargo,
"build",
"--release",
"--locked",
Expand All @@ -58,7 +59,7 @@ def _crate_universe_resolver_bootstrapping_impl(repository_ctx):
result = repository_ctx.execute(
args,
environment = {
"RUSTC": str(rustc_path),
"RUSTC": str(tools.rustc),
},
quiet = False,
)
Expand Down Expand Up @@ -88,11 +89,26 @@ _crate_universe_resolver_bootstrapping = repository_rule(
allow_single_file = ["Cargo.toml"],
default = Label("//crate_universe:Cargo.toml"),
),
"iso_date": attr.string(
doc = "The iso_date of cargo binary the resolver should use. Note `version` must be `beta` or `nightly`",
UebelAndre marked this conversation as resolved.
Show resolved Hide resolved
),
"rust_toolchain_repository_template": attr.string(
doc = (
"The template to use for finding the host `rust_toolchain` repository. `{version}` (eg. '1.53.0'), " +
"`{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{system}` (eg. 'darwin'), and `{arch}` (eg. 'aarch64') " +
"will be replaced in the string if present."
),
default = "rust_{system}_{arch}",
),
"srcs": attr.label(
doc = "Souces to the crate_universe resolver",
allow_files = True,
default = Label("//crate_universe:resolver_srcs"),
),
"version": attr.string(
doc = "The version of cargo the resolver should use",
default = DEFAULT_RUST_VERSION,
),
},
environ = [BOOTSTRAP_ENV_VAR],
)
Expand Down
37 changes: 27 additions & 10 deletions crate_universe/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""A module defining the `crate_universe` rule"""

load("//crate_universe/private:defaults.bzl", "DEFAULT_SHA256_CHECKSUMS", "DEFAULT_URL_TEMPLATE")
load("//crate_universe/private:util.bzl", "get_host_info")
load("//rust:repositories.bzl", "DEFAULT_TOOLCHAIN_TRIPLES")
load("//crate_universe/private:util.bzl", "get_cargo_and_rustc", "get_host_triple")
load("//rust:repositories.bzl", "DEFAULT_RUST_VERSION", "DEFAULT_TOOLCHAIN_TRIPLES")
load("//rust/platform:triple_mappings.bzl", "system_to_binary_ext", "triple_to_system")
load(":bootstrap.bzl", "BOOTSTRAP_ENV_VAR")

DEFAULT_CRATE_REGISTRY_TEMPLATE = "https://crates.io/api/v1/crates/{crate}/{version}/download"
Expand Down Expand Up @@ -64,11 +65,9 @@ def _crate_universe_resolve_impl(repository_ctx):
- The user then calls defs.bzl%pinned_rust_install().
"""

# Get info about the current host's tool locations
resolver_triple, toolchain_repo, extension = get_host_info(repository_ctx)

cargo_path = repository_ctx.path(Label(toolchain_repo + "//:bin/cargo" + extension))
rustc_path = repository_ctx.path(Label(toolchain_repo + "//:bin/rustc" + extension))
host_triple, resolver_triple = get_host_triple(repository_ctx)
tools = get_cargo_and_rustc(repository_ctx, host_triple)
extension = system_to_binary_ext(triple_to_system(host_triple))

if BOOTSTRAP_ENV_VAR in repository_ctx.os.environ and not "RULES_RUST_CRATE_UNIVERSE_RESOLVER_URL_OVERRIDE" in repository_ctx.os.environ:
resolver_label = Label("@rules_rust_crate_universe_bootstrap//:release/crate_universe_resolver" + extension)
Expand Down Expand Up @@ -108,7 +107,7 @@ def _crate_universe_resolve_impl(repository_ctx):
overrides = repository_ctx.attr.overrides,
registry_template = repository_ctx.attr.crate_registry_template,
targets = repository_ctx.attr.supported_targets,
cargo_bin_path = cargo_path,
cargo_bin_path = tools.cargo,
)

input_path = "{name}.resolver_config.json".format(name = repository_ctx.attr.name)
Expand All @@ -135,8 +134,8 @@ def _crate_universe_resolve_impl(repository_ctx):
environment = {
# The resolver invokes `cargo metadata` which relies on `rustc` being on the $PATH
# See https://github.com/rust-lang/cargo/issues/8219
"CARGO": str(cargo_path),
"RUSTC": str(rustc_path),
"CARGO": str(tools.cargo),
"RUSTC": str(tools.rustc),
"RUST_LOG": "info",
},
quiet = False,
Expand Down Expand Up @@ -169,6 +168,9 @@ Environment Variables:
doc = "A template for where to download crates from for the default crate registry. This must contain `{version}` and `{crate}` templates.",
default = DEFAULT_CRATE_REGISTRY_TEMPLATE,
),
"iso_date": attr.string(
doc = "The iso_date of cargo binary the resolver should use. Note `version` must be `beta` or `nightly`",
UebelAndre marked this conversation as resolved.
Show resolved Hide resolved
),
"lockfile": attr.label(
doc = (
"The path to a file which stores pinned information about the generated dependency graph. " +
Expand Down Expand Up @@ -200,6 +202,17 @@ Environment Variables:
doc = "Dictionary of host_triple -> sha256 for resolver binary.",
default = DEFAULT_SHA256_CHECKSUMS,
),
"rust_toolchain_repository_template": attr.string(
doc = (
"The template to use for finding the host `rust_toolchain` repository. `{version}` (eg. '1.53.0'), " +
"`{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{system}` (eg. 'darwin'), and `{arch}` (eg. 'aarch64') " +
"will be replaced in the string if present."
),
default = "rust_{system}_{arch}",
),
"sha256s": attr.string_dict(
doc = "The sha256 checksum of the desired rust artifacts",
),
"supported_targets": attr.string_list(
doc = (
"A list of supported [platform triples](https://doc.rust-lang.org/nightly/rustc/platform-support.html) " +
Expand All @@ -208,6 +221,10 @@ Environment Variables:
allow_empty = False,
default = DEFAULT_TOOLCHAIN_TRIPLES.keys(),
),
"version": attr.string(
doc = "The version of cargo the resolver should use",
default = DEFAULT_RUST_VERSION,
),
},
environ = [
"REPIN",
Expand Down
64 changes: 53 additions & 11 deletions crate_universe/private/util.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
"""Utility functions for the crate_universe resolver"""

load(
"//rust/platform:triple_mappings.bzl",
"system_to_binary_ext",
"triple_to_arch",
"triple_to_system",
)

_CPU_ARCH_ERROR_MSG = """\
Command failed with exit code '{code}': {args}
----------stdout:
Expand Down Expand Up @@ -61,14 +68,14 @@ def _query_cpu_architecture(repository_ctx, expected_archs, is_windows = False):

return arch

def get_host_info(repository_ctx):
"""Query host information for the appropriate triple and toolchain repo name
def get_host_triple(repository_ctx):
"""Query host information for the appropriate triples for the crate_universe resolver

Args:
repository_ctx (repository_ctx): The rule's repository_ctx

Returns:
tuple: A tuple containing a triple (str) and repository name (str)
tuple: The host triple and resolver triple
"""

# Detect the host's cpu architecture
Expand All @@ -79,23 +86,58 @@ def get_host_info(repository_ctx):
"windows": ["x86_64"],
}

# The expected file extension of crate resolver binaries
extension = ""

if "linux" in repository_ctx.os.name:
cpu = _query_cpu_architecture(repository_ctx, supported_architectures["linux"])
host_triple = "{}-unknown-linux-gnu".format(cpu)
resolver_triple = "{}-unknown-linux-gnu".format(cpu)
toolchain_repo = "@rust_linux_{}".format(cpu)
elif "mac" in repository_ctx.os.name:
cpu = _query_cpu_architecture(repository_ctx, supported_architectures["macos"])
host_triple = "{}-apple-darwin".format(cpu)
resolver_triple = "{}-apple-darwin".format(cpu)
toolchain_repo = "@rust_darwin_{}".format(cpu)
elif "win" in repository_ctx.os.name:
cpu = _query_cpu_architecture(repository_ctx, supported_architectures["windows"], True)

# TODO: The resolver triple should be the same as the host but for the time being,
# the resolver is compiled with `-gnu` not `-msvc`.
host_triple = "{}-pc-windows-msvc".format(cpu)
resolver_triple = "{}-pc-windows-gnu".format(cpu)
toolchain_repo = "@rust_windows_{}".format(cpu)
extension = ".exe"
else:
fail("Could not locate resolver for OS " + repository_ctx.os.name)

return (resolver_triple, toolchain_repo, extension)
return (host_triple, resolver_triple)

def get_cargo_and_rustc(repository_ctx, host_triple):
"""Download a cargo and rustc binary based on the host triple.
UebelAndre marked this conversation as resolved.
Show resolved Hide resolved

Args:
repository_ctx (repository_ctx): The rule's context object
host_triple (str): The host's platform triple

Returns:
struct: A struct containing the expected tools
"""

if repository_ctx.attr.version in ("beta", "nightly"):
version_str = "{}-{}".format(repository_ctx.attr.version, repository_ctx.attr.iso_date)
else:
version_str = repository_ctx.attr.version

# Get info about the current host's tool locations
(host_triple, resolver_triple) = get_host_triple(repository_ctx)
system = triple_to_system(host_triple)
extension = system_to_binary_ext(system)
arch = triple_to_arch(host_triple)

rust_toolchain_repository = repository_ctx.attr.rust_toolchain_repository_template
rust_toolchain_repository = rust_toolchain_repository.replace("{version}", version_str)
rust_toolchain_repository = rust_toolchain_repository.replace("{system}", system)
rust_toolchain_repository = rust_toolchain_repository.replace("{triple}", host_triple)
rust_toolchain_repository = rust_toolchain_repository.replace("{arch}", arch)

cargo_path = repository_ctx.path(Label("@{}{}".format(rust_toolchain_repository, "//:bin/cargo" + extension)))
rustc_path = repository_ctx.path(Label("@{}{}".format(rust_toolchain_repository, "//:bin/rustc" + extension)))

return struct(
cargo = cargo_path,
rustc = rustc_path,
)
9 changes: 7 additions & 2 deletions docs/crate_universe.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
## crate_universe

<pre>
crate_universe(<a href="#crate_universe-name">name</a>, <a href="#crate_universe-cargo_toml_files">cargo_toml_files</a>, <a href="#crate_universe-crate_registry_template">crate_registry_template</a>, <a href="#crate_universe-lockfile">lockfile</a>, <a href="#crate_universe-overrides">overrides</a>, <a href="#crate_universe-packages">packages</a>,
<a href="#crate_universe-repo_mapping">repo_mapping</a>, <a href="#crate_universe-resolver_download_url_template">resolver_download_url_template</a>, <a href="#crate_universe-resolver_sha256s">resolver_sha256s</a>, <a href="#crate_universe-supported_targets">supported_targets</a>)
crate_universe(<a href="#crate_universe-name">name</a>, <a href="#crate_universe-cargo_toml_files">cargo_toml_files</a>, <a href="#crate_universe-crate_registry_template">crate_registry_template</a>, <a href="#crate_universe-iso_date">iso_date</a>, <a href="#crate_universe-lockfile">lockfile</a>, <a href="#crate_universe-overrides">overrides</a>,
<a href="#crate_universe-packages">packages</a>, <a href="#crate_universe-repo_mapping">repo_mapping</a>, <a href="#crate_universe-resolver_download_url_template">resolver_download_url_template</a>, <a href="#crate_universe-resolver_sha256s">resolver_sha256s</a>,
<a href="#crate_universe-rust_toolchain_repository_template">rust_toolchain_repository_template</a>, <a href="#crate_universe-sha256s">sha256s</a>, <a href="#crate_universe-supported_targets">supported_targets</a>, <a href="#crate_universe-version">version</a>)
</pre>

A rule for downloading Rust dependencies (crates).
Expand All @@ -33,13 +34,17 @@ Environment Variables:
| <a id="crate_universe-name"></a>name | A unique name for this repository. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
| <a id="crate_universe-cargo_toml_files"></a>cargo_toml_files | A list of Cargo manifests (<code>Cargo.toml</code> files). | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="crate_universe-crate_registry_template"></a>crate_registry_template | A template for where to download crates from for the default crate registry. This must contain <code>{version}</code> and <code>{crate}</code> templates. | String | optional | "https://crates.io/api/v1/crates/{crate}/{version}/download" |
| <a id="crate_universe-iso_date"></a>iso_date | The iso_date of cargo binary the resolver should use. Note <code>version</code> must be <code>beta</code> or <code>nightly</code> | String | optional | "" |
| <a id="crate_universe-lockfile"></a>lockfile | The path to a file which stores pinned information about the generated dependency graph. this target must be a file and will be updated by the repository rule when the <code>REPIN</code> environment variable is set. If this is not set, dependencies will be re-resolved more often, setting this allows caching resolves, but will error if the cache is stale. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
| <a id="crate_universe-overrides"></a>overrides | Mapping of crate name to specification overrides. See [crate.override](#crateoverride) for more details. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
| <a id="crate_universe-packages"></a>packages | A list of crate specifications. See [crate.spec](#cratespec) for more details. | List of strings | optional | [] |
| <a id="crate_universe-repo_mapping"></a>repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.&lt;p&gt;For example, an entry <code>"@foo": "@bar"</code> declares that, for any time this repository depends on <code>@foo</code> (such as a dependency on <code>@foo//some:target</code>, it should actually resolve that dependency within globally-declared <code>@bar</code> (<code>@bar//some:target</code>). | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | required | |
| <a id="crate_universe-resolver_download_url_template"></a>resolver_download_url_template | URL template from which to download the resolver binary. {host_triple} and {extension} will be filled in according to the host platform. | String | optional | "{host_triple}{extension}" |
| <a id="crate_universe-resolver_sha256s"></a>resolver_sha256s | Dictionary of host_triple -&gt; sha256 for resolver binary. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {"aarch64-apple-darwin": "{aarch64-apple-darwin--sha256}", "aarch64-unknown-linux-gnu": "{aarch64-unknown-linux-gnu--sha256}", "x86_64-apple-darwin": "{x86_64-apple-darwin--sha256}", "x86_64-pc-windows-gnu": "{x86_64-pc-windows-gnu--sha256}", "x86_64-unknown-linux-gnu": "{x86_64-unknown-linux-gnu--sha256}"} |
| <a id="crate_universe-rust_toolchain_repository_template"></a>rust_toolchain_repository_template | The template to use for finding the host <code>rust_toolchain</code> repository. <code>{version}</code> (eg. '1.53.0'), <code>{triple}</code> (eg. 'x86_64-unknown-linux-gnu'), <code>{system}</code> (eg. 'darwin'), and <code>{arch}</code> (eg. 'aarch64') will be replaced in the string if present. | String | optional | "rust_{system}_{arch}" |
| <a id="crate_universe-sha256s"></a>sha256s | The sha256 checksum of the desired rust artifacts | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
| <a id="crate_universe-supported_targets"></a>supported_targets | A list of supported [platform triples](https://doc.rust-lang.org/nightly/rustc/platform-support.html) to consider when resoliving dependencies. | List of strings | optional | ["aarch64-apple-darwin", "aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-msvc", "x86_64-unknown-freebsd", "x86_64-unknown-linux-gnu"] |
| <a id="crate_universe-version"></a>version | The version of cargo the resolver should use | String | optional | "1.53.0" |


<a id="#crate.spec"></a>
Expand Down
Loading