Skip to content

Commit c6c6610

Browse files
authored
Merge branch 'main' into workspace
2 parents 8b08cbb + 0d98d6b commit c6c6610

File tree

14 files changed

+653
-105
lines changed

14 files changed

+653
-105
lines changed

crate_universe/bootstrap.bzl

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""A module for declaraing a repository for bootstrapping crate_universe"""
22

3-
load("//crate_universe/private:util.bzl", "get_host_info")
3+
load("//crate_universe/private:util.bzl", "get_cargo_and_rustc", "get_host_triple")
4+
load("//rust:repositories.bzl", "DEFAULT_RUST_VERSION")
5+
load("//rust/platform:triple_mappings.bzl", "system_to_binary_ext", "triple_to_system")
46

57
BOOTSTRAP_ENV_VAR = "RULES_RUST_CRATE_UNIVERSE_BOOTSTRAP"
68

@@ -35,16 +37,15 @@ def _crate_universe_resolver_bootstrapping_impl(repository_ctx):
3537
repository_ctx.file("BUILD.bazel")
3638
return
3739

38-
resolver_triple, toolchain_repo, extension = get_host_info(repository_ctx)
39-
40-
cargo_path = repository_ctx.path(Label(toolchain_repo + "//:bin/cargo" + extension))
41-
rustc_path = repository_ctx.path(Label(toolchain_repo + "//:bin/rustc" + extension))
40+
host_triple, _ = get_host_triple(repository_ctx)
41+
tools = get_cargo_and_rustc(repository_ctx, host_triple)
42+
extension = system_to_binary_ext(triple_to_system(host_triple))
4243

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

4647
args = [
47-
cargo_path,
48+
tools.cargo,
4849
"build",
4950
"--release",
5051
"--locked",
@@ -58,7 +59,7 @@ def _crate_universe_resolver_bootstrapping_impl(repository_ctx):
5859
result = repository_ctx.execute(
5960
args,
6061
environment = {
61-
"RUSTC": str(rustc_path),
62+
"RUSTC": str(tools.rustc),
6263
},
6364
quiet = False,
6465
)
@@ -88,11 +89,26 @@ _crate_universe_resolver_bootstrapping = repository_rule(
8889
allow_single_file = ["Cargo.toml"],
8990
default = Label("//crate_universe:Cargo.toml"),
9091
),
92+
"iso_date": attr.string(
93+
doc = "The iso_date of cargo binary the resolver should use. Note: This can only be set if `version` is `beta` or `nightly`",
94+
),
95+
"rust_toolchain_repository_template": attr.string(
96+
doc = (
97+
"The template to use for finding the host `rust_toolchain` repository. `{version}` (eg. '1.53.0'), " +
98+
"`{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{system}` (eg. 'darwin'), and `{arch}` (eg. 'aarch64') " +
99+
"will be replaced in the string if present."
100+
),
101+
default = "rust_{system}_{arch}",
102+
),
91103
"srcs": attr.label(
92104
doc = "Souces to the crate_universe resolver",
93105
allow_files = True,
94106
default = Label("//crate_universe:resolver_srcs"),
95107
),
108+
"version": attr.string(
109+
doc = "The version of cargo the resolver should use",
110+
default = DEFAULT_RUST_VERSION,
111+
),
96112
},
97113
environ = [BOOTSTRAP_ENV_VAR],
98114
)

crate_universe/defs.bzl

+27-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""A module defining the `crate_universe` rule"""
22

33
load("//crate_universe/private:defaults.bzl", "DEFAULT_SHA256_CHECKSUMS", "DEFAULT_URL_TEMPLATE")
4-
load("//crate_universe/private:util.bzl", "get_host_info")
5-
load("//rust:repositories.bzl", "DEFAULT_TOOLCHAIN_TRIPLES")
4+
load("//crate_universe/private:util.bzl", "get_cargo_and_rustc", "get_host_triple")
5+
load("//rust:repositories.bzl", "DEFAULT_RUST_VERSION", "DEFAULT_TOOLCHAIN_TRIPLES")
6+
load("//rust/platform:triple_mappings.bzl", "system_to_binary_ext", "triple_to_system")
67
load(":bootstrap.bzl", "BOOTSTRAP_ENV_VAR")
78

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

67-
# Get info about the current host's tool locations
68-
resolver_triple, toolchain_repo, extension = get_host_info(repository_ctx)
69-
70-
cargo_path = repository_ctx.path(Label(toolchain_repo + "//:bin/cargo" + extension))
71-
rustc_path = repository_ctx.path(Label(toolchain_repo + "//:bin/rustc" + extension))
68+
host_triple, resolver_triple = get_host_triple(repository_ctx)
69+
tools = get_cargo_and_rustc(repository_ctx, host_triple)
70+
extension = system_to_binary_ext(triple_to_system(host_triple))
7271

