• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1ShBinaryInfo = provider(
2    "Info needed for sh_binary modules",
3    fields = {
4        "sub_dir": "Optional subdirectory to install into",
5        "filename": "Optional name for the installed file",
6    },
7)
8
9def sh_binary(
10        name,
11        srcs,
12        sub_dir = None,
13        filename = None,
14        **kwargs):
15    "Bazel macro to correspond with the sh_binary Soong module."
16
17    internal_name = name + "_internal"
18    native.sh_binary(
19        name = internal_name,
20        srcs = srcs,
21        **kwargs
22    )
23
24    # We need this wrapper rule around native.sh_binary in order to provide extra
25    # attributes such as filename and sub_dir that are useful when building apex.
26    _sh_binary_combiner(
27        name = name,
28        sub_dir = sub_dir,
29        filename = filename,
30        dep = internal_name,
31    )
32
33def _sh_binary_combiner_impl(ctx):
34    dep = ctx.attr.dep[DefaultInfo]
35    output = ctx.outputs.executable
36
37    ctx.actions.run_shell(
38        outputs = [output],
39        inputs = [dep.files_to_run.executable],
40        command = "cp %s %s" % (dep.files_to_run.executable.path, output.path),
41        mnemonic = "CopyNativeShBinary",
42    )
43
44    files = depset(direct = [output], transitive = [dep.files])
45
46    return [
47        DefaultInfo(
48            files = files,
49            runfiles = ctx.runfiles().merge(dep.data_runfiles).merge(dep.default_runfiles),
50            executable = output,
51        ),
52        ShBinaryInfo(
53            sub_dir = ctx.attr.sub_dir,
54            filename = ctx.attr.filename,
55        ),
56    ]
57
58_sh_binary_combiner = rule(
59    implementation = _sh_binary_combiner_impl,
60    attrs = {
61        "sub_dir": attr.string(),
62        "filename": attr.string(),
63        "dep": attr.label(mandatory = True),
64    },
65    provides = [ShBinaryInfo],
66    executable = True,
67    doc = "Wrapper rule around native.sh_binary to provide extra attributes",
68)
69