Skip to content

Commit b04bc68

Browse files
committed
wip
1 parent 7fb4a1f commit b04bc68

File tree

11 files changed

+261
-446
lines changed

11 files changed

+261
-446
lines changed

MODULE.bazel

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,17 @@ bazel_dep(name = "platforms", version = "1.0.0")
1212
bazel_dep(name = "rules_cc", version = "0.1.5")
1313
bazel_dep(name = "rules_python", version = "1.2.0")
1414
bazel_dep(name = "rules_shell", version = "0.5.0")
15-
bazel_dep(name = "bazel_lib", version = "3.1.0")
15+
bazel_dep(name = "bazel_lib", version = "3.1.1")
16+
17+
# XXX https://github.com/bazel-contrib/bazel-lib/pull/1235
18+
archive_override(
19+
module_name = "bazel_lib",
20+
integrity = "sha256-8u81hQmpvra/CqtXvddEjU0iYDp4FmYENUjwoAsX2DI=",
21+
strip_prefix = "bazel-lib-e6420f3ec1503ccdd221a5c75d3dcf272feb4c20",
22+
urls = [
23+
"https://github.com/bazel-contrib/bazel-lib/archive/e6420f3ec1503ccdd221a5c75d3dcf272feb4c20.tar.gz",
24+
],
25+
)
1626

1727
# Dev dependencies
1828
bazel_dep(name = "gazelle", version = "0.41.0", dev_dependency = True, repo_name = "bazel_gazelle")

docs/MODULE.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,14 @@ bazel_dep(
3737
version = "29.3",
3838
repo_name = "com_google_protobuf",
3939
)
40+
41+
# XXX https://github.com/bazel-contrib/bazel-lib/pull/1235
42+
bazel_dep(name = "bazel_lib", version = "3.1.1")
43+
archive_override(
44+
module_name = "bazel_lib",
45+
integrity = "sha256-8u81hQmpvra/CqtXvddEjU0iYDp4FmYENUjwoAsX2DI=",
46+
strip_prefix = "bazel-lib-e6420f3ec1503ccdd221a5c75d3dcf272feb4c20",
47+
urls = [
48+
"https://github.com/bazel-contrib/bazel-lib/archive/e6420f3ec1503ccdd221a5c75d3dcf272feb4c20.tar.gz",
49+
],
50+
)

docs/MODULE.bazel.lock

Lines changed: 34 additions & 442 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/MODULE.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ use_repo(
6767
"rust_example",
6868
)
6969

70+
# XXX https://github.com/bazel-contrib/bazel-lib/pull/1235
71+
bazel_dep(name = "bazel_lib", version = "3.1.1")
72+
archive_override(
73+
module_name = "bazel_lib",
74+
integrity = "sha256-8u81hQmpvra/CqtXvddEjU0iYDp4FmYENUjwoAsX2DI=",
75+
strip_prefix = "bazel-lib-e6420f3ec1503ccdd221a5c75d3dcf272feb4c20",
76+
urls = [
77+
"https://github.com/bazel-contrib/bazel-lib/archive/e6420f3ec1503ccdd221a5c75d3dcf272feb4c20.tar.gz",
78+
],
79+
)
80+
7081
# bazel_dep(name = "rules_android", version = "0.1.1")
7182
# bazel_dep(name = "rules_jvm_external", version = "5.1")
7283
#

foreign_cc/built_tools/private/built_tools_framework.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
44
load("//foreign_cc/private:cc_toolchain_util.bzl", "absolutize_path_in_str")
55
load("//foreign_cc/private:detect_root.bzl", "detect_root")
66
load("//foreign_cc/private:framework.bzl", "get_env_prelude", "wrap_outputs")
7+
load("//foreign_cc/private:resource_sets.bzl", "SIZE_ATTRIBUTES", "get_resource_set")
78
load("//foreign_cc/private/framework:helpers.bzl", "convert_shell_script", "shebang")
89
load("//foreign_cc/private/framework:platform.bzl", "PLATFORM_CONSTRAINTS_RULE_ATTRIBUTES")
910

