Skip to content

Commit 4aaa6de

Browse files
authored
Allow specifying exec and target compatibility constraints (#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://github.com/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.
1 parent f1b19c3 commit 4aaa6de

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

docs/flatten.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,7 +1851,7 @@ rust_repositories(<a href="#rust_repositories-kwargs">kwargs</a>)
18511851
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>,
18521852
<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>,
18531853
<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>,
1854-
<a href="#rust_repository_set-auth">auth</a>, <a href="#rust_repository_set-register_toolchain">register_toolchain</a>)
1854+
<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>)
18551855
</pre>
18561856

18571857
Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains.
@@ -1868,7 +1868,7 @@ Assembles a remote repository for the given toolchain params, produces a proxy r
18681868
| <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>. | `[]` |
18691869
| <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` |
18701870
| <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` |
1871-
| <a id="rust_repository_set-extra_target_triples"></a>extra_target_triples | Additional rust-style targets that this set of toolchains should support. | `[]` |
1871+
| <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. | `{}` |
18721872
| <a id="rust_repository_set-iso_date"></a>iso_date | The date of the tool. | `None` |
18731873
| <a id="rust_repository_set-rustfmt_version"></a>rustfmt_version | The version of rustfmt to be associated with the toolchain. | `None` |
18741874
| <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` |
@@ -1879,6 +1879,8 @@ Assembles a remote repository for the given toolchain params, produces a proxy r
18791879
| <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"]` |
18801880
| <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` |
18811881
| <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` |
1882+
| <a id="rust_repository_set-exec_compatible_with"></a>exec_compatible_with | A list of constraints for the execution platform for this toolchain. | `None` |
1883+
| <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` |
18821884

18831885

18841886
<a id="rust_test_suite"></a>

docs/rust_repositories.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ rust_repositories(<a href="#rust_repositories-kwargs">kwargs</a>)
303303
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>,
304304
<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>,
305305
<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>,
306-
<a href="#rust_repository_set-auth">auth</a>, <a href="#rust_repository_set-register_toolchain">register_toolchain</a>)
306+
<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>)
307307
</pre>
308308

309309
Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains.
@@ -320,7 +320,7 @@ Assembles a remote repository for the given toolchain params, produces a proxy r
320320
| <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>. | `[]` |
321321
| <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` |
322322
| <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` |
323-
| <a id="rust_repository_set-extra_target_triples"></a>extra_target_triples | Additional rust-style targets that this set of toolchains should support. | `[]` |
323+
| <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. | `{}` |
324324
| <a id="rust_repository_set-iso_date"></a>iso_date | The date of the tool. | `None` |
325325
| <a id="rust_repository_set-rustfmt_version"></a>rustfmt_version | The version of rustfmt to be associated with the toolchain. | `None` |
326326
| <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` |
@@ -331,6 +331,8 @@ Assembles a remote repository for the given toolchain params, produces a proxy r
331331
| <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"]` |
332332
| <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` |
333333
| <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` |
334+
| <a id="rust_repository_set-exec_compatible_with"></a>exec_compatible_with | A list of constraints for the execution platform for this toolchain. | `None` |
335+
| <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` |
334336

335337

336338
<a id="rust_toolchain_repository"></a>

rust/repositories.bzl

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ def rust_repository_set(
881881
versions = [],
882882
allocator_library = None,
883883
global_allocator_library = None,
884-
extra_target_triples = [],
884+
extra_target_triples = {},
885885
iso_date = None,
886886
rustfmt_version = None,
887887
edition = None,
@@ -891,7 +891,9 @@ def rust_repository_set(
891891
sha256s = None,
892892
urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
893893
auth = None,
894-
register_toolchain = True):
894+
register_toolchain = True,
895+
exec_compatible_with = None,
896+
default_target_compatible_with = None):
895897
"""Assembles a remote repository for the given toolchain params, produces a proxy repository \
896898
to contain the toolchain declaration, and registers the toolchains.
897899
@@ -905,8 +907,8 @@ def rust_repository_set(
905907
allocator_library (str, optional): Target that provides allocator functions when rust_library targets are
906908
embedded in a cc_binary.
907909
global_allocator_library (str, optional): Target that provides allocator functions a global allocator is used with cc_common.link.
908-
extra_target_triples (list, optional): Additional rust-style targets that this set of
909-
toolchains should support.
910+
extra_target_triples (list or map, optional): Additional rust-style targets that this set of
911+
toolchains should support. If a map, values should be (optional) target_compatible_with lists for that particular target triple.
910912
iso_date (str, optional): The date of the tool.
911913
rustfmt_version (str, optional): The version of rustfmt to be associated with the
912914
toolchain.
@@ -923,6 +925,8 @@ def rust_repository_set(
923925
auth (dict): Auth object compatible with repository_ctx.download to use when downloading files.
924926
See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details.
925927
register_toolchain (bool): If True, the generated `rust_toolchain` target will become a registered toolchain.
928+
exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain.
929+
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.
926930
"""
927931

928932
if version and versions:
@@ -942,8 +946,20 @@ def rust_repository_set(
942946
if version and not versions:
943947
versions = [version]
944948

949+
# extra_target_triples may be a dict or list - make a list we can pass to _get_toolchain_repositories
950+
extra_target_triples_list = []
951+
for extra_target_triple in extra_target_triples:
952+
extra_target_triples_list.append(extra_target_triple)
953+
945954
all_toolchain_names = []
946-
for toolchain in _get_toolchain_repositories(name, exec_triple, extra_target_triples, versions, iso_date):
955+
for toolchain in _get_toolchain_repositories(name, exec_triple, extra_target_triples_list, versions, iso_date):
956+
target_compatible_with = None
957+
if toolchain.target_triple == exec_triple:
958+
# The exec triple implicitly gets a toolchain with itself as a target - use default_target_compatible_with for it
959+
target_compatible_with = default_target_compatible_with
960+
elif type(extra_target_triples) == "dict":
961+
target_compatible_with = extra_target_triples.get(toolchain.target_triple)
962+
947963
all_toolchain_names.append(rust_toolchain_repository(
948964
name = toolchain.name,
949965
allocator_library = allocator_library,
@@ -962,6 +978,8 @@ def rust_repository_set(
962978
target_triple = toolchain.target_triple,
963979
urls = urls,
964980
version = toolchain.channel.version,
981+
exec_compatible_with = exec_compatible_with,
982+
target_compatible_with = target_compatible_with,
965983
))
966984

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

0 commit comments

Comments
 (0)