Skip to content

Add a rule for encoding textprotos #122

@davidgiven

Description

@davidgiven

i.e. the equivalent of protoc --encode=$MESSAGETYPE. This is useful when embedding binary protos in applications. Something like:

proto_encode(
    name = "encoded",
    src = "something.textproto",
    message = "foo.bar.ThingProto",
    deps = [ ":library_proto" ]
)

This can already be implemented with something like this:

def proto_encode(name, src, message, proto, deps=[]):
    native.genrule(
        name = name,
        srcs = [ src ] + deps,
        outs = [ name + ".binpb" ],
        cmd = "protoc --encode={} --descriptor_set_in={} {} < $(location {}) > $@".format(
            message, ":".join(["$(location {})".format(n) for n in deps]), proto, src)
    )

However, this is very clunky: it requires a hard-coded dependency on the system proto compiler rather than using the same one by the rest of the Bazel stuff (I can't tell if this is exposed anywhere?), and it also requires the author to explicitly pass in the filename of the proto being encoded as well as the message name. This violates layering as the consumer of the proto library shouldn't need to know about the filenames of the source files inside the proto library. Having some bazel magic to make this convenient would be, well, very convenient.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P4This is either out of scope or we don't have bandwidth to review a PR. (No assignee)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions