Skip to content

Commit

Permalink
Allow extra packages to be added to oak crate indexes.
Browse files Browse the repository at this point in the history
Other projects using Oak may have additional dependencies. Per
bazelbuild/rules_rust#1609 (comment),
it's recommended that a target only pull dependencies from a single
`crates_repository` target, so those projects shouldn't define their own
crates repository. Instead, they should augment the Oak-defined ones.

I preferred this approach over exporting Oak's deps and annotations
because that's harder to use: (a) it requires defining crates
repositories with the Oak-expected names (or using repo mapping), and
(b) it doesn't work cleanly with Oak's jemalloc repository.

Bug: 355397572
Change-Id: I672d5bcb4dc629904e9944ff7c07b2ae674edbd9
  • Loading branch information
bmclarnon committed Aug 8, 2024
1 parent 4a455b3 commit dfed386
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
6 changes: 3 additions & 3 deletions bazel/crates/oak_crates_index.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ load("@rules_rust//crate_universe:defs.bzl", "crate", "crates_repository")
load("//bazel/crates:jemalloc.bzl", "jemalloc_repository")
load("//bazel/rust:defs.bzl", "RUST_NIGHTLY_VERSION")

def oak_crates_index(cargo_lockfile, lockfile):
def oak_crates_index(cargo_lockfile, lockfile, extra_annotations = {}, extra_packages = {}):
jemalloc_repository()

# Default crate repository - some crates may require std.
Expand Down Expand Up @@ -58,7 +58,7 @@ def oak_crates_index(cargo_lockfile, lockfile):
"tokio": [crate.annotation(
rustc_flags = ["--cfg=tokio_unstable"],
)],
},
} | extra_annotations,
cargo_lockfile = cargo_lockfile, # In Cargo-free mode this is used as output, not input.
lockfile = lockfile, # Shares most contents with cargo_lockfile.
packages = {
Expand Down Expand Up @@ -493,7 +493,7 @@ def oak_crates_index(cargo_lockfile, lockfile):
"rand_chacha": crate.spec(
version = "*",
),
},
} | extra_packages,
rust_version = RUST_NIGHTLY_VERSION,
# We request bare metal support. Because of feature unification, some creates
# in this repository may end up requiring std, thus not being compatible
Expand Down
5 changes: 3 additions & 2 deletions bazel/crates/oak_no_std_crates_index.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
load("@rules_rust//crate_universe:defs.bzl", "crate", "crates_repository")
load("//bazel/rust:defs.bzl", "RUST_NIGHTLY_VERSION")

def oak_no_std_crates_index(cargo_lockfile, lockfile):
def oak_no_std_crates_index(cargo_lockfile, lockfile, extra_annotations = {}, extra_packages = {}):
# All creates in this repository must support no_std.
crates_repository(
name = "oak_no_std_crates_index",
annotations = extra_annotations,
cargo_lockfile = cargo_lockfile, # In Cargo-free mode this is used as output, not input.
lockfile = lockfile, # Shares most contents with cargo_lockfile.
packages = {
Expand Down Expand Up @@ -193,7 +194,7 @@ def oak_no_std_crates_index(cargo_lockfile, lockfile):
# same version as cargo, newer versions had compatibility issues
version = "0.31.2",
),
},
} | extra_packages,
rust_version = RUST_NIGHTLY_VERSION,
supported_platform_triples = [
# Linux for dependencies of build scripts (they run on host):
Expand Down
18 changes: 16 additions & 2 deletions bazel/crates/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,23 @@ load("//bazel/crates:oak_no_std_crates_index.bzl", "oak_no_std_crates_index")

def create_oak_crate_repositories(
cargo_lockfile = "//:Cargo.bazel.lock",
extra_annotations = {},
extra_no_std_annotations = {},
extra_no_std_packages = {},
extra_packages = {},
lockfile = "//:cargo-bazel-lock.json",
no_std_cargo_lockfile = "//:Cargo_no_std.bazel.lock",
no_std_lockfile = "//:cargo-no-std-bazel-lock.json"):
crate_universe_dependencies(bootstrap = True)
oak_crates_index(cargo_lockfile, lockfile)
oak_no_std_crates_index(no_std_cargo_lockfile, no_std_lockfile)
oak_crates_index(
cargo_lockfile,
lockfile,
extra_annotations = extra_annotations,
extra_packages = extra_packages,
)
oak_no_std_crates_index(
no_std_cargo_lockfile,
no_std_lockfile,
extra_annotations = extra_no_std_annotations,
extra_packages = extra_no_std_packages,
)

0 comments on commit dfed386

Please sign in to comment.