• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Generated unittests to verify that a binary is built for the expected architecture."""
2
3load("//build_defs:internal_shell.bzl", "inline_sh_test")
4
5def _arch_test_impl(
6        name,
7        platform,
8        file_platform,
9        bazel_binaries = [],
10        system_binaries = [],
11        **kwargs):
12    """Bazel rule to verify that a Bazel or system binary is built for the aarch64 architecture.
13
14    Args:
15      name: the name of the test.
16      platform: a diagnostic name for this architecture.
17      file_platform: the expected output of `file`.
18      bazel_binaries: a set of binary targets to inspect.
19      system_binaries: a set of paths to system executables to inspect.
20      **kwargs: other keyword arguments that are passed to the test.
21    """
22
23    inline_sh_test(
24        name = name,
25        tools = bazel_binaries,
26        cmd = """
27          for binary in "%s"; do
28            (file -L $$binary | grep -q "%s") \
29                || (echo "Test binary is not an %s binary: "; file -L $$binary; exit 1)
30          done
31        """ % (
32            " ".join(["$(rootpaths %s)" % b for b in bazel_binaries] + system_binaries),
33            file_platform,
34            platform,
35        ),
36        target_compatible_with = select({
37            "//build_defs:" + platform: [],
38            "//conditions:default": ["@platforms//:incompatible"],
39        }),
40        **kwargs
41    )
42
43def aarch64_test(**kwargs):
44    _arch_test_impl(
45        platform = "aarch64",
46        file_platform = "ELF 64-bit LSB.* ARM aarch64",
47        **kwargs
48    )
49
50def x86_64_test(**kwargs):
51    _arch_test_impl(
52        platform = "x86_64",
53        file_platform = "ELF 64-bit LSB.*, ARM x86_64",
54        **kwargs
55    )
56