aboutsummaryrefslogtreecommitdiff
path: root/tools/interface_generator/defs.bzl
blob: d5c6f9163be2dfb88c3f90ef42442693d2c5d4fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def _generate_interface_impl(ctx):
    out_hdrs = [
        ctx.actions.declare_file(ctx.attr.interface + ".h"),
        ctx.actions.declare_file(ctx.attr.interface + "_mock.hpp"),
    ]
    out_srcs = [
        ctx.actions.declare_file(ctx.attr.interface + "_mock.cpp"),
    ]
    args = ["-l", ctx.file.license.path, "-o", out_hdrs[0].dirname]

    ctx.actions.run(
        tools = ctx.attr._interface_generator_tool[DefaultInfo].data_runfiles.files,
        inputs = [ctx.file.license],
        outputs = out_srcs + out_hdrs,
        executable = ctx.executable._interface_generator_tool,
        arguments = args,
        mnemonic = "GenerateInterface",
    )

    compilation_ctx = cc_common.create_compilation_context(
        headers = depset(out_hdrs),
        includes = depset([out_hdrs[0].dirname]),
    )

    return [
        DefaultInfo(files = depset(out_srcs)),
        CcInfo(compilation_context = compilation_ctx),
    ]

generate_interface_rule = rule(
    implementation = _generate_interface_impl,
    attrs = {
        "interface": attr.string(),
        "license": attr.label(allow_single_file = True),
        "_interface_generator_tool": attr.label(
            executable = True,
            cfg = "exec",
            allow_files = True,
            providers = [DefaultInfo],
            default = Label("//:interface_generator"),
        ),
        "outs": attr.output_list(),
    },
)

def generate_interface(name, interface, license, visibility = None):
    generate_interface_rule(
        name = name,
        interface = interface,
        outs = [name + "_mock.cpp"],
        license = license,
        visibility = visibility,
    )