Skip to content

Commit

Permalink
Support repository_set from bzlmod (#3106)
Browse files Browse the repository at this point in the history
  • Loading branch information
illicitonion authored Dec 16, 2024
1 parent 3aecdbe commit c263575
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 186 deletions.
12 changes: 10 additions & 2 deletions docs/src/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,8 @@ str: The name of a registerable rust_analyzer_toolchain.
rust_register_toolchains(<a href="#rust_register_toolchains-dev_components">dev_components</a>, <a href="#rust_register_toolchains-edition">edition</a>, <a href="#rust_register_toolchains-allocator_library">allocator_library</a>, <a href="#rust_register_toolchains-global_allocator_library">global_allocator_library</a>,
<a href="#rust_register_toolchains-register_toolchains">register_toolchains</a>, <a href="#rust_register_toolchains-rustfmt_version">rustfmt_version</a>, <a href="#rust_register_toolchains-rust_analyzer_version">rust_analyzer_version</a>, <a href="#rust_register_toolchains-sha256s">sha256s</a>,
<a href="#rust_register_toolchains-extra_target_triples">extra_target_triples</a>, <a href="#rust_register_toolchains-extra_rustc_flags">extra_rustc_flags</a>, <a href="#rust_register_toolchains-extra_exec_rustc_flags">extra_exec_rustc_flags</a>, <a href="#rust_register_toolchains-urls">urls</a>,
<a href="#rust_register_toolchains-versions">versions</a>, <a href="#rust_register_toolchains-aliases">aliases</a>, <a href="#rust_register_toolchains-hub_name">hub_name</a>, <a href="#rust_register_toolchains-compact_windows_names">compact_windows_names</a>)
<a href="#rust_register_toolchains-versions">versions</a>, <a href="#rust_register_toolchains-aliases">aliases</a>, <a href="#rust_register_toolchains-hub_name">hub_name</a>, <a href="#rust_register_toolchains-compact_windows_names">compact_windows_names</a>, <a href="#rust_register_toolchains-toolchain_triples">toolchain_triples</a>,
<a href="#rust_register_toolchains-rustfmt_toolchain_triples">rustfmt_toolchain_triples</a>, <a href="#rust_register_toolchains-extra_toolchain_infos">extra_toolchain_infos</a>)
</pre>

Emits a default set of toolchains for Linux, MacOS, and Freebsd
Expand Down Expand Up @@ -1404,6 +1405,9 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai
| <a id="rust_register_toolchains-aliases"></a>aliases | A mapping of "full" repository name to another name to use instead. | `{}` |
| <a id="rust_register_toolchains-hub_name"></a>hub_name | The name of the bzlmod hub repository for toolchains. | `None` |
| <a id="rust_register_toolchains-compact_windows_names"></a>compact_windows_names | Whether or not to produce compact repository names for windows toolchains. This is to avoid MAX_PATH issues. | `True` |
| <a id="rust_register_toolchains-toolchain_triples"></a>toolchain_triples | Mapping of rust target triple -> repository name to create. | `{"aarch64-apple-darwin": "rust_darwin_aarch64", "aarch64-pc-windows-msvc": "rust_windows_aarch64", "aarch64-unknown-linux-gnu": "rust_linux_aarch64", "s390x-unknown-linux-gnu": "rust_linux_s390x", "x86_64-apple-darwin": "rust_darwin_x86_64", "x86_64-pc-windows-msvc": "rust_windows_x86_64", "x86_64-unknown-freebsd": "rust_freebsd_x86_64", "x86_64-unknown-linux-gnu": "rust_linux_x86_64"}` |
| <a id="rust_register_toolchains-rustfmt_toolchain_triples"></a>rustfmt_toolchain_triples | Like toolchain_triples, but for rustfmt toolchains. | `{"aarch64-apple-darwin": "rust_darwin_aarch64", "aarch64-pc-windows-msvc": "rust_windows_aarch64", "aarch64-unknown-linux-gnu": "rust_linux_aarch64", "s390x-unknown-linux-gnu": "rust_linux_s390x", "x86_64-apple-darwin": "rust_darwin_x86_64", "x86_64-pc-windows-msvc": "rust_windows_x86_64", "x86_64-unknown-freebsd": "rust_freebsd_x86_64", "x86_64-unknown-linux-gnu": "rust_linux_x86_64"}` |
| <a id="rust_register_toolchains-extra_toolchain_infos"></a>extra_toolchain_infos | (dict[str, dict], optional): Mapping of information about extra toolchains which were created outside of this call, which should be added to the hub repo. | `None` |


