Skip to content

Commit

Permalink
Replace uses of cfg = "host" with cfg = "exec" (bazelbuild#15922)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ckolli5 and Yannic authored Jul 20, 2022
1 parent ae929e8 commit 30f16e5
Show file tree
Hide file tree
Showing 23 changed files with 1,648 additions and 30 deletions.
7 changes: 4 additions & 3 deletions site/docs/creating-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,13 @@ level of the build, this might be the attribute definition:
"worker": attr.label(
default = Label("//work:worker"),
executable = True,
cfg = "host",
cfg = "exec",
)
```

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

### Work action requirements

Expand Down
2 changes: 1 addition & 1 deletion site/docs/skylark/aspects.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ demonstrates how you could pass in a tool to an aspect:
'_protoc' : attr.label(
default = Label('//tools:protoc'),
executable = True,
cfg = "host"
cfg = "exec"
)
}
...
Expand Down
2 changes: 1 addition & 1 deletion src/embedded_tools.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ embedded_tools = rule(
"out": attr.output(mandatory = True),
"tool": attr.label(
executable = True,
cfg = "host",
cfg = "exec",
allow_files = True,
default = Label("//src:create_embedded_tools_sh"),
),
Expand Down
2 changes: 1 addition & 1 deletion src/main/res/winsdk_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ windows_resource_compiler_toolchain = rule(
"rc_exe": attr.label(
allow_files = True,
executable = True,
cfg = "host",
cfg = "exec",
doc = "Label of the resource compiler (or a wrapper script)",
),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ dependencies here.
"Xwatchconversion": attr.bool(default = False, doc = "Don't delete temporary lexers generated from combined grammars."),
"_tool": attr.label(
executable = True,
cfg = "host",
cfg = "exec",
),
},
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ A list of dependencies.
doc = "The location of the tool to use.",
allow_files = True,
default = Label("//foo/bar/baz:target"),
cfg = "host",
cfg = "exec",
executable = True,
),
"out": attr.output(
Expand Down
2 changes: 1 addition & 1 deletion src/test/py/bazel/bazel_windows_cpp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ def testCcCompileWithTreeArtifactAsSource(self):
' attrs = {',
' "_tool": attr.label(',
' executable = True,',
' cfg = "host",',
' cfg = "exec",',
' allow_files = True,',
' default = Label("//:genccs"),',
' )',
Expand Down
2 changes: 1 addition & 1 deletion src/test/py/bazel/bazel_windows_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def testRunPowershellInAction(self):
' "out": attr.output(mandatory = True),',
' "tool": attr.label(',
' executable = True,',
' cfg = "host",',
' cfg = "exec",',
' allow_files = True,',
' default = Label("//:write.bat"),',
' ),',
Expand Down
2 changes: 1 addition & 1 deletion src/test/py/bazel/native_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ exe_test = rule(
attrs = {
"src": attr.label(
allow_single_file = True,
cfg = "host",
cfg = "exec",
executable = True,
),
},
Expand Down
203 changes: 203 additions & 0 deletions src/test/shell/bazel/apple/apple_common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
#!/bin/bash
#
# Copyright 2022 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Common Bash functions to test Apple rules in Bazel.
#

function make_starlark_apple_binary_rule_in() {
local dir="$1"; shift

# All of the attributes below, except for `stamp`, are required as part of the
# implied contract of `apple_common.link_multi_arch_binary` since it asks for
# attributes directly from the rule context. As these requirements are changed
# from implied attributes to function arguments, they can be removed.
cat >> "${dir}/starlark_apple_binary.bzl" <<EOF
def _starlark_apple_binary_impl(ctx):
link_result = apple_common.link_multi_arch_binary(
ctx = ctx,
stamp = ctx.attr.stamp,
)
processed_binary = ctx.actions.declare_file(
'{}_lipobin'.format(ctx.label.name)
)
lipo_inputs = [output.binary for output in link_result.outputs]
if len(lipo_inputs) > 1:
apple_env = {}
xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
apple_env.update(apple_common.apple_host_system_env(xcode_config))
apple_env.update(
apple_common.target_apple_env(
xcode_config,
ctx.fragments.apple.single_arch_platform,
),
)
args = ctx.actions.args()
args.add('-create')
args.add_all(lipo_inputs)
args.add('-output', processed_binary)
ctx.actions.run(
arguments = [args],
env = apple_env,
executable = '/usr/bin/lipo',
execution_requirements = xcode_config.execution_info(),
inputs = lipo_inputs,
outputs = [processed_binary],
)
else:
ctx.actions.symlink(
target_file = lipo_inputs[0],
output = processed_binary,
)
return [
DefaultInfo(files = depset([processed_binary])),
OutputGroupInfo(**link_result.output_groups),
link_result.debug_outputs_provider,
]
starlark_apple_binary = rule(
attrs = {
"_child_configuration_dummy": attr.label(
cfg = apple_common.multi_arch_split,
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
),
"_xcode_config": attr.label(
default = configuration_field(
fragment = "apple",
name = "xcode_config_label",
),
),
"_xcrunwrapper": attr.label(
cfg = "exec",
default = Label("@bazel_tools//tools/objc:xcrunwrapper"),
executable = True,
),
"binary_type": attr.string(default = "executable"),
"bundle_loader": attr.label(),
"deps": attr.label_list(
cfg = apple_common.multi_arch_split,
),
"dylibs": attr.label_list(),
"linkopts": attr.string_list(),
"minimum_os_version": attr.string(),
"platform_type": attr.string(),
"stamp": attr.int(default = -1, values = [-1, 0, 1]),
},
fragments = ["apple", "objc", "cpp"],
implementation = _starlark_apple_binary_impl,
outputs = {
# Provided for compatibility with apple_binary tests only.
"lipobin": "%{name}_lipobin",
},
)
EOF
}

function make_starlark_apple_static_library_rule_in() {
local dir="$1"; shift

# All of the attributes below are required as part of the implied contract of
# `apple_common.link_multi_arch_static_library` since it asks for attributes
# directly from the rule context. As these requirements are changed from
# implied attributes to function arguments, they can be removed.
cat >> "${dir}/starlark_apple_static_library.bzl" <<EOF
def _starlark_apple_static_library_impl(ctx):
if not hasattr(apple_common.platform_type, ctx.attr.platform_type):
fail('Unsupported platform type \"{}\"'.format(ctx.attr.platform_type))
link_result = apple_common.link_multi_arch_static_library(ctx = ctx)
processed_library = ctx.actions.declare_file(
'{}_lipo.a'.format(ctx.label.name)
)
files_to_build = [processed_library]
runfiles = ctx.runfiles(
files = files_to_build,
collect_default = True,
collect_data = True,
)
lipo_inputs = [output.library for output in link_result.outputs]
if len(lipo_inputs) > 1:
apple_env = {}
xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
apple_env.update(apple_common.apple_host_system_env(xcode_config))
apple_env.update(
apple_common.target_apple_env(
xcode_config,
ctx.fragments.apple.single_arch_platform,
),
)
args = ctx.actions.args()
args.add('-create')
args.add_all(lipo_inputs)
args.add('-output', processed_library)
ctx.actions.run(
arguments = [args],
env = apple_env,
executable = '/usr/bin/lipo',
execution_requirements = xcode_config.execution_info(),
inputs = lipo_inputs,
outputs = [processed_library],
)
else:
ctx.actions.symlink(
target_file = lipo_inputs[0],
output = processed_library,
)
providers = [
DefaultInfo(files = depset(files_to_build), runfiles = runfiles),
link_result.objc,
link_result.output_groups,
]
return providers
starlark_apple_static_library = rule(
_starlark_apple_static_library_impl,
attrs = {
'_child_configuration_dummy': attr.label(
cfg = apple_common.multi_arch_split,
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
),
'_xcode_config': attr.label(
default = configuration_field(
fragment = "apple",
name = "xcode_config_label",
),
),
'_xcrunwrapper': attr.label(
executable = True,
cfg = 'exec',
default = Label("@bazel_tools//tools/objc:xcrunwrapper"),
),
'additional_linker_inputs': attr.label_list(
allow_files = True,
),
'avoid_deps': attr.label_list(
cfg = apple_common.multi_arch_split,
default = [],
),
'deps': attr.label_list(
cfg = apple_common.multi_arch_split,
),
'linkopts': attr.string_list(),
'platform_type': attr.string(),
'minimum_os_version': attr.string(),
},
outputs = {
'lipo_archive': '%{name}_lipo.a',
},
cfg = apple_common.apple_crosstool_transition,
fragments = ['apple', 'objc', 'cpp',],
)
EOF
}
2 changes: 1 addition & 1 deletion src/test/shell/bazel/bazel_rules_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ out_rule = rule(_out_rule, attrs = {
"_hello_bin": attr.label(
default = ":hello_bin",
executable = True,
cfg = "host",
cfg = "exec",
),
})
EOF
Expand Down
4 changes: 2 additions & 2 deletions src/test/shell/bazel/cc_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -700,11 +700,11 @@ def _tree_art_impl(ctx):
tree_art_rule = rule(implementation = _tree_art_impl,
attrs = {
"_makes_tree" : attr.label(allow_single_file = True,
cfg = "host",
cfg = "exec",
executable = True,
default = "//${package}:makes_tree_artifacts.sh"),
"_write" : attr.label(allow_single_file = True,
cfg = "host",
cfg = "exec",
executable = True,
default = "//${package}:write.sh")})
Expand Down
Loading

0 comments on commit 30f16e5

Please sign in to comment.