Skip to content

Commit

Permalink
Added support for compact windows repository names, avoiding MAX_PATH (
Browse files Browse the repository at this point in the history
…#3023)

closes #3021
  • Loading branch information
UebelAndre authored Dec 4, 2024
1 parent d9e6084 commit 8e962a1
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 39 deletions.
23 changes: 10 additions & 13 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -983,19 +983,16 @@ tasks:
- "//..."
test_targets:
- "//..."
# # TODO: https://github.com/bazelbuild/rules_rust/issues/3021
# # The length of the workspace name results in paths that are too long
# # on windows for linking.
# extensions_wasm_bindgen_windows:
# platform: windows
# name: Extensions wasm-bindgen
# working_directory: extensions/wasm_bindgen
# build_flags: *aspects_flags
# test_flags: *aspects_flags
# build_targets:
# - "//..."
# test_targets:
# - "//..."
extensions_wasm_bindgen_windows:
platform: windows
name: Extensions wasm-bindgen
working_directory: extensions/wasm_bindgen
build_flags: *aspects_flags
test_flags: *aspects_flags
build_targets:
- "//..."
test_targets:
- "//..."

buildifier:
version: latest
Expand Down
6 changes: 6 additions & 0 deletions cargo/private/cargo_bootstrap.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,15 @@ def _cargo_bootstrap_repository_impl(repository_ctx):
host_triple = get_host_triple(repository_ctx)
cargo_template = repository_ctx.attr.rust_toolchain_cargo_template
rustc_template = repository_ctx.attr.rust_toolchain_rustc_template
compress_windows_names = repository_ctx.attr.compressed_windows_toolchain_names

tools = get_rust_tools(
cargo_template = cargo_template,
rustc_template = rustc_template,
host_triple = host_triple,
channel = channel,
version = version,
compress_windows_names = compress_windows_names,
)

binary_name = repository_ctx.attr.binary or repository_ctx.name
Expand Down Expand Up @@ -264,6 +266,10 @@ cargo_bootstrap_repository = repository_rule(
allow_single_file = ["Cargo.toml"],
mandatory = True,
),
"compressed_windows_toolchain_names": attr.bool(
doc = "Wether or not the toolchain names of windows toolchains are expected to be in a `compressed` format.",
default = True,
),
"env": attr.string_dict(
doc = (
"A mapping of platform triple to a set of environment variables. See " +
Expand Down
36 changes: 34 additions & 2 deletions cargo/private/cargo_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
load("//rust/platform:triple_mappings.bzl", "system_to_binary_ext")

def _resolve_repository_template(
*,
template,
abi = None,
arch = None,
Expand All @@ -11,7 +12,8 @@ def _resolve_repository_template(
tool = None,
triple = None,
vendor = None,
version = None):
version = None,
compress_windows_names = False):
"""Render values into a repository template string
Args:
Expand All @@ -25,6 +27,8 @@ def _resolve_repository_template(
triple (str, optional): The host triple
vendor (str, optional): The host vendor name
version (str, optional): The Rust version used in the toolchain.
compress_windows_names (bool): Whether or not the windows repositories are
to be in the compressed form.
Returns:
string: The resolved template string based on the given parameters
"""
Expand Down Expand Up @@ -52,9 +56,33 @@ def _resolve_repository_template(
if channel:
template = template.replace("{channel}", channel)

if compress_windows_names and system == "windows":
repo, _, target = template.partition("//")
prefix = ""
if repo.startswith("@"):
repo = repo[1:]
prefix = "@"
elif repo.startswith("@@"):
repo = repo[2:]
prefix = "@@"

suffix = ""
if repo.endswith("_tools"):
repo = repo[:-len("_tools")]
suffix = "_tools"

return "{}rw-{}{}//{}".format(prefix, abs(hash(repo)), suffix, target)

return template

def get_rust_tools(cargo_template, rustc_template, host_triple, channel, version):
def get_rust_tools(
*,
cargo_template,
rustc_template,
host_triple,
channel,
version,
compress_windows_names):
"""Retrieve `cargo` and `rustc` labels based on the host triple.
Args:
Expand All @@ -63,6 +91,8 @@ def get_rust_tools(cargo_template, rustc_template, host_triple, channel, version
host_triple (struct): The host's triple. See `@rules_rust//rust/platform:triple.bzl`.
channel (str): The Rust toolchain channel.
version (str): The version (or iso date in case of beta or nightly channels) of Cargo+Rustc to use.
compress_windows_names (bool): Whether or not the windows repositories are
to be in the compressed form.
Returns:
struct: A struct containing the labels of expected tools
Expand All @@ -79,6 +109,7 @@ def get_rust_tools(cargo_template, rustc_template, host_triple, channel, version
system = host_triple.system,
abi = host_triple.abi,
tool = "cargo" + extension,
compress_windows_names = compress_windows_names,
))

rustc_label = Label(_resolve_repository_template(
Expand All @@ -91,6 +122,7 @@ def get_rust_tools(cargo_template, rustc_template, host_triple, channel, version
system = host_triple.system,
abi = host_triple.abi,
tool = "rustc" + extension,
compress_windows_names = compress_windows_names,
))

return struct(
Expand Down
5 changes: 4 additions & 1 deletion crate_universe/deps_bootstrap.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ load("//crate_universe/private:srcs.bzl", "CARGO_BAZEL_SRCS")
# buildifier: disable=bzl-visibility
load("//rust/private:common.bzl", "rust_common")

def cargo_bazel_bootstrap(name = "cargo_bazel_bootstrap", rust_version = rust_common.default_version, **kwargs):
def cargo_bazel_bootstrap(
name = "cargo_bazel_bootstrap",
rust_version = rust_common.default_version,
**kwargs):
"""An optional repository which bootstraps `cargo-bazel` for use with `crates_repository`
Args:
Expand Down
1 change: 1 addition & 0 deletions crate_universe/private/common_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def get_rust_tools(repository_ctx, host_triple):
host_triple = host_triple,
channel = channel,
version = version,
compress_windows_names = repository_ctx.attr.compressed_windows_toolchain_names,
)

def _cargo_home_path(repository_ctx):
Expand Down
4 changes: 4 additions & 0 deletions crate_universe/private/crates_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_i
),
mandatory = True,
),
"compressed_windows_toolchain_names": attr.bool(
doc = "Wether or not the toolchain names of windows toolchains are expected to be in a `compressed` format.",
default = True,
),
"generate_binaries": attr.bool(
doc = (
"Whether to generate `rust_binary` targets for all the binary crates in every package. " +
Expand Down
2 changes: 2 additions & 0 deletions crate_universe/private/internal_extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def _internal_deps_impl(module_ctx):
direct_deps.extend(cargo_bazel_bootstrap(
rust_toolchain_cargo_template = "@rust_host_tools//:bin/{tool}",
rust_toolchain_rustc_template = "@rust_host_tools//:bin/{tool}",
compressed_windows_toolchain_names = False,
))

# is_dev_dep is ignored here. It's not relevant for internal_deps, as dev
Expand All @@ -35,6 +36,7 @@ def _internal_dev_deps_impl(module_ctx):
direct_deps.extend(cross_installer_deps(
rust_toolchain_cargo_template = "@rust_host_tools//:bin/{tool}",
rust_toolchain_rustc_template = "@rust_host_tools//:bin/{tool}",
compressed_windows_toolchain_names = False,
))

# is_dev_dep is ignored here. It's not relevant for internal_deps, as dev
Expand Down
6 changes: 4 additions & 2 deletions docs/src/cargo.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ str: A json encoded string of the environment variables

<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_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>)
<a href="#cargo_bootstrap_repository-compressed_windows_toolchain_names">compressed_windows_toolchain_names</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>

A rule for bootstrapping a Rust binary using [Cargo](https://doc.rust-lang.org/cargo/)
Expand All @@ -197,6 +198,7 @@ A rule for bootstrapping a Rust binary using [Cargo](https://doc.rust-lang.org/c
| <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 `Cargo.toml` file. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="cargo_bootstrap_repository-compressed_windows_toolchain_names"></a>compressed_windows_toolchain_names | Wether or not the toolchain names of windows toolchains are expected to be in a `compressed` format. | Boolean | optional | `True` |
| <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
13 changes: 7 additions & 6 deletions docs/src/crate_universe.md
Original file line number Diff line number Diff line change
Expand Up @@ -750,12 +750,12 @@ str: A json encoded string of the parameters provided
## crates_repository

<pre>
crates_repository(<a href="#crates_repository-name">name</a>, <a href="#crates_repository-annotations">annotations</a>, <a href="#crates_repository-cargo_config">cargo_config</a>, <a href="#crates_repository-cargo_lockfile">cargo_lockfile</a>, <a href="#crates_repository-generate_binaries">generate_binaries</a>,
<a href="#crates_repository-generate_build_scripts">generate_build_scripts</a>, <a href="#crates_repository-generate_target_compatible_with">generate_target_compatible_with</a>, <a href="#crates_repository-generator">generator</a>,
<a href="#crates_repository-generator_sha256s">generator_sha256s</a>, <a href="#crates_repository-generator_urls">generator_urls</a>, <a href="#crates_repository-isolated">isolated</a>, <a href="#crates_repository-lockfile">lockfile</a>, <a href="#crates_repository-manifests">manifests</a>, <a href="#crates_repository-packages">packages</a>, <a href="#crates_repository-quiet">quiet</a>,
<a href="#crates_repository-render_config">render_config</a>, <a href="#crates_repository-repin_instructions">repin_instructions</a>, <a href="#crates_repository-repo_mapping">repo_mapping</a>, <a href="#crates_repository-rust_toolchain_cargo_template">rust_toolchain_cargo_template</a>,
<a href="#crates_repository-rust_toolchain_rustc_template">rust_toolchain_rustc_template</a>, <a href="#crates_repository-rust_version">rust_version</a>, <a href="#crates_repository-splicing_config">splicing_config</a>,
<a href="#crates_repository-supported_platform_triples">supported_platform_triples</a>)
crates_repository(<a href="#crates_repository-name">name</a>, <a href="#crates_repository-annotations">annotations</a>, <a href="#crates_repository-cargo_config">cargo_config</a>, <a href="#crates_repository-cargo_lockfile">cargo_lockfile</a>,
<a href="#crates_repository-compressed_windows_toolchain_names">compressed_windows_toolchain_names</a>, <a href="#crates_repository-generate_binaries">generate_binaries</a>, <a href="#crates_repository-generate_build_scripts">generate_build_scripts</a>,
<a href="#crates_repository-generate_target_compatible_with">generate_target_compatible_with</a>, <a href="#crates_repository-generator">generator</a>, <a href="#crates_repository-generator_sha256s">generator_sha256s</a>, <a href="#crates_repository-generator_urls">generator_urls</a>,
<a href="#crates_repository-isolated">isolated</a>, <a href="#crates_repository-lockfile">lockfile</a>, <a href="#crates_repository-manifests">manifests</a>, <a href="#crates_repository-packages">packages</a>, <a href="#crates_repository-quiet">quiet</a>, <a href="#crates_repository-render_config">render_config</a>, <a href="#crates_repository-repin_instructions">repin_instructions</a>,
<a href="#crates_repository-repo_mapping">repo_mapping</a>, <a href="#crates_repository-rust_toolchain_cargo_template">rust_toolchain_cargo_template</a>, <a href="#crates_repository-rust_toolchain_rustc_template">rust_toolchain_rustc_template</a>,
<a href="#crates_repository-rust_version">rust_version</a>, <a href="#crates_repository-splicing_config">splicing_config</a>, <a href="#crates_repository-supported_platform_triples">supported_platform_triples</a>)
</pre>

A rule for defining and downloading Rust dependencies (crates). This rule
Expand Down Expand Up @@ -856,6 +856,7 @@ CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_i
| <a id="crates_repository-annotations"></a>annotations | Extra settings to apply to crates. See [crate.annotation](#crateannotation). | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> List of strings</a> | optional | `{}` |
| <a id="crates_repository-cargo_config"></a>cargo_config | A [Cargo configuration](https://doc.rust-lang.org/cargo/reference/config.html) file | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
| <a id="crates_repository-cargo_lockfile"></a>cargo_lockfile | The path used to store the `crates_repository` specific [Cargo.lock](https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html) file. In the case that your `crates_repository` corresponds directly with an existing `Cargo.toml` file which has a paired `Cargo.lock` file, that `Cargo.lock` file should be used here, which will keep the versions used by cargo and bazel in sync. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="crates_repository-compressed_windows_toolchain_names"></a>compressed_windows_toolchain_names | Wether or not the toolchain names of windows toolchains are expected to be in a `compressed` format. | Boolean | optional | `True` |
| <a id="crates_repository-generate_binaries"></a>generate_binaries | Whether to generate `rust_binary` targets for all the binary crates in every package. By default only the `rust_library` targets are generated. | Boolean | optional | `False` |
| <a id="crates_repository-generate_build_scripts"></a>generate_build_scripts | Whether or not to generate [cargo build scripts](https://doc.rust-lang.org/cargo/reference/build-scripts.html) by default. | Boolean | optional | `True` |
| <a id="crates_repository-generate_target_compatible_with"></a>generate_target_compatible_with | DEPRECATED: Moved to `render_config`. | Boolean | optional | `True` |
Expand Down
Loading

0 comments on commit 8e962a1

Please sign in to comment.