Skip to content

Commit d712ce6

Browse files
eytankidronnlopezgi
authored andcommitted
Replace create_exec_properties_dict with create_rbe_exec_properties_dict (bazelbuild#763)
* Replace create_exec_properties_dict with create_rbe_exec_properties_dict And also replace merge_dicts with dicts.add * Change initialization order. In repositories/repositories.bzl in the function repositories(), I reversed the order of handling io_bazel_rules_docker" and "bazel_skylib" * Add bazel_toolchains_repositories to bazel_toolchains_client WORKSPACE * Remove bazel symlinks that were unintentionally introduced in prev commit. Also add them to .gitignore. * Modify gitignore * Add repositories to tests/rbe_repo/examples/toolchain_config_host/WORKSPACE as well. * Remove dependency from rules/rbe_repo/build_gen.bzl to skylib Despite the fact that we are still deprecating merge_dicts and recommending that users use dict.add from skylib, we decided not to add that dependency from rules/rbe_repo/build_gen.bzl and instead use a locally defined _merge_dicts. The reason for doing this is that it complicated the setup for any WORKSAPCE that only want to use rbe_autconfig. We don't want these simple WORKSPACEs to need to call respositories() in order not to break.
1 parent 6978bf8 commit d712ce6

File tree

8 files changed

+64
-69
lines changed

8 files changed

+64
-69
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# Ignore all bazel-* symlinks. There is no full list since this can change
1313
# based on the name of the directory bazel is cloned into.
1414
/bazel-*
15+
/tests/rbe_repo/examples/bazel_toolchains_client/bazel-*
16+
/tests/rbe_repo/examples/toolchain_config_host/bazel-*
1517
/rbe-test-output/
1618
# Ignore outputs generated during Bazel bootstrapping.
1719
/output/

WORKSPACE

+5-5
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ rbe_autoconfig(
564564
rbe_autoconfig_root(name = "rbe_autoconfig_root")
565565

566566
# Define several exec property repo rules to be used in testing.
567-
load("//rules/exec_properties:exec_properties.bzl", "create_exec_properties_dict", "custom_exec_properties", "rbe_exec_properties")
567+
load("//rules/exec_properties:exec_properties.bzl", "create_rbe_exec_properties_dict", "custom_exec_properties", "rbe_exec_properties")
568568

569569
# A standard RBE execution property set repo rule.
570570
rbe_exec_properties(
@@ -575,15 +575,15 @@ rbe_exec_properties(
575575
rbe_exec_properties(
576576
name = "exec_properties_with_override",
577577
override_constants = {
578-
"NETWORK_ON": create_exec_properties_dict(docker_network = "off"),
578+
"NETWORK_ON": create_rbe_exec_properties_dict(docker_network = "off"),
579579
},
580580
)
581581

582582
# A custom execution property set repo rule defining its own name for the network on property set.
583583
custom_exec_properties(
584584
name = "network_on_exec_properties",
585585
constants = {
586-
"BESPOKE_NETWORK_ON": create_exec_properties_dict(docker_network = "standard"),
586+
"BESPOKE_NETWORK_ON": create_rbe_exec_properties_dict(docker_network = "standard"),
587587
},
588588
)
589589

@@ -601,14 +601,14 @@ rbe_autoconfig(
601601
use_legacy_platform_definition = False,
602602
)
603603

604-
load("@bazel_toolchains//rules/exec_properties:exec_properties.bzl", "merge_dicts")
604+
load("@bazel_skylib//lib:dicts.bzl", "dicts")
605605

606606
# Use in the BazelCI.
607607
rbe_autoconfig(
608608
name = "buildkite_config",
609609
base_container_digest = "sha256:4bfd33aa9ce73e28718385b8c01608a79bc6546906f01cf9329311cace1766a1",
610610
digest = "sha256:c20046852a2d7910c55d76e0ec9c182b37532a9f0360d22dd5c9a1451b7c3a15",
611-
exec_properties = merge_dicts(DOCKER_SIBLINGS_CONTAINERS, NETWORK_ON),
611+
exec_properties = dicts.add(DOCKER_SIBLINGS_CONTAINERS, NETWORK_ON),
612612
registry = "marketplace.gcr.io",
613613
repository = "google/bazel",
614614
use_legacy_platform_definition = False,

rules/exec_properties/README.md

+17-23
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
exec_properties.bzl contains the following Starlark macros:
44
* rbe_exec_properties
55
* custom_exec_properties
6-
* create_exec_properties_dict
7-
* merge_dicts
6+
* create_rbe_exec_properties_dict
87

98
## rbe_exec_properties
109

@@ -27,16 +26,11 @@ It is highly recommended to use a globally unique name for this repo rule (and
2726
definitely not `exec_properties`) for reasons that are discussed in more details
2827
[below](#anti-patterns).
2928

30-
## create_exec_properties_dict
29+
## create_rbe_exec_properties_dict
3130

32-
`create_exec_properties_dict` is a Starlark macro that creates a dictionary of
33-
remote execution properties. `create_exec_properties_dict` ensures that the
34-
created dictionary is compatible with what RBE supports.
35-
36-
## merge_dicts
37-
38-
`merge_dicts` is a Starlark macro that merges dictionaries of remote execution
39-
properties.
31+
`create_rbe_exec_properties_dict` is a Starlark macro that creates a dictionary
32+
of remote execution properties. `create_rbe_exec_properties_dict` ensures that
33+
the created dictionary is compatible with what RBE supports.
4034

4135
## Use cases
4236

@@ -101,7 +95,7 @@ In the `WORKSPACE` file, call
10195
rbe_exec_properties(
10296
name = "exec_properties",
10397
override_constants = {
104-
"NETWORK_ON": create_exec_properties_dict(docker_network = "off"),
98+
"NETWORK_ON": create_rbe_exec_properties_dict(docker_network = "off"),
10599
},
106100
)
107101
```
@@ -131,7 +125,7 @@ In the `WORKSPACE` file, call:
131125
custom_exec_properties(
132126
name = "proj_a_prefix_high_mem_machine_exec_property",
133127
constants = {
134-
"HIGH_MEM_MACHINE": create_exec_properties_dict(gce_machine_type = "n1-highmem-8"),
128+
"HIGH_MEM_MACHINE": create_rbe_exec_properties_dict(gce_machine_type = "n1-highmem-8"),
135129
},
136130
)
137131
```
@@ -174,27 +168,27 @@ avoid.
174168
}
175169
```
176170

177-
Instead, always prefer using `create_exec_properties_dict` like so:
171+
Instead, always prefer using `create_rbe_exec_properties_dict` like so:
178172
```
179-
create_exec_properties_dict(
173+
create_rbe_exec_properties_dict(
180174
gce_machine_type = "n1-highmem-8",
181175
docker_privileged = True,
182176
docker_sibling_containers = True,
183177
)
184178
```
185179

186-
`create_exec_properties_dict` is better because typos in key names will be
180+
`create_rbe_exec_properties_dict` is better because typos in key names will be
187181
caught early, while parsing the Bazel code, instead of having RBE just ignore
188182
keys that it doesn't recognize and having the developer spend more time that is
189183
necessary trying to figure out what went wrong. Furthermore,
190-
`create_exec_properties_dict` will perform some validation about the values.
184+
`create_rbe_exec_properties_dict` will perform some validation about the values.
191185

192-
### Anti-pattern 2 - Do not call create_exec_properties_dict from a target.
186+
### Anti-pattern 2 - Do not call create_rbe_exec_properties_dict from a target.
193187

194-
Instead `create_exec_properties_dict` should be called from the `WORKSPACE` in
195-
the context of creating a local repo, typically using `custom_exec_properties`.
196-
It can also be called from a `BUILD` file in the context of defining a
197-
platform.
188+
Instead `create_rbe_exec_properties_dict` should be called from the `WORKSPACE`
189+
in the context of creating a local repo, typically using
190+
`custom_exec_properties`. It can also be called from a `BUILD` file in the
191+
context of defining a platform.
198192

199193
Here is what might go wrong if it is called directly when populating the
200194
`exec_properties` field of a target.
@@ -208,7 +202,7 @@ executed remotely on RBE, should run on a high memory machine such as
208202
foo_library(
209203
name="my_lib",
210204
...
211-
exec_properties = create_exec_properties_dict(gce_machine_type = "n1-highmem-8"),
205+
exec_properties = create_rbe_exec_properties_dict(gce_machine_type = "n1-highmem-8"),
212206
)
213207
```
214208

rules/exec_properties/exec_properties.bzl

+17-26
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ PARAMS = {
200200
}
201201

202202
def create_exec_properties_dict(**kwargs):
203+
fail("create_exec_properties_dict is deprecated. Please use create_rbe_exec_properties_dict instead.")
204+
205+
def create_rbe_exec_properties_dict(**kwargs):
203206
"""Return a dict with exec_properties that are supported by RBE.
204207
205208
Args:
@@ -236,19 +239,7 @@ def create_exec_properties_dict(**kwargs):
236239
return dict
237240

238241
def merge_dicts(*dict_args):
239-
"""Merge any number of dicts into a new dict.
240-
241-
Args:
242-
*dict_args: A list of zero or more dicts.
243-
244-
Returns:
245-
A merge of the input dicts. Precedence goes to key value pairs in latter dicts.
246-
"""
247-
result = {}
248-
for dictionary in dict_args:
249-
if dictionary:
250-
result.update(dictionary)
251-
return result
242+
fail("merge_dicts is deprecated. Please use dicts.add() instead. See https://github.com/bazelbuild/bazel-skylib/blob/master/docs/dicts_doc.md")
252243

253244
def _exec_property_sets_repository_impl(repository_ctx):
254245
repository_ctx.file(
@@ -325,21 +316,21 @@ def custom_exec_properties(name, constants):
325316
)
326317

327318
# STANDARD_PROPERTY_SETS is the SoT for the list of constants that rbe_exec_properties defines.
328-
# For more information about what each parameter of create_exec_properties_dict() means, see
319+
# For more information about what each parameter of create_rbe_exec_properties_dict() means, see
329320
# https://cloud.google.com/remote-build-execution/docs/remote-execution-properties.
330321
STANDARD_PROPERTY_SETS = {
331-
"NETWORK_ON": create_exec_properties_dict(docker_network = "standard"),
332-
"NETWORK_OFF": create_exec_properties_dict(docker_network = "off"),
333-
"DOCKER_PRIVILEGED": create_exec_properties_dict(docker_privileged = True),
334-
"NOT_DOCKER_PRIVILEGED": create_exec_properties_dict(docker_privileged = False),
335-
"DOCKER_RUN_AS_ROOT": create_exec_properties_dict(docker_run_as_root = True),
336-
"NOT_DOCKER_RUN_AS_ROOT": create_exec_properties_dict(docker_run_as_root = False),
337-
"DOCKER_SIBLINGS_CONTAINERS": create_exec_properties_dict(docker_sibling_containers = True),
338-
"NOT_DOCKER_SIBLINGS_CONTAINERS": create_exec_properties_dict(docker_sibling_containers = False),
339-
"DOCKER_USE_URANDOM": create_exec_properties_dict(docker_use_urandom = True),
340-
"NOT_DOCKER_USE_URANDOM": create_exec_properties_dict(docker_use_urandom = False),
341-
"LINUX": create_exec_properties_dict(os_family = "Linux"),
342-
"WINDOWS": create_exec_properties_dict(os_family = "Windows"),
322+
"NETWORK_ON": create_rbe_exec_properties_dict(docker_network = "standard"),
323+
"NETWORK_OFF": create_rbe_exec_properties_dict(docker_network = "off"),
324+
"DOCKER_PRIVILEGED": create_rbe_exec_properties_dict(docker_privileged = True),
325+
"NOT_DOCKER_PRIVILEGED": create_rbe_exec_properties_dict(docker_privileged = False),
326+
"DOCKER_RUN_AS_ROOT": create_rbe_exec_properties_dict(docker_run_as_root = True),
327+
"NOT_DOCKER_RUN_AS_ROOT": create_rbe_exec_properties_dict(docker_run_as_root = False),
328+
"DOCKER_SIBLINGS_CONTAINERS": create_rbe_exec_properties_dict(docker_sibling_containers = True),
329+
"NOT_DOCKER_SIBLINGS_CONTAINERS": create_rbe_exec_properties_dict(docker_sibling_containers = False),
330+
"DOCKER_USE_URANDOM": create_rbe_exec_properties_dict(docker_use_urandom = True),
331+
"NOT_DOCKER_USE_URANDOM": create_rbe_exec_properties_dict(docker_use_urandom = False),
332+
"LINUX": create_rbe_exec_properties_dict(os_family = "Linux"),
333+
"WINDOWS": create_rbe_exec_properties_dict(os_family = "Windows"),
343334
}
344335

345336
def rbe_exec_properties(name, override_constants = None):

rules/rbe_repo.bzl

+7-7
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,17 @@ Here is an example of an rbe_autoconfig that configures its underlying platform
258258
to set the size of the shared memory partition for the docker container to 128
259259
megabytes.
260260
261-
load("@bazel_toolchains//rules/exec_properties:exec_properties.bzl", "create_exec_properties_dict")
261+
load("@bazel_toolchains//rules/exec_properties:exec_properties.bzl", "create_rbe_exec_properties_dict")
262262
263263
rbe_autoconfig(
264264
name = "rbe_default",
265265
use_legacy_platform_definition = False,
266-
exec_properties = create_exec_properties_dict(docker_shm_size = "128m"),
266+
exec_properties = create_rbe_exec_properties_dict(docker_shm_size = "128m"),
267267
)
268268
269-
Note the use of create_exec_properties_dict. This is a Bazel macro that makes
270-
it convenient to create the dicts used in exec_properties. You should always
271-
prefer to use it over composing the dict manually.
269+
Note the use of create_rbe_exec_properties_dict. This is a Bazel macro that
270+
makes it convenient to create the dicts used in exec_properties. You should
271+
always prefer to use it over composing the dict manually.
272272
273273
Additionally, there are standard execution property dicts that you may want to
274274
use. These standard dicts should always be preferred if defined. These standard
@@ -290,8 +290,8 @@ rbe_autoconfig(
290290
exec_properties = NETWORK_ON,
291291
)
292292
293-
For more information on create_exec_properties_dict, rbe_exec_properties and
294-
other related Bazel macros, see https://github.com/bazelbuild/bazel-toolchains/tree/master/rules/exec_properties
293+
For more information on create_rbe_exec_properties_dict, rbe_exec_properties
294+
and other related Bazel macros, see https://github.com/bazelbuild/bazel-toolchains/tree/master/rules/exec_properties
295295
296296
NOTES:
297297

rules/rbe_repo/build_gen.bzl

+11-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@ load(
1919
"JAVA_CONFIG_DIR",
2020
"PLATFORM_DIR",
2121
)
22-
load("//rules/exec_properties:exec_properties.bzl", "create_exec_properties_dict", "merge_dicts")
22+
load("//rules/exec_properties:exec_properties.bzl", "create_rbe_exec_properties_dict")
2323

2424
_CC_TOOLCHAIN = ":cc-compiler-k8"
2525

26+
# Defining a local version of dicts.add in order not to create a dependency on bazel_skylib.
27+
def _merge_dicts(*dict_args):
28+
result = {}
29+
for dictionary in dict_args:
30+
if dictionary:
31+
result.update(dictionary)
32+
return result
33+
2634
def create_config_aliases(ctx, toolchain_config_spec_name):
2735
"""Produces BUILD files with alias for the C++ and Java toolchain targets.
2836
@@ -160,11 +168,11 @@ def _create_platform(ctx, exec_properties, image_name, name, cc_toolchain_target
160168
("\",\n \"").join(ctx.attr.target_compatible_with) +
161169
"\",")
162170

163-
platform_exec_properties = create_exec_properties_dict(
171+
platform_exec_properties = create_rbe_exec_properties_dict(
164172
container_image = "docker://%s" % image_name,
165173
os_family = "Linux",
166174
)
167-
platform_exec_properties = merge_dicts(platform_exec_properties, exec_properties)
175+
platform_exec_properties = _merge_dicts(platform_exec_properties, exec_properties)
168176

169177
ctx.template(
170178
PLATFORM_DIR + "/BUILD",

tests/rules/exec_properties/BUILD

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
# limitations under the License.
1414

1515
load(":compare.bzl", "compare_dicts_test")
16-
load("//rules/exec_properties:exec_properties.bzl", "create_exec_properties_dict")
16+
load("//rules/exec_properties:exec_properties.bzl", "create_rbe_exec_properties_dict")
1717

1818
compare_dicts_test(
1919
name = "docker_network_compare_test",
20-
actual = create_exec_properties_dict(docker_network = "standard"),
20+
actual = create_rbe_exec_properties_dict(docker_network = "standard"),
2121
expected = {"dockerNetwork": "standard"},
2222
)
2323

2424
compare_dicts_test(
2525
name = "multiple_properties_compare_test",
26-
actual = create_exec_properties_dict(
26+
actual = create_rbe_exec_properties_dict(
2727
docker_privileged = True,
2828
os_family = "Windows",
2929
),
@@ -35,7 +35,7 @@ compare_dicts_test(
3535

3636
compare_dicts_test(
3737
name = "labels_compare_test",
38-
actual = create_exec_properties_dict(labels = {
38+
actual = create_rbe_exec_properties_dict(labels = {
3939
"abc": "123",
4040
"def": "456",
4141
}),

tests/rules/exec_properties/compare.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""This file contains a test rule compare_dicts_test for unit testing create_exec_properties_dict.
15+
"""This file contains a test rule compare_dicts_test for unit testing create_rbe_exec_properties_dict.
1616
1717
"""
1818

0 commit comments

Comments
 (0)