Skip to content

Commit 796305a

Browse files
committed
wip
1 parent 7fb4a1f commit 796305a

File tree

11 files changed

+255
-445
lines changed

11 files changed

+255
-445
lines changed

MODULE.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ bazel_dep(name = "rules_python", version = "1.2.0")
1414
bazel_dep(name = "rules_shell", version = "0.5.0")
1515
bazel_dep(name = "bazel_lib", version = "3.1.0")
1616

17+
# XXX https://github.com/bazel-contrib/bazel-lib/pull/1235
18+
archive_override(
19+
module_name = "bazel_lib",
20+
integrity = "sha256-YqxrdqZHfcsRmBEucrb2Muuy3BTig93o1BpfS9pP4IA=",
21+
strip_prefix = "bazel-lib-c083e9fdb9f0e3ebdfde3e4e8e8b77c75123eda5",
22+
urls = [
23+
"https://github.com/bazel-contrib/bazel-lib/archive/c083e9fdb9f0e3ebdfde3e4e8e8b77c75123eda5.tar.gz",
24+
],
25+
)
26+
1727
# Dev dependencies
1828
bazel_dep(name = "gazelle", version = "0.41.0", dev_dependency = True, repo_name = "bazel_gazelle")
1929
bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.7.1", dev_dependency = True)

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.0")
43+
archive_override(
44+
module_name = "bazel_lib",
45+
integrity = "sha256-YqxrdqZHfcsRmBEucrb2Muuy3BTig93o1BpfS9pP4IA=",
46+
strip_prefix = "bazel-lib-c083e9fdb9f0e3ebdfde3e4e8e8b77c75123eda5",
47+
urls = [
48+
"https://github.com/bazel-contrib/bazel-lib/archive/c083e9fdb9f0e3ebdfde3e4e8e8b77c75123eda5.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.0")
72+
archive_override(
73+
module_name = "bazel_lib",
74+
integrity = "sha256-YqxrdqZHfcsRmBEucrb2Muuy3BTig93o1BpfS9pP4IA=",
75+
strip_prefix = "bazel-lib-c083e9fdb9f0e3ebdfde3e4e8e8b77c75123eda5",
76+
urls = [
77+
"https://github.com/bazel-contrib/bazel-lib/archive/c083e9fdb9f0e3ebdfde3e4e8e8b77c75123eda5.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:
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: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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, short = True):
31+
if size == _DEFAULT_SIZE:
32+
short_name = _DEFAULT_SIZE
33+
else:
34+
short_name = "{}_{}".format(size, resource)
35+
36+
if short:
37+
return short_name
38+
return "//foreign_cc/settings:size_" + short_name
39+
40+
def create_settings():
41+
"""create the settings that configure these functions."""
42+
string_flag(
43+
name = "size_default",
44+
build_setting_default = _DEFAULT_SIZE,
45+
values = _SIZES.keys() + [_DEFAULT_SIZE],
46+
visibility = ["//visibility:public"],
47+
)
48+
49+
for size, cfg in _SIZES.items():
50+
if cfg == None:
51+
continue
52+
53+
# keep this in sync with the docs and the above helper!
54+
int_flag(
55+
name = "size_{}_cpu".format(size),
56+
build_setting_default = _SIZES[size]["cpu"],
57+
visibility = ["//visibility:public"],
58+
)
59+
60+
int_flag(
61+
name = "size_{}_mem".format(size),
62+
build_setting_default = _SIZES[size]["mem"],
63+
visibility = ["//visibility:public"],
64+
)
65+
66+
SIZE_ATTRIBUTES = {
67+
"resource_size": attr.string(
68+
values = _SIZES.keys() + [_DEFAULT_SIZE],
69+
default = _DEFAULT_SIZE,
70+
mandatory = False,
71+
doc = (
72+
"Set the approximate size of this build. This does two things:" +
73+
"1. Sets the environment variables to tell the underlying build system " +
74+
" the requested parallization; examples are CMAKE_BUILD_PARALLEL_LEVEL " +
75+
" for cmake or MAKEFLAGS for autotools. " +
76+
"2. Sets the resource_set attribute on the action to tell bazel how many cores " +
77+
" are being used, so it schedules appropriately." +
78+
"The sizes map to labels, which can be used to override the meaning of the sizes. See " +
79+
"@rules_foreign_cc//foreign_cc/settings:size_{size}_{cpu|mem}"
80+
),
81+
),
82+
"_size_config": attr.string_keyed_label_dict(
83+
default = {
84+
_setting(size, resource): _setting(size, resource, short = False)
85+
for size in _SIZES.keys()
86+
for resource in ["cpu", "mem"]
87+
} | {
88+
_DEFAULT_SIZE: _setting(size = _DEFAULT_SIZE, resource = None, short = False),
89+
},
90+
),
91+
}
92+
93+
def _get_size_config(size_config, size, resource):
94+
s = size_config.get(
95+
_setting(size = size, resource = resource),
96+
)
97+
98+
if s == None:
99+
fail("unknown size: ", size)
100+
101+
return s[BuildSettingInfo].value
102+
103+
def get_resource_set(attr):
104+
""" get the resource set as configured by the settings and attrs
105+
106+
Args:
107+
attr: the ctx.attr associated with the target
108+
Returns:
109+
A tuple of:
110+
- the resource_set, or None if it's the bazel default
111+
- cpu_cores, or 0 if it's the bazel default
112+
- mem in MB, or 0 if it's the bazel default
113+
"""
114+
size = _DEFAULT_SIZE
115+
if attr.resource_size != _DEFAULT_SIZE:
116+
size = attr.resource_size
117+
else:
118+
size = _get_size_config(attr._size_config, _DEFAULT_SIZE, None)
119+
120+
if size == _DEFAULT_SIZE:
121+
return None, 0, 0
122+
123+
cpu_value = _get_size_config(attr._size_config, size, "cpu")
124+
mem_value = _get_size_config(attr._size_config, size, "mem")
125+
126+
if cpu_value < 0:
127+
fail("cpu must be >= 0")
128+
129+
if mem_value < 0:
130+
fail("cpu must be >= 0")
131+
132+
resource_set = resource_set_for(
133+
cpu_cores = cpu_value,
134+
mem_mb = mem_value,
135+
)
136+
137+
if resource_set:
138+
actual_resources = resource_set("", "")
139+
actual_cpu = actual_resources.get("cpu", 0)
140+
actual_mem = actual_resources.get("mem", 0)
141+
else:
142+
actual_cpu = 0
143+
actual_mem = 0
144+
145+
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-YqxrdqZHfcsRmBEucrb2Muuy3BTig93o1BpfS9pP4IA=",
127+
strip_prefix = "bazel-lib-c083e9fdb9f0e3ebdfde3e4e8e8b77c75123eda5",
128+
urls = [
129+
"https://github.com/bazel-contrib/bazel-lib/archive/c083e9fdb9f0e3ebdfde3e4e8e8b77c75123eda5.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)