From 2a7bacd40aff2d7253c8001e85ab2c56ff1fdcc1 Mon Sep 17 00:00:00 2001 From: Abraham Zukor Date: Thu, 13 Feb 2025 17:59:32 -0800 Subject: [PATCH 1/4] Basic Intra Workspace Dependency Functionality --- crate_universe/extensions.bzl | 43 +++++++ crate_universe/private/generate_utils.bzl | 36 +++++- crate_universe/src/config.rs | 5 + crate_universe/src/metadata/dependency.rs | 23 +++- .../src/metadata/metadata_annotation.rs | 22 +++- crate_universe/src/rendering.rs | 111 ++++++++++++------ .../src/rendering/template_engine.rs | 5 + .../templates/partials/module/deps_map.j2 | 12 +- 8 files changed, 212 insertions(+), 45 deletions(-) diff --git a/crate_universe/extensions.bzl b/crate_universe/extensions.bzl index aa48cc849b..2478ea9936 100644 --- a/crate_universe/extensions.bzl +++ b/crate_universe/extensions.bzl @@ -492,6 +492,11 @@ def _collect_render_config(module, repository): if default_alias_rule_bzl: config_kwargs["default_alias_rule"] = str(default_alias_rule_bzl) + if "intra_workspace_dependencies_workspace_toml" in config_kwargs: + default_intra_workspace_dependencies_workspace_toml = config_kwargs.pop("intra_workspace_dependencies_workspace_toml") + if default_intra_workspace_dependencies_workspace_toml: + config_kwargs["intra_workspace_dependencies_workspace_toml"] = str(default_intra_workspace_dependencies_workspace_toml) + if "default_alias_rule_name" in config_kwargs: config_kwargs["default_alias_rule"] = "{}:{}".format( config_kwargs.get("default_alias_rule", ""), @@ -1338,6 +1343,44 @@ can be found below where the supported keys for each template can be found in th doc = "Whether to generate `target_compatible_with` annotations on the generated BUILD files. This catches a `target_triple` being targeted that isn't declared in `supported_platform_triples`.", default = True, ), + "intra_workspace_dependencies_workspace_toml": attr.label( + doc = """ + Whether to generate intra (cargo) workspace dependency aliases. + If None (the default), you are responsible for maintaining all of the dependencies + between your crates. If Some, the `crates_repository` will attempt to generate + intra-workspace dependencies. The value should be the label of the workspace `Cargo.toml` file. + This relies on you to have created the library targets for each intra-workspace dependency. + These targets should have the following properties: + - They should be in the same bazel module as the `cargo_lockfile` + - They should have the same name as the library name in your cargo toml. + - They should have the same relative bazel path as their cargo path. For example + if your workspace has the following layout + ```text + [workspace]/ + WORKSPACE.bazel + BUILD.bazel + Cargo.toml + Cargo.Bazel.lock + foo/ + Cargo.toml + BUILD + src/ + main.rs + bar/ + Cargo.toml + BUILD + src/ + lib.rs + ``` + Where `foo/Cargo.toml` contains + ```toml + [dependencies] + bar.path = "../bar" + ``` + Then `bar/BUILD` must define a `rust_library` target names `bar` + """, + default = None, + ), "platforms_template": attr.string( doc = "The base template to use for platform names. See [platforms documentation](https://docs.bazel.build/versions/main/platforms.html). The available format keys are [`{triple}`].", default = "@rules_rust//rust/platform:{triple}", diff --git a/crate_universe/private/generate_utils.bzl b/crate_universe/private/generate_utils.bzl index 54517fb0d1..0a33de5475 100644 --- a/crate_universe/private/generate_utils.bzl +++ b/crate_universe/private/generate_utils.bzl @@ -100,7 +100,8 @@ def render_config( platforms_template = "@rules_rust//rust/platform:{triple}", regen_command = None, vendor_mode = None, - generate_rules_license_metadata = False): + generate_rules_license_metadata = False, + intra_workspace_dependencies_workspace_toml = None): """Various settings used to configure rendered outputs The template parameters each support a select number of format keys. A description of each key @@ -143,6 +144,38 @@ def render_config( regen_command (str, optional): An optional command to demonstrate how generated files should be regenerated. vendor_mode (str, optional): An optional configuration for rendirng content to be rendered into repositories. generate_rules_license_metadata (bool, optional): Whether to generate rules license metedata + intra_workspace_dependencies_workspace_toml (Label, optional): Whether to generate intra (cargo) workspace dependency aliases. + If None (the default), you are responsible for maintaining all of the dependencies + between your crates. If Some, the `crates_repository` will generate intra-workspace dependencies. The value + should be the label of the workspace `Cargo.toml` file. This relies on you to have created the library targets + for each intra-workspace dependency. These targets should have the following properties: + - They should be in the same bazel module as the `cargo_lockfile` + - They should have the same name as the library name in your cargo toml. + - They should have the same relative bazel path as their cargo path. For example + if your workspace has the following layout + ```text + [workspace]/ + WORKSPACE.bazel + BUILD.bazel + Cargo.toml + Cargo.Bazel.lock + foo/ + Cargo.toml + BUILD + src/ + main.rs + bar/ + Cargo.toml + BUILD + src/ + lib.rs + ``` + Where `foo/Cargo.toml` contains + ```toml + [dependencies] + bar.path = "../bar" + ``` + Then `bar/BUILD` must define a `rust_library` target names `bar` Returns: string: A json encoded struct to match the Rust `config::RenderConfig` struct @@ -161,6 +194,7 @@ def render_config( platforms_template = platforms_template, regen_command = regen_command, vendor_mode = vendor_mode, + intra_workspace_dependencies_workspace_toml = intra_workspace_dependencies_workspace_toml, )) def _crate_id(name, version): diff --git a/crate_universe/src/config.rs b/crate_universe/src/config.rs index 5f9b14822e..0f5ef212de 100644 --- a/crate_universe/src/config.rs +++ b/crate_universe/src/config.rs @@ -107,6 +107,10 @@ pub(crate) struct RenderConfig { /// Whether to generate cargo_toml_env_vars targets. /// This is expected to always be true except for bootstrapping. pub(crate) generate_cargo_toml_env_vars: bool, + + /// The label of the cargo lockfile, used to create build aliases for intra-workspace dependencies. + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) intra_workspace_dependencies_workspace_toml: Option