blob: 7d6e1fd5ca547f668b95bb0b8b66fc313130af5d [file] [log] [blame]
Treehugger Robot588aae72020-08-21 10:01:58 +00001// Copyright 2020 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rust
16
17import (
Zach Johnson3df4e632020-11-06 11:56:27 -080018 "fmt"
19 "strings"
20
Treehugger Robot588aae72020-08-21 10:01:58 +000021 "android/soong/android"
22)
23
24var (
25 defaultProtobufFlags = []string{""}
26)
27
Zach Johnson3df4e632020-11-06 11:56:27 -080028const (
29 grpcSuffix = "_grpc"
30)
31
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050032type PluginType int
33
34const (
35 Protobuf PluginType = iota
36 Grpc
37)
38
Treehugger Robot588aae72020-08-21 10:01:58 +000039func init() {
40 android.RegisterModuleType("rust_protobuf", RustProtobufFactory)
41 android.RegisterModuleType("rust_protobuf_host", RustProtobufHostFactory)
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050042 android.RegisterModuleType("rust_grpcio", RustGrpcioFactory)
43 android.RegisterModuleType("rust_grpcio_host", RustGrpcioHostFactory)
Treehugger Robot588aae72020-08-21 10:01:58 +000044}
45
46var _ SourceProvider = (*protobufDecorator)(nil)
47
48type ProtobufProperties struct {
49 // Path to the proto file that will be used to generate the source
50 Proto *string `android:"path,arch_variant"`
51
52 // List of additional flags to pass to aprotoc
53 Proto_flags []string `android:"arch_variant"`
Zach Johnson3df4e632020-11-06 11:56:27 -080054
55 // List of libraries which export include paths required for this module
Ivan Lozano9b443832020-11-17 13:39:30 -050056 Header_libs []string `android:"arch_variant,variant_prepend"`
Treehugger Robot588aae72020-08-21 10:01:58 +000057}
58
59type protobufDecorator struct {
60 *BaseSourceProvider
61
62 Properties ProtobufProperties
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050063 plugin PluginType
Treehugger Robot588aae72020-08-21 10:01:58 +000064}
65
66func (proto *protobufDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path {
67 var protoFlags android.ProtoFlags
Matthew Maurer5819e582020-11-06 01:40:41 +000068 var pluginPaths android.Paths
Treehugger Robot588aae72020-08-21 10:01:58 +000069
70 protoFlags.OutTypeFlag = "--rust_out"
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050071 outDir := android.PathForModuleOut(ctx)
Treehugger Robot588aae72020-08-21 10:01:58 +000072
Matthew Maurer5819e582020-11-06 01:40:41 +000073 pluginPaths, protoFlags = proto.setupPlugin(ctx, protoFlags, outDir)
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050074
Treehugger Robot588aae72020-08-21 10:01:58 +000075 protoFlags.Flags = append(protoFlags.Flags, defaultProtobufFlags...)
76 protoFlags.Flags = append(protoFlags.Flags, proto.Properties.Proto_flags...)
77
Matthew Maurer5819e582020-11-06 01:40:41 +000078 protoFlags.Deps = append(protoFlags.Deps, pluginPaths...)
Treehugger Robot588aae72020-08-21 10:01:58 +000079
80 protoFile := android.OptionalPathForModuleSrc(ctx, proto.Properties.Proto)
81 if !protoFile.Valid() {
82 ctx.PropertyErrorf("proto", "invalid path to proto file")
83 }
84
Zach Johnson3df4e632020-11-06 11:56:27 -080085 // Add exported dependency include paths
86 for _, include := range deps.depIncludePaths {
87 protoFlags.Flags = append(protoFlags.Flags, "-I"+include.String())
88 }
89
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -070090 stem := proto.BaseSourceProvider.getStem(ctx)
91 // rust protobuf-codegen output <stem>.rs
92 stemFile := android.PathForModuleOut(ctx, stem+".rs")
93 // add mod_<stem>.rs to import <stem>.rs
94 modFile := android.PathForModuleOut(ctx, "mod_"+stem+".rs")
95 // mod_<stem>.rs is the main/first output file to be included/compiled
96 outputs := android.WritablePaths{modFile, stemFile}
Zach Johnson3df4e632020-11-06 11:56:27 -080097 if proto.plugin == Grpc {
98 outputs = append(outputs, android.PathForModuleOut(ctx, stem+grpcSuffix+".rs"))
99 }
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700100 depFile := android.PathForModuleOut(ctx, "mod_"+stem+".d")
Treehugger Robot588aae72020-08-21 10:01:58 +0000101
102 rule := android.NewRuleBuilder()
103 android.ProtoRule(ctx, rule, protoFile.Path(), protoFlags, protoFlags.Deps, outDir, depFile, outputs)
Zach Johnson3df4e632020-11-06 11:56:27 -0800104 rule.Command().Text("printf '" + proto.getModFileContents(ctx) + "' >").Output(modFile)
Treehugger Robot588aae72020-08-21 10:01:58 +0000105 rule.Build(pctx, ctx, "protoc_"+protoFile.Path().Rel(), "protoc "+protoFile.Path().Rel())
106
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700107 proto.BaseSourceProvider.OutputFiles = android.Paths{modFile, stemFile}
108 return modFile
Treehugger Robot588aae72020-08-21 10:01:58 +0000109}
110
Zach Johnson3df4e632020-11-06 11:56:27 -0800111func (proto *protobufDecorator) getModFileContents(ctx ModuleContext) string {
112 stem := proto.BaseSourceProvider.getStem(ctx)
113 lines := []string{
114 "// @generated",
115 fmt.Sprintf("pub mod %s;", stem),
116 }
117
118 if proto.plugin == Grpc {
119 lines = append(lines, fmt.Sprintf("pub mod %s%s;", stem, grpcSuffix))
120 lines = append(
121 lines,
122 "pub mod empty {",
123 " pub use protobuf::well_known_types::Empty;",
124 "}")
125 }
126
127 return strings.Join(lines, "\\n")
128}
129
Matthew Maurer5819e582020-11-06 01:40:41 +0000130func (proto *protobufDecorator) setupPlugin(ctx ModuleContext, protoFlags android.ProtoFlags, outDir android.ModuleOutPath) (android.Paths, android.ProtoFlags) {
131 pluginPaths := []android.Path{}
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500132
133 if proto.plugin == Protobuf {
Matthew Maurer5819e582020-11-06 01:40:41 +0000134 pluginPath := ctx.Config().HostToolPath(ctx, "protoc-gen-rust")
135 pluginPaths = append(pluginPaths, pluginPath)
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500136 protoFlags.Flags = append(protoFlags.Flags, "--plugin="+pluginPath.String())
137 } else if proto.plugin == Grpc {
Matthew Maurer5819e582020-11-06 01:40:41 +0000138 grpcPath := ctx.Config().HostToolPath(ctx, "grpc_rust_plugin")
139 protobufPath := ctx.Config().HostToolPath(ctx, "protoc-gen-rust")
140 pluginPaths = append(pluginPaths, grpcPath, protobufPath)
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500141 protoFlags.Flags = append(protoFlags.Flags, "--grpc_out="+outDir.String())
Matthew Maurer5819e582020-11-06 01:40:41 +0000142 protoFlags.Flags = append(protoFlags.Flags, "--plugin=protoc-gen-grpc="+grpcPath.String())
143 protoFlags.Flags = append(protoFlags.Flags, "--plugin=protoc-gen-rust="+protobufPath.String())
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500144 } else {
145 ctx.ModuleErrorf("Unknown protobuf plugin type requested")
146 }
147
Matthew Maurer5819e582020-11-06 01:40:41 +0000148 return pluginPaths, protoFlags
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500149}
150
Treehugger Robot588aae72020-08-21 10:01:58 +0000151func (proto *protobufDecorator) SourceProviderProps() []interface{} {
152 return append(proto.BaseSourceProvider.SourceProviderProps(), &proto.Properties)
153}
154
155func (proto *protobufDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps {
156 deps = proto.BaseSourceProvider.SourceProviderDeps(ctx, deps)
157 deps.Rustlibs = append(deps.Rustlibs, "libprotobuf")
Zach Johnson3df4e632020-11-06 11:56:27 -0800158 deps.HeaderLibs = append(deps.SharedLibs, proto.Properties.Header_libs...)
159
160 if proto.plugin == Grpc {
161 deps.Rustlibs = append(deps.Rustlibs, "libgrpcio", "libfutures")
162 deps.HeaderLibs = append(deps.HeaderLibs, "libprotobuf-cpp-full")
163 }
164
Treehugger Robot588aae72020-08-21 10:01:58 +0000165 return deps
166}
167
168// rust_protobuf generates protobuf rust code from the provided proto file. This uses the protoc-gen-rust plugin for
169// protoc. Additional flags to the protoc command can be passed via the proto_flags property. This module type will
170// create library variants that can be used as a crate dependency by adding it to the rlibs, dylibs, and rustlibs
171// properties of other modules.
172func RustProtobufFactory() android.Module {
173 module, _ := NewRustProtobuf(android.HostAndDeviceSupported)
174 return module.Init()
175}
176
177// A host-only variant of rust_protobuf. Refer to rust_protobuf for more details.
178func RustProtobufHostFactory() android.Module {
179 module, _ := NewRustProtobuf(android.HostSupported)
180 return module.Init()
181}
182
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500183func RustGrpcioFactory() android.Module {
184 module, _ := NewRustGrpcio(android.HostAndDeviceSupported)
185 return module.Init()
186}
187
188// A host-only variant of rust_protobuf. Refer to rust_protobuf for more details.
189func RustGrpcioHostFactory() android.Module {
190 module, _ := NewRustGrpcio(android.HostSupported)
191 return module.Init()
192}
193
Treehugger Robot588aae72020-08-21 10:01:58 +0000194func NewRustProtobuf(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) {
195 protobuf := &protobufDecorator{
196 BaseSourceProvider: NewSourceProvider(),
197 Properties: ProtobufProperties{},
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500198 plugin: Protobuf,
199 }
200
201 module := NewSourceProviderModule(hod, protobuf, false)
202
203 return module, protobuf
204}
205
206func NewRustGrpcio(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) {
207 protobuf := &protobufDecorator{
208 BaseSourceProvider: NewSourceProvider(),
209 Properties: ProtobufProperties{},
210 plugin: Grpc,
Treehugger Robot588aae72020-08-21 10:01:58 +0000211 }
212
213 module := NewSourceProviderModule(hod, protobuf, false)
214
215 return module, protobuf
216}