Skip to content

Commit

Permalink
Allow specifying exec and target compatibility constraints (bazelbuil…
Browse files Browse the repository at this point in the history
…d#1971)

Extra target triples may only be appropriate for specific platforms
(e.g. if you register a toolchain with the exec_triple
`x86_64-unknown-linux-gnu` with an extra target triple of
`x86_64-unknown-linux-musl`, you may want differing
`target_compatible_with` values for each of those two target triples.

This allows for specifying explicit `exec_compatible_with` lists per
toolchain (which was already supported by `rust_toolchain_repository`
just not proxied by `rust_repository_set`), and explicit
`target_compatible_with` lists, both for the default triple and for
extra ones.

I'm not 100% sure on our pre-existing assumption that every toolchain
supports compiling targeting its own exec platform, i.e. the
'`[exec_triple]`' part of https:/bazelbuild/rules_rust/blob/937e63399b111a6d7ee53b187e4d113300b089e9/rust/repositories.bzl#L845
but it _does_ generally hold, and supplying a way of removing that
default assumption is definitely a separate PR for another day if it
becomes relevant to anyone.
  • Loading branch information
illicitonion authored and mir-cmg committed Jul 10, 2023
1 parent 4333280 commit d7f2635
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
6 changes: 4 additions & 2 deletions docs/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -1853,7 +1853,7 @@ rust_repositories(<a href="#rust_repositories-kwargs">kwargs</a>)
rust_repository_set(<a href="#rust_repository_set-name">name</a>, <a href="#rust_repository_set-exec_triple">exec_triple</a>, <a href="#rust_repository_set-target_settings">target_settings</a>, <a href="#rust_repository_set-version">version</a>, <a href="#rust_repository_set-versions">versions</a>, <a href="#rust_repository_set-allocator_library">allocator_library</a>,
<a href="#rust_repository_set-global_allocator_library">global_allocator_library</a>, <a href="#rust_repository_set-extra_target_triples">extra_target_triples</a>, <a href="#rust_repository_set-iso_date">iso_date</a>, <a href="#rust_repository_set-rustfmt_version">rustfmt_version</a>,
<a href="#rust_repository_set-edition">edition</a>, <a href="#rust_repository_set-dev_components">dev_components</a>, <a href="#rust_repository_set-extra_rustc_flags">extra_rustc_flags</a>, <a href="#rust_repository_set-extra_exec_rustc_flags">extra_exec_rustc_flags</a>, <a href="#rust_repository_set-sha256s">sha256s</a>, <a href="#rust_repository_set-urls">urls</a>,
<a href="#rust_repository_set-auth">auth</a>, <a href="#rust_repository_set-register_toolchain">register_toolchain</a>)
<a href="#rust_repository_set-auth">auth</a>, <a href="#rust_repository_set-register_toolchain">register_toolchain</a>, <a href="#rust_repository_set-exec_compatible_with">exec_compatible_with</a>, <a href="#rust_repository_set-default_target_compatible_with">default_target_compatible_with</a>)
</pre>

Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains.
Expand All @@ -1870,7 +1870,7 @@ Assembles a remote repository for the given toolchain params, produces a proxy r
| <a id="rust_repository_set-versions"></a>versions | A list of toolchain versions to download. This paramter only accepts one versions per channel. E.g. <code>["1.65.0", "nightly/2022-11-02", "beta/2020-12-30"]</code>. | `[]` |
| <a id="rust_repository_set-allocator_library"></a>allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | `None` |
| <a id="rust_repository_set-global_allocator_library"></a>global_allocator_library | Target that provides allocator functions a global allocator is used with cc_common.link. | `None` |
| <a id="rust_repository_set-extra_target_triples"></a>extra_target_triples | Additional rust-style targets that this set of toolchains should support. | `[]` |
| <a id="rust_repository_set-extra_target_triples"></a>extra_target_triples | Additional rust-style targets that this set of toolchains should support. If a map, values should be (optional) target_compatible_with lists for that particular target triple. | `{}` |
| <a id="rust_repository_set-iso_date"></a>iso_date | The date of the tool. | `None` |
| <a id="rust_repository_set-rustfmt_version"></a>rustfmt_version | The version of rustfmt to be associated with the toolchain. | `None` |
| <a id="rust_repository_set-edition"></a>edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its <code>edition</code> attribute. | `None` |
Expand All @@ -1881,6 +1881,8 @@ Assembles a remote repository for the given toolchain params, produces a proxy r
| <a id="rust_repository_set-urls"></a>urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). | `["https://static.rust-lang.org/dist/{}.tar.gz"]` |
| <a id="rust_repository_set-auth"></a>auth | Auth object compatible with repository_ctx.download to use when downloading files. See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. | `None` |
| <a id="rust_repository_set-register_toolchain"></a>register_toolchain | If True, the generated <code>rust_toolchain</code> target will become a registered toolchain. | `True` |
| <a id="rust_repository_set-exec_compatible_with"></a>exec_compatible_with | A list of constraints for the execution platform for this toolchain. | `None` |
| <a id="rust_repository_set-default_target_compatible_with"></a>default_target_compatible_with | A list of constraints for the target platform for this toolchain when the exec platform is the same as the target platform. | `None` |


