Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crate_universe: Make module extension restarts less expensive. #2691

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions crate_universe/extension.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ load("//crate_universe:defs.bzl", _crate_universe_crate = "crate")
load("//crate_universe/private:crates_vendor.bzl", "CRATES_VENDOR_ATTRS", "generate_config_file", "generate_splicing_manifest")
load("//crate_universe/private:generate_utils.bzl", "CARGO_BAZEL_GENERATOR_SHA256", "CARGO_BAZEL_GENERATOR_URL", "GENERATOR_ENV_VARS", "render_config")
load("//crate_universe/private:urls.bzl", "CARGO_BAZEL_SHA256S", "CARGO_BAZEL_URLS")
load("//crate_universe/private/module_extensions:cargo_bazel_bootstrap.bzl", "get_cargo_bazel_runner")
load("//crate_universe/private/module_extensions:cargo_bazel_bootstrap.bzl", "get_cargo_bazel_runner", "get_host_cargo_rustc")
load("//rust/platform:triple.bzl", "get_host_triple")

# A list of labels which may be relative (and if so, is within the repo the rule is generated in).
Expand Down Expand Up @@ -228,7 +228,7 @@ def _package_to_json(p):
})

def _get_generator(module_ctx):
"""Query Network Resources to local a `cargo-bazel` binary.
"""Query Network Resources to local a `cargo-bazel` binary.

Based off get_generator in crates_universe/private/generate_utils.bzl

Expand Down Expand Up @@ -276,6 +276,16 @@ def _get_generator(module_ctx):
return output

def _crate_impl(module_ctx):
# Preload all external repositories. Calling `module_ctx.path` will cause restarts of the implementation
# function of the module extension, so we want to trigger all restarts before we start the actual work.
# Once https://github.com/bazelbuild/bazel/issues/22729 has been fixed, this code can be removed.
get_host_cargo_rustc(module_ctx)
for mod in module_ctx.modules:
for cfg in mod.tags.from_cargo:
module_ctx.path(cfg.cargo_lockfile)
for m in cfg.manifests:
module_ctx.path(m)

cargo_bazel_output = _get_generator(module_ctx)
cargo_bazel = get_cargo_bazel_runner(module_ctx, cargo_bazel_output)

Expand Down
21 changes: 16 additions & 5 deletions crate_universe/private/module_extensions/cargo_bazel_bootstrap.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,32 @@ cargo_bazel_bootstrap = module_extension(
doc = """Module extension to generate the cargo_bazel binary.""",
)

def get_cargo_bazel_runner(module_ctx, cargo_bazel):
"""A helper function to allow executing cargo_bazel in module extensions.
def get_host_cargo_rustc(module_ctx):
"""A helper function to get the path to the host cargo and rustc binaries.

Args:
module_ctx: The module extension's context.
cargo_bazel: Path The path to a `cargo-bazel` binary
Returns:
A function that can be called to execute cargo_bazel.
"""
A tuple of path to cargo, path to rustc.

"""
host_triple = get_host_triple(module_ctx)
binary_ext = system_to_binary_ext(host_triple.system)

cargo_path = str(module_ctx.path(Label("@rust_host_tools//:bin/cargo{}".format(binary_ext))))
rustc_path = str(module_ctx.path(Label("@rust_host_tools//:bin/rustc{}".format(binary_ext))))
return cargo_path, rustc_path

def get_cargo_bazel_runner(module_ctx, cargo_bazel):
"""A helper function to allow executing cargo_bazel in module extensions.

Args:
module_ctx: The module extension's context.
cargo_bazel: Path The path to a `cargo-bazel` binary
Returns:
A function that can be called to execute cargo_bazel.
"""
cargo_path, rustc_path = get_host_cargo_rustc(module_ctx)

# Placing this as a nested function allows users to call this right at the
# start of a module extension, thus triggering any restarts as early as
Expand Down
Loading