@@ -35,6 +36,7 @@ FOREIGN_CC_BUILT_TOOLS_ATTRS = {
3536

3637
# this would be cleaner as x | y, but that's not supported in bazel 5.4.0
3738
FOREIGN_CC_BUILT_TOOLS_ATTRS.update(PLATFORM_CONSTRAINTS_RULE_ATTRIBUTES)
39+
FOREIGN_CC_BUILT_TOOLS_ATTRS.update(SIZE_ATTRIBUTES)
3840

3941
# Common fragments for all built_tool rules
4042
FOREIGN_CC_BUILT_TOOLS_FRAGMENTS = [
@@ -153,6 +155,14 @@ def built_tool_rule_impl(ctx, script_lines, out_dir, mnemonic, additional_tools
153155
if additional_tools:
154156
tools = depset(transitive = [tools, additional_tools])
155157

158+
resource_set, cpu, _mem = get_resource_set(ctx.attr)
159+
env = None
160+
if cpu > 0:
161+
env = {
162+
"CMAKE_BUILD_PARALLEL_LEVEL": str(cpu),
163+
"MAKEFLAGS": "-j{}".format(cpu),
164+
}
165+
156166
# The use of `run_shell` here is intended to ensure bash is correctly setup on windows
157167
# environments. This should not be replaced with `run` until a cross platform implementation
158168
# is found that guarantees bash exists or appropriately errors out.
@@ -161,6 +171,8 @@ def built_tool_rule_impl(ctx, script_lines, out_dir, mnemonic, additional_tools
161171
inputs = ctx.attr.srcs.files,
162172
outputs = [out_dir, wrapped_outputs.log_file],
163173
tools = tools,
174+
env = env,
175+
resource_set = resource_set,
164176
use_default_shell_env = True,
165177
command = wrapped_outputs.wrapper_script_file.path,
166178
execution_requirements = {"block-network": ""},

foreign_cc/private/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ bzl_library(
4343
":cc_toolchain_util",
4444
":detect_root",
4545
":detect_xcompile.bzl",
46+
":resource_sets",
4647
":run_shell_file_utils",
4748
"//foreign_cc:providers",
4849
"//foreign_cc/private/framework:helpers",
@@ -96,3 +97,13 @@ bzl_library(
9697
srcs = ["run_shell_file_utils.bzl"],
9798
visibility = ["//foreign_cc:__subpackages__"],
9899
)
100+
101+
bzl_library(
102+
name = "resource_sets",
103+
srcs = ["resource_sets.bzl"],
104+
visibility = ["//foreign_cc:__subpackages__"],
105+
deps = [
106+
"@bazel_lib//lib:resource_sets",
107+
"@bazel_skylib//rules:common_settings",
108+
],
109+
)

foreign_cc/private/framework.bzl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
99
load("@rules_cc//cc:defs.bzl", "CcInfo", "cc_common")
1010
load("//foreign_cc:providers.bzl", "ForeignCcArtifactInfo", "ForeignCcDepsInfo")
1111
load("//foreign_cc/private:detect_root.bzl", "filter_containing_dirs_from_inputs")
12+
load("//foreign_cc/private:resource_sets.bzl", "SIZE_ATTRIBUTES", "get_resource_set")
1213
load(
1314
"//foreign_cc/private/framework:helpers.bzl",
1415
"convert_shell_script",
@@ -252,6 +253,7 @@ CC_EXTERNAL_RULE_ATTRIBUTES = {
252253

253254
# this would be cleaner as x | y, but that's not supported in bazel 5.4.0
254255
CC_EXTERNAL_RULE_ATTRIBUTES.update(PLATFORM_CONSTRAINTS_RULE_ATTRIBUTES)
256+
CC_EXTERNAL_RULE_ATTRIBUTES.update(SIZE_ATTRIBUTES)
255257

256258
# A list of common fragments required by rules using this framework
257259
CC_EXTERNAL_RULE_FRAGMENTS = [
@@ -542,6 +544,13 @@ def cc_external_rule_impl(ctx, attrs):
542544
for tool in attrs.tools_deps:
543545
tool_runfiles += tool[DefaultInfo].default_runfiles.files.to_list()
544546

547+
resource_set, cpu, _mem = get_resource_set(ctx.attr)
548+
env = None
549+
if cpu > 0:
550+
env = {
551+
"CMAKE_BUILD_PARALLEL_LEVEL": str(cpu),
552+
"MAKEFLAGS": "-j{}".format(cpu),
553+
}
545554
ctx.actions.run_shell(
546555
mnemonic = "Cc" + attrs.configure_name.capitalize() + "MakeRule",
547556
inputs = depset(inputs.declared_inputs),
@@ -561,6 +570,8 @@ def cc_external_rule_impl(ctx, attrs):
561570
configure_name = attrs.configure_name,
562571
lib_name = lib_name,
563572
),
573+
resource_set = resource_set,
574+
env = env,
564575
)
565576

566577
# Gather runfiles transitively as per the documentation in:
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
"""Resource set definitions for build actions"""
2+
3+
load("@bazel_lib//lib:resource_sets.bzl", "resource_set_for")
4+
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo", "int_flag", "string_flag")
5+
6+
_DEFAULT_SIZE = "default"
7+
_SIZES = {
8+
"enormous": {
9+
"cpu": 16,
10+
"mem": 2048,
11+
},
12+
"large": {
13+
"cpu": 8,
14+
"mem": 1024,
15+
},
16+
"medium": {
17+
"cpu": 4,
18+
"mem": 500,
19+
},
20+
"small": {
21+
"cpu": 2,
22+
"mem": 250,
23+
},
24+
"tiny": {
25+
"cpu": 1,
26+
"mem": 250,
27+
},
28+
}
29+
30+
def _setting(size, resource, mode):
31+
if size == _DEFAULT_SIZE:
32+
short_name = _DEFAULT_SIZE
33+
else:
34+
short_name = "{}_{}".format(size, resource)
35+
36+
if mode == "key":
37+
return "_size_config_" + short_name
38+
elif mode == "label":
39+
return "//foreign_cc/settings:size_" + short_name
40+
else:
41+
fail("unknown mode", mode)
42+
43+
def create_settings():
44+
"""create the settings that configure these functions."""
45+
string_flag(
46+
name = "size_default",
47+
build_setting_default = _DEFAULT_SIZE,
48+
values = _SIZES.keys() + [_DEFAULT_SIZE],
49+
visibility = ["//visibility:public"],
50+
)
51+
52+
for size, cfg in _SIZES.items():
53+
if cfg == None:
54+
continue
55+
56+
# keep this in sync with the docs and the above helper!
57+
int_flag(
58+
name = "size_{}_cpu".format(size),
59+
build_setting_default = _SIZES[size]["cpu"],
60+
visibility = ["//visibility:public"],
61+
)
62+
63+
int_flag(
64+
name = "size_{}_mem".format(size),
65+
build_setting_default = _SIZES[size]["mem"],
66+
visibility = ["//visibility:public"],
67+
)
68+
69+
SIZE_ATTRIBUTES = {
70+
"resource_size": attr.string(
71+
values = _SIZES.keys() + [_DEFAULT_SIZE],
72+
default = _DEFAULT_SIZE,
73+
mandatory = False,
74+
doc = (
75+
"Set the approximate size of this build. This does two things:" +
76+
"1. Sets the environment variables to tell the underlying build system " +
77+
" the requested parallelization; examples are CMAKE_BUILD_PARALLEL_LEVEL " +
78+
" for cmake or MAKEFLAGS for autotools. " +
79+
"2. Sets the resource_set attribute on the action to tell bazel how many cores " +
80+
" are being used, so it schedules appropriately." +
81+
"The sizes map to labels, which can be used to override the meaning of the sizes. See " +
82+
"@rules_foreign_cc//foreign_cc/settings:size_{size}_{cpu|mem}"
83+
),
84+
),
85+
} | {
86+
_setting(size = size, resource = resource, mode = "key"): attr.label(
87+
default = _setting(size, resource, mode = "label"),
88+
providers = [BuildSettingInfo],
89+
)
90+
for size in _SIZES.keys()
91+
for resource in ["cpu", "mem"]
92+
} | {
93+
_setting(size = _DEFAULT_SIZE, resource = None, mode = "key"): attr.label(
94+
default = _setting(size = _DEFAULT_SIZE, resource = None, mode = "label"),
95+
providers = [BuildSettingInfo],
96+
),
97+
}
98+
99+
def _get_size_config(attr, size, resource):
100+
name = _setting(size = size, resource = resource, mode = "key")
101+
s = getattr(attr, name, None)
102+
103+
if s == None:
104+
fail("unknown size: ", size)
105+
106+
return s[BuildSettingInfo].value
107+
108+
def get_resource_set(attr):
109+
""" get the resource set as configured by the settings and attrs
110+
111+
Args:
112+
attr: the ctx.attr associated with the target
113+
Returns:
114+
A tuple of:
115+
- the resource_set, or None if it's the bazel default
116+
- cpu_cores, or 0 if it's the bazel default
117+
- mem in MB, or 0 if it's the bazel default
118+
"""
119+
size = _DEFAULT_SIZE
120+
if attr.resource_size != _DEFAULT_SIZE:
121+
size = attr.resource_size
122+
else:
123+
size = _get_size_config(attr, _DEFAULT_SIZE, None)
124+
125+
if size == _DEFAULT_SIZE:
126+
return None, 0, 0
127+
128+
cpu_value = _get_size_config(attr, size, "cpu")
129+
mem_value = _get_size_config(attr, size, "mem")
130+
131+
if cpu_value < 0:
132+
fail("cpu must be >= 0")
133+
134+
if mem_value < 0:
135+
fail("mem must be >= 0")
136+
137+
resource_set = resource_set_for(
138+
cpu_cores = cpu_value,
139+
mem_mb = mem_value,
140+
)
141+
142+
if resource_set:
143+
actual_resources = resource_set("", "")
144+
actual_cpu = actual_resources.get("cpu", 0)
145+
actual_mem = actual_resources.get("mem", 0)
146+
else:
147+
actual_cpu = 0
148+
actual_mem = 0
149+
150+
return resource_set, actual_cpu, actual_mem

foreign_cc/repositories.bzl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,15 @@ def rules_foreign_cc_dependencies(
119119
],
120120
)
121121

122+
# XXX https://github.com/bazel-contrib/bazel-lib/pull/1235
122123
maybe(
123124
http_archive,
124125
name = "bazel_lib",
125-
sha256 = "fd0fe4df9b6b7837d5fd765c04ffcea462530a08b3d98627fb6be62a693f4e12",
126-
strip_prefix = "bazel-lib-3.1.0",
127-
url = "https://github.com/bazel-contrib/bazel-lib/releases/download/v3.1.0/bazel-lib-v3.1.0.tar.gz",
126+
integrity = "sha256-8u81hQmpvra/CqtXvddEjU0iYDp4FmYENUjwoAsX2DI=",
127+
strip_prefix = "bazel-lib-e6420f3ec1503ccdd221a5c75d3dcf272feb4c20",
128+
urls = [
129+
"https://github.com/bazel-contrib/bazel-lib/archive/e6420f3ec1503ccdd221a5c75d3dcf272feb4c20.tar.gz",
130+
],
128131
)
129132

130133
maybe(

foreign_cc/settings/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load("//foreign_cc/private:resource_sets.bzl", _create_settings = "create_settings")
2+
3+
_create_settings()

0 commit comments

Comments
 (0)