tokio-rs / prost

PROST! a Protocol Buffers implementation for the Rust Language
Apache License 2.0
3.9k stars 504 forks source link

How to compile protos using protoc directly #330

Closed DrRibosome closed 2 years ago

DrRibosome commented 4 years ago

Is there a way to compile .proto files using prost as a protoc plugin directly (eg, by calling protoc ... directly), without compiling via a build.rs? Ive been looking through the documentation but cant find any examples.

I've been looking through the source but didnt see any suitable binary targets that might be used with protoc

danburkert commented 4 years ago

prost currently doesn't ship a protoc plugin. Previous versions did, and I wouldn't be surprised if we did again in the future, but for now it's not a part of the project. You may be interested in https://github.com/danburkert/prost/pull/313

avinassh commented 4 years ago

why the newer versions did not include plugin? or why was it removed

benma commented 4 years ago

If it helps, I made a quick Rust cli tool to compile my protobufs. It does not use protoc ... but does the job anyway without build.rs.

fn main() {
    let messages_dir = std::env::var("MESSAGES_DIR").expect("MESSAGES_DIR env var not set");
    // OUT_DIR env var also required and is handled by prost.
    prost_build::compile_protos(&["my.proto"], &[&messages_dir]).unwrap();
}
danburkert commented 3 years ago

why the newer versions did not include plugin? or why was it removed

It was removed because no one was using it at the time, and I didn't want to continue supporting it. The way prost-build works out of the box is, in my opinion, the correct way for the vast majority of applications. Adding it back is non trivial for a few reasons:

henrikrudstrom commented 3 years ago

But it does use the protoc binary under the hood right? at least i've stumbled upon some references to it in the docs. regarding config options, protoc plugins can recieve options from the command line which are available in the CodeGeneratorRequest passed to the plugin. For example if the plugin is called protoc-gen-prost we can pass an arg to protoc --prost_opt=key=value,another_key=another_value.

I think a good usecase for the protoc plugin is to be compatible with grpc tooling, in-house tooling or for example buf/build

neoeinstein commented 2 years ago

I know that this is a long old issue, but as mentioned, buf has come out with some great tooling around the use of protoc plugins to generate code for many languages. Having prost capable there would make it easy to snap into an ecosystem built around effective polyglot protobuf. (And that could then flow up to tonic as well.)

LucioFranco commented 2 years ago

As of now support for anything major in prost will not likely happen inside the prost crate but might come as support externally https://github.com/tokio-rs/prost/pull/492#issuecomment-1036396499