<a id="rust_repositories"></a>
Expand Down Expand Up @@ -1467,6 +1471,10 @@ Assembles a remote repository for the given toolchain params, produces a proxy r
| <a id="rust_repository_set-aliases"></a>aliases | Replacement names to use for toolchains created by this macro. | `{}` |
| <a id="rust_repository_set-compact_windows_names"></a>compact_windows_names | Whether or not to produce compact repository names for windows toolchains. This is to avoid MAX_PATH issues. | `True` |

**RETURNS**

dict[str, dict]: A dict of informations about all generated toolchains.


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

Expand Down Expand Up @@ -1579,7 +1587,7 @@ Assembles a remote repository for the given toolchain params, produces a proxy r

**RETURNS**

str: The name of the registerable toolchain created by this rule.
dict[str, str]: Information about the registerable toolchain created by this rule.


<a id="rust_analyzer_aspect"></a>
Expand Down
12 changes: 10 additions & 2 deletions docs/src/rust_repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ str: The name of a registerable rust_analyzer_toolchain.
rust_register_toolchains(<a href="#rust_register_toolchains-dev_components">dev_components</a>, <a href="#rust_register_toolchains-edition">edition</a>, <a href="#rust_register_toolchains-allocator_library">allocator_library</a>, <a href="#rust_register_toolchains-global_allocator_library">global_allocator_library</a>,
<a href="#rust_register_toolchains-register_toolchains">register_toolchains</a>, <a href="#rust_register_toolchains-rustfmt_version">rustfmt_version</a>, <a href="#rust_register_toolchains-rust_analyzer_version">rust_analyzer_version</a>, <a href="#rust_register_toolchains-sha256s">sha256s</a>,
<a href="#rust_register_toolchains-extra_target_triples">extra_target_triples</a>, <a href="#rust_register_toolchains-extra_rustc_flags">extra_rustc_flags</a>, <a href="#rust_register_toolchains-extra_exec_rustc_flags">extra_exec_rustc_flags</a>, <a href="#rust_register_toolchains-urls">urls</a>,
<a href="#rust_register_toolchains-versions">versions</a>, <a href="#rust_register_toolchains-aliases">aliases</a>, <a href="#rust_register_toolchains-hub_name">hub_name</a>, <a href="#rust_register_toolchains-compact_windows_names">compact_windows_names</a>)
<a href="#rust_register_toolchains-versions">versions</a>, <a href="#rust_register_toolchains-aliases">aliases</a>, <a href="#rust_register_toolchains-hub_name">hub_name</a>, <a href="#rust_register_toolchains-compact_windows_names">compact_windows_names</a>, <a href="#rust_register_toolchains-toolchain_triples">toolchain_triples</a>,
<a href="#rust_register_toolchains-rustfmt_toolchain_triples">rustfmt_toolchain_triples</a>, <a href="#rust_register_toolchains-extra_toolchain_infos">extra_toolchain_infos</a>)
</pre>

Emits a default set of toolchains for Linux, MacOS, and Freebsd
Expand Down Expand Up @@ -218,6 +219,9 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai
| <a id="rust_register_toolchains-aliases"></a>aliases | A mapping of "full" repository name to another name to use instead. | `{}` |
| <a id="rust_register_toolchains-hub_name"></a>hub_name | The name of the bzlmod hub repository for toolchains. | `None` |
| <a id="rust_register_toolchains-compact_windows_names"></a>compact_windows_names | Whether or not to produce compact repository names for windows toolchains. This is to avoid MAX_PATH issues. | `True` |
| <a id="rust_register_toolchains-toolchain_triples"></a>toolchain_triples | Mapping of rust target triple -> repository name to create. | `{"aarch64-apple-darwin": "rust_darwin_aarch64", "aarch64-pc-windows-msvc": "rust_windows_aarch64", "aarch64-unknown-linux-gnu": "rust_linux_aarch64", "s390x-unknown-linux-gnu": "rust_linux_s390x", "x86_64-apple-darwin": "rust_darwin_x86_64", "x86_64-pc-windows-msvc": "rust_windows_x86_64", "x86_64-unknown-freebsd": "rust_freebsd_x86_64", "x86_64-unknown-linux-gnu": "rust_linux_x86_64"}` |
| <a id="rust_register_toolchains-rustfmt_toolchain_triples"></a>rustfmt_toolchain_triples | Like toolchain_triples, but for rustfmt toolchains. | `{"aarch64-apple-darwin": "rust_darwin_aarch64", "aarch64-pc-windows-msvc": "rust_windows_aarch64", "aarch64-unknown-linux-gnu": "rust_linux_aarch64", "s390x-unknown-linux-gnu": "rust_linux_s390x", "x86_64-apple-darwin": "rust_darwin_x86_64", "x86_64-pc-windows-msvc": "rust_windows_x86_64", "x86_64-unknown-freebsd": "rust_freebsd_x86_64", "x86_64-unknown-linux-gnu": "rust_linux_x86_64"}` |
| <a id="rust_register_toolchains-extra_toolchain_infos"></a>extra_toolchain_infos | (dict[str, dict], optional): Mapping of information about extra toolchains which were created outside of this call, which should be added to the hub repo. | `None` |


