summaryrefslogtreecommitdiff
path: root/rust/protobuf.go
diff options
context:
space:
mode:
author Ivan Lozano <ivanlozano@google.com> 2020-11-02 15:06:26 -0500
committer Ivan Lozano <ivanlozano@google.com> 2020-11-05 08:53:52 -0500
commit6a3d1e9831c896addeaf452e3e4dd2e599c8d80c (patch)
tree68fc748ea5b874eda4c3d97f1b06a360a0938314 /rust/protobuf.go
parente83dea5aac2c307441af85c2ef5bc744bc015326 (diff)
Add rust_grpcio module type.
Adds a new SourceProvider type to generate grpcio code from protos. Since it's so similar to protobuf, it's basically just a different type of rust_protobuf. Bug: 171504899 Test: Example module compiles. Change-Id: I9882f3ac4d4aeaae0191f2b557e9612b5c7a9ac9
Diffstat (limited to 'rust/protobuf.go')
-rw-r--r--rust/protobuf.go58
1 files changed, 55 insertions, 3 deletions
diff --git a/rust/protobuf.go b/rust/protobuf.go
index ebb1c3cb6..28a4597f9 100644
--- a/rust/protobuf.go
+++ b/rust/protobuf.go
@@ -22,9 +22,18 @@ var (
defaultProtobufFlags = []string{""}
)
+type PluginType int
+
+const (
+ Protobuf PluginType = iota
+ Grpc
+)
+
func init() {
android.RegisterModuleType("rust_protobuf", RustProtobufFactory)
android.RegisterModuleType("rust_protobuf_host", RustProtobufHostFactory)
+ android.RegisterModuleType("rust_grpcio", RustGrpcioFactory)
+ android.RegisterModuleType("rust_grpcio_host", RustGrpcioHostFactory)
}
var _ SourceProvider = (*protobufDecorator)(nil)
@@ -41,15 +50,18 @@ type protobufDecorator struct {
*BaseSourceProvider
Properties ProtobufProperties
+ plugin PluginType
}
func (proto *protobufDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path {
var protoFlags android.ProtoFlags
- pluginPath := ctx.Config().HostToolPath(ctx, "protoc-gen-rust")
+ var pluginPath android.Path
protoFlags.OutTypeFlag = "--rust_out"
+ outDir := android.PathForModuleOut(ctx)
+
+ pluginPath, protoFlags = proto.setupPlugin(ctx, protoFlags, outDir)
- protoFlags.Flags = append(protoFlags.Flags, " --plugin="+pluginPath.String())
protoFlags.Flags = append(protoFlags.Flags, defaultProtobufFlags...)
protoFlags.Flags = append(protoFlags.Flags, proto.Properties.Proto_flags...)
@@ -60,7 +72,6 @@ func (proto *protobufDecorator) GenerateSource(ctx ModuleContext, deps PathDeps)
ctx.PropertyErrorf("proto", "invalid path to proto file")
}
- outDir := android.PathForModuleOut(ctx)
stem := proto.BaseSourceProvider.getStem(ctx)
// rust protobuf-codegen output <stem>.rs
stemFile := android.PathForModuleOut(ctx, stem+".rs")
@@ -79,6 +90,23 @@ func (proto *protobufDecorator) GenerateSource(ctx ModuleContext, deps PathDeps)
return modFile
}
+func (proto *protobufDecorator) setupPlugin(ctx ModuleContext, protoFlags android.ProtoFlags, outDir android.ModuleOutPath) (android.Path, android.ProtoFlags) {
+ var pluginPath android.Path
+
+ if proto.plugin == Protobuf {
+ pluginPath = ctx.Config().HostToolPath(ctx, "protoc-gen-rust")
+ protoFlags.Flags = append(protoFlags.Flags, "--plugin="+pluginPath.String())
+ } else if proto.plugin == Grpc {
+ pluginPath = ctx.Config().HostToolPath(ctx, "grpc_rust_plugin")
+ protoFlags.Flags = append(protoFlags.Flags, "--grpc_out="+outDir.String())
+ protoFlags.Flags = append(protoFlags.Flags, "--plugin=protoc-gen-grpc="+pluginPath.String())
+ } else {
+ ctx.ModuleErrorf("Unknown protobuf plugin type requested")
+ }
+
+ return pluginPath, protoFlags
+}
+
func (proto *protobufDecorator) SourceProviderProps() []interface{} {
return append(proto.BaseSourceProvider.SourceProviderProps(), &proto.Properties)
}
@@ -104,10 +132,34 @@ func RustProtobufHostFactory() android.Module {
return module.Init()
}
+func RustGrpcioFactory() android.Module {
+ module, _ := NewRustGrpcio(android.HostAndDeviceSupported)
+ return module.Init()
+}
+
+// A host-only variant of rust_protobuf. Refer to rust_protobuf for more details.
+func RustGrpcioHostFactory() android.Module {
+ module, _ := NewRustGrpcio(android.HostSupported)
+ return module.Init()
+}
+
func NewRustProtobuf(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) {
protobuf := &protobufDecorator{
BaseSourceProvider: NewSourceProvider(),
Properties: ProtobufProperties{},
+ plugin: Protobuf,
+ }
+
+ module := NewSourceProviderModule(hod, protobuf, false)
+
+ return module, protobuf
+}
+
+func NewRustGrpcio(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) {
+ protobuf := &protobufDecorator{
+ BaseSourceProvider: NewSourceProvider(),
+ Properties: ProtobufProperties{},
+ plugin: Grpc,
}
module := NewSourceProviderModule(hod, protobuf, false)