<a id="rust_test_suite"></a>
Expand Down
6 changes: 4 additions & 2 deletions docs/rust_repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ rust_repositories(<a href="#rust_repositories-kwargs">kwargs</a>)
rust_repository_set(<a href="#rust_repository_set-name">name</a>, <a href="#rust_repository_set-exec_triple">exec_triple</a>, <a href="#rust_repository_set-target_settings">target_settings</a>, <a href="#rust_repository_set-version">version</a>, <a href="#rust_repository_set-versions">versions</a>, <a href="#rust_repository_set-allocator_library">allocator_library</a>,
<a href="#rust_repository_set-global_allocator_library">global_allocator_library</a>, <a href="#rust_repository_set-extra_target_triples">extra_target_triples</a>, <a href="#rust_repository_set-iso_date">iso_date</a>, <a href="#rust_repository_set-rustfmt_version">rustfmt_version</a>,
<a href="#rust_repository_set-edition">edition</a>, <a href="#rust_repository_set-dev_components">dev_components</a>, <a href="#rust_repository_set-extra_rustc_flags">extra_rustc_flags</a>, <a href="#rust_repository_set-extra_exec_rustc_flags">extra_exec_rustc_flags</a>, <a href="#rust_repository_set-sha256s">sha256s</a>, <a href="#rust_repository_set-urls">urls</a>,
<a href="#rust_repository_set-auth">auth</a>, <a href="#rust_repository_set-register_toolchain">register_toolchain</a>)
<a href="#rust_repository_set-auth">auth</a>, <a href="#rust_repository_set-register_toolchain">register_toolchain</a>, <a href="#rust_repository_set-exec_compatible_with">exec_compatible_with</a>, <a href="#rust_repository_set-default_target_compatible_with">default_target_compatible_with</a>)
</pre>

Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains.
Expand All @@ -320,7 +320,7 @@ Assembles a remote repository for the given toolchain params, produces a proxy r
| <a id="rust_repository_set-versions"></a>versions | A list of toolchain versions to download. This paramter only accepts one versions per channel. E.g. <code>["1.65.0", "nightly/2022-11-02", "beta/2020-12-30"]</code>. | `[]` |
| <a id="rust_repository_set-allocator_library"></a>allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | `None` |
| <a id="rust_repository_set-global_allocator_library"></a>global_allocator_library | Target that provides allocator functions a global allocator is used with cc_common.link. | `None` |
| <a id="rust_repository_set-extra_target_triples"></a>extra_target_triples | Additional rust-style targets that this set of toolchains should support. | `[]` |
| <a id="rust_repository_set-extra_target_triples"></a>extra_target_triples | Additional rust-style targets that this set of toolchains should support. If a map, values should be (optional) target_compatible_with lists for that particular target triple. | `{}` |
| <a id="rust_repository_set-iso_date"></a>iso_date | The date of the tool. | `None` |
| <a id="rust_repository_set-rustfmt_version"></a>rustfmt_version | The version of rustfmt to be associated with the toolchain. | `None` |
| <a id="rust_repository_set-edition"></a>edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its <code>edition</code> attribute. | `None` |
Expand All @@ -331,6 +331,8 @@ Assembles a remote repository for the given toolchain params, produces a proxy r
| <a id="rust_repository_set-urls"></a>urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). | `["https://static.rust-lang.org/dist/{}.tar.gz"]` |
| <a id="rust_repository_set-auth"></a>auth | Auth object compatible with repository_ctx.download to use when downloading files. See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. | `None` |
| <a id="rust_repository_set-register_toolchain"></a>register_toolchain | If True, the generated <code>rust_toolchain</code> target will become a registered toolchain. | `True` |
| <a id="rust_repository_set-exec_compatible_with"></a>exec_compatible_with | A list of constraints for the execution platform for this toolchain. | `None` |
| <a id="rust_repository_set-default_target_compatible_with"></a>default_target_compatible_with | A list of constraints for the target platform for this toolchain when the exec platform is the same as the target platform. | `None` |


