rules_proto icon indicating copy to clipboard operation
rules_proto copied to clipboard

Add a rule for encoding textprotos

Open davidgiven opened this issue 3 years ago • 1 comments
trafficstars

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.

davidgiven avatar Jun 04 '22 10:06 davidgiven