Skip to content

Commit 30f16e5

Browse files
ckolli5Yannic
andauthored
Replace uses of cfg = "host" with cfg = "exec" (bazelbuild#15922)
This increases the compatibility for builds where host and exec platform are different (e.g., with remote execution). Closes bazelbuild#15785. PiperOrigin-RevId: 461897893 Change-Id: I734ccdd1ae13e7c625cc22c894bc482fe080d70b Co-authored-by: Yannic Bonenberger <[email protected]>
1 parent ae929e8 commit 30f16e5

23 files changed

+1648
-30
lines changed

site/docs/creating-workers.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,13 @@ level of the build, this might be the attribute definition:
167167
"worker": attr.label(
168168
default = Label("//work:worker"),
169169
executable = True,
170-
cfg = "host",
170+
cfg = "exec",
171171
)
172172
```
173173

174-
`cfg = "host"` indicates that the worker should be built to run on your host
175-
platform.
174+
`cfg = "exec"` indicates that the worker should be built to run on your
175+
execution platform rather than on the target platform (i.e., the worker is used
176+
as tool during the build).
176177

177178
### Work action requirements
178179

site/docs/skylark/aspects.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ demonstrates how you could pass in a tool to an aspect:
281281
'_protoc' : attr.label(
282282
default = Label('//tools:protoc'),
283283
executable = True,
284-
cfg = "host"
284+
cfg = "exec"
285285
)
286286
}
287287
...

src/embedded_tools.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ embedded_tools = rule(
3535
"out": attr.output(mandatory = True),
3636
"tool": attr.label(
3737
executable = True,
38-
cfg = "host",
38+
cfg = "exec",
3939
allow_files = True,
4040
default = Label("//src:create_embedded_tools_sh"),
4141
),

src/main/res/winsdk_toolchain.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ windows_resource_compiler_toolchain = rule(
6262
"rc_exe": attr.label(
6363
allow_files = True,
6464
executable = True,
65-
cfg = "host",
65+
cfg = "exec",
6666
doc = "Label of the resource compiler (or a wrapper script)",
6767
),
6868
},

src/test/java/com/google/devtools/build/skydoc/testdata/function_wrap_multiple_lines_test/input.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ dependencies here.
4747
"Xwatchconversion": attr.bool(default = False, doc = "Don't delete temporary lexers generated from combined grammars."),
4848
"_tool": attr.label(
4949
executable = True,
50-
cfg = "host",
50+
cfg = "exec",
5151
),
5252
},
5353
)

src/test/java/com/google/devtools/build/skydoc/testdata/misc_apis_test/input.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ A list of dependencies.
4444
doc = "The location of the tool to use.",
4545
allow_files = True,
4646
default = Label("//foo/bar/baz:target"),
47-
cfg = "host",
47+
cfg = "exec",
4848
executable = True,
4949
),
5050
"out": attr.output(

src/test/py/bazel/bazel_windows_cpp_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ def testCcCompileWithTreeArtifactAsSource(self):
861861
' attrs = {',
862862
' "_tool": attr.label(',
863863
' executable = True,',
864-
' cfg = "host",',
864+
' cfg = "exec",',
865865
' allow_files = True,',
866866
' default = Label("//:genccs"),',
867867
' )',

src/test/py/bazel/bazel_windows_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def testRunPowershellInAction(self):
177177
' "out": attr.output(mandatory = True),',
178178
' "tool": attr.label(',
179179
' executable = True,',
180-
' cfg = "host",',
180+
' cfg = "exec",',
181181
' allow_files = True,',
182182
' default = Label("//:write.bat"),',
183183
' ),',

src/test/py/bazel/native_test.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ exe_test = rule(
6868
attrs = {
6969
"src": attr.label(
7070
allow_single_file = True,
71-
cfg = "host",
71+
cfg = "exec",
7272
executable = True,
7373
),
7474
},
+203
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2022 The Bazel Authors. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
# Common Bash functions to test Apple rules in Bazel.
18+
#
19+
20+
function make_starlark_apple_binary_rule_in() {
21+
local dir="$1"; shift
22+
23+
# All of the attributes below, except for `stamp`, are required as part of the
24+
# implied contract of `apple_common.link_multi_arch_binary` since it asks for
25+
# attributes directly from the rule context. As these requirements are changed
26+
# from implied attributes to function arguments, they can be removed.
27+
cat >> "${dir}/starlark_apple_binary.bzl" <<EOF
28+
def _starlark_apple_binary_impl(ctx):
29+
link_result = apple_common.link_multi_arch_binary(
30+
ctx = ctx,
31+
stamp = ctx.attr.stamp,
32+
)
33+
processed_binary = ctx.actions.declare_file(
34+
'{}_lipobin'.format(ctx.label.name)
35+
)
36+
lipo_inputs = [output.binary for output in link_result.outputs]
37+
if len(lipo_inputs) > 1:
38+
apple_env = {}
39+
xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
40+
apple_env.update(apple_common.apple_host_system_env(xcode_config))
41+
apple_env.update(
42+
apple_common.target_apple_env(
43+
xcode_config,
44+
ctx.fragments.apple.single_arch_platform,
45+
),
46+
)
47+
args = ctx.actions.args()
48+
args.add('-create')
49+
args.add_all(lipo_inputs)
50+
args.add('-output', processed_binary)
51+
ctx.actions.run(
52+
arguments = [args],
53+
env = apple_env,
54+
executable = '/usr/bin/lipo',
55+
execution_requirements = xcode_config.execution_info(),
56+
inputs = lipo_inputs,
57+
outputs = [processed_binary],
58+
)
59+
else:
60+
ctx.actions.symlink(
61+
target_file = lipo_inputs[0],
62+
output = processed_binary,
63+
)
64+
return [
65+
DefaultInfo(files = depset([processed_binary])),
66+
OutputGroupInfo(**link_result.output_groups),
67+
link_result.debug_outputs_provider,
68+
]
69+
70+
starlark_apple_binary = rule(
71+
attrs = {
72+
"_child_configuration_dummy": attr.label(
73+
cfg = apple_common.multi_arch_split,
74+
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
75+
),
76+
"_xcode_config": attr.label(
77+
default = configuration_field(
78+
fragment = "apple",
79+
name = "xcode_config_label",
80+
),
81+
),
82+
"_xcrunwrapper": attr.label(
83+
cfg = "exec",
84+
default = Label("@bazel_tools//tools/objc:xcrunwrapper"),
85+
executable = True,
86+
),
87+
"binary_type": attr.string(default = "executable"),
88+
"bundle_loader": attr.label(),
89+
"deps": attr.label_list(
90+
cfg = apple_common.multi_arch_split,
91+
),
92+
"dylibs": attr.label_list(),
93+
"linkopts": attr.string_list(),
94+
"minimum_os_version": attr.string(),
95+
"platform_type": attr.string(),
96+
"stamp": attr.int(default = -1, values = [-1, 0, 1]),
97+
},
98+
fragments = ["apple", "objc", "cpp"],
99+
implementation = _starlark_apple_binary_impl,
100+
outputs = {
101+
# Provided for compatibility with apple_binary tests only.
102+
"lipobin": "%{name}_lipobin",
103+
},
104+
)
105+
EOF
106+
}
107+
108+
function make_starlark_apple_static_library_rule_in() {
109+
local dir="$1"; shift
110+
111+
# All of the attributes below are required as part of the implied contract of
112+
# `apple_common.link_multi_arch_static_library` since it asks for attributes
113+
# directly from the rule context. As these requirements are changed from
114+
# implied attributes to function arguments, they can be removed.
115+
cat >> "${dir}/starlark_apple_static_library.bzl" <<EOF
116+
def _starlark_apple_static_library_impl(ctx):
117+
if not hasattr(apple_common.platform_type, ctx.attr.platform_type):
118+
fail('Unsupported platform type \"{}\"'.format(ctx.attr.platform_type))
119+
link_result = apple_common.link_multi_arch_static_library(ctx = ctx)
120+
processed_library = ctx.actions.declare_file(
121+
'{}_lipo.a'.format(ctx.label.name)
122+
)
123+
files_to_build = [processed_library]
124+
runfiles = ctx.runfiles(
125+
files = files_to_build,
126+
collect_default = True,
127+
collect_data = True,
128+
)
129+
lipo_inputs = [output.library for output in link_result.outputs]
130+
if len(lipo_inputs) > 1:
131+
apple_env = {}
132+
xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
133+
apple_env.update(apple_common.apple_host_system_env(xcode_config))
134+
apple_env.update(
135+
apple_common.target_apple_env(
136+
xcode_config,
137+
ctx.fragments.apple.single_arch_platform,
138+
),
139+
)
140+
args = ctx.actions.args()
141+
args.add('-create')
142+
args.add_all(lipo_inputs)
143+
args.add('-output', processed_library)
144+
ctx.actions.run(
145+
arguments = [args],
146+
env = apple_env,
147+
executable = '/usr/bin/lipo',
148+
execution_requirements = xcode_config.execution_info(),
149+
inputs = lipo_inputs,
150+
outputs = [processed_library],
151+
)
152+
else:
153+
ctx.actions.symlink(
154+
target_file = lipo_inputs[0],
155+
output = processed_library,
156+
)
157+
providers = [
158+
DefaultInfo(files = depset(files_to_build), runfiles = runfiles),
159+
link_result.objc,
160+
link_result.output_groups,
161+
]
162+
return providers
163+
164+
starlark_apple_static_library = rule(
165+
_starlark_apple_static_library_impl,
166+
attrs = {
167+
'_child_configuration_dummy': attr.label(
168+
cfg = apple_common.multi_arch_split,
169+
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
170+
),
171+
'_xcode_config': attr.label(
172+
default = configuration_field(
173+
fragment = "apple",
174+
name = "xcode_config_label",
175+
),
176+
),
177+
'_xcrunwrapper': attr.label(
178+
executable = True,
179+
cfg = 'exec',
180+
default = Label("@bazel_tools//tools/objc:xcrunwrapper"),
181+
),
182+
'additional_linker_inputs': attr.label_list(
183+
allow_files = True,
184+
),
185+
'avoid_deps': attr.label_list(
186+
cfg = apple_common.multi_arch_split,
187+
default = [],
188+
),
189+
'deps': attr.label_list(
190+
cfg = apple_common.multi_arch_split,
191+
),
192+
'linkopts': attr.string_list(),
193+
'platform_type': attr.string(),
194+
'minimum_os_version': attr.string(),
195+
},
196+
outputs = {
197+
'lipo_archive': '%{name}_lipo.a',
198+
},
199+
cfg = apple_common.apple_crosstool_transition,
200+
fragments = ['apple', 'objc', 'cpp',],
201+
)
202+
EOF
203+
}

src/test/shell/bazel/bazel_rules_test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ out_rule = rule(_out_rule, attrs = {
583583
"_hello_bin": attr.label(
584584
default = ":hello_bin",
585585
executable = True,
586-
cfg = "host",
586+
cfg = "exec",
587587
),
588588
})
589589
EOF

src/test/shell/bazel/cc_integration_test.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -700,11 +700,11 @@ def _tree_art_impl(ctx):
700700
tree_art_rule = rule(implementation = _tree_art_impl,
701701
attrs = {
702702
"_makes_tree" : attr.label(allow_single_file = True,
703-
cfg = "host",
703+
cfg = "exec",
704704
executable = True,
705705
default = "//${package}:makes_tree_artifacts.sh"),
706706
"_write" : attr.label(allow_single_file = True,
707-
cfg = "host",
707+
cfg = "exec",
708708
executable = True,
709709
default = "//${package}:write.sh")})
710710

0 commit comments

Comments
 (0)