<a id="rust_toolchain_repository"></a>
Expand Down
28 changes: 23 additions & 5 deletions rust/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ def rust_repository_set(
versions = [],
allocator_library = None,
global_allocator_library = None,
extra_target_triples = [],
extra_target_triples = {},
iso_date = None,
rustfmt_version = None,
edition = None,
Expand All @@ -891,7 +891,9 @@ def rust_repository_set(
sha256s = None,
urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
auth = None,
register_toolchain = True):
register_toolchain = True,
exec_compatible_with = None,
default_target_compatible_with = None):
"""Assembles a remote repository for the given toolchain params, produces a proxy repository \
to contain the toolchain declaration, and registers the toolchains.
Expand All @@ -905,8 +907,8 @@ def rust_repository_set(
allocator_library (str, optional): Target that provides allocator functions when rust_library targets are
embedded in a cc_binary.
global_allocator_library (str, optional): Target that provides allocator functions a global allocator is used with cc_common.link.
extra_target_triples (list, optional): Additional rust-style targets that this set of
toolchains should support.
extra_target_triples (list or map, optional): Additional rust-style targets that this set of
toolchains should support. If a map, values should be (optional) target_compatible_with lists for that particular target triple.
iso_date (str, optional): The date of the tool.
rustfmt_version (str, optional): The version of rustfmt to be associated with the
toolchain.
Expand All @@ -923,6 +925,8 @@ def rust_repository_set(
auth (dict): Auth object compatible with repository_ctx.download to use when downloading files.
See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details.
register_toolchain (bool): If True, the generated `rust_toolchain` target will become a registered toolchain.
exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain.
default_target_compatible_with (list, optional): A list of constraints for the target platform for this toolchain when the exec platform is the same as the target platform.
"""

if version and versions:
Expand All @@ -942,8 +946,20 @@ def rust_repository_set(
if version and not versions:
versions = [version]

# extra_target_triples may be a dict or list - make a list we can pass to _get_toolchain_repositories
extra_target_triples_list = []
for extra_target_triple in extra_target_triples:
extra_target_triples_list.append(extra_target_triple)

all_toolchain_names = []
for toolchain in _get_toolchain_repositories(name, exec_triple, extra_target_triples, versions, iso_date):
for toolchain in _get_toolchain_repositories(name, exec_triple, extra_target_triples_list, versions, iso_date):
target_compatible_with = None
if toolchain.target_triple == exec_triple:
# The exec triple implicitly gets a toolchain with itself as a target - use default_target_compatible_with for it
target_compatible_with = default_target_compatible_with
elif type(extra_target_triples) == "dict":
target_compatible_with = extra_target_triples.get(toolchain.target_triple)

all_toolchain_names.append(rust_toolchain_repository(
name = toolchain.name,
allocator_library = allocator_library,
Expand All @@ -962,6 +978,8 @@ def rust_repository_set(
target_triple = toolchain.target_triple,
urls = urls,
version = toolchain.channel.version,
exec_compatible_with = exec_compatible_with,
target_compatible_with = target_compatible_with,
))

# This repository exists to allow `rust_repository_set` to work with the `maybe` wrapper.
Expand Down

0 comments on commit d7f2635

Please sign in to comment.