Skip to content

Convert cxxbridge-cmd dependency to bindep #2230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions crate_universe/private/crate.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def _workspace_member(version, sha256 = None):
def _spec(
package = None,
version = None,
artifact = None,
lib = None,
default_features = True,
features = [],
git = None,
Expand All @@ -33,6 +35,8 @@ def _spec(
Args:
package (str, optional): The explicit name of the package (used when attempting to alias a crate).
version (str, optional): The exact version of the crate. Cannot be used with `git`.
artifact (str, optional): Set to "bin" to pull in a binary crate as an artifact dependency.
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.
default_features (bool, optional): Maps to the `default-features` flag.
features (list, optional): A list of features to use for the crate
git (str, optional): The Git url to use for the crate. Cannot be used with `version`.
Expand All @@ -43,16 +47,25 @@ def _spec(
Returns:
string: A json encoded string of all inputs
"""
return json.encode(struct(
package = package,
default_features = default_features,
features = features,
version = version,
git = git,
branch = branch,
tag = tag,
rev = rev,
))
return json.encode({
k: v
for k, v in {
"package": package,
"version": version,
"artifact": artifact,
"lib": lib,
"default_features": default_features,
"features": features,
"git": git,
"branch": branch,
"tag": tag,
"rev": rev,
}.items()
# The `cargo_toml` crate parses unstable fields to a flattened
# BTreeMap<String, toml::Value> and toml::Value does not support null,
# so we must omit null values.
if v != None
})

def _assert_absolute(label):
"""Ensure a given label is an absolute label
Expand Down
21 changes: 4 additions & 17 deletions crate_universe/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,15 @@ impl MetadataGenerator for Generator {
cargo_lock::Lockfile::load(lock_path)?
};

let mut other_options = vec!["--locked".to_owned()];
if self.cargo_bin.is_nightly()? {
other_options.push("-Zbindeps".to_owned());
}
let other_options = vec!["--locked".to_owned(), "-Zbindeps".to_owned()];

let metadata = self
.cargo_bin
.metadata_command()?
.current_dir(manifest_dir)
.manifest_path(manifest_path.as_ref())
.other_options(other_options)
.env("RUSTC_BOOTSTRAP", "1") // for -Zbindeps
.exec()?;

Ok((metadata, lockfile))
Expand Down Expand Up @@ -113,9 +111,8 @@ impl Cargo {
pub fn command(&self) -> Result<Command> {
let mut command = Command::new(&self.path);
command.envs(self.env()?);
if self.is_nightly()? {
command.arg("-Zbindeps");
}
command.arg("-Zbindeps");
command.env("RUSTC_BOOTSTRAP", "1"); // for -Zbindeps
Ok(command)
}

Expand All @@ -140,16 +137,6 @@ impl Cargo {
Ok(full_version.clone().unwrap())
}

pub fn is_nightly(&self) -> Result<bool> {
let full_version = self.full_version()?;
let version_str = full_version.split(' ').nth(1);
if let Some(version_str) = version_str {
let version = Version::parse(version_str).context("Failed to parse cargo version")?;
return Ok(version.pre.as_str() == "nightly");
}
bail!("Couldn't parse cargo version");
}

pub fn use_sparse_registries_for_crates_io(&self) -> Result<bool> {
let full_version = self.full_version()?;
let version_str = full_version.split(' ').nth(1);
Expand Down
54 changes: 10 additions & 44 deletions examples/crate_universe/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ no_cargo_crate_repositories()

crates_repository(
name = "using_cxx",
annotations = {
# FIXME: this annotation shouldn't be needed when using `artifact = "bin"`
"cxxbridge-cmd": [crate.annotation(
gen_binaries = ["cxxbridge"],
)],
},
cargo_lockfile = "//using_cxx:Cargo.Bazel.lock",
# `generator` is not necessary in official releases.
# See load satement for `cargo_bazel_bootstrap`.
Expand All @@ -308,6 +314,10 @@ crates_repository(
"cxx": crate.spec(
version = "1.0.109",
),
"cxxbridge-cmd": crate.spec(
artifact = "bin",
version = "1.0.109",
),
},
splicing_config = splicing_config(
resolver_version = "2",
Expand All @@ -321,50 +331,6 @@ load(

using_cxx_crate_repositories()

# The codegen tool needed by cxx.
http_archive(
name = "cxxbridge-cmd",
build_file_content = """
load("@rules_rust//rust:defs.bzl", "rust_binary")
load("@cxxbridge_cmd_deps//:defs.bzl", "aliases", "all_crate_deps")

rust_binary(
name = "cxxbridge-cmd",
srcs = glob(["src/**/*.rs"]),
aliases = aliases(),
data = [
"src/gen/include/cxx.h",
],
edition = "2021",
visibility = ["//visibility:public"],
deps = all_crate_deps(
normal = True,
),
)
""",
sha256 = "d93600487d429c8bf013ee96719af4e62e809ac57fc4cac24f17cf58e4526009",
strip_prefix = "cxxbridge-cmd-1.0.109",
type = "tar.gz",
urls = ["https://crates.io/api/v1/crates/cxxbridge-cmd/1.0.109/download"],
)

crates_repository(
name = "cxxbridge_cmd_deps",
cargo_lockfile = "//using_cxx:cxxbridge-cmd.Cargo.lock",
# `generator` is not necessary in official releases.
# See load satement for `cargo_bazel_bootstrap`.
generator = "@cargo_bazel_bootstrap//:cargo-bazel",
lockfile = "//using_cxx:cxxbridge-cmd.Cargo.Bazel.lock",
manifests = ["@cxxbridge-cmd//:Cargo.toml"],
splicing_config = splicing_config(
resolver_version = "2",
),
)

load("@cxxbridge_cmd_deps//:defs.bzl", cxxbridge_cmd_deps = "crate_repositories")

cxxbridge_cmd_deps()

###############################################################################
# V E N D O R E X T E R N A L
###############################################################################
Expand Down
108 changes: 108 additions & 0 deletions examples/crate_universe/using_cxx/Cargo.Bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading