Skip to content

Add resource_set attr to all expensive rules #3474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ bazel_dep(name = "rules_cc", version = "0.1.1")
bazel_dep(name = "rules_license", version = "1.0.0")
bazel_dep(name = "rules_shell", version = "0.3.0")
bazel_dep(name = "apple_support", version = "1.17.1", repo_name = "build_bazel_apple_support")
bazel_dep(name = "aspect_bazel_lib", version = "2.19.4")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to pull in this dependency? Could we instead add the attribute directly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could vendor this whole file if we preferred: https://github.com/bazel-contrib/bazel-lib/blob/main/lib/resource_sets.bzl - I don't have a strong preference, WDYT?


internal_deps = use_extension("//rust/private:internal_extensions.bzl", "i")
use_repo(
Expand Down
1 change: 1 addition & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ bzl_library(
"@bazel_tools//tools:bzl_srcs",
],
deps = [
"@aspect_bazel_lib//lib:resource_sets",
"@bazel_skylib//lib:paths",
"@bazel_skylib//lib:selects",
"@bazel_skylib//lib:structs",
Expand Down
1 change: 1 addition & 0 deletions docs/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ bazel_dep(name = "stardoc", version = "0.7.2")
bazel_dep(name = "protobuf", version = "29.0", repo_name = "com_google_protobuf")
bazel_dep(name = "rules_cc", version = "0.1.1")
bazel_dep(name = "rules_shell", version = "0.3.0")
bazel_dep(name = "aspect_bazel_lib", version = "2.19.4")
1 change: 1 addition & 0 deletions extensions/bindgen/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "platforms", version = "0.0.11")
bazel_dep(name = "rules_cc", version = "0.1.1")
bazel_dep(name = "llvm-project", version = "17.0.3.bcr.2")
bazel_dep(name = "aspect_bazel_lib", version = "2.19.4")

rust_ext = use_extension("//:extensions.bzl", "rust_ext")
use_repo(
Expand Down
1 change: 1 addition & 0 deletions extensions/prost/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "rules_cc", version = "0.1.1")
bazel_dep(name = "rules_proto", version = "7.1.0")
bazel_dep(name = "protobuf", version = "28.3", repo_name = "com_google_protobuf")
bazel_dep(name = "aspect_bazel_lib", version = "2.19.4")

rust_ext = use_extension("//:extensions.bzl", "rust_ext")
use_repo(
Expand Down
5 changes: 3 additions & 2 deletions extensions/prost/private/prost.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Rules for building protos in Rust with Prost and Tonic."""

load("@aspect_bazel_lib//lib:resource_sets.bzl", "resource_set_attr")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("@rules_proto//proto:defs.bzl", "ProtoInfo", "proto_common")
load("@rules_proto//proto:proto_common.bzl", proto_toolchains = "toolchains")
Expand Down Expand Up @@ -365,7 +366,7 @@ rust_prost_aspect = aspect(
executable = True,
default = Label("//private:protoc_wrapper"),
),
} | RUSTC_ATTRS,
} | RUSTC_ATTRS | resource_set_attr,
fragments = ["cpp"],
toolchains = [
TOOLCHAIN_TYPE,
Expand Down Expand Up @@ -406,7 +407,7 @@ def _rust_prost_library_impl(ctx):
rust_prost_library = rule(
doc = "A rule for generating a Rust library using Prost.",
implementation = _rust_prost_library_impl,
attrs = {
attrs = resource_set_attr | {
"proto": attr.label(
doc = "A `proto_library` target for which to generate Rust gencode.",
providers = [ProtoInfo],
Expand Down
1 change: 1 addition & 0 deletions extensions/protobuf/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ bazel_dep(name = "platforms", version = "0.0.11")
bazel_dep(name = "rules_cc", version = "0.1.1")
bazel_dep(name = "rules_proto", version = "7.1.0")
bazel_dep(name = "protobuf", version = "28.3", repo_name = "com_google_protobuf")
bazel_dep(name = "aspect_bazel_lib", version = "2.19.4")

bazel_dep(name = "bazel_ci_rules", version = "1.0.0", dev_dependency = True)

Expand Down
3 changes: 2 additions & 1 deletion extensions/protobuf/proto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

"""Rust Protobuf Rules"""

load("@aspect_bazel_lib//lib:resource_sets.bzl", "resource_set_attr")
load("@rules_proto//proto:defs.bzl", "ProtoInfo")

# buildifier: disable=bzl-visibility
Expand Down Expand Up @@ -280,7 +281,7 @@ def _rust_proto_library_impl(ctx):

rust_proto_library = rule(
implementation = _rust_proto_library_impl,
attrs = {
attrs = resource_set_attr | {
"crate_name": attr.string(
doc = """\
Crate name to use for this target.
Expand Down
1 change: 1 addition & 0 deletions extensions/wasm_bindgen/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "platforms", version = "0.0.11")
bazel_dep(name = "rules_cc", version = "0.1.1")
bazel_dep(name = "aspect_rules_js", version = "2.1.2")
bazel_dep(name = "aspect_bazel_lib", version = "2.19.4")

rust_ext = use_extension("//:extensions.bzl", "rust_ext")
use_repo(
Expand Down
3 changes: 2 additions & 1 deletion extensions/wasm_bindgen/private/wasm_bindgen_test.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Bazel test rules for [wasm-bindgen](https://crates.io/crates/wasm-bindgen)"""

load("@aspect_bazel_lib//lib:resource_sets.bzl", "resource_set_attr")
load("@rules_rust//rust:defs.bzl", "rust_common")

# buildifier: disable=bzl-visibility
Expand Down Expand Up @@ -329,7 +330,7 @@ rust_wasm_bindgen_test = rule(
executable = True,
default = Label("//private:wasm_bindgen_test_wrapper"),
),
} | RUSTC_ATTRS,
} | RUSTC_ATTRS | resource_set_attr,
fragments = ["cpp"],
toolchains = [
str(Label("//:toolchain_type")),
Expand Down
1 change: 1 addition & 0 deletions rust/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ bzl_library(
":bazel_tools_bzl_lib",
":rules_cc_bzl_lib",
"//rust/platform:bzl_lib",
"@aspect_bazel_lib//lib:resource_sets",
"@bazel_skylib//lib:paths",
"@bazel_skylib//rules:common_settings",
],
Expand Down
13 changes: 7 additions & 6 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

"""Rust rule implementations"""

load("@aspect_bazel_lib//lib:resource_sets.bzl", "resource_set_attr")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("//rust/private:common.bzl", "COMMON_PROVIDERS", "rust_common")
Expand Down Expand Up @@ -817,7 +818,7 @@ _rust_test_attrs = {
rust_library = rule(
implementation = _rust_library_impl,
provides = COMMON_PROVIDERS,
attrs = _common_attrs | {
attrs = _common_attrs | resource_set_attr | {
"disable_pipelining": attr.bool(
default = False,
doc = dedent("""\
Expand Down Expand Up @@ -915,7 +916,7 @@ _rust_static_library_transition = transition(

rust_static_library = rule(
implementation = _rust_static_library_impl,
attrs = _common_attrs | {
attrs = _common_attrs | resource_set_attr | {
"platform": attr.label(
doc = "Optional platform to transition the static library to.",
default = None,
Expand Down Expand Up @@ -964,7 +965,7 @@ _rust_shared_library_transition = transition(

rust_shared_library = rule(
implementation = _rust_shared_library_impl,
attrs = _common_attrs | _experimental_use_cc_common_link_attrs | {
attrs = _common_attrs | _experimental_use_cc_common_link_attrs | resource_set_attr | {
"platform": attr.label(
doc = "Optional platform to transition the shared library to.",
default = None,
Expand Down Expand Up @@ -1017,7 +1018,7 @@ rust_proc_macro = rule(
# need to declare `_allowlist_function_transition`, see
# https://docs.bazel.build/versions/main/skylark/config.html#user-defined-transitions.
attrs = dict(
_common_attrs.items(),
(_common_attrs | resource_set_attr).items(),
_allowlist_function_transition = attr.label(
default = Label("//tools/allowlists/function_transition_allowlist"),
),
Expand Down Expand Up @@ -1105,7 +1106,7 @@ _rust_binary_transition = transition(
rust_binary = rule(
implementation = _rust_binary_impl,
provides = COMMON_PROVIDERS,
attrs = _common_attrs | _rust_binary_attrs | {
attrs = _common_attrs | _rust_binary_attrs | resource_set_attr | {
"platform": attr.label(
doc = "Optional platform to transition the binary to.",
default = None,
Expand Down Expand Up @@ -1294,7 +1295,7 @@ _rust_test_transition = transition(
rust_test = rule(
implementation = _rust_test_impl,
provides = COMMON_PROVIDERS,
attrs = _common_attrs | _rust_test_attrs | {
attrs = _common_attrs | _rust_test_attrs | resource_set_attr | {
"platform": attr.label(
doc = "Optional platform to transition the test to.",
default = None,
Expand Down
8 changes: 7 additions & 1 deletion rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

"""Functionality for constructing actions that invoke the Rust compiler"""

load("@aspect_bazel_lib//lib:resource_sets.bzl", "resource_set")
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load(
"@bazel_tools//tools/build_defs/cc:action_names.bzl",
Expand Down Expand Up @@ -1357,6 +1358,10 @@ def rustc_compile_action(
action_outputs.append(dsym_folder)

if ctx.executable._process_wrapper:
rustc_resource_set = resource_set(ctx.attr)
if rustc_resource_set == None:
rustc_resource_set = get_rustc_resource_set(toolchain)

# Run as normal
ctx.actions.run(
executable = ctx.executable._process_wrapper,
Expand All @@ -1371,8 +1376,8 @@ def rustc_compile_action(
formatted_version,
len(crate_info.srcs.to_list()),
),
resource_set = rustc_resource_set,
toolchain = "@rules_rust//rust:toolchain_type",
resource_set = get_rustc_resource_set(toolchain),
)
if args_metadata:
ctx.actions.run(
Expand All @@ -1388,6 +1393,7 @@ def rustc_compile_action(
formatted_version,
len(crate_info.srcs.to_list()),
),
resource_set = resource_set(ctx.attr),
toolchain = "@rules_rust//rust:toolchain_type",
)
elif hasattr(ctx.executable, "_bootstrap_process_wrapper"):
Expand Down
8 changes: 8 additions & 0 deletions rust/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ def rules_rust_dependencies():
url = "https://github.com/bazelbuild/apple_support/releases/download/1.17.1/apple_support.1.17.1.tar.gz",
)

maybe(
http_archive,
name = "aspect_bazel_lib",
sha256 = "9a44f457810ce64ec36a244cc7c807607541ab88f2535e07e0bf2976ef4b73fe",
strip_prefix = "bazel-lib-2.19.4",
url = "https://github.com/bazel-contrib/bazel-lib/releases/download/v2.19.4/bazel-lib-v2.19.4.tar.gz",
)

# process_wrapper needs a low-dependency way to process json.
maybe(
http_archive,
Expand Down
3 changes: 2 additions & 1 deletion test/unit/consistent_crate_name/with_modified_crate_name.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""A custom rule that threats all its dependencies as direct dependencies."""

load("@aspect_bazel_lib//lib:resource_sets.bzl", "resource_set_attr")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")

# buildifier: disable=bzl-visibility
Expand Down Expand Up @@ -56,7 +57,7 @@ def _with_modified_crate_name_impl(ctx):

with_modified_crate_name = rule(
implementation = _with_modified_crate_name_impl,
attrs = {
attrs = resource_set_attr | {
"deps": attr.label_list(),
"src": attr.label(
allow_single_file = [".rs"],
Expand Down
3 changes: 2 additions & 1 deletion test/unit/force_all_deps_direct/generator.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""A custom rule that threats all its dependencies as direct dependencies."""

load("@aspect_bazel_lib//lib:resource_sets.bzl", "resource_set_attr")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")

# buildifier: disable=bzl-visibility
Expand Down Expand Up @@ -73,7 +74,7 @@ def _generator_impl(ctx):

generator = rule(
implementation = _generator_impl,
attrs = {
attrs = resource_set_attr | {
"deps": attr.label_list(
doc = "List of other libraries to be linked to this library target.",
providers = [CrateInfo],
Expand Down
3 changes: 2 additions & 1 deletion test/unit/pipelined_compilation/wrap.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""A custom rule that wraps a crate called to_wrap."""

load("@aspect_bazel_lib//lib:resource_sets.bzl", "resource_set_attr")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")

# buildifier: disable=bzl-visibility
Expand Down Expand Up @@ -82,7 +83,7 @@ def _wrap_impl(ctx):

wrap = rule(
implementation = _wrap_impl,
attrs = {
attrs = resource_set_attr | {
"crate_name": attr.string(),
"generate_metadata": attr.bool(default = False),
"target": attr.label(),
Expand Down