From 8b8150db501869d45bbcc46f100da1862ac45eec Mon Sep 17 00:00:00 2001 From: UebelAndre Date: Wed, 6 Nov 2024 09:52:51 -0800 Subject: [PATCH] Added `cargo_config` attribute to `cargo_bootstrap_repository`. (#2986) This can be useful for workspaces which have a `config.toml` for the workspace (used by `crate_universe`) and want to share the same configuration with any bootstrap repositories. --- cargo/private/cargo_bootstrap.bzl | 19 ++++++++++++++++++- crate_universe/deps_bootstrap.bzl | 5 ++++- docs/src/cargo.md | 7 ++++--- docs/src/flatten.md | 7 ++++--- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/cargo/private/cargo_bootstrap.bzl b/cargo/private/cargo_bootstrap.bzl index beaac8acda..b634aa3e0c 100644 --- a/cargo/private/cargo_bootstrap.bzl +++ b/cargo/private/cargo_bootstrap.bzl @@ -27,6 +27,7 @@ def cargo_bootstrap( rustc_bin, binary, cargo_manifest, + cargo_config = None, environment = {}, quiet = False, build_mode = "release", @@ -40,6 +41,7 @@ def cargo_bootstrap( rustc_bin (path): The path to a Rustc binary. binary (str): The binary to build (the `--bin` parameter for Cargo). cargo_manifest (path): The path to a Cargo manifest (Cargo.toml file). + cargo_config (path, optional): The path to a Cargo configuration file (Config.toml) to use. environment (dict): Environment variables to use during execution. quiet (bool, optional): Whether or not to print output from the Cargo command. build_mode (str, optional): The build mode to use @@ -54,6 +56,9 @@ def cargo_bootstrap( if not target_dir: target_dir = repository_ctx.path(".") + if cargo_config: + repository_ctx.symlink(cargo_config, repository_ctx.path(".cargo/config.toml")) + args = [ cargo_bin, "build", @@ -173,6 +178,9 @@ def _detect_changes(repository_ctx): repository_ctx.path(repository_ctx.attr.cargo_lockfile) repository_ctx.path(repository_ctx.attr.cargo_toml) + if repository_ctx.attr.cargo_config: + repository_ctx.path(repository_ctx.attr.cargo_config) + def _cargo_bootstrap_repository_impl(repository_ctx): # Pretend to Bazel that this rule's input files have been used, so that it will re-run the rule if they change. _detect_changes(repository_ctx) @@ -203,10 +211,15 @@ def _cargo_bootstrap_repository_impl(repository_ctx): # be gathered. environment = dict(_collect_environ(repository_ctx, "*").items() + _collect_environ(repository_ctx, host_triple.str).items()) + cargo_config = None + if repository_ctx.attr.cargo_config: + cargo_config = repository_ctx.path(repository_ctx.attr.cargo_config) + built_binary = cargo_bootstrap( repository_ctx = repository_ctx, cargo_bin = repository_ctx.path(tools.cargo), rustc_bin = repository_ctx.path(tools.rustc), + cargo_config = cargo_config, binary = binary_name, cargo_manifest = repository_ctx.path(repository_ctx.attr.cargo_toml), build_mode = repository_ctx.attr.build_mode, @@ -237,13 +250,17 @@ cargo_bootstrap_repository = repository_rule( ], default = "release", ), + "cargo_config": attr.label( + doc = "The path of the Cargo configuration (`Config.toml`) file.", + allow_single_file = True, + ), "cargo_lockfile": attr.label( doc = "The lockfile of the crate_universe resolver", allow_single_file = ["Cargo.lock"], mandatory = True, ), "cargo_toml": attr.label( - doc = "The path of the crate_universe resolver manifest (`Cargo.toml` file)", + doc = "The path of the `Cargo.toml` file.", allow_single_file = ["Cargo.toml"], mandatory = True, ), diff --git a/crate_universe/deps_bootstrap.bzl b/crate_universe/deps_bootstrap.bzl index e4fda6de97..615b42be4f 100644 --- a/crate_universe/deps_bootstrap.bzl +++ b/crate_universe/deps_bootstrap.bzl @@ -1,5 +1,6 @@ """A module is used to assist in bootstrapping cargo-bazel""" +load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") load("//cargo:defs.bzl", "cargo_bootstrap_repository") load("//crate_universe/private:srcs.bzl", "CARGO_BAZEL_SRCS") @@ -14,7 +15,9 @@ def cargo_bazel_bootstrap(name = "cargo_bazel_bootstrap", rust_version = rust_co rust_version (str, optional): The rust version to use. Defaults to the default of `cargo_bootstrap_repository`. **kwargs: kwargs to pass through to cargo_bootstrap_repository. """ - cargo_bootstrap_repository( + + maybe( + cargo_bootstrap_repository, name = name, srcs = CARGO_BAZEL_SRCS, binary = "cargo-bazel", diff --git a/docs/src/cargo.md b/docs/src/cargo.md index 39624a112e..72b58ac7b7 100644 --- a/docs/src/cargo.md +++ b/docs/src/cargo.md @@ -178,8 +178,8 @@ str: A json encoded string of the environment variables ## cargo_bootstrap_repository
-cargo_bootstrap_repository(name, srcs, binary, build_mode, cargo_lockfile, cargo_toml, env,
-                           env_label, repo_mapping, rust_toolchain_cargo_template,
+cargo_bootstrap_repository(name, srcs, binary, build_mode, cargo_config, cargo_lockfile, cargo_toml,
+                           env, env_label, repo_mapping, rust_toolchain_cargo_template,
                            rust_toolchain_rustc_template, timeout, version)
 
@@ -194,8 +194,9 @@ A rule for bootstrapping a Rust binary using [Cargo](https://doc.rust-lang.org/c | srcs | Souce files of the crate to build. Passing source files here can be used to trigger rebuilds when changes are made | List of labels | optional | `[]` | | binary | The binary to build (the `--bin` parameter for Cargo). If left empty, the repository name will be used. | String | optional | `""` | | build_mode | The build mode the binary should be built with | String | optional | `"release"` | +| cargo_config | The path of the Cargo configuration (`Config.toml`) file. | Label | optional | `None` | | cargo_lockfile | The lockfile of the crate_universe resolver | Label | required | | -| cargo_toml | The path of the crate_universe resolver manifest (`Cargo.toml` file) | Label | required | | +| cargo_toml | The path of the `Cargo.toml` file. | Label | required | | | env | A mapping of platform triple to a set of environment variables. See [cargo_env](#cargo_env) for usage details. Additionally, the platform triple `*` applies to all platforms. | Dictionary: String -> String | optional | `{}` | | env_label | A mapping of platform triple to a set of environment variables. This attribute differs from `env` in that all variables passed here must be fully qualified labels of files. See [cargo_env](#cargo_env) for usage details. Additionally, the platform triple `*` applies to all platforms. | Dictionary: String -> String | optional | `{}` | | repo_mapping | In `WORKSPACE` context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.

For example, an entry `"@foo": "@bar"` declares that, for any time this repository depends on `@foo` (such as a dependency on `@foo//some:target`, it should actually resolve that dependency within globally-declared `@bar` (`@bar//some:target`).

This attribute is _not_ supported in `MODULE.bazel` context (when invoking a repository rule inside a module extension's implementation function). | Dictionary: String -> String | optional | | diff --git a/docs/src/flatten.md b/docs/src/flatten.md index c0da9eae5a..c16189963b 100644 --- a/docs/src/flatten.md +++ b/docs/src/flatten.md @@ -2215,8 +2215,8 @@ generated source files are also ignored by this aspect. ## cargo_bootstrap_repository
-cargo_bootstrap_repository(name, srcs, binary, build_mode, cargo_lockfile, cargo_toml, env,
-                           env_label, repo_mapping, rust_toolchain_cargo_template,
+cargo_bootstrap_repository(name, srcs, binary, build_mode, cargo_config, cargo_lockfile, cargo_toml,
+                           env, env_label, repo_mapping, rust_toolchain_cargo_template,
                            rust_toolchain_rustc_template, timeout, version)
 
@@ -2231,8 +2231,9 @@ A rule for bootstrapping a Rust binary using [Cargo](https://doc.rust-lang.org/c | srcs | Souce files of the crate to build. Passing source files here can be used to trigger rebuilds when changes are made | List of labels | optional | `[]` | | binary | The binary to build (the `--bin` parameter for Cargo). If left empty, the repository name will be used. | String | optional | `""` | | build_mode | The build mode the binary should be built with | String | optional | `"release"` | +| cargo_config | The path of the Cargo configuration (`Config.toml`) file. | Label | optional | `None` | | cargo_lockfile | The lockfile of the crate_universe resolver | Label | required | | -| cargo_toml | The path of the crate_universe resolver manifest (`Cargo.toml` file) | Label | required | | +| cargo_toml | The path of the `Cargo.toml` file. | Label | required | | | env | A mapping of platform triple to a set of environment variables. See [cargo_env](#cargo_env) for usage details. Additionally, the platform triple `*` applies to all platforms. | Dictionary: String -> String | optional | `{}` | | env_label | A mapping of platform triple to a set of environment variables. This attribute differs from `env` in that all variables passed here must be fully qualified labels of files. See [cargo_env](#cargo_env) for usage details. Additionally, the platform triple `*` applies to all platforms. | Dictionary: String -> String | optional | `{}` | | repo_mapping | In `WORKSPACE` context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.

For example, an entry `"@foo": "@bar"` declares that, for any time this repository depends on `@foo` (such as a dependency on `@foo//some:target`, it should actually resolve that dependency within globally-declared `@bar` (`@bar//some:target`).

This attribute is _not_ supported in `MODULE.bazel` context (when invoking a repository rule inside a module extension's implementation function). | Dictionary: String -> String | optional | |