Skip to content

Commit

Permalink
Moved generate_target_compatible_with into render_config. (#2329)
Browse files Browse the repository at this point in the history
  • Loading branch information
rickvanprim authored Dec 14, 2023
1 parent 1cd9061 commit 2a5ca6a
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 126 deletions.
5 changes: 1 addition & 4 deletions crate_universe/private/crates_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,7 @@ CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_i
default = True,
),
"generate_target_compatible_with": attr.bool(
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."
),
doc = "DEPRECATED: Moved to `render_config`.",
default = True,
),
"generator": attr.string(
Expand Down
35 changes: 17 additions & 18 deletions crate_universe/private/crates_vendor.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ call {bin} {args} %@%

CARGO_BAZEL_GENERATOR_PATH = "CARGO_BAZEL_GENERATOR_PATH"

def _default_render_config():
return json.decode(render_config())

def _runfiles_path(file, is_windows):
if is_windows:
runtime_pwd_var = "%RUNTIME_PWD%"
Expand Down Expand Up @@ -153,7 +156,7 @@ def _write_config_file(ctx):
repository_name = ctx.attr.repository_name,
output_pkg = _get_output_package(ctx),
workspace_name = workspace_name,
rendering_config = dict(json.decode(ctx.attr.render_config)) if ctx.attr.render_config else None,
render_config = dict(json.decode(ctx.attr.render_config)) if ctx.attr.render_config else None,
),
)

Expand All @@ -173,7 +176,7 @@ def generate_config_file(
repository_name,
output_pkg,
workspace_name,
rendering_config):
render_config):
"""Writes the rendering config to cargo-bazel-config.json.
Args:
Expand All @@ -182,21 +185,20 @@ def generate_config_file(
annotations: Any annotations provided.
generate_binaries (bool): Whether to generate binaries for the crates.
generate_build_scripts (bool): Whether to generate BUILD.bazel files.
generate_target_compatible_with (bool): Whether to generate
`target_compatible_with` annotations on generated BUILD files.
generate_target_compatible_with (bool): DEPRECATED: Moved to `render_config`.
supported_platform_triples (str): The platform triples to support in
the generated BUILD.bazel files.
repository_name (str): The name of the repository to generate.
output_pkg: The path to the package containing the build files.
workspace_name (str): The name of the workspace.
rendering_config: The rendering config to use.
render_config: The render config to use.
Returns:
file: The cargo-bazel-config.json written.
"""
default_render_config = json.decode(render_config())
if rendering_config == None:
rendering_config = default_render_config
default_render_config = _default_render_config()
if render_config == None:
render_config = default_render_config

if mode == "local":
build_file_base_template = "@{}//{}/{{name}}-{{version}}:BUILD.bazel"
Expand All @@ -205,7 +207,7 @@ def generate_config_file(
)
else:
build_file_base_template = "@{}//{}:BUILD.{{name}}-{{version}}.bazel"
crate_label_template = rendering_config["crate_label_template"]
crate_label_template = render_config["crate_label_template"]

