Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a rule for encoding textprotos #122

Open
davidgiven opened this issue Jun 4, 2022 · 1 comment
Open

Add a rule for encoding textprotos #122

davidgiven opened this issue Jun 4, 2022 · 1 comment
Labels
P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee)

Comments

@davidgiven
Copy link

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.

@comius comius added the P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) label Jun 10, 2022
@hauserx
Copy link

hauserx commented Dec 8, 2024

Not sure if there is better way now, but below seems to do the trick in hermetic way. This solves just the issue of not using system protoc, not the other ones:

genrule(
    name = "default_build_language_pbtxt_to_pb",
    srcs = [
        ":default_build_language.pbtxt",
    ],
    outs = ["default_build_language.pb"],
    tools = [
        "@protobuf//:protoc",
        "@bazel_tools//src/main/protobuf:build_proto",
    ],
    cmd = "cat $(execpath :default_build_language.pbtxt) | " +
        "$(execpath @protobuf//:protoc) " +
        "--encode=blaze_query.BuildLanguage " +
        "--deterministic_output " +
        "--descriptor_set_in=$(execpath @bazel_tools//src/main/protobuf:build_proto) " +
        "> $@",
    visibility = ["//visibility:public"],
)

(full example in https://github.com/cameron-martin/bazel-lsp/pull/67/files)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee)
Projects
None yet
Development

No branches or pull requests

3 participants