mirror of
https://github.com/bazelbuild/bazel-skylib
synced 2024-11-27 05:43:25 +00:00
6abad3de5f
This attribute is incorrectly being built in the host configuration when (like any test) it will run in the target configuration. This means that cross compilation will be broken and options that differ between host and target (e.g. `NDEBUG`) will not be as set by the user. I confirmed that without this fix, a test binary with `assert(false)` passes when run under `native_test`. Additionally, the use of `allow_single_file` precludes rules that return multiple files in their DefaultInfo (like `py_binary`). Instead, we can use `allow_files` and access via `ctx.executable`.
130 lines
4.5 KiB
Python
130 lines
4.5 KiB
Python
# Copyright 2019 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.
|
|
|
|
"""native_binary() and native_test() rule implementations.
|
|
|
|
These rules let you wrap a pre-built binary or script in a conventional binary
|
|
and test rule respectively. They fulfill the same goal as sh_binary and sh_test
|
|
do, but they run the wrapped binary directly, instead of through Bash, so they
|
|
don't depend on Bash and work with --shell_exectuable="".
|
|
"""
|
|
|
|
load("//rules/private:copy_file_private.bzl", "copy_bash", "copy_cmd")
|
|
|
|
def _impl_rule(ctx, is_windows):
|
|
out = ctx.actions.declare_file(ctx.attr.out)
|
|
if is_windows:
|
|
copy_cmd(ctx, ctx.executable.src, out)
|
|
else:
|
|
copy_bash(ctx, ctx.executable.src, out)
|
|
runfiles = ctx.runfiles(files = ctx.files.data)
|
|
|
|
# Bazel 4.x LTS does not support `merge_all`.
|
|
# TODO: remove `merge` branch once we drop support for Bazel 4.x.
|
|
if hasattr(runfiles, "merge_all"):
|
|
runfiles = runfiles.merge_all([
|
|
d[DefaultInfo].default_runfiles
|
|
for d in ctx.attr.data + [ctx.attr.src]
|
|
])
|
|
else:
|
|
for d in ctx.attr.data:
|
|
runfiles = runfiles.merge(d[DefaultInfo].default_runfiles)
|
|
runfiles = runfiles.merge(ctx.attr.src[DefaultInfo].default_runfiles)
|
|
|
|
return DefaultInfo(
|
|
executable = out,
|
|
files = depset([out]),
|
|
runfiles = runfiles,
|
|
)
|
|
|
|
def _impl(ctx):
|
|
return _impl_rule(ctx, ctx.attr.is_windows)
|
|
|
|
_ATTRS = {
|
|
"src": attr.label(
|
|
executable = True,
|
|
# This must be used instead of `allow_single_file` because otherwise a
|
|
# target with multiple default outputs (e.g. py_binary) would not be
|
|
# allowed.
|
|
allow_files = True,
|
|
mandatory = True,
|
|
cfg = "target",
|
|
),
|
|
"data": attr.label_list(allow_files = True),
|
|
# "out" is attr.string instead of attr.output, so that it is select()'able.
|
|
"out": attr.string(mandatory = True),
|
|
"is_windows": attr.bool(mandatory = True),
|
|
}
|
|
|
|
_native_binary = rule(
|
|
implementation = _impl,
|
|
attrs = _ATTRS,
|
|
executable = True,
|
|
)
|
|
|
|
_native_test = rule(
|
|
implementation = _impl,
|
|
attrs = _ATTRS,
|
|
test = True,
|
|
)
|
|
|
|
def native_binary(name, src, out, data = None, **kwargs):
|
|
"""Wraps a pre-built binary or script with a binary rule.
|
|
|
|
You can "bazel run" this rule like any other binary rule, and use it as a tool in genrule.tools for example. You can also augment the binary with runfiles.
|
|
|
|
Args:
|
|
name: The name of the rule.
|
|
src: label; path of the pre-built executable
|
|
out: output; an output name for the copy of the binary. (Bazel requires that this rule make a copy of 'src'.)
|
|
data: list of labels; data dependencies
|
|
**kwargs: The <a href="https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes-binaries">common attributes for binaries</a>.
|
|
"""
|
|
_native_binary(
|
|
name = name,
|
|
src = src,
|
|
out = out,
|
|
data = data,
|
|
is_windows = select({
|
|
"@bazel_tools//src/conditions:host_windows": True,
|
|
"//conditions:default": False,
|
|
}),
|
|
**kwargs
|
|
)
|
|
|
|
def native_test(name, src, out, data = None, **kwargs):
|
|
"""Wraps a pre-built binary or script with a test rule.
|
|
|
|
You can "bazel test" this rule like any other test rule. You can also augment the binary with
|
|
runfiles.
|
|
|
|
Args:
|
|
name: The name of the test rule.
|
|
src: label; path of the pre-built executable
|
|
out: output; an output name for the copy of the binary. (Bazel requires that this rule make a copy of 'src'.)
|
|
data: list of labels; data dependencies
|
|
**kwargs: The <a href="https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes-tests">common attributes for tests</a>.
|
|
"""
|
|
_native_test(
|
|
name = name,
|
|
src = src,
|
|
out = out,
|
|
data = data,
|
|
is_windows = select({
|
|
"@bazel_tools//src/conditions:host_windows": True,
|
|
"//conditions:default": False,
|
|
}),
|
|
**kwargs
|
|
)
|