Skip to content

Commit

Permalink
Added cargo_config attribute to cargo_bootstrap_repository. (#2986)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
UebelAndre authored Nov 6, 2024
1 parent 6f65a04 commit 8b8150d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
19 changes: 18 additions & 1 deletion cargo/private/cargo_bootstrap.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def cargo_bootstrap(
rustc_bin,
binary,
cargo_manifest,
cargo_config = None,
environment = {},
quiet = False,
build_mode = "release",
Expand All @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
),
Expand Down
5 changes: 4 additions & 1 deletion crate_universe/deps_bootstrap.bzl
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions docs/src/cargo.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ str: A json encoded string of the environment variables
## cargo_bootstrap_repository

<pre>
cargo_bootstrap_repository(<a href="#cargo_bootstrap_repository-name">name</a>, <a href="#cargo_bootstrap_repository-srcs">srcs</a>, <a href="#cargo_bootstrap_repository-binary">binary</a>, <a href="#cargo_bootstrap_repository-build_mode">build_mode</a>, <a href="#cargo_bootstrap_repository-cargo_lockfile">cargo_lockfile</a>, <a href="#cargo_bootstrap_repository-cargo_toml">cargo_toml</a>, <a href="#cargo_bootstrap_repository-env">env</a>,
<a href="#cargo_bootstrap_repository-env_label">env_label</a>, <a href="#cargo_bootstrap_repository-repo_mapping">repo_mapping</a>, <a href="#cargo_bootstrap_repository-rust_toolchain_cargo_template">rust_toolchain_cargo_template</a>,
cargo_bootstrap_repository(<a href="#cargo_bootstrap_repository-name">name</a>, <a href="#cargo_bootstrap_repository-srcs">srcs</a>, <a href="#cargo_bootstrap_repository-binary">binary</a>, <a href="#cargo_bootstrap_repository-build_mode">build_mode</a>, <a href="#cargo_bootstrap_repository-cargo_config">cargo_config</a>, <a href="#cargo_bootstrap_repository-cargo_lockfile">cargo_lockfile</a>, <a href="#cargo_bootstrap_repository-cargo_toml">cargo_toml</a>,
<a href="#cargo_bootstrap_repository-env">env</a>, <a href="#cargo_bootstrap_repository-env_label">env_label</a>, <a href="#cargo_bootstrap_repository-repo_mapping">repo_mapping</a>, <a href="#cargo_bootstrap_repository-rust_toolchain_cargo_template">rust_toolchain_cargo_template</a>,
<a href="#cargo_bootstrap_repository-rust_toolchain_rustc_template">rust_toolchain_rustc_template</a>, <a href="#cargo_bootstrap_repository-timeout">timeout</a>, <a href="#cargo_bootstrap_repository-version">version</a>)
</pre>

Expand All @@ -194,8 +194,9 @@ A rule for bootstrapping a Rust binary using [Cargo](https://doc.rust-lang.org/c
| <a id="cargo_bootstrap_repository-srcs"></a>srcs | Souce files of the crate to build. Passing source files here can be used to trigger rebuilds when changes are made | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="cargo_bootstrap_repository-binary"></a>binary | The binary to build (the `--bin` parameter for Cargo). If left empty, the repository name will be used. | String | optional | `""` |
| <a id="cargo_bootstrap_repository-build_mode"></a>build_mode | The build mode the binary should be built with | String | optional | `"release"` |
| <a id="cargo_bootstrap_repository-cargo_config"></a>cargo_config | The path of the Cargo configuration (`Config.toml`) file. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
| <a id="cargo_bootstrap_repository-cargo_lockfile"></a>cargo_lockfile | The lockfile of the crate_universe resolver | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="cargo_bootstrap_repository-cargo_toml"></a>cargo_toml | The path of the crate_universe resolver manifest (`Cargo.toml` file) | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="cargo_bootstrap_repository-cargo_toml"></a>cargo_toml | The path of the `Cargo.toml` file. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="cargo_bootstrap_repository-env"></a>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. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
| <a id="cargo_bootstrap_repository-env_label"></a>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. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
| <a id="cargo_bootstrap_repository-repo_mapping"></a>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.<br><br>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`).<br><br>This attribute is _not_ supported in `MODULE.bazel` context (when invoking a repository rule inside a module extension's implementation function). | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | |
Expand Down
7 changes: 4 additions & 3 deletions docs/src/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -2215,8 +2215,8 @@ generated source files are also ignored by this aspect.
## cargo_bootstrap_repository

<pre>
cargo_bootstrap_repository(<a href="#cargo_bootstrap_repository-name">name</a>, <a href="#cargo_bootstrap_repository-srcs">srcs</a>, <a href="#cargo_bootstrap_repository-binary">binary</a>, <a href="#cargo_bootstrap_repository-build_mode">build_mode</a>, <a href="#cargo_bootstrap_repository-cargo_lockfile">cargo_lockfile</a>, <a href="#cargo_bootstrap_repository-cargo_toml">cargo_toml</a>, <a href="#cargo_bootstrap_repository-env">env</a>,
<a href="#cargo_bootstrap_repository-env_label">env_label</a>, <a href="#cargo_bootstrap_repository-repo_mapping">repo_mapping</a>, <a href="#cargo_bootstrap_repository-rust_toolchain_cargo_template">rust_toolchain_cargo_template</a>,
cargo_bootstrap_repository(<a href="#cargo_bootstrap_repository-name">name</a>, <a href="#cargo_bootstrap_repository-srcs">srcs</a>, <a href="#cargo_bootstrap_repository-binary">binary</a>, <a href="#cargo_bootstrap_repository-build_mode">build_mode</a>, <a href="#cargo_bootstrap_repository-cargo_config">cargo_config</a>, <a href="#cargo_bootstrap_repository-cargo_lockfile">cargo_lockfile</a>, <a href="#cargo_bootstrap_repository-cargo_toml">cargo_toml</a>,
<a href="#cargo_bootstrap_repository-env">env</a>, <a href="#cargo_bootstrap_repository-env_label">env_label</a>, <a href="#cargo_bootstrap_repository-repo_mapping">repo_mapping</a>, <a href="#cargo_bootstrap_repository-rust_toolchain_cargo_template">rust_toolchain_cargo_template</a>,
<a href="#cargo_bootstrap_repository-rust_toolchain_rustc_template">rust_toolchain_rustc_template</a>, <a href="#cargo_bootstrap_repository-timeout">timeout</a>, <a href="#cargo_bootstrap_repository-version">version</a>)
</pre>

Expand All @@ -2231,8 +2231,9 @@ A rule for bootstrapping a Rust binary using [Cargo](https://doc.rust-lang.org/c
| <a id="cargo_bootstrap_repository-srcs"></a>srcs | Souce files of the crate to build. Passing source files here can be used to trigger rebuilds when changes are made | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="cargo_bootstrap_repository-binary"></a>binary | The binary to build (the `--bin` parameter for Cargo). If left empty, the repository name will be used. | String | optional | `""` |
| <a id="cargo_bootstrap_repository-build_mode"></a>build_mode | The build mode the binary should be built with | String | optional | `"release"` |
| <a id="cargo_bootstrap_repository-cargo_config"></a>cargo_config | The path of the Cargo configuration (`Config.toml`) file. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
| <a id="cargo_bootstrap_repository-cargo_lockfile"></a>cargo_lockfile | The lockfile of the crate_universe resolver | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="cargo_bootstrap_repository-cargo_toml"></a>cargo_toml | The path of the crate_universe resolver manifest (`Cargo.toml` file) | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="cargo_bootstrap_repository-cargo_toml"></a>cargo_toml | The path of the `Cargo.toml` file. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="cargo_bootstrap_repository-env"></a>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. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
| <a id="cargo_bootstrap_repository-env_label"></a>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. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
| <a id="cargo_bootstrap_repository-repo_mapping"></a>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.<br><br>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`).<br><br>This attribute is _not_ supported in `MODULE.bazel` context (when invoking a repository rule inside a module extension's implementation function). | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | |
Expand Down

0 comments on commit 8b8150d

Please sign in to comment.