<a id="rust_repositories"></a>
Expand Down Expand Up @@ -281,6 +285,10 @@ Assembles a remote repository for the given toolchain params, produces a proxy r
| <a id="rust_repository_set-aliases"></a>aliases | Replacement names to use for toolchains created by this macro. | `{}` |
| <a id="rust_repository_set-compact_windows_names"></a>compact_windows_names | Whether or not to produce compact repository names for windows toolchains. This is to avoid MAX_PATH issues. | `True` |

**RETURNS**

dict[str, dict]: A dict of informations about all generated toolchains.


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

Expand Down Expand Up @@ -325,7 +333,7 @@ Assembles a remote repository for the given toolchain params, produces a proxy r

**RETURNS**

str: The name of the registerable toolchain created by this rule.
dict[str, str]: Information about the registerable toolchain created by this rule.


<a id="rust_toolchain_repository_proxy"></a>
Expand Down
143 changes: 137 additions & 6 deletions examples/musl_cross_compiling/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,137 @@
###############################################################################
# Bazel now uses Bzlmod by default to manage external dependencies.
# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel.
#
# For more details, please check https://github.com/bazelbuild/bazel/issues/18958
###############################################################################
module(
name = "musl_cross_compiling_example",
version = "0.0.0",
)

bazel_dep(
name = "rules_rust",
version = "0.0.0",
)
local_path_override(
module_name = "rules_rust",
path = "../..",
)

bazel_dep(
name = "platforms",
version = "0.0.10",
)
bazel_dep(
name = "rules_shell",
version = "0.3.0",
)

RUST_EDITION = "2021"

RUST_VERSION = "1.80.0"

rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(
edition = RUST_EDITION,
versions = [RUST_VERSION],
)

