Skip to content

Commit af1b60c

Browse files
committed
Add resource_set attr to all expensive rules
This allows better control over resource usage for expensive rules in resource-constrained systems.
1 parent 0cb272d commit af1b60c

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ bazel_dep(name = "rules_cc", version = "0.1.1")
1616
bazel_dep(name = "rules_license", version = "1.0.0")
1717
bazel_dep(name = "rules_shell", version = "0.3.0")
1818
bazel_dep(name = "apple_support", version = "1.17.1", repo_name = "build_bazel_apple_support")
19+
bazel_dep(name = "aspect_bazel_lib", version = "2.19.4")
1920

2021
internal_deps = use_extension("//rust/private:internal_extensions.bzl", "i")
2122
use_repo(

rust/private/rust.bzl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
"""Rust rule implementations"""
1616

17+
load("@aspect_bazel_lib//lib:resource_sets.bzl", "resource_set_attr")
1718
load("@bazel_skylib//lib:paths.bzl", "paths")
1819
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
1920
load("//rust/private:common.bzl", "COMMON_PROVIDERS", "rust_common")
@@ -817,7 +818,7 @@ _rust_test_attrs = {
817818
rust_library = rule(
818819
implementation = _rust_library_impl,
819820
provides = COMMON_PROVIDERS,
820-
attrs = _common_attrs | {
821+
attrs = _common_attrs | resource_set_attr | {
821822
"disable_pipelining": attr.bool(
822823
default = False,
823824
doc = dedent("""\
@@ -915,7 +916,7 @@ _rust_static_library_transition = transition(
915916

916917
rust_static_library = rule(
917918
implementation = _rust_static_library_impl,
918-
attrs = _common_attrs | {
919+
attrs = _common_attrs | resource_set_attr | {
919920
"platform": attr.label(
920921
doc = "Optional platform to transition the static library to.",
921922
default = None,
@@ -964,7 +965,7 @@ _rust_shared_library_transition = transition(
964965

965966
rust_shared_library = rule(
966967
implementation = _rust_shared_library_impl,
967-
attrs = _common_attrs | _experimental_use_cc_common_link_attrs | {
968+
attrs = _common_attrs | _experimental_use_cc_common_link_attrs | resource_set_attr | {
968969
"platform": attr.label(
969970
doc = "Optional platform to transition the shared library to.",
970971
default = None,
@@ -1017,7 +1018,7 @@ rust_proc_macro = rule(
10171018
# need to declare `_allowlist_function_transition`, see
10181019
# https://docs.bazel.build/versions/main/skylark/config.html#user-defined-transitions.
10191020
attrs = dict(
1020-
_common_attrs.items(),
1021+
(_common_attrs | resource_set_attr).items(),
10211022
_allowlist_function_transition = attr.label(
10221023
default = Label("//tools/allowlists/function_transition_allowlist"),
10231024
),
@@ -1105,7 +1106,7 @@ _rust_binary_transition = transition(
11051106
rust_binary = rule(
11061107
implementation = _rust_binary_impl,
11071108
provides = COMMON_PROVIDERS,
1108-
attrs = _common_attrs | _rust_binary_attrs | {
1109+
attrs = _common_attrs | _rust_binary_attrs | resource_set_attr | {
11091110
"platform": attr.label(
11101111
doc = "Optional platform to transition the binary to.",
11111112
default = None,
@@ -1294,7 +1295,7 @@ _rust_test_transition = transition(
12941295
rust_test = rule(
12951296
implementation = _rust_test_impl,
12961297
provides = COMMON_PROVIDERS,
1297-
attrs = _common_attrs | _rust_test_attrs | {
1298+
attrs = _common_attrs | _rust_test_attrs | resource_set_attr | {
12981299
"platform": attr.label(
12991300
doc = "Optional platform to transition the test to.",
13001301
default = None,

rust/private/rustc.bzl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

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

17+
load("@aspect_bazel_lib//lib:resource_sets.bzl", "resource_set")
1718
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
1819
load(
1920
"@bazel_tools//tools/build_defs/cc:action_names.bzl",
@@ -1357,6 +1358,9 @@ def rustc_compile_action(
13571358
action_outputs.append(dsym_folder)
13581359

13591360
if ctx.executable._process_wrapper:
1361+
rustc_resource_set = resource_set(ctx.attr)
1362+
if rustc_resource_set == None:
1363+
rustc_resource_set = get_rustc_resource_set(toolchain)
13601364
# Run as normal
13611365
ctx.actions.run(
13621366
executable = ctx.executable._process_wrapper,
@@ -1371,8 +1375,8 @@ def rustc_compile_action(
13711375
formatted_version,
13721376
len(crate_info.srcs.to_list()),
13731377
),
1378+
resource_set = rustc_resource_set,
13741379
toolchain = "@rules_rust//rust:toolchain_type",
1375-
resource_set = get_rustc_resource_set(toolchain),
13761380
)
13771381
if args_metadata:
13781382
ctx.actions.run(
@@ -1388,6 +1392,7 @@ def rustc_compile_action(
13881392
formatted_version,
13891393
len(crate_info.srcs.to_list()),
13901394
),
1395+
resource_set = resource_set(ctx.attr),
13911396
toolchain = "@rules_rust//rust:toolchain_type",
13921397
)
13931398
elif hasattr(ctx.executable, "_bootstrap_process_wrapper"):

test/unit/pipelined_compilation/wrap.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""A custom rule that wraps a crate called to_wrap."""
22

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

56
# buildifier: disable=bzl-visibility
67
load("//rust/private:providers.bzl", "BuildInfo", "CrateInfo", "DepInfo", "DepVariantInfo")
@@ -82,7 +83,7 @@ def _wrap_impl(ctx):
8283

8384
wrap = rule(
8485
implementation = _wrap_impl,
85-
attrs = {
86+
attrs = resource_set_attr | {
8687
"crate_name": attr.string(),
8788
"generate_metadata": attr.bool(default = False),
8889
"target": attr.label(),

0 commit comments

Comments
 (0)