updates = {
"build_file_template": build_file_base_template.format(
Expand All @@ -221,25 +223,25 @@ def generate_config_file(
}

for key in updates:
if rendering_config[key] != default_render_config[key]:
if render_config[key] != default_render_config[key]:
fail("The `crates_vendor.render_config` attribute does not support the `{}` parameter. Please update {} to remove this value.".format(
key,
ctx.label,
))

rendering_config.update(updates)
render_config.update(updates)

# Allow users to override the regen command.
if "regen_command" not in rendering_config or not rendering_config["regen_command"]:
rendering_config.update({"regen_command": "bazel run {}".format(ctx.label)})
if "regen_command" not in render_config or not render_config["regen_command"]:
render_config.update({"regen_command": "bazel run {}".format(ctx.label)})

config_data = compile_config(
crate_annotations = annotations,
generate_binaries = generate_binaries,
generate_build_scripts = generate_build_scripts,
generate_target_compatible_with = generate_target_compatible_with,
cargo_config = None,
render_config = rendering_config,
render_config = render_config,
supported_platform_triples = supported_platform_triples,
repository_name = repository_name or ctx.label.name,
)
Expand Down Expand Up @@ -383,10 +385,7 @@ CRATES_VENDOR_ATTRS = {
default = True,
),
"generate_target_compatible_with": attr.bool(
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."
),
doc = "DEPRECATED: Moved to `render_config`.",
default = True,
),
"manifests": attr.label_list(
Expand Down
14 changes: 12 additions & 2 deletions crate_universe/private/generate_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def render_config(
crate_repository_template = "{repository}__{name}-{version}",
crates_module_template = "//:{file}",
default_package_name = None,
generate_target_compatible_with = True,
platforms_template = "@rules_rust//rust/platform:{triple}",
regen_command = None,
vendor_mode = None):
Expand Down Expand Up @@ -114,6 +115,9 @@ def render_config(
file names used for the crates module. The available format keys are [`{file}`].
default_package_name (str, optional): The default package name to use in the rendered macros. This affects the
auto package detection of things like `all_crate_deps`.
generate_target_compatible_with (bool, optional): 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`.
platforms_template (str, optional): 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}`].
Expand All @@ -129,6 +133,7 @@ def render_config(
crate_repository_template = crate_repository_template,
crates_module_template = crates_module_template,
default_package_name = default_package_name,
generate_target_compatible_with = generate_target_compatible_with,
platforms_template = platforms_template,
regen_command = regen_command,
vendor_mode = vendor_mode,
Expand Down Expand Up @@ -228,7 +233,7 @@ def compile_config(
`crates_repository.annotations` or `crates_vendor.annotations`.
generate_binaries (bool): Whether to generate `rust_binary` targets for all bins.
generate_build_scripts (bool): Whether or not to globally disable build scripts.
generate_target_compatible_with (bool): Whether to emit `target_compatible_with` on generated rules
generate_target_compatible_with (bool): DEPRECATED: Moved to `render_config`.
cargo_config (str): The optional contents of a [Cargo config][cargo_config].
render_config (dict): The deserialized dict of the `render_config` function.
supported_platform_triples (list): A list of platform triples
Expand Down Expand Up @@ -258,10 +263,15 @@ def compile_config(
if unexpected:
fail("The following annotations use `additive_build_file` which is not supported for {}: {}".format(repository_name, unexpected))

# Deprecated: Apply `generate_target_compatible_with` to `render_config`.
if not generate_target_compatible_with:
# buildifier: disable=print
print("DEPRECATED: 'generate_target_compatible_with' has been moved to 'render_config'")
render_config.update({"generate_target_compatible_with": False})

config = struct(
generate_binaries = generate_binaries,
generate_build_scripts = generate_build_scripts,
generate_target_compatible_with = generate_target_compatible_with,
annotations = annotations,
cargo_config = cargo_config,
rendering = _update_render_config(
Expand Down
9 changes: 2 additions & 7 deletions crate_universe/src/cli/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,8 @@ pub fn generate(opt: GenerateOptions) -> Result<()> {
let context = Context::try_from_path(lockfile)?;

// Render build files
let outputs = Renderer::new(
config.rendering,
config.supported_platform_triples,
config.generate_target_compatible_with,
)
.render(&context)?;
let outputs = Renderer::new(config.rendering, config.supported_platform_triples)
.render(&context)?;

// Write the outputs to disk
write_outputs(outputs, &opt.repository_dir, opt.dry_run)?;
Expand Down Expand Up @@ -116,7 +112,6 @@ pub fn generate(opt: GenerateOptions) -> Result<()> {
let outputs = Renderer::new(
config.rendering.clone(),
config.supported_platform_triples.clone(),
config.generate_target_compatible_with,
)
.render(&context)?;

Expand Down
1 change: 0 additions & 1 deletion crate_universe/src/cli/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ pub fn vendor(opt: VendorOptions) -> Result<()> {
let outputs = Renderer::new(
config.rendering.clone(),
config.supported_platform_triples.clone(),
config.generate_target_compatible_with,
)
.render(&context)?;

Expand Down
24 changes: 10 additions & 14 deletions crate_universe/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ pub struct RenderConfig {
/// In general, this should be be unset to allow the macros to do auto-detection in the analysis phase.
pub default_package_name: Option<String>,

/// 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`.
#[serde(default = "default_generate_target_compatible_with")]
pub generate_target_compatible_with: bool,

/// The pattern to use for platform constraints.
/// Eg. `@rules_rust//rust/platform:{triple}`.
#[serde(default = "default_platforms_template")]
Expand All @@ -95,6 +100,7 @@ impl Default for RenderConfig {
crates_module_template: default_crates_module_template(),
crate_repository_template: default_crate_repository_template(),
default_package_name: Option::default(),
generate_target_compatible_with: default_generate_target_compatible_with(),
platforms_template: default_platforms_template(),
regen_command: String::default(),
vendor_mode: Option::default(),
Expand Down Expand Up @@ -122,6 +128,10 @@ fn default_platforms_template() -> String {
"@rules_rust//rust/platform:{triple}".to_owned()
}

fn default_generate_target_compatible_with() -> bool {
true
}

/// A representation of some Git identifier used to represent the "revision" or "pin" of a checkout.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Commitish {
Expand Down Expand Up @@ -615,13 +625,6 @@ pub struct Config {
/// Whether or not to generate Cargo build scripts by default
pub generate_build_scripts: bool,

/// A set of platform triples to use in generated select statements
#[serde(
default = "default_generate_target_compatible_with",
skip_serializing_if = "skip_generate_target_compatible_with"
)]
pub generate_target_compatible_with: bool,

/// Additional settings to apply to generated crates
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub annotations: BTreeMap<CrateId, CrateAnnotations>,
Expand All @@ -644,13 +647,6 @@ impl Config {
}
}

fn default_generate_target_compatible_with() -> bool {
true
}
fn skip_generate_target_compatible_with(value: &bool) -> bool {
*value
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
8 changes: 4 additions & 4 deletions crate_universe/src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ mod test {
);

assert_eq!(
Digest("8f0083824e10a40624cd13ddff0075766243dd6360c02f1f8f1f6f46bd48f923".to_owned()),
Digest("379610c3d0f58778cb066f51da66894f10d0e7ea903e4621c61534b4d1344e6f".to_owned()),
digest,
);
}
Expand Down Expand Up @@ -256,7 +256,7 @@ mod test {
);

assert_eq!(
Digest("8ea331f11b949e4a27606ca68a63706ca78b47863400f416d69dbd3e9cc0981b".to_owned()),
Digest("c4d5c9def86c1af758a29f144d8d9ab66b1c762d36f878d1e7f9b6e09782c512".to_owned()),
digest,
);
}
Expand Down Expand Up @@ -287,7 +287,7 @@ mod test {
);

assert_eq!(
Digest("fec0aa7bad4eb7b194540873f0b7ff971fcc8f9e50fd845de30995d75fd18bb2".to_owned()),
Digest("ab158c3dd56e1771bfaed167c661f9c6c33f1effdf6d870f80640384ad1bffaf".to_owned()),
digest,
);
}
Expand Down Expand Up @@ -336,7 +336,7 @@ mod test {
);

assert_eq!(
Digest("924160ca64c021d10a1f52b0a3171b16cc4d3f2ac672c5336fd68c668b531a83".to_owned()),
Digest("b707269f173b2f78ae500317f9ae54df3c05c88972531c277045d0eb756fc681".to_owned()),
digest,
);
}
Expand Down
Loading

0 comments on commit 2a5ca6a

Please sign in to comment.