diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index ce9eef0f41..ec333fcec4 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -196,7 +196,12 @@ tasks: build_flags: - "--enable_bzlmod" build_targets: + - "//bindgen/3rdparty:bindgen" + - "//crate_universe:cargo_bazel_bin" + - "//proto/prost/private:prost_runtime" - "//tools/runfiles" + - "//util/import" + - "//wasm_bindgen/3rdparty:wasm_bindgen" ubuntu2004_clang: name: With Clang platform: ubuntu2004 diff --git a/MODULE.bazel b/MODULE.bazel index dcf354496d..87aa5893a0 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -7,19 +7,122 @@ module( print("WARNING: The rules_rust Bazel module is still highly experimental and subject to change at any time. Only use it to try out bzlmod for now.") # buildifier: disable=print -bazel_dep(name = "platforms", version = "0.0.8") -bazel_dep(name = "rules_cc", version = "0.0.9") -bazel_dep(name = "bazel_skylib", version = "1.5.0") +bazel_dep( + name = "bazel_skylib", + version = "1.5.0", +) +bazel_dep( + name = "platforms", + version = "0.0.8", +) +bazel_dep( + name = "rules_cc", + version = "0.0.9", +) +bazel_dep( + name = "rules_proto", + version = "5.3.0-21.7", +) bazel_dep( name = "apple_support", version = "1.11.1", repo_name = "build_bazel_apple_support", ) +bazel_dep( + name = "protobuf", + version = "21.7", + repo_name = "com_google_protobuf", +) internal_deps = use_extension("//rust/private:extensions.bzl", "internal_deps") use_repo( internal_deps, + "bazelci_rules", + "com_google_googleapis", + "cui", + "cui__anyhow-1.0.75", + "cui__cargo-lock-9.0.0", + "cui__cargo-platform-0.1.4", + "cui__cargo_metadata-0.18.1", + "cui__cargo_toml-0.17.1", + "cui__cfg-expr-0.15.5", + "cui__clap-4.3.11", + "cui__crates-index-2.2.0", + "cui__hex-0.4.3", + "cui__indoc-2.0.4", + "cui__itertools-0.11.0", + "cui__maplit-1.0.2", + "cui__normpath-1.1.1", + "cui__pathdiff-0.2.1", + "cui__regex-1.10.2", + "cui__semver-1.0.20", + "cui__serde-1.0.190", + "cui__serde_json-1.0.108", + "cui__serde_starlark-0.1.14", + "cui__sha2-0.10.8", + "cui__spectral-0.6.0", + "cui__tempfile-3.8.1", + "cui__tera-1.19.1", + "cui__textwrap-0.16.0", + "cui__toml-0.8.6", + "cui__tracing-0.1.40", + "cui__tracing-subscriber-0.3.17", + "generated_inputs_in_external_repo", + "libc", + "llvm-raw", + "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_bindgen__bindgen-0.65.1", + "rules_rust_bindgen__bindgen-cli-0.65.1", + "rules_rust_bindgen__clang-sys-1.6.1", + "rules_rust_bindgen__clap-4.3.3", + "rules_rust_bindgen__clap_complete-4.3.1", + "rules_rust_bindgen__env_logger-0.10.0", + "rules_rust_prost", + "rules_rust_prost__h2-0.3.19", + "rules_rust_prost__heck", + "rules_rust_prost__prost-0.11.9", + "rules_rust_prost__prost-types-0.11.9", + "rules_rust_prost__protoc-gen-prost-0.2.2", + "rules_rust_prost__protoc-gen-tonic-0.2.2", + "rules_rust_prost__tokio-1.28.2", + "rules_rust_prost__tokio-stream-0.1.14", + "rules_rust_prost__tonic-0.9.2", + "rules_rust_test_load_arbitrary_tool", "rules_rust_tinyjson", + "rules_rust_toolchain_test_target_json", + "rules_rust_util_import__aho-corasick-0.7.15", + "rules_rust_util_import__lazy_static-1.4.0", + "rules_rust_util_import__proc-macro2-1.0.33", + "rules_rust_util_import__quickcheck-1.0.3", + "rules_rust_util_import__quote-1.0.10", + "rules_rust_util_import__syn-1.0.82", + "rules_rust_wasm_bindgen__anyhow-1.0.71", + "rules_rust_wasm_bindgen__assert_cmd-1.0.8", + "rules_rust_wasm_bindgen__diff-0.1.13", + "rules_rust_wasm_bindgen__docopt-1.1.1", + "rules_rust_wasm_bindgen__env_logger-0.8.4", + "rules_rust_wasm_bindgen__log-0.4.19", + "rules_rust_wasm_bindgen__predicates-1.0.8", + "rules_rust_wasm_bindgen__rayon-1.7.0", + "rules_rust_wasm_bindgen__rouille-3.6.2", + "rules_rust_wasm_bindgen__serde-1.0.171", + "rules_rust_wasm_bindgen__serde_derive-1.0.171", + "rules_rust_wasm_bindgen__serde_json-1.0.102", + "rules_rust_wasm_bindgen__tempfile-3.6.0", + "rules_rust_wasm_bindgen__ureq-2.8.0", + "rules_rust_wasm_bindgen__walrus-0.19.0", + "rules_rust_wasm_bindgen__wasm-bindgen-0.2.88", + "rules_rust_wasm_bindgen__wasm-bindgen-cli-support-0.2.88", + "rules_rust_wasm_bindgen__wasm-bindgen-shared-0.2.88", + "rules_rust_wasm_bindgen__wasmparser-0.102.0", + "rules_rust_wasm_bindgen__wasmprinter-0.2.60", + "rules_rust_wasm_bindgen_cli", ) rust = use_extension("//rust:extensions.bzl", "rust") diff --git a/bindgen/repositories.bzl b/bindgen/repositories.bzl index a9077c3269..b3b6b5c6a3 100644 --- a/bindgen/repositories.bzl +++ b/bindgen/repositories.bzl @@ -22,7 +22,12 @@ BINDGEN_VERSION = "0.65.1" # buildifier: disable=unnamed-macro def rust_bindgen_dependencies(): - """Declare dependencies needed for bindgen.""" + """Declare dependencies needed for bindgen. + + Returns: + list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories + defined by this macro. + """ maybe( http_archive, @@ -38,9 +43,10 @@ def rust_bindgen_dependencies(): ], ) + bindgen_name = "rules_rust_bindgen__bindgen-cli-{}".format(BINDGEN_VERSION) maybe( http_archive, - name = "rules_rust_bindgen__bindgen-cli-{}".format(BINDGEN_VERSION), + name = bindgen_name, sha256 = "33373a4e0ec8b6fa2654e0c941ad16631b0d564cfd20e7e4b3db4c5b28f4a237", type = "tar.gz", urls = ["https://crates.io/api/v1/crates/bindgen-cli/{}/download".format(BINDGEN_VERSION)], @@ -48,7 +54,12 @@ def rust_bindgen_dependencies(): build_file = Label("//bindgen/3rdparty:BUILD.bindgen-cli.bazel"), ) - crate_repositories() + direct_deps = [ + struct(repo = "llvm-raw", is_dev_dep = False), + struct(repo = bindgen_name, is_dev_dep = False), + ] + direct_deps.extend(crate_repositories()) + return direct_deps # buildifier: disable=unnamed-macro def rust_bindgen_register_toolchains(register_toolchains = True): diff --git a/crate_universe/repositories.bzl b/crate_universe/repositories.bzl index 642435fb31..e8f6486cc8 100644 --- a/crate_universe/repositories.bzl +++ b/crate_universe/repositories.bzl @@ -14,13 +14,18 @@ def crate_universe_dependencies(rust_version = rust_common.default_version, boot rust_version (str, optional): The version of rust to use when generating dependencies. bootstrap (bool, optional): If true, a `cargo_bootstrap_repository` target will be generated. **kwargs: Arguments to pass through to cargo_bazel_bootstrap. + + Returns: + list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories + defined by this macro. """ third_party_deps() if bootstrap: cargo_bazel_bootstrap(rust_version = rust_version, **kwargs) - _vendor_crate_repositories() + direct_deps = _vendor_crate_repositories() crates_vendor_deps() cross_installer_deps() + return direct_deps diff --git a/docs/crate_universe.md b/docs/crate_universe.md index 5f8c2d000d..047358b3d6 100644 --- a/docs/crate_universe.md +++ b/docs/crate_universe.md @@ -738,6 +738,11 @@ Define dependencies of the `cargo-bazel` Rust target | bootstrap | If true, a cargo_bootstrap_repository target will be generated. | `False` | | kwargs | Arguments to pass through to cargo_bazel_bootstrap. | none | +**RETURNS** + +list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories + defined by this macro. + diff --git a/docs/flatten.md b/docs/flatten.md index 00af38c3ca..f94739ce22 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -1750,6 +1750,11 @@ rust_bindgen_dependencies() Declare dependencies needed for bindgen. +**RETURNS** + +list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories + defined by this macro. + @@ -1803,11 +1808,22 @@ Registers the default toolchains for the `rules_rust` [bindgen][bg] rules. ## rust_prost_dependencies
-rust_prost_dependencies()
+rust_prost_dependencies(bzlmod)
 
+Declares repositories needed for prost. + +**PARAMETERS** + + +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| bzlmod | Whether bzlmod is enabled. | `False` | +**RETURNS** +list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories + defined by this macro. @@ -2120,6 +2136,12 @@ Declare dependencies needed for the `rules_rust` [wasm-bindgen][wb] rules. +**RETURNS** + +list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories + defined by this macro. + + ## rust_wasm_bindgen_register_toolchains diff --git a/docs/rust_bindgen.md b/docs/rust_bindgen.md index f11e928b29..1a496cd747 100644 --- a/docs/rust_bindgen.md +++ b/docs/rust_bindgen.md @@ -130,6 +130,11 @@ rust_bindgen_dependencies() Declare dependencies needed for bindgen. +**RETURNS** + +list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories + defined by this macro. + diff --git a/docs/rust_proto.md b/docs/rust_proto.md index 4ef5151492..ca48d265a6 100644 --- a/docs/rust_proto.md +++ b/docs/rust_proto.md @@ -377,11 +377,22 @@ rust_binary( ## rust_prost_dependencies
-rust_prost_dependencies()
+rust_prost_dependencies(bzlmod)
 
+Declares repositories needed for prost. + +**PARAMETERS** + + +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| bzlmod | Whether bzlmod is enabled. | `False` | +**RETURNS** +list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories + defined by this macro. diff --git a/docs/rust_wasm_bindgen.md b/docs/rust_wasm_bindgen.md index 0f954a5f36..0075888afe 100644 --- a/docs/rust_wasm_bindgen.md +++ b/docs/rust_wasm_bindgen.md @@ -154,6 +154,12 @@ Declare dependencies needed for the `rules_rust` [wasm-bindgen][wb] rules. +**RETURNS** + +list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories + defined by this macro. + + ## rust_wasm_bindgen_register_toolchains diff --git a/proto/prost/repositories.bzl b/proto/prost/repositories.bzl index d0ae717d48..9e849fb3ee 100644 --- a/proto/prost/repositories.bzl +++ b/proto/prost/repositories.bzl @@ -2,26 +2,47 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") +load("//proto/prost/private/3rdparty/crates:crates.bzl", "crate_repositories") -def rust_prost_dependencies(): - maybe( - http_archive, - name = "rules_proto", - sha256 = "dc3fb206a2cb3441b485eb1e423165b231235a1ea9b031b4433cf7bc1fa460dd", - strip_prefix = "rules_proto-5.3.0-21.7", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/refs/tags/5.3.0-21.7.tar.gz", - "https://github.com/bazelbuild/rules_proto/archive/refs/tags/5.3.0-21.7.tar.gz", - ], - ) +def rust_prost_dependencies(bzlmod = False): + """Declares repositories needed for prost. - maybe( - http_archive, - name = "com_google_protobuf", - sha256 = "52b6160ae9266630adb5e96a9fc645215336371a740e87d411bfb63ea2f268a0", - strip_prefix = "protobuf-3.18.0", - urls = ["https://github.com/protocolbuffers/protobuf/releases/download/v3.18.0/protobuf-all-3.18.0.tar.gz"], - ) + Args: + bzlmod (bool): Whether bzlmod is enabled. + + Returns: + list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories + defined by this macro. + """ + + direct_deps = [ + struct(repo = "rules_rust_prost__heck", is_dev_dep = False), + ] + if bzlmod: + # Without bzlmod, this function is normally called by the + # rust_prost_dependencies function in the private directory. + # However, the private directory is inaccessible, plus there's no + # reason to keep two rust_prost_dependencies functions with bzlmod. + direct_deps.extend(crate_repositories()) + else: + maybe( + http_archive, + name = "rules_proto", + sha256 = "dc3fb206a2cb3441b485eb1e423165b231235a1ea9b031b4433cf7bc1fa460dd", + strip_prefix = "rules_proto-5.3.0-21.7", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/refs/tags/5.3.0-21.7.tar.gz", + "https://github.com/bazelbuild/rules_proto/archive/refs/tags/5.3.0-21.7.tar.gz", + ], + ) + + maybe( + http_archive, + name = "com_google_protobuf", + sha256 = "52b6160ae9266630adb5e96a9fc645215336371a740e87d411bfb63ea2f268a0", + strip_prefix = "protobuf-3.18.0", + urls = ["https://github.com/protocolbuffers/protobuf/releases/download/v3.18.0/protobuf-all-3.18.0.tar.gz"], + ) maybe( http_archive, @@ -32,3 +53,4 @@ def rust_prost_dependencies(): strip_prefix = "heck-0.4.1", build_file = Label("@rules_rust//proto/prost/private/3rdparty/crates:BUILD.heck-0.4.1.bazel"), ) + return direct_deps diff --git a/rust/private/extensions.bzl b/rust/private/extensions.bzl index 05f9076d4a..542d70fa53 100644 --- a/rust/private/extensions.bzl +++ b/rust/private/extensions.bzl @@ -1,11 +1,48 @@ """Bzlmod module extensions that are only used internally""" load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load("//bindgen:repositories.bzl", "rust_bindgen_dependencies") +load("//crate_universe:repositories.bzl", "crate_universe_dependencies") +load("//proto/prost:repositories.bzl", "rust_prost_dependencies") load("//rust/private:repository_utils.bzl", "TINYJSON_KWARGS") +load("//test:deps.bzl", "rules_rust_test_deps") +load("//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies") +load("//util/import:deps.bzl", "import_deps") +load("//wasm_bindgen:repositories.bzl", "rust_wasm_bindgen_dependencies") -def _internal_deps_impl(_module_ctx): +def _internal_deps_impl(module_ctx): + # This should contain the subset of WORKSPACE.bazel that defines + # repositories. + + # We don't want rules_rust_dependencies, as they contain things like + # rules_cc, which is already declared in MODULE.bazel. + direct_deps = [struct(repo = "rules_rust_tinyjson", is_dev_dep = False)] http_archive(**TINYJSON_KWARGS) + direct_deps.extend(crate_universe_dependencies()) + direct_deps.extend(rust_prost_dependencies(bzlmod = True)) + direct_deps.extend(rust_bindgen_dependencies()) + direct_deps.extend(rust_analyzer_dependencies()) + direct_deps.extend(import_deps()) + direct_deps.extend(rust_wasm_bindgen_dependencies()) + direct_deps.extend(rules_rust_test_deps()) + + http_archive( + name = "bazelci_rules", + sha256 = "eca21884e6f66a88c358e580fd67a6b148d30ab57b1680f62a96c00f9bc6a07e", + strip_prefix = "bazelci_rules-1.0.0", + url = "https://github.com/bazelbuild/continuous-integration/releases/download/rules-1.0.0/bazelci_rules-1.0.0.tar.gz", + ) + direct_deps.append(struct(repo = "bazelci_rules", is_dev_dep = True)) + + # is_dev_dep is ignored here. It's not relevant for internal_deps, as dev + # dependencies are only relevant for module extensions that can be used + # by other MODULES. + return module_ctx.extension_metadata( + root_module_direct_deps = [repo.repo for repo in direct_deps], + root_module_direct_dev_deps = [], + ) + internal_deps = module_extension( doc = "Dependencies for rules_rust", implementation = _internal_deps_impl, diff --git a/test/deps.bzl b/test/deps.bzl index db3228abe7..412128a8d9 100644 --- a/test/deps.bzl +++ b/test/deps.bzl @@ -25,11 +25,15 @@ rust_library( """ def rules_rust_test_deps(): - """Load dependencies for rules_rust tests""" + """Load dependencies for rules_rust tests - load_arbitrary_tool_test() + Returns: + list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories + defined by this macro. + """ - generated_inputs_in_external_repo() + direct_deps = load_arbitrary_tool_test() + direct_deps.extend(generated_inputs_in_external_repo()) maybe( http_archive, @@ -58,3 +62,11 @@ def rules_rust_test_deps(): strip_prefix = "googleapis-18becb1d1426feb7399db144d7beeb3284f1ccb0", sha256 = "b8c487191eb942361af905e40172644eab490190e717c3d09bf83e87f3994fff", ) + + direct_deps.extend([ + struct(repo = "libc", is_dev_dep = True), + struct(repo = "rules_rust_toolchain_test_target_json", is_dev_dep = True), + struct(repo = "com_google_googleapis", is_dev_dep = True), + ]) + + return direct_deps diff --git a/test/generated_inputs/external_repo.bzl b/test/generated_inputs/external_repo.bzl index c6a8f39fc5..7907c145ca 100644 --- a/test/generated_inputs/external_repo.bzl +++ b/test/generated_inputs/external_repo.bzl @@ -62,3 +62,4 @@ def generated_inputs_in_external_repo(): _generated_inputs_in_external_repo( name = "generated_inputs_in_external_repo", ) + return [struct(repo = "generated_inputs_in_external_repo", is_dev_dep = True)] diff --git a/test/load_arbitrary_tool/load_arbitrary_tool_test.bzl b/test/load_arbitrary_tool/load_arbitrary_tool_test.bzl index 5d8d91d6e0..f0421f30df 100644 --- a/test/load_arbitrary_tool/load_arbitrary_tool_test.bzl +++ b/test/load_arbitrary_tool/load_arbitrary_tool_test.bzl @@ -37,3 +37,4 @@ def load_arbitrary_tool_test(): _load_arbitrary_tool_test( name = "rules_rust_test_load_arbitrary_tool", ) + return [struct(repo = "rules_rust_test_load_arbitrary_tool", is_dev_dep = True)] diff --git a/tools/rust_analyzer/deps.bzl b/tools/rust_analyzer/deps.bzl index c957340679..6d9946a323 100644 --- a/tools/rust_analyzer/deps.bzl +++ b/tools/rust_analyzer/deps.bzl @@ -6,7 +6,7 @@ load("//tools/rust_analyzer/3rdparty/crates:defs.bzl", "crate_repositories") def rust_analyzer_dependencies(): """Define dependencies of the `rust_analyzer` Bazel tools""" - crate_repositories() + return crate_repositories() # For legacy support gen_rust_project_dependencies = rust_analyzer_dependencies diff --git a/util/import/deps.bzl b/util/import/deps.bzl index ffce2b3a23..83f883a16a 100644 --- a/util/import/deps.bzl +++ b/util/import/deps.bzl @@ -5,7 +5,7 @@ The dependencies for running the gen_rust_project binary. load("//util/import/3rdparty/crates:defs.bzl", "crate_repositories") def import_deps(): - crate_repositories() + return crate_repositories() # For legacy support gen_rust_project_dependencies = import_deps diff --git a/wasm_bindgen/repositories.bzl b/wasm_bindgen/repositories.bzl index 4b62b3b176..0c37b013be 100644 --- a/wasm_bindgen/repositories.bzl +++ b/wasm_bindgen/repositories.bzl @@ -25,8 +25,15 @@ def rust_wasm_bindgen_dependencies(): """Declare dependencies needed for the `rules_rust` [wasm-bindgen][wb] rules. [wb]: https://github.com/rustwasm/wasm-bindgen + + Returns: + list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories + defined by this macro. """ + direct_deps = [ + struct(repo = "rules_rust_wasm_bindgen_cli", is_dev_dep = False), + ] maybe( http_archive, name = "rules_rust_wasm_bindgen_cli", @@ -39,7 +46,8 @@ def rust_wasm_bindgen_dependencies(): patches = [Label("//wasm_bindgen/3rdparty/patches:resolver.patch")], ) - crate_repositories() + direct_deps.extend(crate_repositories()) + return direct_deps # buildifier: disable=unnamed-macro def rust_wasm_bindgen_register_toolchains(register_toolchains = True):