# This overrides a default rust_repository_set created by rust_register_toolchain.
# It must be named exactly this.
# Each exec triple needs one of these calls per target triple it supports.
# The first call needs all of the attrs, the subsequent calls should only set name, target_triple, and target_compatible_with.
rust.repository_set(
name = "rust_linux_x86_64",
edition = RUST_EDITION,
exec_triple = "x86_64-unknown-linux-gnu",
target_compatible_with = [
"@//linker_config:unknown",
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
target_triple = "x86_64-unknown-linux-gnu",
versions = [RUST_VERSION],
)
rust.repository_set(
name = "rust_linux_x86_64",
target_compatible_with = [
"@//linker_config:musl",
"@platforms//cpu:arm64",
"@platforms//os:linux",
],
target_triple = "aarch64-unknown-linux-musl",
)
rust.repository_set(
name = "rust_linux_x86_64",
target_compatible_with = [
"@//linker_config:musl",
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
target_triple = "x86_64-unknown-linux-musl",
)

# We don't need to register a repository_set for exec_triple == target_triple if we're not customising it in any way:
# one will get registered by default.
# But we do for the Linux case above, because we want to add the "@//linker_config:unknown" constraint in that case.
rust.repository_set(
name = "rust_darwin_x86_64",
edition = RUST_EDITION,
exec_triple = "x86_64-apple-darwin",
target_compatible_with = [
"@//linker_config:musl",
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
target_triple = "x86_64-unknown-linux-musl",
versions = [RUST_VERSION],
)
rust.repository_set(
name = "rust_darwin_x86_64",
target_compatible_with = [
"@//linker_config:musl",
"@platforms//cpu:arm64",
"@platforms//os:linux",
],
target_triple = "aarch64-unknown-linux-musl",
)
rust.repository_set(
name = "rust_darwin_aarch64",
edition = RUST_EDITION,
exec_triple = "aarch64-apple-darwin",
target_compatible_with = [
"@//linker_config:musl",
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
target_triple = "x86_64-unknown-linux-musl",
versions = [RUST_VERSION],
)
rust.repository_set(
name = "rust_darwin_aarch64",
target_compatible_with = [
"@//linker_config:musl",
"@platforms//cpu:arm64",
"@platforms//os:linux",
],
target_triple = "aarch64-unknown-linux-musl",
)
use_repo(rust, "rust_toolchains")

register_toolchains("@rust_toolchains//:all")

crate = use_extension(
"@rules_rust//crate_universe:extensions.bzl",
"crate",
)
crate.from_cargo(
name = "cu",
cargo_lockfile = "//:Cargo.Bazel.lock",
manifests = [
"//:Cargo.toml",
"//:local_proc_macro/Cargo.toml",
],
)
use_repo(crate, "cu")

bazel_dep(name = "toolchains_musl", version = "0.1.20", dev_dependency = True)

toolchains_musl = use_extension("@toolchains_musl//:toolchains_musl.bzl", "toolchains_musl", dev_dependency = True)
toolchains_musl.config(
extra_target_compatible_with = ["@//linker_config:musl"],
)
143 changes: 0 additions & 143 deletions examples/musl_cross_compiling/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -1,143 +0,0 @@
local_repository(
name = "rules_rust",
path = "../..",
)

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains", "rust_repository_set")

rules_rust_dependencies()

EDITION = "2021"

# Before 1.80.0, proc macros couldn't be used when exec!=target where exec and target platforms use different shared library extension (i.e. so vs dylib) because of an error in rustc's handling of extensions.
RUST_VERSION = "1.80.0"

rust_repository_set(
name = "darwin_x86_64_to_x86_64_musl_tuple",
edition = EDITION,
exec_triple = "x86_64-apple-darwin",
# Setting this extra_target_triples allows differentiating the musl case from the non-musl case, in case multiple linux-targeting toolchains are registered.
extra_target_triples = {
"x86_64-unknown-linux-musl": [
"@//linker_config:musl",
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
},
versions = [RUST_VERSION],
)

rust_repository_set(
name = "darwin_arm64_to_x86_64_musl_tuple",
edition = EDITION,
exec_triple = "aarch64-apple-darwin",
extra_target_triples = {
"x86_64-unknown-linux-musl": [
"@//linker_config:musl",
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
},
versions = [RUST_VERSION],
)

rust_repository_set(
name = "darwin_x86_64_to_arm64_musl_tuple",
edition = EDITION,
exec_triple = "x86_64-apple-darwin",
# Setting this extra_target_triples allows differentiating the musl case from the non-musl case, in case multiple linux-targeting toolchains are registered.
extra_target_triples = {
"aarch64-unknown-linux-musl": [
"@//linker_config:musl",
"@platforms//cpu:arm64",
"@platforms//os:linux",
],
},
versions = [RUST_VERSION],
)

rust_repository_set(
name = "darwin_arm64_to_arm64_musl_tuple",
edition = EDITION,
exec_triple = "aarch64-apple-darwin",
extra_target_triples = {
"aarch64-unknown-linux-musl": [
"@//linker_config:musl",
"@platforms//cpu:arm64",
"@platforms//os:linux",
],
},
versions = [RUST_VERSION],
)

# This overrides a default rust_repository_set created by rust_register_toolchain.
# It must be named exactly this, and must be called before rust_register_toolchain is.
rust_repository_set(
name = "rust_linux_x86_64",
edition = EDITION,
exec_triple = "x86_64-unknown-linux-gnu",
# Setting this extra_target_triples allows differentiating the musl case from the non-musl case, in case multiple linux-targeting toolchains are registered.
extra_target_triples = {
"aarch64-unknown-linux-musl": [
"@//linker_config:musl",
"@platforms//cpu:arm64",
"@platforms//os:linux",
],
"x86_64-unknown-linux-gnu": [
"@//linker_config:unknown",
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
"x86_64-unknown-linux-musl": [
"@//linker_config:musl",
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
},
versions = [RUST_VERSION],
)

rust_register_toolchains(
edition = EDITION,
versions = [RUST_VERSION],
)

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "musl_toolchains",
sha256 = "1e6cf99f35277dbb9c3b341a9986d0f33cf70e0cc76a58f062d2d9b7ab56eeeb",
url = "https://github.com/bazel-contrib/musl-toolchain/releases/download/v0.1.17/musl_toolchain-v0.1.17.tar.gz",
)

load("@musl_toolchains//:repositories.bzl", "load_musl_toolchains")

# Setting this extra_target_triples allows differentiating the musl case from the non-musl case, in case multiple linux-targeting toolchains are registered.
load_musl_toolchains(extra_target_compatible_with = ["@//linker_config:musl"])

load("@musl_toolchains//:toolchains.bzl", "register_musl_toolchains")

register_musl_toolchains()

load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")

crate_universe_dependencies(bootstrap = True)

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

crates_repository(
name = "cu",
cargo_lockfile = "//:Cargo.Bazel.lock",
# `generator` is not necessary in official releases.
# See load statement for `cargo_bazel_bootstrap`.
generator = "@cargo_bazel_bootstrap//:cargo-bazel",
lockfile = "//:Cargo.Bazel.lock.json",
manifests = [
"//:Cargo.toml",
"//:local_proc_macro/Cargo.toml",
],
)

load("@cu//:defs.bzl", "crate_repositories")

crate_repositories()
Loading

0 comments on commit c263575

Please sign in to comment.