Skip to content

Commit

Permalink
Added bzlmod support to core rules_rust
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Dec 2, 2024
1 parent c3d71ed commit e42501e
Show file tree
Hide file tree
Showing 32 changed files with 1,007 additions and 814 deletions.
36 changes: 36 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ bzlmod_flags: &bzlmod_flags
bzlmod_plus_repo_names_flags: &bzlmod_plus_repo_names_flags
# `--lockfile_mode=error` is omitted because the repo names leak into the lock file.
- "--incompatible_use_plus_in_repo_names"
no_bzlmod_shell_commands: &no_bzlmod_shell_commands
- echo "common --noenable_bzlmod --enable_workspace" >> user.bazelrc
no_bzlmod_rbe_shell_commands: &no_bzlmod_rbe_shell_commands
- sed -i 's/^# load("@bazel_ci_rules/load("@bazel_ci_rules/' WORKSPACE.bazel
- sed -i 's/^# rbe_preconfig/rbe_preconfig/' WORKSPACE.bazel
- echo "common --noenable_bzlmod --enable_workspace" >> user.bazelrc
single_rust_channel_targets: &single_rust_channel_targets
- "--"
- "//..."
Expand Down Expand Up @@ -91,6 +97,36 @@ tasks:
windows:
build_targets: *default_windows_targets
test_targets: *default_windows_targets
ubuntu2004_no_bzlmod:
name: No Bzlmod
platform: ubuntu2004
shell_commands: *no_bzlmod_shell_commands
build_targets: *default_linux_targets
test_targets: *default_linux_targets
coverage_targets: *default_linux_targets
post_shell_commands: *coverage_validation_post_shell_commands
run_targets:
- //test:query_test_binary
rbe_ubuntu2004_no_bzlmod:
name: No Bzlmod
platform: rbe_ubuntu2004
shell_commands: *no_bzlmod_rbe_shell_commands
build_targets: *default_linux_targets
test_targets: *default_linux_targets
macos_no_bzlmod:
name: No Bzlmod
platform: macos_arm64
shell_commands: *no_bzlmod_shell_commands
build_targets: *default_macos_targets
test_targets: *default_macos_targets
coverage_targets: *default_macos_targets
post_shell_commands: *coverage_validation_post_shell_commands
windows_no_bzlmod:
name: No Bzlmod
platform: windows
shell_commands: *no_bzlmod_shell_commands
build_targets: *default_windows_targets
test_targets: *default_windows_targets
windows_no_runfiles:
name: No Runfiles
platform: windows
Expand Down
4 changes: 0 additions & 4 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ build --incompatible_merge_fixed_and_default_shell_env
## Bzlmod
###############################################################################

# TODO: migrate all dependencies from WORKSPACE to MODULE.bazel
# https://github.com/bazelbuild/rules_rust/issues/2181
common --noenable_bzlmod --enable_workspace

# Disable the bzlmod lockfile, so we don't accidentally commit MODULE.bazel.lock
common --lockfile_mode=off

Expand Down
101 changes: 69 additions & 32 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ module(
version = "0.54.1",
)

###############################################################################
## Core
###############################################################################

