Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
45 changes: 37 additions & 8 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,27 @@ bazel_dep(name = "rules_java", version = "9.7.0")
bazel_dep(name = "rules_jvm_external", version = "7.1")
bazel_dep(name = "rules_license", version = "1.0.0")
bazel_dep(name = "rules_python", version = "2.2.0") # Keep this in sync with cmake/MODULE.bazel.in. # noqa
bazel_dep(name = "rules_rust", version = "0.71.2") # When upgrading, locally test that crate vendoring still works via `tools/workspace/crate_universe/upgrade.sh && bazel test //tools/install/libdrake/...`.
Comment thread
jwnimmer-tri marked this conversation as resolved.
bazel_dep(name = "rules_rs", version = "0.0.102")
bazel_dep(name = "rules_shell", version = "0.8.0")
bazel_dep(name = "with_cfg.bzl", version = "1.0.0")

# Configure the Rust toolchain.

rules_rust = use_extension("@rules_rs//rs:rules_rust.bzl", "rules_rust")
use_repo(rules_rust, "rules_rust")

rust_toolchains = use_extension(
"@rules_rs//rs/toolchains:module_extension.bzl",
"toolchains",
)
rust_toolchains.toolchain(
edition = "2021",
version = "1.96.0",
)
use_repo(rust_toolchains, "default_rust_toolchains")

register_toolchains("@default_rust_toolchains//:all")

# Configure the C++ toolchain.

