Build native_binary/test src in correct configuration (#341)

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`.
This commit is contained in:
Geoffrey Martin-Noble 2022-04-05 14:09:55 -07:00 committed by GitHub
parent cc51024fc2
commit 6abad3de5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 4 deletions

View File

@ -25,9 +25,9 @@ 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.file.src, out)
copy_cmd(ctx, ctx.executable.src, out)
else:
copy_bash(ctx, ctx.file.src, out)
copy_bash(ctx, ctx.executable.src, out)
runfiles = ctx.runfiles(files = ctx.files.data)
# Bazel 4.x LTS does not support `merge_all`.
@ -54,9 +54,12 @@ def _impl(ctx):
_ATTRS = {
"src": attr.label(
executable = True,
allow_single_file = 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 = "host",
cfg = "target",
),
"data": attr.label_list(allow_files = True),
# "out" is attr.string instead of attr.output, so that it is select()'able.