Inspect protobuf plugins

Created at: 2020-07-02 Index

Notes

Plugins are executable programs

Protoc find a plugin from command line arguments, for example

% protoc -I src --go_out=dest/

The above command line has an argument --go_out, protoc get it and guess that the word "go" is a plugin, then it search through the PATH environment variable to find an executable named "protoc-gen-go", if the executable program exists then it is considered a plugin to compile .proto files into golang source codes.

Feed plugin via stdin

Once a plugin is found, protoc spawn a process of the plugin, parse input .proto files into descriptors and send them to plugin process via stdin.

Input and Output data are also protobuf messages

Protoc gives the plugin process a chunk of data of the protobuf type CodeGeneratorRequest, which has details of parsed .proto file and all kinds of descriptors including the AST, and then expect a response of the protobuf type CodeGeneratorResponse.

Both message types are defined in the url pluin.proto.

Plugins add no limit on language choices.

Since plugins are normal executable programs, it's possible to write code generators of languages using another language. Some languange's protobuf stubs can be build using simpler bootstrap languages such as Python or any of the favoriate languages that already support protobuf.

References

There are example projects using Python, a typical one is protoc-docs-plugin.