Skip to content

Commit

Permalink
[Crate Universe] Add support for package overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
rdelfin committed Oct 31, 2022
1 parent 478fc3a commit 616aaef
Show file tree
Hide file tree
Showing 15 changed files with 2,890 additions and 1,840 deletions.
4 changes: 4 additions & 0 deletions crate_universe/private/crate.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def _annotation(
data = None,
data_glob = None,
deps = None,
dep_overrides = None,
gen_build_script = None,
patch_args = None,
patch_tool = None,
Expand Down Expand Up @@ -114,6 +115,8 @@ def _annotation(
data (list, optional): A list of labels to add to a crate's `rust_library::data` attribute.
data_glob (list, optional): A list of glob patterns to add to a crate's `rust_library::data` attribute.
deps (list, optional): A list of labels to add to a crate's `rust_library::deps` attribute.
dep_overrides (dict, optional): A dictionary of of crate names that should be replaced with a Bazel dependency
in the `rust_library::deps` attribute.
gen_build_script (bool, optional): An authorative flag to determine whether or not to produce
`cargo_build_script` targets for the current crate.
patch_args (list, optional): The `patch_args` attribute of a Bazel repository rule. See
Expand Down Expand Up @@ -159,6 +162,7 @@ def _annotation(
data = data,
data_glob = data_glob,
deps = deps,
dep_overrides = dep_overrides,
gen_build_script = gen_build_script,
patch_args = patch_args,
patch_tool = patch_tool,
Expand Down
5 changes: 5 additions & 0 deletions crate_universe/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ pub struct CrateAnnotations {
/// [deps](https://bazelbuild.github.io/rules_rust/defs.html#rust_library-deps) attribute.
pub deps: Option<BTreeSet<String>>,

/// A dictionary of of crate names that should be replaced with a Bazel dependency in the
/// [deps](https://bazelbuild.github.io/rules_rust/defs.html#rust_library-deps) attribute.
pub dep_overrides: Option<BTreeMap<String, String>>,

/// Additional data to pass to
/// [proc_macro_deps](https://bazelbuild.github.io/rules_rust/defs.html#rust_library-proc_macro_deps) attribute.
pub proc_macro_deps: Option<BTreeSet<String>>,
Expand Down Expand Up @@ -293,6 +297,7 @@ impl Add for CrateAnnotations {
let output = CrateAnnotations {
gen_build_script,
deps: joined_extra_member!(self.deps, rhs.deps, BTreeSet::new, BTreeSet::extend),
dep_overrides: joined_extra_member!(self.dep_overrides, rhs.dep_overrides, BTreeMap::new, BTreeMap::extend),
proc_macro_deps: joined_extra_member!(self.proc_macro_deps, rhs.proc_macro_deps, BTreeSet::new, BTreeSet::extend),
crate_features: joined_extra_member!(self.crate_features, rhs.crate_features, BTreeSet::new, BTreeSet::extend),
data: joined_extra_member!(self.data, rhs.data, BTreeSet::new, BTreeSet::extend),
Expand Down
11 changes: 11 additions & 0 deletions crate_universe/src/context/crate_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,17 @@ impl CrateContext {
}
}

// Dependency overrides
if let Some(dep_overrides) = &crate_extra.dep_overrides {
for (crate_name, new_dep) in dep_overrides {
let dep_filter = |dep: &CrateDependency| &dep.id.name == crate_name;
if self.common_attrs.deps.any_matches(dep_filter) {
self.common_attrs.deps.remove_if(dep_filter);
self.common_attrs.extra_deps.insert(new_dep.clone());
}
}
}

// Compile data glob
if let Some(extra) = &crate_extra.compile_data_glob {
self.common_attrs.compile_data_glob.extend(extra.clone());
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 @@ -252,7 +252,7 @@ mod test {

assert_eq!(
digest,
Digest("756a613410573552bb8a85d6fcafd24a9df3000b8d943bf74c38bda9c306ef0e".to_owned())
Digest("fc2781c16252a8a893e1110bcc901b80ec6d58a73442ebd7dc1abdee516fce5e".to_owned())
);
}

Expand Down
25 changes: 25 additions & 0 deletions crate_universe/src/utils/starlark/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,31 @@ impl<T: Ord> SelectList<T> {
};
}

pub fn any_matches<F>(&mut self, mut match_fn: F) -> bool
where
F: FnMut(&T) -> bool,
{
self.common.iter().any(&mut match_fn)
|| self
.selects
.iter()
.any(|(_, set)| set.iter().any(&mut match_fn))
}

pub fn remove_if<F>(&mut self, mut filter: F)
where
F: FnMut(&T) -> bool,
{
for (_cfg, set) in self.selects.iter_mut() {
if set.iter().any(&mut filter) {
set.retain(|item| !filter(item));
}
}
if self.common.iter().any(&mut filter) {
self.common.retain(|item| !filter(item));
}
}

// TODO: This should probably be added to the [Select] trait
pub fn get_iter<'a>(&'a self, config: Option<&String>) -> Option<btree_set::Iter<T>> {
match config {
Expand Down
5 changes: 3 additions & 2 deletions docs/crate_universe.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ crate.annotation(<a href="#crate.annotation-version">version</a>, <a href="#crat
<a href="#crate.annotation-build_script_tools">build_script_tools</a>, <a href="#crate.annotation-build_script_data_glob">build_script_data_glob</a>, <a href="#crate.annotation-build_script_deps">build_script_deps</a>, <a href="#crate.annotation-build_script_env">build_script_env</a>,
<a href="#crate.annotation-build_script_proc_macro_deps">build_script_proc_macro_deps</a>, <a href="#crate.annotation-build_script_rustc_env">build_script_rustc_env</a>, <a href="#crate.annotation-build_script_toolchains">build_script_toolchains</a>,
<a href="#crate.annotation-compile_data">compile_data</a>, <a href="#crate.annotation-compile_data_glob">compile_data_glob</a>, <a href="#crate.annotation-crate_features">crate_features</a>, <a href="#crate.annotation-data">data</a>, <a href="#crate.annotation-data_glob">data_glob</a>, <a href="#crate.annotation-deps">deps</a>,
<a href="#crate.annotation-gen_build_script">gen_build_script</a>, <a href="#crate.annotation-patch_args">patch_args</a>, <a href="#crate.annotation-patch_tool">patch_tool</a>, <a href="#crate.annotation-patches">patches</a>, <a href="#crate.annotation-proc_macro_deps">proc_macro_deps</a>, <a href="#crate.annotation-rustc_env">rustc_env</a>,
<a href="#crate.annotation-rustc_env_files">rustc_env_files</a>, <a href="#crate.annotation-rustc_flags">rustc_flags</a>, <a href="#crate.annotation-shallow_since">shallow_since</a>)
<a href="#crate.annotation-dep_overrides">dep_overrides</a>, <a href="#crate.annotation-gen_build_script">gen_build_script</a>, <a href="#crate.annotation-patch_args">patch_args</a>, <a href="#crate.annotation-patch_tool">patch_tool</a>, <a href="#crate.annotation-patches">patches</a>, <a href="#crate.annotation-proc_macro_deps">proc_macro_deps</a>,
<a href="#crate.annotation-rustc_env">rustc_env</a>, <a href="#crate.annotation-rustc_env_files">rustc_env_files</a>, <a href="#crate.annotation-rustc_flags">rustc_flags</a>, <a href="#crate.annotation-shallow_since">shallow_since</a>)
</pre>

A collection of extra attributes and settings for a particular crate
Expand All @@ -532,6 +532,7 @@ A collection of extra attributes and settings for a particular crate
| <a id="crate.annotation-data"></a>data | A list of labels to add to a crate's <code>rust_library::data</code> attribute. | <code>None</code> |
| <a id="crate.annotation-data_glob"></a>data_glob | A list of glob patterns to add to a crate's <code>rust_library::data</code> attribute. | <code>None</code> |
| <a id="crate.annotation-deps"></a>deps | A list of labels to add to a crate's <code>rust_library::deps</code> attribute. | <code>None</code> |
| <a id="crate.annotation-dep_overrides"></a>dep_overrides | A dictionary of of crate names that should be replaced with a Bazel dependency in the <code>rust_library::deps</code> attribute. | <code>None</code> |
| <a id="crate.annotation-gen_build_script"></a>gen_build_script | An authorative flag to determine whether or not to produce <code>cargo_build_script</code> targets for the current crate. | <code>None</code> |
| <a id="crate.annotation-patch_args"></a>patch_args | The <code>patch_args</code> attribute of a Bazel repository rule. See [http_archive.patch_args](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patch_args) | <code>None</code> |
| <a id="crate.annotation-patch_tool"></a>patch_tool | The <code>patch_tool</code> attribute of a Bazel repository rule. See [http_archive.patch_tool](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patch_tool) | <code>None</code> |
Expand Down
64 changes: 32 additions & 32 deletions examples/crate_universe/cargo_aliases/Cargo.Bazel.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ version = 3

[[package]]
name = "aho-corasick"
version = "0.7.18"
version = "0.7.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
checksum = "b4f55bd91a0978cbfd91c457a164bab8b4001c833b7f323132c0a4e1922dd44e"
dependencies = [
"memchr",
]
Expand Down Expand Up @@ -54,9 +54,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"

[[package]]
name = "clap"
version = "3.2.8"
version = "3.2.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "190814073e85d238f31ff738fcb0bf6910cedeb73376c87cd69291028966fd83"
checksum = "71655c45cb9845d3270c9d6df84ebe72b4dad3c2ba3f7023ad47c144e4e473a5"
dependencies = [
"atty",
"bitflags",
Expand All @@ -71,9 +71,9 @@ dependencies = [

[[package]]
name = "clap_derive"
version = "3.2.7"
version = "3.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "759bf187376e1afa7b85b959e6a664a3e7a95203415dba952ad19139e798f902"
checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65"
dependencies = [
"heck",
"proc-macro-error",
Expand All @@ -93,19 +93,19 @@ dependencies = [

[[package]]
name = "ctor"
version = "0.1.22"
version = "0.1.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f877be4f7c9f246b183111634f75baa039715e3f46ce860677d3b19a69fb229c"
checksum = "6d2301688392eb071b0bf1a37be05c469d3cc4dbbd95df672fe28ab021e6a096"
dependencies = [
"quote",
"syn",
]

[[package]]
name = "env_logger"
version = "0.9.0"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3"
checksum = "c90bf5f19754d10198ccb95b70664fc925bd1fc090a0fd9a6ebc54acc8cd6272"
dependencies = [
"atty",
"humantime",
Expand All @@ -116,9 +116,9 @@ dependencies = [

[[package]]
name = "getrandom"
version = "0.2.7"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6"
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
dependencies = [
"cfg-if",
"libc",
Expand All @@ -127,9 +127,9 @@ dependencies = [

[[package]]
name = "hashbrown"
version = "0.12.1"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db0d4cf898abf0081f964436dc980e96670a0f36863e4b83aaacdb65c9d7ccc3"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"

[[package]]
name = "heck"
Expand Down Expand Up @@ -164,9 +164,9 @@ dependencies = [

[[package]]
name = "libc"
version = "0.2.126"
version = "0.2.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89"

[[package]]
name = "log"
Expand Down Expand Up @@ -213,15 +213,15 @@ dependencies = [

[[package]]
name = "once_cell"
version = "1.13.0"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1"
checksum = "e82dad04139b71a90c080c8463fe0dc7902db5192d939bd0950f074d014339e1"

[[package]]
name = "os_str_bytes"
version = "6.1.0"
version = "6.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21326818e99cfe6ce1e524c2a805c189a99b5ae555a35d19f9a284b427d86afa"
checksum = "3baf96e39c5359d2eb0dd6ccb42c62b91d9678aa68160d261b9e0ccbf9e9dea9"

[[package]]
name = "ppv-lite86"
Expand Down Expand Up @@ -255,18 +255,18 @@ dependencies = [

[[package]]
name = "proc-macro2"
version = "1.0.40"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd96a1e8ed2596c337f8eae5f24924ec83f5ad5ab21ea8e455d3566c69fbcaf7"
checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725"
dependencies = [
"unicode-ident",
]

[[package]]
name = "quote"
version = "1.0.20"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
dependencies = [
"proc-macro2",
]
Expand Down Expand Up @@ -294,9 +294,9 @@ dependencies = [

[[package]]
name = "rand_core"
version = "0.6.3"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
Expand Down Expand Up @@ -326,9 +326,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"

[[package]]
name = "syn"
version = "1.0.98"
version = "1.0.103"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c50aef8a904de4c23c788f104b7dddc7d6f79c647c7c8ce4cc8f73eb0ca773dd"
checksum = "a864042229133ada95abf3b54fdc62ef5ccabe9515b64717bcb9a1919e59445d"
dependencies = [
"proc-macro2",
"quote",
Expand All @@ -346,15 +346,15 @@ dependencies = [

[[package]]
name = "textwrap"
version = "0.15.0"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d"

[[package]]
name = "unicode-ident"
version = "1.0.1"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c"
checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"

[[package]]
name = "value-bag"
Expand Down
Loading

0 comments on commit 616aaef

Please sign in to comment.