From b8ec16f55e12add4c03116c200bb54cab585c378 Mon Sep 17 00:00:00 2001 From: UebelAndre Date: Mon, 24 Jan 2022 23:35:39 -0800 Subject: [PATCH 1/2] Allow users to load dependencies but optionally not register toolchains --- WORKSPACE.bazel | 6 ++- docs/BUILD.bazel | 8 +-- docs/WORKSPACE.bazel | 6 ++- docs/index.md | 6 ++- examples/WORKSPACE.bazel | 6 ++- rust/repositories.bzl | 111 +++++++++++++++++++++++---------------- 6 files changed, 86 insertions(+), 57 deletions(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 429da7f978..316052975e 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -1,8 +1,10 @@ workspace(name = "rules_rust") -load("@rules_rust//rust:repositories.bzl", "rust_repositories") +load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains") -rust_repositories(include_rustc_srcs = True) +rules_rust_dependencies() + +rust_register_toolchains(include_rustc_srcs = True) load("@rules_rust//proto:repositories.bzl", "rust_proto_repositories") diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index f2eeb8e87b..8a25c03fe1 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -127,12 +127,14 @@ PAGES = dict([ page( name = "rust_repositories", symbols = [ + "rules_rust_depednencies", + "rust_register_toolchains", "rust_repositories", "rust_repository_set", - "rust_toolchain", - "rust_toolchain_repository", - "rust_toolchain_repository_proxy", "rust_stdlib_filegroup", + "rust_toolchain_repository_proxy", + "rust_toolchain_repository", + "rust_toolchain", ], ), page( diff --git a/docs/WORKSPACE.bazel b/docs/WORKSPACE.bazel index 2dc55293f5..c3f3862895 100644 --- a/docs/WORKSPACE.bazel +++ b/docs/WORKSPACE.bazel @@ -5,9 +5,11 @@ local_repository( path = "..", ) -load("@rules_rust//rust:repositories.bzl", "rust_repositories") +load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains") -rust_repositories(include_rustc_srcs = True) +rules_rust_dependencies() + +rust_register_toolchains(include_rustc_srcs = True) load("@rules_rust//proto:repositories.bzl", "rust_proto_repositories") diff --git a/docs/index.md b/docs/index.md index f196154a1b..022d88cd55 100644 --- a/docs/index.md +++ b/docs/index.md @@ -28,9 +28,11 @@ http_archive( ], ) -load("@rules_rust//rust:repositories.bzl", "rust_repositories") +load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains") -rust_repositories() +rules_rust_dependencies() + +rust_register_toolchains() ``` The rules are under active development, as such the lastest commit on the diff --git a/examples/WORKSPACE.bazel b/examples/WORKSPACE.bazel index 74512ae009..47dec43a65 100644 --- a/examples/WORKSPACE.bazel +++ b/examples/WORKSPACE.bazel @@ -8,9 +8,11 @@ local_repository( path = "..", ) -load("@rules_rust//rust:repositories.bzl", "rust_repositories") +load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains") -rust_repositories(include_rustc_srcs = True) +rules_rust_dependencies() + +rust_register_toolchains(include_rustc_srcs = True) load("@rules_rust//bindgen:repositories.bzl", "rust_bindgen_repositories") diff --git a/rust/repositories.bzl b/rust/repositories.bzl index 181ccf40b8..3bd9d1f68c 100644 --- a/rust/repositories.bzl +++ b/rust/repositories.bzl @@ -31,16 +31,45 @@ DEFAULT_TOOLCHAIN_TRIPLES = { "x86_64-unknown-linux-gnu": "rust_linux_x86_64", } +def rules_rust_dependencies(): + """Dependencies used in the implementation of `rules_rust`.""" + maybe( + http_archive, + name = "rules_cc", + urls = ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.1/rules_cc-0.0.1.tar.gz"], + sha256 = "4dccbfd22c0def164c8f47458bd50e0c7148f3d92002cdb459c2a96a68498241", + ) + + maybe( + http_archive, + name = "bazel_skylib", + urls = [ + "https://github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz", + "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz", + ], + sha256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d", + ) + + # Make the iOS simulator constraint available, which is referenced in abi_to_constraints() + # rules_rust does not require this dependency; it is just imported as a convenience for users. + maybe( + http_archive, + name = "build_bazel_apple_support", + sha256 = "76df040ade90836ff5543888d64616e7ba6c3a7b33b916aa3a4b68f342d1b447", + url = "https://github.com/bazelbuild/apple_support/releases/download/0.11.0/apple_support.0.11.0.tar.gz", + ) + # buildifier: disable=unnamed-macro -def rust_repositories( - version = rust_common.default_version, +def rust_register_toolchains( + dev_components = False, + edition = None, + include_rustc_srcs = False, iso_date = None, + register_toolchains = True, rustfmt_version = None, - edition = None, - dev_components = False, sha256s = None, - include_rustc_srcs = False, - urls = DEFAULT_STATIC_RUST_URL_TEMPLATES): + urls = DEFAULT_STATIC_RUST_URL_TEMPLATES, + version = rust_common.default_version): """Emits a default set of toolchains for Linux, MacOS, and Freebsd Skip this macro and call the `rust_repository_set` macros directly if you need a compiler for \ @@ -60,65 +89,52 @@ def rust_repositories( See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more details. Args: - version (str, optional): The version of Rust. Either "nightly", "beta", or an exact version. Defaults to a modern version. - iso_date (str, optional): The date of the nightly or beta release (ignored if the version is a specific version). - rustfmt_version (str, optional): The version of rustfmt. Either "nightly", "beta", or an exact version. Defaults to `version` if not specified. - edition (str, optional): The rust edition to be used by default (2015, 2018 (default), or 2021) dev_components (bool, optional): Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". - sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. Defaults to None. + edition (str, optional): The rust edition to be used by default (2015, 2018 (default), or 2021) include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support. See [rust_toolchain_repository.include_rustc_srcs](#rust_toolchain_repository-include_rustc_srcs). for more details - urls (list, optional): 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). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz'] + iso_date (str, optional): The date of the nightly or beta release (ignored if the version is a specific version). + register_toolchains (bool): If true, repositories will be generated to produce and register `rust_toolchain` targets. + rustfmt_version (str, optional): The version of rustfmt. Either "nightly", "beta", or an exact version. Defaults to `version` if not specified. + sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. + urls (list, optional): 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). + version (str, optional): The version of Rust. Either "nightly", "beta", or an exact version. Defaults to a modern version. """ - if dev_components and version != "nightly": fail("Rust version must be set to \"nightly\" to enable rustc-dev components") if not rustfmt_version: rustfmt_version = version - maybe( - http_archive, - name = "rules_cc", - urls = ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.1/rules_cc-0.0.1.tar.gz"], - sha256 = "4dccbfd22c0def164c8f47458bd50e0c7148f3d92002cdb459c2a96a68498241", - ) - - maybe( - http_archive, - name = "bazel_skylib", - urls = [ - "https://github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz", - "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz", - ], - sha256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d", - ) - - # Make the iOS simulator constraint available, which is referenced in abi_to_constraints() - # rules_rust does not require this dependency; it is just imported as a convenience for users. - maybe( - http_archive, - name = "build_bazel_apple_support", - sha256 = "76df040ade90836ff5543888d64616e7ba6c3a7b33b916aa3a4b68f342d1b447", - url = "https://github.com/bazelbuild/apple_support/releases/download/0.11.0/apple_support.0.11.0.tar.gz", - ) - for exec_triple, name in DEFAULT_TOOLCHAIN_TRIPLES.items(): maybe( rust_repository_set, name = name, + dev_components = dev_components, + edition = edition, exec_triple = exec_triple, extra_target_triples = ["wasm32-unknown-unknown", "wasm32-wasi"], - version = version, + include_rustc_srcs = include_rustc_srcs, iso_date = iso_date, + register_toolchain = register_toolchains, rustfmt_version = rustfmt_version, - edition = edition, - dev_components = dev_components, sha256s = sha256s, - include_rustc_srcs = include_rustc_srcs, urls = urls, + version = version, ) +# buildifier: disable=unnamed-macro +def rust_repositories(**kwargs): + """**Deprecated**: Use [rules_rust_dependencies](#rules_rust_dependencies) \ + and [rust_register_toolchains](#rust_register_toolchains) directly. + + Args: + **kwargs (dict): Keyword arguments for the `rust_register_toolchains` macro. + """ + rules_rust_dependencies() + + rust_register_toolchains(**kwargs) + def _rust_toolchain_repository_impl(ctx): """The implementation of the rust toolchain repository rule.""" @@ -265,7 +281,8 @@ def rust_repository_set( dev_components = False, sha256s = None, urls = DEFAULT_STATIC_RUST_URL_TEMPLATES, - auth = None): + auth = None, + register_toolchain = True): """Assembles a remote repository for the given toolchain params, produces a proxy repository \ to contain the toolchain declaration, and registers the toolchains. @@ -290,6 +307,7 @@ def rust_repository_set( urls (list, optional): 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). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz'] 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. """ rust_toolchain_repository( @@ -325,8 +343,9 @@ def rust_repository_set( )) # Register toolchains - native.register_toolchains(*all_toolchain_names) - native.register_toolchains(str(Label("//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain"))) + if register_toolchain: + native.register_toolchains(*all_toolchain_names) + native.register_toolchains(str(Label("//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain"))) # Inform users that they should be using the canonical name if it's not detected if "rules_rust" not in native.existing_rules(): From 2b3853271e774e76338137a18c7d7e06018d3a8f Mon Sep 17 00:00:00 2001 From: UebelAndre Date: Thu, 27 Jan 2022 08:27:50 -0800 Subject: [PATCH 2/2] Regenerate documentation --- docs/flatten.md | 34 +++++++-------------------------- docs/rust_repositories.md | 40 ++++++++++----------------------------- 2 files changed, 17 insertions(+), 57 deletions(-) diff --git a/docs/flatten.md b/docs/flatten.md index 4d3d531391..a31416cee7 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -13,6 +13,7 @@ * [extra_rustc_flags](#extra_rustc_flags) * [fail_when_enabled](#fail_when_enabled) * [incompatible_flag](#incompatible_flag) +* [rules_rust_depednencies](#rules_rust_depednencies) * [rust_analyzer](#rust_analyzer) * [rust_analyzer_aspect](#rust_analyzer_aspect) * [rust_binary](#rust_binary) @@ -31,6 +32,7 @@ * [rust_proto_repositories](#rust_proto_repositories) * [rust_proto_toolchain](#rust_proto_toolchain) * [rust_proto_transitive_repositories](#rust_proto_transitive_repositories) +* [rust_register_toolchains](#rust_register_toolchains) * [rust_repositories](#rust_repositories) * [rust_repository_set](#rust_repository_set) * [rust_shared_library](#rust_shared_library) @@ -1754,40 +1756,17 @@ This macro should be called immediately after the `rust_proto_repositories` macr ## rust_repositories
-rust_repositories(version, iso_date, rustfmt_version, edition, dev_components, sha256s,
-                  include_rustc_srcs, urls)
+rust_repositories(kwargs)
 
-Emits a default set of toolchains for Linux, MacOS, and Freebsd - -Skip this macro and call the `rust_repository_set` macros directly if you need a compiler for other hosts or for additional target triples. - -The `sha256` attribute represents a dict associating tool subdirectories to sha256 hashes. As an example: -```python -{ - "rust-1.46.0-x86_64-unknown-linux-gnu": "e3b98bc3440fe92817881933f9564389eccb396f5f431f33d48b979fa2fbdcf5", - "rustfmt-1.4.12-x86_64-unknown-linux-gnu": "1894e76913303d66bf40885a601462844eec15fca9e76a6d13c390d7000d64b0", - "rust-std-1.46.0-x86_64-unknown-linux-gnu": "ac04aef80423f612c0079829b504902de27a6997214eb58ab0765d02f7ec1dbc", -} -``` -This would match for `exec_triple = "x86_64-unknown-linux-gnu"`. If not specified, rules_rust pulls from a non-exhaustive list of known checksums.. - -See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more details. - +**Deprecated**: Use [rules_rust_dependencies](#rules_rust_dependencies) and [rust_register_toolchains](#rust_register_toolchains) directly. **PARAMETERS** | Name | Description | Default Value | | :------------- | :------------- | :------------- | -| version | The version of Rust. Either "nightly", "beta", or an exact version. Defaults to a modern version. | "1.58.1" | -| iso_date | The date of the nightly or beta release (ignored if the version is a specific version). | None | -| rustfmt_version | The version of rustfmt. Either "nightly", "beta", or an exact version. Defaults to version if not specified. | None | -| edition | The rust edition to be used by default (2015, 2018 (default), or 2021) | None | -| dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | False | -| sha256s | A dict associating tool subdirectories to sha256 hashes. Defaults to None. | None | -| include_rustc_srcs | Whether to download rustc's src code. This is required in order to use rust-analyzer support. See [rust_toolchain_repository.include_rustc_srcs](#rust_toolchain_repository-include_rustc_srcs). for more details | False | -| 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). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz'] | ["https://static.rust-lang.org/dist/{}.tar.gz"] | +| kwargs | Keyword arguments for the rust_register_toolchains macro. | none | @@ -1796,7 +1775,7 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai
 rust_repository_set(name, version, exec_triple, include_rustc_srcs, extra_target_triples, iso_date,
-                    rustfmt_version, edition, dev_components, sha256s, urls, auth)
+                    rustfmt_version, edition, dev_components, sha256s, urls, auth, register_toolchain)
 
Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains. @@ -1821,6 +1800,7 @@ N.B. A "proxy repository" is needed to allow for registering the toolchain (with | sha256s | A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details. | None | | 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). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz'] | ["https://static.rust-lang.org/dist/{}.tar.gz"] | | 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 | +| register_toolchain | If True, the generated rust_toolchain target will become a registered toolchain. | True | diff --git a/docs/rust_repositories.md b/docs/rust_repositories.md index bee95d0489..d488ab68fa 100644 --- a/docs/rust_repositories.md +++ b/docs/rust_repositories.md @@ -1,12 +1,14 @@ # Rust Repositories +* [rules_rust_depednencies](#rules_rust_depednencies) +* [rust_register_toolchains](#rust_register_toolchains) * [rust_repositories](#rust_repositories) * [rust_repository_set](#rust_repository_set) -* [rust_toolchain](#rust_toolchain) -* [rust_toolchain_repository](#rust_toolchain_repository) -* [rust_toolchain_repository_proxy](#rust_toolchain_repository_proxy) * [rust_stdlib_filegroup](#rust_stdlib_filegroup) +* [rust_toolchain_repository_proxy](#rust_toolchain_repository_proxy) +* [rust_toolchain_repository](#rust_toolchain_repository) +* [rust_toolchain](#rust_toolchain) @@ -172,40 +174,17 @@ Generates a toolchain-bearing repository that declares the toolchains from some ## rust_repositories
-rust_repositories(version, iso_date, rustfmt_version, edition, dev_components, sha256s,
-                  include_rustc_srcs, urls)
+rust_repositories(kwargs)
 
-Emits a default set of toolchains for Linux, MacOS, and Freebsd - -Skip this macro and call the `rust_repository_set` macros directly if you need a compiler for other hosts or for additional target triples. - -The `sha256` attribute represents a dict associating tool subdirectories to sha256 hashes. As an example: -```python -{ - "rust-1.46.0-x86_64-unknown-linux-gnu": "e3b98bc3440fe92817881933f9564389eccb396f5f431f33d48b979fa2fbdcf5", - "rustfmt-1.4.12-x86_64-unknown-linux-gnu": "1894e76913303d66bf40885a601462844eec15fca9e76a6d13c390d7000d64b0", - "rust-std-1.46.0-x86_64-unknown-linux-gnu": "ac04aef80423f612c0079829b504902de27a6997214eb58ab0765d02f7ec1dbc", -} -``` -This would match for `exec_triple = "x86_64-unknown-linux-gnu"`. If not specified, rules_rust pulls from a non-exhaustive list of known checksums.. - -See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more details. - +**Deprecated**: Use [rules_rust_dependencies](#rules_rust_dependencies) and [rust_register_toolchains](#rust_register_toolchains) directly. **PARAMETERS** | Name | Description | Default Value | | :------------- | :------------- | :------------- | -| version | The version of Rust. Either "nightly", "beta", or an exact version. Defaults to a modern version. | "1.58.1" | -| iso_date | The date of the nightly or beta release (ignored if the version is a specific version). | None | -| rustfmt_version | The version of rustfmt. Either "nightly", "beta", or an exact version. Defaults to version if not specified. | None | -| edition | The rust edition to be used by default (2015, 2018 (default), or 2021) | None | -| dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | False | -| sha256s | A dict associating tool subdirectories to sha256 hashes. Defaults to None. | None | -| include_rustc_srcs | Whether to download rustc's src code. This is required in order to use rust-analyzer support. See [rust_toolchain_repository.include_rustc_srcs](#rust_toolchain_repository-include_rustc_srcs). for more details | False | -| 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). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz'] | ["https://static.rust-lang.org/dist/{}.tar.gz"] | +| kwargs | Keyword arguments for the rust_register_toolchains macro. | none | @@ -214,7 +193,7 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai
 rust_repository_set(name, version, exec_triple, include_rustc_srcs, extra_target_triples, iso_date,
-                    rustfmt_version, edition, dev_components, sha256s, urls, auth)
+                    rustfmt_version, edition, dev_components, sha256s, urls, auth, register_toolchain)
 
Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains. @@ -239,5 +218,6 @@ N.B. A "proxy repository" is needed to allow for registering the toolchain (with | sha256s | A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details. | None | | 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). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz'] | ["https://static.rust-lang.org/dist/{}.tar.gz"] | | 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 | +| register_toolchain | If True, the generated rust_toolchain target will become a registered toolchain. | True |