blob: 235b4ad2b9aaa1e6c475fc9f6e010d089121da22 [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 {
Ivan Lozano57f434e2020-10-28 09:32:10 -040049 // List of realtive paths to proto files that will be used to generate the source
50 Protos []string `android:"path,arch_variant"`
Treehugger Robot588aae72020-08-21 10:01:58 +000051
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
Ivan Lozano57f434e2020-10-28 09:32:10 -040069 var protoNames []string
Treehugger Robot588aae72020-08-21 10:01:58 +000070
71 protoFlags.OutTypeFlag = "--rust_out"
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050072 outDir := android.PathForModuleOut(ctx)
Treehugger Robot588aae72020-08-21 10:01:58 +000073
Matthew Maurer5819e582020-11-06 01:40:41 +000074 pluginPaths, protoFlags = proto.setupPlugin(ctx, protoFlags, outDir)
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050075
Treehugger Robot588aae72020-08-21 10:01:58 +000076 protoFlags.Flags = append(protoFlags.Flags, defaultProtobufFlags...)
77 protoFlags.Flags = append(protoFlags.Flags, proto.Properties.Proto_flags...)
78
Matthew Maurer5819e582020-11-06 01:40:41 +000079 protoFlags.Deps = append(protoFlags.Deps, pluginPaths...)
Treehugger Robot588aae72020-08-21 10:01:58 +000080
Ivan Lozano57f434e2020-10-28 09:32:10 -040081 protoFiles := android.PathsForModuleSrc(ctx, proto.Properties.Protos)
Treehugger Robot588aae72020-08-21 10:01:58 +000082
Zach Johnson3df4e632020-11-06 11:56:27 -080083 // Add exported dependency include paths
84 for _, include := range deps.depIncludePaths {
85 protoFlags.Flags = append(protoFlags.Flags, "-I"+include.String())
86 }
87
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -070088 stem := proto.BaseSourceProvider.getStem(ctx)
Ivan Lozano57f434e2020-10-28 09:32:10 -040089
90 // The mod_stem.rs file is used to avoid collisions if this is not included as a crate.
91 stemFile := android.PathForModuleOut(ctx, "mod_"+stem+".rs")
92
93 // stemFile must be first here as the first path in BaseSourceProvider.OutputFiles is the library entry-point.
94 outputs := android.WritablePaths{stemFile}
Treehugger Robot588aae72020-08-21 10:01:58 +000095
96 rule := android.NewRuleBuilder()
Ivan Lozano57f434e2020-10-28 09:32:10 -040097 for _, protoFile := range protoFiles {
98 protoName := strings.TrimSuffix(protoFile.Base(), ".proto")
99 protoNames = append(protoNames, protoName)
Treehugger Robot588aae72020-08-21 10:01:58 +0000100
Ivan Lozano57f434e2020-10-28 09:32:10 -0400101 protoOut := android.PathForModuleOut(ctx, protoName+".rs")
102 ruleOutputs := android.WritablePaths{android.WritablePath(protoOut)}
103
104 if proto.plugin == Grpc {
105 grpcOut := android.PathForModuleOut(ctx, protoName+grpcSuffix+".rs")
106 ruleOutputs = append(ruleOutputs, android.WritablePath(grpcOut))
107 }
108
109 depFile := android.PathForModuleOut(ctx, protoName+".d")
110
111 android.ProtoRule(ctx, rule, protoFile, protoFlags, protoFlags.Deps, outDir, depFile, ruleOutputs)
112 outputs = append(outputs, ruleOutputs...)
113 }
114
115 rule.Command().
116 Implicits(outputs.Paths()).
117 Text("printf '" + proto.genModFileContents(ctx, protoNames) + "' >").
118 Output(stemFile)
119
120 rule.Build(pctx, ctx, "protoc_"+ctx.ModuleName(), "protoc "+ctx.ModuleName())
121
122 proto.BaseSourceProvider.OutputFiles = outputs.Paths()
123
124 // mod_stem.rs is the entry-point for our library modules, so this is what we return.
125 return stemFile
Treehugger Robot588aae72020-08-21 10:01:58 +0000126}
127
Ivan Lozano57f434e2020-10-28 09:32:10 -0400128func (proto *protobufDecorator) genModFileContents(ctx ModuleContext, protoNames []string) string {
Zach Johnson3df4e632020-11-06 11:56:27 -0800129 lines := []string{
Ivan Lozano57f434e2020-10-28 09:32:10 -0400130 "// @Soong generated Source",
131 }
132 for _, protoName := range protoNames {
133 lines = append(lines, fmt.Sprintf("pub mod %s;", protoName))
134
135 if proto.plugin == Grpc {
136 lines = append(lines, fmt.Sprintf("pub mod %s%s;", protoName, grpcSuffix))
137 }
Zach Johnson3df4e632020-11-06 11:56:27 -0800138 }
139
140 if proto.plugin == Grpc {
Zach Johnson3df4e632020-11-06 11:56:27 -0800141 lines = append(
142 lines,
143 "pub mod empty {",
144 " pub use protobuf::well_known_types::Empty;",
145 "}")
146 }
147
148 return strings.Join(lines, "\\n")
149}
150
Matthew Maurer5819e582020-11-06 01:40:41 +0000151func (proto *protobufDecorator) setupPlugin(ctx ModuleContext, protoFlags android.ProtoFlags, outDir android.ModuleOutPath) (android.Paths, android.ProtoFlags) {
152 pluginPaths := []android.Path{}
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500153
154 if proto.plugin == Protobuf {
Matthew Maurer5819e582020-11-06 01:40:41 +0000155 pluginPath := ctx.Config().HostToolPath(ctx, "protoc-gen-rust")
156 pluginPaths = append(pluginPaths, pluginPath)
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500157 protoFlags.Flags = append(protoFlags.Flags, "--plugin="+pluginPath.String())
158 } else if proto.plugin == Grpc {
Matthew Maurer5819e582020-11-06 01:40:41 +0000159 grpcPath := ctx.Config().HostToolPath(ctx, "grpc_rust_plugin")
160 protobufPath := ctx.Config().HostToolPath(ctx, "protoc-gen-rust")
161 pluginPaths = append(pluginPaths, grpcPath, protobufPath)
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500162 protoFlags.Flags = append(protoFlags.Flags, "--grpc_out="+outDir.String())
Matthew Maurer5819e582020-11-06 01:40:41 +0000163 protoFlags.Flags = append(protoFlags.Flags, "--plugin=protoc-gen-grpc="+grpcPath.String())
164 protoFlags.Flags = append(protoFlags.Flags, "--plugin=protoc-gen-rust="+protobufPath.String())
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500165 } else {
166 ctx.ModuleErrorf("Unknown protobuf plugin type requested")
167 }
168
Matthew Maurer5819e582020-11-06 01:40:41 +0000169 return pluginPaths, protoFlags
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500170}
171
Treehugger Robot588aae72020-08-21 10:01:58 +0000172func (proto *protobufDecorator) SourceProviderProps() []interface{} {
173 return append(proto.BaseSourceProvider.SourceProviderProps(), &proto.Properties)
174}
175
176func (proto *protobufDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps {
177 deps = proto.BaseSourceProvider.SourceProviderDeps(ctx, deps)
178 deps.Rustlibs = append(deps.Rustlibs, "libprotobuf")
Zach Johnson3df4e632020-11-06 11:56:27 -0800179 deps.HeaderLibs = append(deps.SharedLibs, proto.Properties.Header_libs...)
180
181 if proto.plugin == Grpc {
182 deps.Rustlibs = append(deps.Rustlibs, "libgrpcio", "libfutures")
183 deps.HeaderLibs = append(deps.HeaderLibs, "libprotobuf-cpp-full")
184 }
185
Treehugger Robot588aae72020-08-21 10:01:58 +0000186 return deps
187}
188
189// rust_protobuf generates protobuf rust code from the provided proto file. This uses the protoc-gen-rust plugin for
190// protoc. Additional flags to the protoc command can be passed via the proto_flags property. This module type will
191// create library variants that can be used as a crate dependency by adding it to the rlibs, dylibs, and rustlibs
192// properties of other modules.
193func RustProtobufFactory() android.Module {
194 module, _ := NewRustProtobuf(android.HostAndDeviceSupported)
195 return module.Init()
196}
197
198// A host-only variant of rust_protobuf. Refer to rust_protobuf for more details.
199func RustProtobufHostFactory() android.Module {
200 module, _ := NewRustProtobuf(android.HostSupported)
201 return module.Init()
202}
203
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500204func RustGrpcioFactory() android.Module {
205 module, _ := NewRustGrpcio(android.HostAndDeviceSupported)
206 return module.Init()
207}
208
209// A host-only variant of rust_protobuf. Refer to rust_protobuf for more details.
210func RustGrpcioHostFactory() android.Module {
211 module, _ := NewRustGrpcio(android.HostSupported)
212 return module.Init()
213}
214
Treehugger Robot588aae72020-08-21 10:01:58 +0000215func NewRustProtobuf(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) {
216 protobuf := &protobufDecorator{
217 BaseSourceProvider: NewSourceProvider(),
218 Properties: ProtobufProperties{},
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500219 plugin: Protobuf,
220 }
221
222 module := NewSourceProviderModule(hod, protobuf, false)
223
224 return module, protobuf
225}
226
227func NewRustGrpcio(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) {
228 protobuf := &protobufDecorator{
229 BaseSourceProvider: NewSourceProvider(),
230 Properties: ProtobufProperties{},
231 plugin: Grpc,
Treehugger Robot588aae72020-08-21 10:01:58 +0000232 }
233
234 module := NewSourceProviderModule(hod, protobuf, false)
235
236 return module, protobuf
237}