Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 18 | "fmt" |
| 19 | "strings" |
| 20 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 21 | "android/soong/android" |
| 22 | ) |
| 23 | |
| 24 | var ( |
| 25 | defaultProtobufFlags = []string{""} |
| 26 | ) |
| 27 | |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 28 | const ( |
| 29 | grpcSuffix = "_grpc" |
| 30 | ) |
| 31 | |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 32 | type PluginType int |
| 33 | |
| 34 | const ( |
| 35 | Protobuf PluginType = iota |
| 36 | Grpc |
| 37 | ) |
| 38 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 39 | func init() { |
| 40 | android.RegisterModuleType("rust_protobuf", RustProtobufFactory) |
| 41 | android.RegisterModuleType("rust_protobuf_host", RustProtobufHostFactory) |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 42 | android.RegisterModuleType("rust_grpcio", RustGrpcioFactory) |
| 43 | android.RegisterModuleType("rust_grpcio_host", RustGrpcioHostFactory) |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | var _ SourceProvider = (*protobufDecorator)(nil) |
| 47 | |
| 48 | type 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 Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 54 | |
| 55 | // List of libraries which export include paths required for this module |
Ivan Lozano | 9b44383 | 2020-11-17 13:39:30 -0500 | [diff] [blame^] | 56 | Header_libs []string `android:"arch_variant,variant_prepend"` |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | type protobufDecorator struct { |
| 60 | *BaseSourceProvider |
| 61 | |
| 62 | Properties ProtobufProperties |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 63 | plugin PluginType |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | func (proto *protobufDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path { |
| 67 | var protoFlags android.ProtoFlags |
Matthew Maurer | 5819e58 | 2020-11-06 01:40:41 +0000 | [diff] [blame] | 68 | var pluginPaths android.Paths |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 69 | |
| 70 | protoFlags.OutTypeFlag = "--rust_out" |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 71 | outDir := android.PathForModuleOut(ctx) |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 72 | |
Matthew Maurer | 5819e58 | 2020-11-06 01:40:41 +0000 | [diff] [blame] | 73 | pluginPaths, protoFlags = proto.setupPlugin(ctx, protoFlags, outDir) |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 74 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 75 | protoFlags.Flags = append(protoFlags.Flags, defaultProtobufFlags...) |
| 76 | protoFlags.Flags = append(protoFlags.Flags, proto.Properties.Proto_flags...) |
| 77 | |
Matthew Maurer | 5819e58 | 2020-11-06 01:40:41 +0000 | [diff] [blame] | 78 | protoFlags.Deps = append(protoFlags.Deps, pluginPaths...) |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 79 | |
| 80 | protoFile := android.OptionalPathForModuleSrc(ctx, proto.Properties.Proto) |
| 81 | if !protoFile.Valid() { |
| 82 | ctx.PropertyErrorf("proto", "invalid path to proto file") |
| 83 | } |
| 84 | |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 85 | // Add exported dependency include paths |
| 86 | for _, include := range deps.depIncludePaths { |
| 87 | protoFlags.Flags = append(protoFlags.Flags, "-I"+include.String()) |
| 88 | } |
| 89 | |
Chih-Hung Hsieh | c49649c | 2020-10-01 21:25:05 -0700 | [diff] [blame] | 90 | 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 Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 97 | if proto.plugin == Grpc { |
| 98 | outputs = append(outputs, android.PathForModuleOut(ctx, stem+grpcSuffix+".rs")) |
| 99 | } |
Chih-Hung Hsieh | c49649c | 2020-10-01 21:25:05 -0700 | [diff] [blame] | 100 | depFile := android.PathForModuleOut(ctx, "mod_"+stem+".d") |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 101 | |
| 102 | rule := android.NewRuleBuilder() |
| 103 | android.ProtoRule(ctx, rule, protoFile.Path(), protoFlags, protoFlags.Deps, outDir, depFile, outputs) |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 104 | rule.Command().Text("printf '" + proto.getModFileContents(ctx) + "' >").Output(modFile) |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 105 | rule.Build(pctx, ctx, "protoc_"+protoFile.Path().Rel(), "protoc "+protoFile.Path().Rel()) |
| 106 | |
Chih-Hung Hsieh | c49649c | 2020-10-01 21:25:05 -0700 | [diff] [blame] | 107 | proto.BaseSourceProvider.OutputFiles = android.Paths{modFile, stemFile} |
| 108 | return modFile |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 111 | func (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 Maurer | 5819e58 | 2020-11-06 01:40:41 +0000 | [diff] [blame] | 130 | func (proto *protobufDecorator) setupPlugin(ctx ModuleContext, protoFlags android.ProtoFlags, outDir android.ModuleOutPath) (android.Paths, android.ProtoFlags) { |
| 131 | pluginPaths := []android.Path{} |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 132 | |
| 133 | if proto.plugin == Protobuf { |
Matthew Maurer | 5819e58 | 2020-11-06 01:40:41 +0000 | [diff] [blame] | 134 | pluginPath := ctx.Config().HostToolPath(ctx, "protoc-gen-rust") |
| 135 | pluginPaths = append(pluginPaths, pluginPath) |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 136 | protoFlags.Flags = append(protoFlags.Flags, "--plugin="+pluginPath.String()) |
| 137 | } else if proto.plugin == Grpc { |
Matthew Maurer | 5819e58 | 2020-11-06 01:40:41 +0000 | [diff] [blame] | 138 | grpcPath := ctx.Config().HostToolPath(ctx, "grpc_rust_plugin") |
| 139 | protobufPath := ctx.Config().HostToolPath(ctx, "protoc-gen-rust") |
| 140 | pluginPaths = append(pluginPaths, grpcPath, protobufPath) |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 141 | protoFlags.Flags = append(protoFlags.Flags, "--grpc_out="+outDir.String()) |
Matthew Maurer | 5819e58 | 2020-11-06 01:40:41 +0000 | [diff] [blame] | 142 | protoFlags.Flags = append(protoFlags.Flags, "--plugin=protoc-gen-grpc="+grpcPath.String()) |
| 143 | protoFlags.Flags = append(protoFlags.Flags, "--plugin=protoc-gen-rust="+protobufPath.String()) |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 144 | } else { |
| 145 | ctx.ModuleErrorf("Unknown protobuf plugin type requested") |
| 146 | } |
| 147 | |
Matthew Maurer | 5819e58 | 2020-11-06 01:40:41 +0000 | [diff] [blame] | 148 | return pluginPaths, protoFlags |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 149 | } |
| 150 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 151 | func (proto *protobufDecorator) SourceProviderProps() []interface{} { |
| 152 | return append(proto.BaseSourceProvider.SourceProviderProps(), &proto.Properties) |
| 153 | } |
| 154 | |
| 155 | func (proto *protobufDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps { |
| 156 | deps = proto.BaseSourceProvider.SourceProviderDeps(ctx, deps) |
| 157 | deps.Rustlibs = append(deps.Rustlibs, "libprotobuf") |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 158 | 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 Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 165 | 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. |
| 172 | func 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. |
| 178 | func RustProtobufHostFactory() android.Module { |
| 179 | module, _ := NewRustProtobuf(android.HostSupported) |
| 180 | return module.Init() |
| 181 | } |
| 182 | |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 183 | func 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. |
| 189 | func RustGrpcioHostFactory() android.Module { |
| 190 | module, _ := NewRustGrpcio(android.HostSupported) |
| 191 | return module.Init() |
| 192 | } |
| 193 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 194 | func NewRustProtobuf(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) { |
| 195 | protobuf := &protobufDecorator{ |
| 196 | BaseSourceProvider: NewSourceProvider(), |
| 197 | Properties: ProtobufProperties{}, |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 198 | plugin: Protobuf, |
| 199 | } |
| 200 | |
| 201 | module := NewSourceProviderModule(hod, protobuf, false) |
| 202 | |
| 203 | return module, protobuf |
| 204 | } |
| 205 | |
| 206 | func NewRustGrpcio(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) { |
| 207 | protobuf := &protobufDecorator{ |
| 208 | BaseSourceProvider: NewSourceProvider(), |
| 209 | Properties: ProtobufProperties{}, |
| 210 | plugin: Grpc, |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | module := NewSourceProviderModule(hod, protobuf, false) |
| 214 | |
| 215 | return module, protobuf |
| 216 | } |