Skip to content

Commit 841a733

Browse files
authored
Support artifact dependencies in crate.spec (#2229)
Followup to #2226. That PR made bindeps work for crates repositories that use `manifests`. https://github.com/bazelbuild/rules_rust/blob/d86faee8b85cc334e0529193142f4b61362e79a3/examples/crate_universe/WORKSPACE.bazel#L60 This PR makes it work for crates repositories that use `packages`. https://github.com/bazelbuild/rules_rust/blob/43b101e6b3e679b333c2622d7615233edf344624/examples/crate_universe/WORKSPACE.bazel#L317-L320
1 parent d86faee commit 841a733

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

crate_universe/private/crate.bzl

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def _workspace_member(version, sha256 = None):
1818
def _spec(
1919
package = None,
2020
version = None,
21+
artifact = None,
22+
lib = None,
2123
default_features = True,
2224
features = [],
2325
git = None,
@@ -33,6 +35,8 @@ def _spec(
3335
Args:
3436
package (str, optional): The explicit name of the package (used when attempting to alias a crate).
3537
version (str, optional): The exact version of the crate. Cannot be used with `git`.
38+
artifact (str, optional): Set to "bin" to pull in a binary crate as an artifact dependency. Requires a nightly Cargo.
39+
lib (bool, optional): If using `artifact = "bin"`, additionally setting `lib = True` declares a dependency on both the package's library and binary, as opposed to just the binary.
3640
default_features (bool, optional): Maps to the `default-features` flag.
3741
features (list, optional): A list of features to use for the crate
3842
git (str, optional): The Git url to use for the crate. Cannot be used with `version`.
@@ -43,16 +47,25 @@ def _spec(
4347
Returns:
4448
string: A json encoded string of all inputs
4549
"""
46-
return json.encode(struct(
47-
package = package,
48-
default_features = default_features,
49-
features = features,
50-
version = version,
51-
git = git,
52-
branch = branch,
53-
tag = tag,
54-
rev = rev,
55-
))
50+
return json.encode({
51+
k: v
52+
for k, v in {
53+
"artifact": artifact,
54+
"branch": branch,
55+
"default_features": default_features,
56+
"features": features,
57+
"git": git,
58+
"lib": lib,
59+
"package": package,
60+
"rev": rev,
61+
"tag": tag,
62+
"version": version,
63+
}.items()
64+
# The `cargo_toml` crate parses unstable fields to a flattened
65+
# BTreeMap<String, toml::Value> and toml::Value does not support null,
66+
# so we must omit null values.
67+
if v != None
68+
})
5669

5770
def _assert_absolute(label):
5871
"""Ensure a given label is an absolute label

docs/crate_universe.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ list: A list of labels to generated rust targets (str)
569569
## crate.spec
570570

571571
<pre>
572-
crate.spec(<a href="#crate.spec-package">package</a>, <a href="#crate.spec-version">version</a>, <a href="#crate.spec-default_features">default_features</a>, <a href="#crate.spec-features">features</a>, <a href="#crate.spec-git">git</a>, <a href="#crate.spec-branch">branch</a>, <a href="#crate.spec-tag">tag</a>, <a href="#crate.spec-rev">rev</a>)
572+
crate.spec(<a href="#crate.spec-package">package</a>, <a href="#crate.spec-version">version</a>, <a href="#crate.spec-artifact">artifact</a>, <a href="#crate.spec-lib">lib</a>, <a href="#crate.spec-default_features">default_features</a>, <a href="#crate.spec-features">features</a>, <a href="#crate.spec-git">git</a>, <a href="#crate.spec-branch">branch</a>, <a href="#crate.spec-tag">tag</a>, <a href="#crate.spec-rev">rev</a>)
573573
</pre>
574574

575575
A constructor for a crate dependency.
@@ -586,6 +586,8 @@ See [specifying dependencies][sd] in the Cargo book for more details.
586586
| :------------- | :------------- | :------------- |
587587
| <a id="crate.spec-package"></a>package | The explicit name of the package (used when attempting to alias a crate). | `None` |
588588
| <a id="crate.spec-version"></a>version | The exact version of the crate. Cannot be used with <code>git</code>. | `None` |
589+
| <a id="crate.spec-artifact"></a>artifact | Set to "bin" to pull in a binary crate as an artifact dependency. Requires a nightly Cargo. | `None` |
590+
| <a id="crate.spec-lib"></a>lib | If using <code>artifact = "bin"</code>, additionally setting <code>lib = True</code> declares a dependency on both the package's library and binary, as opposed to just the binary. | `None` |
589591
| <a id="crate.spec-default_features"></a>default_features | Maps to the <code>default-features</code> flag. | `True` |
590592
| <a id="crate.spec-features"></a>features | A list of features to use for the crate | `[]` |
591593
| <a id="crate.spec-git"></a>git | The Git url to use for the crate. Cannot be used with <code>version</code>. | `None` |

0 commit comments

Comments
 (0)