Upgrade rust tooling
This is a combination of 2 commits and a merge that had to be made
into one to build successfully. Below is the each commit message.
======================
Merge commit '6b8c7d81975eefbe58bece57a9fa9d4665ca222a' into rules_rust
======================
Upgrade rules_rust to version 0.25.1
======================
Migrate rust dependencies to crate_universe.
We've been using cargo raze for managing rust dependencies,
however cargo-raze has some flaws and it's not being updated
(last commit on main was on Dec 2022). Meanwhile, crate_universe
ships with rules_rust which we already depend on.
crates_universe doesn't handle binary only dependencies, however
these are easy to add manually as explained here:
http://bazelbuild.github.io/rules_rust/crate_universe.html#binary-dependencies
We only depend on cxxbridge_cmd which I've added.
crates_universe didnd't allow for extra_aliased_targets which we use for
cxx (cxx_cc target). I've added that functionality and built a
cargo-bazel binary which has been deployed to our Build-Dependencies.
Crates now live in @crate_index//:[crate_name].
After adding a crate, you have to use
`CARGO_BAZEL_REPIN=1 bazel build //foo/bar` to have cargo generate the
new build files and the lock file. It will error if the manifest(s)
don't match the lock file.
If you want to update the lock file without building anything in
particular you can run
`CARGO_BAZEL_REPIN=1 bazel sync --only=crate_index`
Change-Id: Iaa47218e6012cf891c98a5c9340eeaa8b7e67c12
Signed-off-by: Adam Snaider <adsnaider@gmail.com>
diff --git a/tools/bazel b/tools/bazel
index 2bfebef..97f31d9 100755
--- a/tools/bazel
+++ b/tools/bazel
@@ -105,6 +105,10 @@
ENVIRONMENT_VARIABLES+=(LANG="${LANG:-C}")
ENVIRONMENT_VARIABLES+=(BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1)
+if [[ ! -z "${CARGO_BAZEL_REPIN+x}" ]]; then
+ ENVIRONMENT_VARIABLES+=(CARGO_BAZEL_REPIN="${CARGO_BAZEL_REPIN}")
+fi
+
if [[ ! -z "${DISPLAY+x}" ]]; then
ENVIRONMENT_VARIABLES+=(DISPLAY="${DISPLAY}")
fi
diff --git a/tools/build_rules/autocxx.bzl b/tools/build_rules/autocxx.bzl
index 4679a6f..d4b6be6 100644
--- a/tools/build_rules/autocxx.bzl
+++ b/tools/build_rules/autocxx.bzl
@@ -225,7 +225,7 @@
),
"_cxx_codegen": attr.label(
executable = True,
- default = Label("@//third_party/cargo:cargo_bin_cxxbridge"),
+ default = Label("@cxxbridge-cmd//:cxxbridge-cmd"),
cfg = "exec",
),
"_libclang": attr.label(
@@ -324,7 +324,7 @@
target_compatible_with = target_compatible_with,
testonly = testonly,
deps = deps + libs + [
- "//third_party/cargo:cxx_cc",
+ "@crate_index//:cxx_cc",
],
srcs = [gen_cc_srcs_name],
hdrs = [gen_hdr_srcs_name],
@@ -337,13 +337,13 @@
testonly = testonly,
srcs = srcs + cxxbridge_srcs,
proc_macro_deps = [
- "//third_party/cargo:cxxbridge_macro",
+ "@crate_index//:cxxbridge-macro",
],
crate_features = crate_features,
crate_name = crate_name,
deps = deps + rs_deps + [
cc_library_name,
- "//third_party/cargo:cxx",
+ "@crate_index//:cxx",
"//third_party/autocxx",
],
compile_data = [gen_compile_data_name],
diff --git a/tools/lint/BUILD b/tools/lint/BUILD
index a881124..907f17c 100644
--- a/tools/lint/BUILD
+++ b/tools/lint/BUILD
@@ -32,7 +32,7 @@
name = "rustfmt",
srcs = ["rustfmt.sh"],
data = [
- "@rust__x86_64-unknown-linux-gnu_tools//:rustfmt",
+ "@rust__x86_64-unknown-linux-gnu__stable_tools//:rustfmt",
],
deps = [
"@bazel_tools//tools/bash/runfiles",
@@ -89,8 +89,6 @@
"//:gazelle-runner",
"//tools/go:mirror_go_repos",
"//tools/go:tweak_gazelle_go_deps",
- "//tools/rust:tweak_cargo_raze_output",
- "@cargo_raze//:raze",
"@go_sdk//:bin/go",
],
env = {
diff --git a/tools/lint/run-ci.sh b/tools/lint/run-ci.sh
index c8d6501..419a9fd 100755
--- a/tools/lint/run-ci.sh
+++ b/tools/lint/run-ci.sh
@@ -68,30 +68,6 @@
./tools/lint/rustfmt
}
-cargo_lockfile() {
- cd "${BUILD_WORKSPACE_DIRECTORY}"
- cp third_party/cargo/Cargo.raze.lock Cargo.lock
- external/rust__x86_64-unknown-linux-gnu_tools/bin/cargo generate-lockfile --locked --manifest-path=Cargo.toml
- rm Cargo.lock
-}
-
-cargo_raze() {
- local -r cargo_raze="$(readlink -f external/cargo_raze/impl/cargo_raze_bin)"
- export CARGO="$(readlink -f external/rust__x86_64-unknown-linux-gnu_tools/bin/cargo)"
- export RUSTC="$(readlink -f external/rust__x86_64-unknown-linux-gnu_tools/bin/rustc)"
- cd "${BUILD_WORKSPACE_DIRECTORY}"
- # Note we don't run with --generate-lockfile here. If there's a new
- # dependency, we don't want to download it, just failing with an error
- # is sufficient.
- "${cargo_raze}" --manifest-path=Cargo.toml
-}
-
-tweak_cargo_raze() {
- local -r tweaker="$(readlink -f tools/rust/tweak_cargo_raze_output)"
- cd "${BUILD_WORKSPACE_DIRECTORY}"
- "${tweaker}" .
-}
-
buildifier() {
./tools/lint/buildifier
}
@@ -122,9 +98,6 @@
tweak_gazelle_go_deps
clean_up_go_mirrors
rustfmt
- cargo_lockfile
- cargo_raze
- tweak_cargo_raze
buildifier
prettier
yapf
diff --git a/tools/lint/rustfmt.sh b/tools/lint/rustfmt.sh
index 8539101..cc12585 100755
--- a/tools/lint/rustfmt.sh
+++ b/tools/lint/rustfmt.sh
@@ -11,7 +11,7 @@
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v2 ---
-readonly RUSTFMT="$(rlocation rust__x86_64-unknown-linux-gnu_tools/bin/rustfmt)"
+readonly RUSTFMT="$(rlocation rust__x86_64-unknown-linux-gnu__stable_tools/bin/rustfmt)"
# Run everything from the root of the tree.
cd "${BUILD_WORKSPACE_DIRECTORY}"
diff --git a/tools/rust/BUILD b/tools/rust/BUILD
index a2d9317..707c40a 100644
--- a/tools/rust/BUILD
+++ b/tools/rust/BUILD
@@ -1,6 +1,5 @@
load("@rules_rust//rust:toolchain.bzl", "rust_stdlib_filegroup", "rust_toolchain")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
-load("@rules_rust//rust:defs.bzl", "rust_binary")
# We have to declare our toolchains individually to get the corect constraints
# configured so we can robustly select the correct one for each of our
@@ -16,7 +15,7 @@
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
- toolchain = "@rust__x86_64-unknown-linux-gnu_tools//:rust_toolchain",
+ toolchain = "@rust__x86_64-unknown-linux-gnu__stable_tools//:rust_toolchain",
toolchain_type = "@rules_rust//rust:toolchain",
)
@@ -33,7 +32,7 @@
# subtle order dependencies.
"//tools/platforms/hardware:raspberry_pi",
],
- toolchain = "@rust__armv7-unknown-linux-gnueabihf_tools//:rust_toolchain",
+ toolchain = "@rust__armv7-unknown-linux-gnueabihf__stable_tools//:rust_toolchain",
toolchain_type = "@rules_rust//rust:toolchain",
)
@@ -47,7 +46,7 @@
"@platforms//os:linux",
"@platforms//cpu:arm64",
],
- toolchain = "@rust__aarch64-unknown-linux-gnu_tools//:rust_toolchain",
+ toolchain = "@rust__aarch64-unknown-linux-gnu__stable_tools//:rust_toolchain",
toolchain_type = "@rules_rust//rust:toolchain",
)
@@ -62,7 +61,7 @@
"@platforms//cpu:armv7",
"//tools/platforms/hardware:roborio",
],
- toolchain = "@rust__arm-unknown-linux-gnueabi_tools//:rust_toolchain",
+ toolchain = "@rust__arm-unknown-linux-gnueabi__stable_tools//:rust_toolchain",
toolchain_type = "@rules_rust//rust:toolchain",
)
@@ -95,8 +94,7 @@
clippy_driver = ":noop_error_exit",
default_edition = "2021",
dylib_ext = ".so",
- exec_triple = "none",
- os = "none",
+ exec_triple = "none-none-none",
rust_doc = ":noop_error_exit",
rust_std = ":empty_stdlib",
rustc = ":noop_error_exit",
@@ -106,7 +104,7 @@
staticlib_ext = ".a",
stdlib_linkflags = [],
tags = ["manual"],
- target_triple = "none",
+ target_triple = "none-none-none",
)
toolchain(
@@ -121,17 +119,6 @@
toolchain_type = "@rules_rust//rust:toolchain",
)
-rust_binary(
- name = "tweak_cargo_raze_output",
- srcs = ["tweak_cargo_raze_output.rs"],
- target_compatible_with = ["//tools/platforms/rust:has_support"],
- visibility = ["//visibility:public"],
- deps = [
- "//third_party/cargo:anyhow",
- "//third_party/cargo:toml",
- ],
-)
-
cc_library(
name = "forward_allocator",
srcs = ["forward_allocator.c"],
diff --git a/tools/rust/tweak_cargo_raze_output.rs b/tools/rust/tweak_cargo_raze_output.rs
deleted file mode 100644
index 88a9d0a..0000000
--- a/tools/rust/tweak_cargo_raze_output.rs
+++ /dev/null
@@ -1,102 +0,0 @@
-//! A tool that postprocesses cargo-raze output to do what we want.
-//!
-//! Currently this is limited to removing extraneous BUILD files which cargo-raze places in all the
-//! third_party packages we feed to it, which we don't want to use. We're hand-writing BUILD files
-//! for these dependencies, without intending them to be used as separate Bazel workspaces, so it's
-//! easiest for them to all reference the top-level //third_party/cargo package.
-use std::{
- env, fs,
- io::{self, ErrorKind},
- path::Path,
-};
-
-use anyhow::Context;
-use toml::Value;
-
-fn filter_not_found(result: io::Result<()>) -> io::Result<()> {
- match result {
- Err(e) if matches!(e.kind(), ErrorKind::NotFound) => Ok(()),
- r => r,
- }
-}
-
-fn main() -> anyhow::Result<()> {
- let argv: Vec<_> = env::args().collect();
- let workspace_path = Path::new(&argv[1]);
- let cargo_toml_path = workspace_path.join("Cargo.toml");
- eprintln!("Loading Cargo.toml from {:?}", cargo_toml_path);
- let cargo_toml_contents = fs::read(&cargo_toml_path)
- .with_context(|| format!("Failed to read Cargo.toml: {:?}", cargo_toml_path))?;
- let cargo_toml_contents = std::str::from_utf8(&cargo_toml_contents).with_context(|| {
- format!(
- "Failed to interpret Cargo.toml contents as UTF-8: {:?}",
- cargo_toml_path
- )
- })?;
- let cargo_toml: Value = cargo_toml_contents
- .parse()
- .with_context(|| format!("Failed to parse Cargo.toml contents: {:?}", cargo_toml_path))?;
-
- let package_aliases_dir = cargo_toml["workspace"]["metadata"]["raze"]["package_aliases_dir"]
- .as_str()
- .with_context(|| {
- format!(
- "Found non-string package_aliases_dir in Cargo.toml: {:?}",
- cargo_toml_path
- )
- })?;
-
- let workspace_members = cargo_toml["workspace"]["members"]
- .as_array()
- .with_context(|| {
- format!(
- "Did not find workspace members in Cargo.toml: {:?}",
- cargo_toml_path
- )
- })?;
- for member in workspace_members.iter() {
- let member = member.as_str().with_context(|| {
- format!(
- "Found non-string workspace member in Cargo.toml: {:?}",
- cargo_toml_path
- )
- })?;
-
- // First delete the BUILD file.
- let member_build = workspace_path
- .join(member)
- .join(package_aliases_dir)
- .join("BUILD.bazel");
- filter_not_found(fs::remove_file(&member_build)).with_context(|| {
- format!(
- "Failed to remove workspace member BUILD.bazel: {:?}",
- member_build
- )
- })?;
-
- // Then go and delete each folder in reverse order, but only if it's now empty to avoid
- // overeager deletion. The file we're deleting should be the only thing in these folders,
- // but if somebody wrote something else for some reason then we don't want to silently
- // delete it.
- let mut folder_path = workspace_path.join(member);
- for d in Path::new(package_aliases_dir)
- .components()
- .scan(&mut folder_path, |a, b| {
- **a = a.join(b);
- Some(a.clone())
- })
- .collect::<Vec<_>>()
- .iter()
- .rev()
- {
- filter_not_found(fs::remove_dir(d)).with_context(|| {
- format!(
- "Failed to remove workspace member package_aliases directory: {:?}",
- d
- )
- })?;
- }
- }
- eprintln!("All done");
- Ok(())
-}