bazel_dep(
name = "bazel_features",
version = "1.21.0",
Expand All @@ -31,20 +35,75 @@ bazel_dep(
repo_name = "build_bazel_apple_support",
)

internal_deps = use_extension("//rust/private:internal_extensions.bzl", "i")
use_repo(
internal_deps,
"rrra__anyhow-1.0.71",
"rrra__clap-4.3.11",
"rrra__env_logger-0.10.0",
"rrra__itertools-0.11.0",
"rrra__log-0.4.19",
"rrra__serde-1.0.171",
"rrra__serde_json-1.0.102",
"rules_rust_tinyjson",
)

rust = use_extension("//rust:extensions.bzl", "rust")
rust.toolchain(edition = "2021")
use_repo(rust, "rust_toolchains")

register_toolchains(
"@rust_toolchains//:all",
)

rust_host_tools = use_extension("//rust:extensions.bzl", "rust_host_tools")
use_repo(rust_host_tools, "rust_host_tools")

rust_test = use_extension("//test:test_extensions.bzl", "rust_test", dev_dependency = True)
use_repo(
rust_test,
"generated_inputs_in_external_repo",
"libc",
"rules_rust_test_load_arbitrary_tool",
"rules_rust_toolchain_test_target_json",
"t3p",
"t3p__serde-1.0.215",
"t3p__serde_json-1.0.133",
)

bazel_dep(
name = "rules_python",
version = "0.40.0",
dev_dependency = True,
)
bazel_dep(
name = "rules_testing",
version = "0.6.0",
version = "0.7.0",
dev_dependency = True,
)
bazel_dep(
name = "bazel_ci_rules",
version = "1.0.0",
dev_dependency = True,
)

internal_deps = use_extension("//rust/private:extensions.bzl", "i")
###############################################################################
## Crate Universe
###############################################################################

crate_universe_internal_deps = use_extension(
"//crate_universe/private:internal_extensions.bzl",
"i",
)
use_repo(
internal_deps,
crate_universe_internal_deps,
"cargo_bazel.buildifier-darwin-amd64",
"cargo_bazel.buildifier-darwin-arm64",
"cargo_bazel.buildifier-linux-amd64",
"cargo_bazel.buildifier-linux-arm64",
"cargo_bazel.buildifier-linux-s390x",
"cargo_bazel.buildifier-windows-amd64.exe",
"cargo_bazel_bootstrap",
"cui",
"cui__anyhow-1.0.89",
"cui__camino-1.1.9",
Expand Down Expand Up @@ -76,37 +135,15 @@ use_repo(
"cui__tracing-0.1.40",
"cui__tracing-subscriber-0.3.18",
"cui__url-2.5.2",
"rrra__anyhow-1.0.71",
"rrra__clap-4.3.11",
"rrra__env_logger-0.10.0",
"rrra__itertools-0.11.0",
"rrra__log-0.4.19",
"rrra__serde-1.0.171",
"rrra__serde_json-1.0.102",
"rules_rust_tinyjson",
)

rust = use_extension("//rust:extensions.bzl", "rust")
rust.toolchain(edition = "2021")
use_repo(rust, "rust_toolchains")

register_toolchains(
"@rust_toolchains//:all",
crate_universe_internal_dev_deps = use_extension(
"//crate_universe/private:internal_extensions.bzl",
"i_dev",
dev_dependency = True,
)

rust_host_tools = use_extension("//rust:extensions.bzl", "rust_host_tools")
use_repo(rust_host_tools, "rust_host_tools")

cargo_bazel_bootstrap = use_extension("//crate_universe/private/module_extensions:cargo_bazel_bootstrap.bzl", "cargo_bazel_bootstrap")
use_repo(cargo_bazel_bootstrap, "cargo_bazel_bootstrap")

rust_test = use_extension("//test:test_extensions.bzl", "rust_test", dev_dependency = True)
use_repo(
rust_test,
"generated_inputs_in_external_repo",
"libc",
"rules_rust_test_load_arbitrary_tool",
"rules_rust_toolchain_test_target_json",
"t3p__serde-1.0.214",
"t3p__serde_json-1.0.132",
crate_universe_internal_dev_deps,
"cross_rs",
"cross_rs_host_bin",
)
Empty file added cargo/extensions.bzl
Empty file.
9 changes: 9 additions & 0 deletions crate_universe/deps_bootstrap.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def cargo_bazel_bootstrap(name = "cargo_bazel_bootstrap", rust_version = rust_co
name (str, optional): The name of the `cargo_bootstrap_repository`.
rust_version (str, optional): The rust version to use. Defaults to the default of `cargo_bootstrap_repository`.
**kwargs: kwargs to pass through to cargo_bootstrap_repository.
Returns:
list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories
defined by this macro.
"""

maybe(
Expand All @@ -28,3 +32,8 @@ def cargo_bazel_bootstrap(name = "cargo_bazel_bootstrap", rust_version = rust_co
timeout = 900,
**kwargs
)

return [struct(
repo = name,
is_dev_dep = False,
)]
10 changes: 5 additions & 5 deletions crate_universe/docs_bzlmod.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ You find the latest version on the [release page](https://github.com/bazelbuild/
After adding `rules_rust` in your MODULE.bazel, set the following to begin using `crate_universe`:
```starlark
crate = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
// # ... Dependencies
use_repo(crate, "crates")
```
Expand All @@ -55,7 +55,7 @@ The crates_repository rule can ingest a root Cargo.toml file and generate Bazel
You find a complete example in the in the [example folder](../examples/bzlmod/all_crate_deps).
```starlark
crate = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
crate.from_cargo(
name = "crates",
Expand Down Expand Up @@ -143,7 +143,7 @@ crates_repository supports this through the packages attribute,
as shown below.
```starlark
crate = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
crate.spec(package = "serde", features = ["derive"], version = "1.0")
crate.spec(package = "serde_json", version = "1.0")
Expand Down Expand Up @@ -218,7 +218,7 @@ register_toolchains("@rust_toolchains//:all")
###############################################################################
# R U S T C R A T E S
###############################################################################
crate = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
```
Note, it is important to load the crate_universe rules otherwise you will get an error
Expand Down Expand Up @@ -326,7 +326,7 @@ There are some more examples of using crate_universe with bzlmod in the [example
"""

load(
"//crate_universe:extension.bzl",
"//crate_universe:extensions.bzl",
_crate = "crate",
)

Expand Down
Loading

0 comments on commit e42501e

Please sign in to comment.