Skip to content

Commit

Permalink
Merge pull request #1 from illicitonion/overrides-patch0
Browse files Browse the repository at this point in the history
Externalise alias keys
  • Loading branch information
ograff authored Jun 6, 2024
2 parents 2afb44c + 24156c4 commit 8c494c8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 41 deletions.
22 changes: 22 additions & 0 deletions crate_universe/src/context/crate_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ pub(crate) enum Rule {
BuildScript(TargetAttributes),
}

impl Rule {
/// The keys that can be used in override_targets to override these Rule sources.
/// These intentionally match the accepted `Target.kind`s returned by cargo-metadata.
pub(crate) fn override_target_key(&self) -> &'static str {
match self {
Self::Library(..) => "lib",
Self::ProcMacro(..) => "proc-macro",
Self::Binary(..) => "bin",
Self::BuildScript(..) => "custom-build",
}
}

pub(crate) fn crate_name(&self) -> &str {
match self {
Self::Library(attrs)
| Self::ProcMacro(attrs)
| Self::Binary(attrs)
| Self::BuildScript(attrs) => attrs.crate_name,
}
}
}

/// A set of attributes common to most `rust_library`, `rust_proc_macro`, and other
/// [core rules of `rules_rust`](https://bazelbuild.github.io/rules_rust/defs.html).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand Down
54 changes: 13 additions & 41 deletions crate_universe/src/rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,17 +371,16 @@ impl Renderer {
}

for rule in &krate.targets {
match rule {
Rule::BuildScript(target) => {
if let Some(alternate_build_script) = krate.override_targets.get("build_script")
{
starlark.push(Starlark::Alias(Alias {
rule: AliasRule::default().rule(),
name: target.crate_name.clone(),
actual: alternate_build_script.clone(),
tags: BTreeSet::from(["manual".to_owned()]),
}));
} else {
if let Some(override_target) = krate.override_targets.get(rule.override_target_key()) {
starlark.push(Starlark::Alias(Alias {
rule: AliasRule::default().rule(),
name: target.crate_name().clone(),
actual: override_target.clone(),
tags: BTreeSet::from(["manual".to_owned()]),
}));
} else {
match rule {
Rule::BuildScript(target) => {
load("@rules_rust//cargo:defs.bzl", "cargo_build_script");
let cargo_build_script =
self.make_cargo_build_script(platforms, krate, target)?;
Expand All @@ -393,45 +392,18 @@ impl Renderer {
tags: BTreeSet::from(["manual".to_owned()]),
}));
}
}
Rule::ProcMacro(target) => {
if let Some(alternate_proc_macro) = krate.override_targets.get("proc_macro") {
starlark.push(Starlark::Alias(Alias {
rule: AliasRule::default().rule(),
name: target.crate_name.clone(),
actual: alternate_proc_macro.clone(),
tags: BTreeSet::from(["manual".to_owned()]),
}));
} else {
Rule::ProcMacro(target) => {
load("@rules_rust//rust:defs.bzl", "rust_proc_macro");
let rust_proc_macro =
self.make_rust_proc_macro(platforms, krate, target)?;
starlark.push(Starlark::RustProcMacro(rust_proc_macro));
}
}
Rule::Library(target) => {
if let Some(alternate_library) = krate.override_targets.get("lib") {
starlark.push(Starlark::Alias(Alias {
rule: AliasRule::default().rule(),
name: target.crate_name.clone(),
actual: alternate_library.clone(),
tags: BTreeSet::from(["manual".to_owned()]),
}));
} else {
Rule::Library(target) => {
load("@rules_rust//rust:defs.bzl", "rust_library");
let rust_library = self.make_rust_library(platforms, krate, target)?;
starlark.push(Starlark::RustLibrary(rust_library));
}
}
Rule::Binary(target) => {
if let Some(alternate_binary) = krate.override_targets.get("bin") {
starlark.push(Starlark::Alias(Alias {
rule: AliasRule::default().rule(),
name: target.crate_name.clone(),
actual: alternate_binary.clone(),
tags: BTreeSet::from(["manual".to_owned()]),
}));
} else {
Rule::Binary(target) => {
load("@rules_rust//rust:defs.bzl", "rust_binary");
let rust_binary = self.make_rust_binary(platforms, krate, target)?;
starlark.push(Starlark::RustBinary(rust_binary));
Expand Down

0 comments on commit 8c494c8

Please sign in to comment.