7372
if BOOTSTRAP_ENV_VAR in repository_ctx.os.environ and not "RULES_RUST_CRATE_UNIVERSE_RESOLVER_URL_OVERRIDE" in repository_ctx.os.environ:
7473
resolver_label = Label("@rules_rust_crate_universe_bootstrap//:release/crate_universe_resolver" + extension)
@@ -108,7 +107,7 @@ def _crate_universe_resolve_impl(repository_ctx):
108107
overrides = repository_ctx.attr.overrides,
109108
registry_template = repository_ctx.attr.crate_registry_template,
110109
targets = repository_ctx.attr.supported_targets,
111-
cargo_bin_path = cargo_path,
110+
cargo_bin_path = tools.cargo,
112111
)
113112

114113
input_path = "{name}.resolver_config.json".format(name = repository_ctx.attr.name)
@@ -135,8 +134,8 @@ def _crate_universe_resolve_impl(repository_ctx):
135134
environment = {
136135
# The resolver invokes `cargo metadata` which relies on `rustc` being on the $PATH
137136
# See https://github.com/rust-lang/cargo/issues/8219
138-
"CARGO": str(cargo_path),
139-
"RUSTC": str(rustc_path),
137+
"CARGO": str(tools.cargo),
138+
"RUSTC": str(tools.rustc),
140139
"RUST_LOG": "info",
141140
},
142141
quiet = False,
@@ -169,6 +168,9 @@ Environment Variables:
169168
doc = "A template for where to download crates from for the default crate registry. This must contain `{version}` and `{crate}` templates.",
170169
default = DEFAULT_CRATE_REGISTRY_TEMPLATE,
171170
),
171+
"iso_date": attr.string(
172+
doc = "The iso_date of cargo binary the resolver should use. Note: This can only be set if `version` is `beta` or `nightly`",
173+
),
172174
"lockfile": attr.label(
173175
doc = (
174176
"The path to a file which stores pinned information about the generated dependency graph. " +
@@ -200,6 +202,17 @@ Environment Variables:
200202
doc = "Dictionary of host_triple -> sha256 for resolver binary.",
201203
default = DEFAULT_SHA256_CHECKSUMS,
202204
),
205+
"rust_toolchain_repository_template": attr.string(
206+
doc = (
207+
"The template to use for finding the host `rust_toolchain` repository. `{version}` (eg. '1.53.0'), " +
208+
"`{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{system}` (eg. 'darwin'), and `{arch}` (eg. 'aarch64') " +
209+
"will be replaced in the string if present."
210+
),
211+
default = "rust_{system}_{arch}",
212+
),
213+
"sha256s": attr.string_dict(
214+
doc = "The sha256 checksum of the desired rust artifacts",
215+
),
203216
"supported_targets": attr.string_list(
204217
doc = (
205218
"A list of supported [platform triples](https://doc.rust-lang.org/nightly/rustc/platform-support.html) " +
@@ -208,6 +221,10 @@ Environment Variables:
208221
allow_empty = False,
209222
default = DEFAULT_TOOLCHAIN_TRIPLES.keys(),
210223
),
224+
"version": attr.string(
225+
doc = "The version of cargo the resolver should use",
226+
default = DEFAULT_RUST_VERSION,
227+
),
211228
},
212229
environ = [
213230
"REPIN",

crate_universe/private/util.bzl

+53-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
"""Utility functions for the crate_universe resolver"""
22

3+
load(
4+
"//rust/platform:triple_mappings.bzl",
5+
"system_to_binary_ext",
6+
"triple_to_arch",
7+
"triple_to_system",
8+
)
9+
310
_CPU_ARCH_ERROR_MSG = """\
411
Command failed with exit code '{code}': {args}
512
----------stdout:
@@ -61,14 +68,14 @@ def _query_cpu_architecture(repository_ctx, expected_archs, is_windows = False):
6168

6269
return arch
6370

64-
def get_host_info(repository_ctx):
65-
"""Query host information for the appropriate triple and toolchain repo name
71+
def get_host_triple(repository_ctx):
72+
"""Query host information for the appropriate triples for the crate_universe resolver
6673
6774
Args:
6875
repository_ctx (repository_ctx): The rule's repository_ctx
6976
7077
Returns:
71-
tuple: A tuple containing a triple (str) and repository name (str)
78+
tuple: The host triple and resolver triple
7279
"""
7380

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

82-
# The expected file extension of crate resolver binaries
83-
extension = ""
84-
8589
if "linux" in repository_ctx.os.name:
8690
cpu = _query_cpu_architecture(repository_ctx, supported_architectures["linux"])
91+
host_triple = "{}-unknown-linux-gnu".format(cpu)
8792
resolver_triple = "{}-unknown-linux-gnu".format(cpu)
88-
toolchain_repo = "@rust_linux_{}".format(cpu)
8993
elif "mac" in repository_ctx.os.name:
9094
cpu = _query_cpu_architecture(repository_ctx, supported_architectures["macos"])
95+
host_triple = "{}-apple-darwin".format(cpu)
9196
resolver_triple = "{}-apple-darwin".format(cpu)
92-
toolchain_repo = "@rust_darwin_{}".format(cpu)
9397
elif "win" in repository_ctx.os.name:
9498
cpu = _query_cpu_architecture(repository_ctx, supported_architectures["windows"], True)
99+
100+
# TODO: The resolver triple should be the same as the host but for the time being,
101+
# the resolver is compiled with `-gnu` not `-msvc`.
102+
host_triple = "{}-pc-windows-msvc".format(cpu)
95103
resolver_triple = "{}-pc-windows-gnu".format(cpu)
96-
toolchain_repo = "@rust_windows_{}".format(cpu)
97-
extension = ".exe"
98104
else:
99105
fail("Could not locate resolver for OS " + repository_ctx.os.name)
100106

101-
return (resolver_triple, toolchain_repo, extension)
107+
return (host_triple, resolver_triple)
108+
109+
def get_cargo_and_rustc(repository_ctx, host_triple):
110+
"""Retrieve a cargo and rustc binary based on the host triple.
111+
112+
Args:
113+
repository_ctx (repository_ctx): The rule's context object
114+
host_triple (str): The host's platform triple
115+
116+
Returns:
117+
struct: A struct containing the expected tools
118+
"""
119+
120+
if repository_ctx.attr.version in ("beta", "nightly"):
121+
version_str = "{}-{}".format(repository_ctx.attr.version, repository_ctx.attr.iso_date)
122+
else:
123+
version_str = repository_ctx.attr.version
124+
125+
# Get info about the current host's tool locations
126+
(host_triple, resolver_triple) = get_host_triple(repository_ctx)
127+
system = triple_to_system(host_triple)
128+
extension = system_to_binary_ext(system)
129+
arch = triple_to_arch(host_triple)
130+
131+
rust_toolchain_repository = repository_ctx.attr.rust_toolchain_repository_template
132+
rust_toolchain_repository = rust_toolchain_repository.replace("{version}", version_str)
133+
rust_toolchain_repository = rust_toolchain_repository.replace("{system}", system)
134+
rust_toolchain_repository = rust_toolchain_repository.replace("{triple}", host_triple)
135+
rust_toolchain_repository = rust_toolchain_repository.replace("{arch}", arch)
136+
137+
cargo_path = repository_ctx.path(Label("@{}{}".format(rust_toolchain_repository, "//:bin/cargo" + extension)))
138+
rustc_path = repository_ctx.path(Label("@{}{}".format(rust_toolchain_repository, "//:bin/rustc" + extension)))
139+
140+
return struct(
141+
cargo = cargo_path,
142+
rustc = rustc_path,
143+
)

docs/crate_universe.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
## crate_universe
1010

1111
<pre>
12-
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>,
13-
<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>)
12+
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>,
13+
<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>,
14+
<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>)
1415
</pre>
1516

1617
A rule for downloading Rust dependencies (crates).
@@ -33,13 +34,17 @@ Environment Variables:
3334
| <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 | |
3435
| <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 | [] |
3536
| <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" |
37+
| <a id="crate_universe-iso_date"></a>iso_date | The iso_date of cargo binary the resolver should use. Note: This can only be set if <code>version</code> is <code>beta</code> or <code>nightly</code> | String | optional | "" |
3638
| <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 |
3739
| <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 | {} |
3840
| <a id="crate_universe-packages"></a>packages | A list of crate specifications. See [crate.spec](#cratespec) for more details. | List of strings | optional | [] |
3941
| <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 | |
4042
| <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}" |
4143
| <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}"} |
44+
| <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}" |
45+
| <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 | {} |
4246
| <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"] |
47+
| <a id="crate_universe-version"></a>version | The version of cargo the resolver should use | String | optional | "1.53.0" |
4348

4449

4550
<a id="#crate.spec"></a>

0 commit comments

Comments
 (0)