Skip to content

Commit

Permalink
Allow external crates to be aliased to local targets
Browse files Browse the repository at this point in the history
In some cases it may be desirable to alias a public crate to a locally
define rust_library target, even for dependencies of crates that are not
being overriden.

Support this use case by swapping out `alias` for `rust_library` when
a user overrides the target using an annotation.
  • Loading branch information
ograff committed Jun 3, 2024
1 parent e3f6258 commit d4c7613
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 21 deletions.
6 changes: 5 additions & 1 deletion crate_universe/private/crate.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ def _annotation(
rustc_env = None,
rustc_env_files = None,
rustc_flags = None,
shallow_since = None):
shallow_since = None,
override_targets = None):
"""A collection of extra attributes and settings for a particular crate
Args:
Expand Down Expand Up @@ -166,6 +167,8 @@ def _annotation(
rustc_flags (list, optional): A list of strings to set on a crate's `rust_library::rustc_flags` attribute.
shallow_since (str, optional): An optional timestamp used for crates originating from a git repository
instead of a crate registry. This flag optimizes fetching the source code.
override_targets (dict, optional): A dictionary of alternate tagets to use when something depends on this crate to allow
the parent repo to provide its own version of this dependency. Keys can be `proc_marco`, `build_script`, `lib`, `bin`.
Returns:
string: A json encoded string containing the specified version and separately all other inputs.
Expand Down Expand Up @@ -209,6 +212,7 @@ def _annotation(
rustc_env_files = _stringify_list(rustc_env_files),
rustc_flags = rustc_flags,
shallow_since = shallow_since,
override_targets = override_targets,
),
))

Expand Down
4 changes: 4 additions & 0 deletions crate_universe/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ pub(crate) struct CrateAnnotations {

/// Transition rule to use instead of `native.alias()`.
pub(crate) alias_rule: Option<AliasRule>,

/// The crates to use instead of the generated one.
pub(crate) override_targets: Option<BTreeMap<String, Label>>,
}

macro_rules! joined_extra_member {
Expand Down Expand Up @@ -410,6 +413,7 @@ impl Add for CrateAnnotations {
patches: joined_extra_member!(self.patches, rhs.patches, BTreeSet::new, BTreeSet::extend),
extra_aliased_targets: joined_extra_member!(self.extra_aliased_targets, rhs.extra_aliased_targets, BTreeMap::new, BTreeMap::extend),
alias_rule: self.alias_rule.or(rhs.alias_rule),
override_targets: self.override_targets.or(rhs.override_targets),
};

output
Expand Down
10 changes: 10 additions & 0 deletions crate_universe/src/context/crate_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ pub(crate) struct CrateContext {
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub(crate) alias_rule: Option<AliasRule>,

/// Targets to use instead of the default target for the crate.
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
#[serde(default)]
pub(crate) override_targets: BTreeMap<String, Label>,
}

impl CrateContext {
Expand Down Expand Up @@ -497,6 +502,7 @@ impl CrateContext {
disable_pipelining: false,
extra_aliased_targets: BTreeMap::new(),
alias_rule: None,
override_targets: BTreeMap::new(),
}
.with_overrides(extras)
}
Expand Down Expand Up @@ -668,6 +674,10 @@ impl CrateContext {
}
}
}

if let Some(override_targets) = &crate_extra.override_targets {
self.override_targets.extend(override_targets.clone());
}
}

self
Expand Down
4 changes: 4 additions & 0 deletions crate_universe/src/context/platforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ mod test {
disable_pipelining: false,
extra_aliased_targets: BTreeMap::default(),
alias_rule: None,
override_targets: BTreeMap::default(),
};

let configurations =
Expand Down Expand Up @@ -215,6 +216,7 @@ mod test {
disable_pipelining: false,
extra_aliased_targets: BTreeMap::default(),
alias_rule: None,
override_targets: BTreeMap::default(),
}
}

Expand Down Expand Up @@ -300,6 +302,7 @@ mod test {
disable_pipelining: false,
extra_aliased_targets: BTreeMap::default(),
alias_rule: None,
override_targets: BTreeMap::default(),
};

let configurations =
Expand Down Expand Up @@ -365,6 +368,7 @@ mod test {
disable_pipelining: false,
extra_aliased_targets: BTreeMap::default(),
alias_rule: None,
override_targets: BTreeMap::default(),
};

let configurations =
Expand Down
2 changes: 1 addition & 1 deletion crate_universe/src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ mod test {
);

assert_eq!(
Digest("4148d6b336e574a67417f77fb4727e9c1b014a0b5ab90771f57285f96bca0fee".to_owned()),
Digest("610cbb406b7452d32ae31c45ec82cd3b3b1fb184c3411ef613c948d88492441b".to_owned()),
digest,
);
}
Expand Down
91 changes: 72 additions & 19 deletions crate_universe/src/rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,31 +373,69 @@ impl Renderer {
for rule in &krate.targets {
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)?;
starlark.push(Starlark::CargoBuildScript(cargo_build_script));
starlark.push(Starlark::Alias(Alias {
rule: AliasRule::default().rule(),
name: target.crate_name.clone(),
actual: Label::from_str("_bs").unwrap(),
tags: BTreeSet::from(["manual".to_owned()]),
}));
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 {
load("@rules_rust//cargo:defs.bzl", "cargo_build_script");
let cargo_build_script =
self.make_cargo_build_script(platforms, krate, target)?;
starlark.push(Starlark::CargoBuildScript(cargo_build_script));
starlark.push(Starlark::Alias(Alias {
rule: AliasRule::default().rule(),
name: target.crate_name.clone(),
actual: Label::from_str("_bs").unwrap(),
tags: BTreeSet::from(["manual".to_owned()]),
}));
}
}
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));
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 {
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) => {
load("@rules_rust//rust:defs.bzl", "rust_library");
let rust_library = self.make_rust_library(platforms, krate, target)?;
starlark.push(Starlark::RustLibrary(rust_library));
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 {
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) => {
load("@rules_rust//rust:defs.bzl", "rust_binary");
let rust_binary = self.make_rust_binary(platforms, krate, target)?;
starlark.push(Starlark::RustBinary(rust_binary));
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 {
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 Expand Up @@ -967,6 +1005,7 @@ mod test {
disable_pipelining: false,
extra_aliased_targets: BTreeMap::default(),
alias_rule: None,
override_targets: BTreeMap::default(),
},
);

Expand Down Expand Up @@ -1004,6 +1043,7 @@ mod test {
disable_pipelining: true,
extra_aliased_targets: BTreeMap::default(),
alias_rule: None,
override_targets: BTreeMap::default(),
},
);

Expand Down Expand Up @@ -1044,6 +1084,7 @@ mod test {
disable_pipelining: false,
extra_aliased_targets: BTreeMap::default(),
alias_rule: None,
override_targets: BTreeMap::default(),
},
);

Expand Down Expand Up @@ -1084,6 +1125,7 @@ mod test {
disable_pipelining: false,
extra_aliased_targets: BTreeMap::default(),
alias_rule: None,
override_targets: BTreeMap::default(),
},
);

Expand Down Expand Up @@ -1121,6 +1163,7 @@ mod test {
disable_pipelining: false,
extra_aliased_targets: BTreeMap::default(),
alias_rule: None,
override_targets: BTreeMap::default(),
},
);

Expand Down Expand Up @@ -1160,6 +1203,7 @@ mod test {
disable_pipelining: false,
extra_aliased_targets: BTreeMap::default(),
alias_rule: None,
override_targets: BTreeMap::default(),
},
);

Expand Down Expand Up @@ -1214,6 +1258,7 @@ mod test {
disable_pipelining: false,
extra_aliased_targets: BTreeMap::default(),
alias_rule: None,
override_targets: BTreeMap::default(),
},
);

Expand Down Expand Up @@ -1247,6 +1292,7 @@ mod test {
disable_pipelining: false,
extra_aliased_targets: BTreeMap::default(),
alias_rule: None,
override_targets: BTreeMap::default(),
},
);

Expand Down Expand Up @@ -1286,6 +1332,7 @@ mod test {
disable_pipelining: false,
extra_aliased_targets: BTreeMap::default(),
alias_rule: None,
override_targets: BTreeMap::default(),
},
);

Expand Down Expand Up @@ -1337,6 +1384,7 @@ mod test {
disable_pipelining: false,
extra_aliased_targets: BTreeMap::default(),
alias_rule: None,
override_targets: BTreeMap::default(),
},
);

Expand Down Expand Up @@ -1447,6 +1495,7 @@ mod test {
disable_pipelining: false,
extra_aliased_targets: BTreeMap::default(),
alias_rule: None,
override_targets: BTreeMap::default(),
},
);

Expand Down Expand Up @@ -1493,6 +1542,7 @@ mod test {
disable_pipelining: false,
extra_aliased_targets: BTreeMap::default(),
alias_rule: None,
override_targets: BTreeMap::default(),
},
);

Expand Down Expand Up @@ -1545,6 +1595,7 @@ mod test {
repository: None,
license: None,
alias_rule: None,
override_targets: BTreeMap::default(),
},
);

Expand Down Expand Up @@ -1608,6 +1659,7 @@ mod test {
repository: None,
license: None,
alias_rule: None,
override_targets: BTreeMap::default(),
},
);

Expand Down Expand Up @@ -1678,6 +1730,7 @@ mod test {
disable_pipelining: false,
extra_aliased_targets: BTreeMap::default(),
alias_rule: None,
override_targets: BTreeMap::default(),
},
);

Expand Down

0 comments on commit d4c7613

Please sign in to comment.