cc_configure = use_extension(
Expand Down Expand Up @@ -383,14 +400,26 @@ crate_universe = use_extension(
"//tools/workspace/crate_universe:extensions.bzl",
"crate_universe",
)
use_repo(
crate_universe,
"crate",
"crate__cfg-if-1.0.4",
"crate__clarabel-0.11.1",
"crate__paste-1.0.15",
"crate_licenses",
use_repo(crate_universe, "crate_licenses")

crate = use_extension("@rules_rs//rs:extensions.bzl", "crate")
crate.from_cargo(
cargo_lock = "@crate_licenses//:Cargo.lock",
cargo_toml = "@crate_licenses//:Cargo.toml",
platform_triples = [
"aarch64-apple-darwin",
"aarch64-unknown-linux-gnu",
"x86_64-apple-darwin",
"x86_64-unknown-linux-gnu",
],
)
crate.annotation(
crate = "clarabel",
patches = [
"//tools/workspace/crate_universe:patches/clarabel_blas.patch",
],
)
use_repo(crate, "crates")

# Incorporate Drake-specific patches to modules. The list of patches lives in a
# separate file so that our cmake/MODULE.bazel.in can also include it.
Expand Down
3 changes: 1 addition & 2 deletions tools/workspace/clarabel_cpp_internal/package.BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load("@drake//tools/skylark:cc.bzl", "cc_library")
load("@drake//tools/skylark:cc_hidden.bzl", "cc_wrap_static_archive_hidden")
load("@drake//tools/workspace:vendor_cxx.bzl", "cc_library_vendored")
load("@drake//tools/workspace/crate_universe:defs.bzl", "all_crate_deps")
load("@rules_rust//rust:defs.bzl", "rust_static_library")
load("@rules_rs//rs:rust_static_library.bzl", "rust_static_library")

package(default_visibility = ["//visibility:private"])

Expand Down Expand Up @@ -45,7 +45,6 @@ rust_static_library(
),
crate_features = ["sdp"],
edition = "2021",
proc_macro_deps = all_crate_deps(proc_macro = True),
deps = all_crate_deps(),
)

Expand Down
46 changes: 17 additions & 29 deletions tools/workspace/crate_universe/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,41 +1,29 @@
load(
"@rules_rust//crate_universe:defs.bzl",
"crate",
"crates_vendor",
"render_config",
)
load("//tools/install:install.bzl", "install")
load("//tools/lint:lint.bzl", "add_lint_tests")
load("//tools/skylark:drake_py.bzl", "drake_py_binary")

exports_files(glob(["lock/**"]))

# This is a maintainer-only tool for Drake Developers to manage our Rust
# dependencies. See README.md for instructions on how to run `upgrade.sh`.
crates_vendor(
name = "crate",
annotations = {
"clarabel": [crate.annotation(
patches = [
"@drake//tools/workspace/crate_universe:patches/clarabel_blas.patch", # noqa
],
)],
},
cargo_lockfile = ":lock/Cargo.toml.lock",
manifests = [
"@clarabel_cpp_internal//:rust_wrapper/Cargo.toml",
],
mode = "remote",
render_config = render_config(
generate_rules_license_metadata = True,
),
tags = ["manual"],
vendor_path = "lock/details",
)

install(
name = "install",
visibility = ["//visibility:public"],
deps = ["@crate_licenses//:install"],
)

drake_py_binary(
name = "upgrade",
srcs = ["upgrade.py"],
data = [
"@crate_licenses//:Cargo.lock",
"@crate_licenses//:Cargo.toml",
"@crate_licenses//:src/lib.rs",
"@rules_rust//tools/upstream_wrapper:cargo",
],
env = {
"DRAKE_CARGO_MANIFEST_RLOCATIONPATH": "$(rlocationpath @crate_licenses//:Cargo.toml)",
"DRAKE_CARGO_RLOCATIONPATH": "$(rlocationpath @rules_rust//tools/upstream_wrapper:cargo)",
},
deps = ["@rules_python//python/runfiles"],
)

add_lint_tests()
21 changes: 15 additions & 6 deletions tools/workspace/crate_universe/BUILD.crate_licenses.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@
# This is the BUILD file for our @crate_licenses module extension.
# Refer to extensions.bzl for how it gets used.

load("@drake//tools/install:install.bzl", "install", "install_license")
load("@drake//tools/install:install.bzl", "install")
load(
"@drake//tools/workspace/crate_universe:defs.bzl",
"install_crate_license",
)
load(":lock/repo_names.bzl", "REPO_NAMES")

exports_files([
"Cargo.lock",
"Cargo.toml",
"src/lib.rs",
])

[
install_license(
install_crate_license(
name = "install_{}".format(name),
doc_dest = "share/doc/{}".format(name),
licenses = [
"@{}//:license".format(name),
],
crate = "@crates//:{}".format(name),
doc_dest = "share/doc/crate_universe/{}".format(name),
tags = ["install"],
)
for name in REPO_NAMES
]
Expand Down
10 changes: 6 additions & 4 deletions tools/workspace/crate_universe/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ The purpose of `//tools/workspace/crate_universe` is to provide a central
location to declare Drake's (pinned) dependencies on Rust crates, and the
module extension to load them into our `MODULE.bazel`.

To this end, we use the crate_universe rule set from Bazel's rules_rust:
http://bazelbuild.github.io/rules_rust/crate_universe.html
To this end, we use the Cargo dependency resolver from rules_rs:
https://github.com/hermeticbuild/rules_rs

The files in our subdirectory `lock/**` are all generated by `upgrade.sh`.
The Cargo manifest comes from Clarabel.cpp's `rust_wrapper/Cargo.toml`.
The lockfile and `lock/repo_names.bzl` are kept under `lock/` and managed by
`upgrade.py`.

## Upgrading

To upgrade Drake's pinned Rust dependencies, run this tool:
tools/workspace/crate_universe/upgrade.sh
bazel run //tools/workspace/crate_universe:upgrade
42 changes: 36 additions & 6 deletions tools/workspace/crate_universe/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
load(
"//tools/workspace/crate_universe/lock/details:crates.bzl",
_all_crate_deps = "all_crate_deps",
)
load("@crates//:defs.bzl", _all_crate_deps = "all_crate_deps")
load("@rules_rust//rust/private:providers.bzl", "CrateInfo")
load("//tools/install:install.bzl", "InstallInfo")

def all_crate_deps(**kwargs):
def all_crate_deps():
"""Returns the fully qualified labels of all crate_universe crates."""
return _all_crate_deps(package_name = "", **kwargs)
return _all_crate_deps()

def _install_crate_license_impl(ctx):
package_directory = ctx.attr.crate.label.workspace_root
if ctx.attr.crate.label.package:
package_directory += "/" + ctx.attr.crate.label.package
license_files = [
file
for file in ctx.attr.crate[CrateInfo].compile_data.to_list()
if file.dirname == package_directory and
file.basename.upper().startswith("LICENSE")
]
if not license_files:
fail("{} has no recognized license file".format(ctx.attr.crate.label))
license_file = license_files[0]

return [InstallInfo(
install_actions = [struct(
src = license_file,
dst = ctx.attr.doc_dest + "/" + license_file.basename,
)],
rename = {},
installed_files = {},
)]

install_crate_license = rule(
implementation = _install_crate_license_impl,
attrs = {
"crate": attr.label(mandatory = True, providers = [CrateInfo]),
"doc_dest": attr.string(mandatory = True),
},
)
16 changes: 7 additions & 9 deletions tools/workspace/crate_universe/extensions.bzl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
load(
"//tools/workspace/crate_universe/lock/details:crates.bzl",
"crate_repositories",
)

def _crate_licenses_repository_impl(repo_ctx):
my_dir = "@drake//tools/workspace/crate_universe"
manifest = repo_ctx.path(
Label("@clarabel_cpp_internal//:rust_wrapper/Cargo.toml"),
)
repo_ctx.symlink(manifest, "Cargo.toml")
repo_ctx.symlink(manifest.dirname.get_child("src"), "src")
repo_ctx.symlink(Label(my_dir + ":lock/Cargo.lock"), "Cargo.lock")
repo_ctx.symlink(
Label(my_dir + ":BUILD.crate_licenses.bazel"),
"BUILD.bazel",
Expand All @@ -20,11 +21,8 @@ crate_licenses_repository = repository_rule(

def _impl(module_ctx):
crate_licenses_repository(name = "crate_licenses")
root_module_direct_deps = ["crate_licenses"]
direct_deps = crate_repositories()
root_module_direct_deps.extend([repo.repo for repo in direct_deps])
return module_ctx.extension_metadata(
root_module_direct_deps = root_module_direct_deps,
root_module_direct_deps = ["crate_licenses"],
root_module_direct_dev_deps = [],
)

Expand Down
134 changes: 0 additions & 134 deletions tools/workspace/crate_universe/lock/details/BUILD.amd-0.2.2.bazel

This file was deleted.

Loading