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 ( |
| 18 | "android/soong/android" |
| 19 | ) |
| 20 | |
| 21 | var ( |
| 22 | defaultProtobufFlags = []string{""} |
| 23 | ) |
| 24 | |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 25 | type PluginType int |
| 26 | |
| 27 | const ( |
| 28 | Protobuf PluginType = iota |
| 29 | Grpc |
| 30 | ) |
| 31 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 32 | func init() { |
| 33 | android.RegisterModuleType("rust_protobuf", RustProtobufFactory) |
| 34 | android.RegisterModuleType("rust_protobuf_host", RustProtobufHostFactory) |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 35 | android.RegisterModuleType("rust_grpcio", RustGrpcioFactory) |
| 36 | android.RegisterModuleType("rust_grpcio_host", RustGrpcioHostFactory) |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | var _ SourceProvider = (*protobufDecorator)(nil) |
| 40 | |
| 41 | type ProtobufProperties struct { |
| 42 | // Path to the proto file that will be used to generate the source |
| 43 | Proto *string `android:"path,arch_variant"` |
| 44 | |
| 45 | // List of additional flags to pass to aprotoc |
| 46 | Proto_flags []string `android:"arch_variant"` |
| 47 | } |
| 48 | |
| 49 | type protobufDecorator struct { |
| 50 | *BaseSourceProvider |
| 51 | |
| 52 | Properties ProtobufProperties |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 53 | plugin PluginType |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | func (proto *protobufDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path { |
| 57 | var protoFlags android.ProtoFlags |
Matthew Maurer | 5819e58 | 2020-11-06 01:40:41 +0000 | [diff] [blame] | 58 | var pluginPaths android.Paths |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 59 | |
| 60 | protoFlags.OutTypeFlag = "--rust_out" |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 61 | outDir := android.PathForModuleOut(ctx) |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 62 | |
Matthew Maurer | 5819e58 | 2020-11-06 01:40:41 +0000 | [diff] [blame] | 63 | pluginPaths, protoFlags = proto.setupPlugin(ctx, protoFlags, outDir) |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 64 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 65 | protoFlags.Flags = append(protoFlags.Flags, defaultProtobufFlags...) |
| 66 | protoFlags.Flags = append(protoFlags.Flags, proto.Properties.Proto_flags...) |
| 67 | |
Matthew Maurer | 5819e58 | 2020-11-06 01:40:41 +0000 | [diff] [blame] | 68 | protoFlags.Deps = append(protoFlags.Deps, pluginPaths...) |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 69 | |
| 70 | protoFile := android.OptionalPathForModuleSrc(ctx, proto.Properties.Proto) |
| 71 | if !protoFile.Valid() { |
| 72 | ctx.PropertyErrorf("proto", "invalid path to proto file") |
| 73 | } |
| 74 | |
Chih-Hung Hsieh | c49649c | 2020-10-01 21:25:05 -0700 | [diff] [blame] | 75 | stem := proto.BaseSourceProvider.getStem(ctx) |
| 76 | // rust protobuf-codegen output <stem>.rs |
| 77 | stemFile := android.PathForModuleOut(ctx, stem+".rs") |
| 78 | // add mod_<stem>.rs to import <stem>.rs |
| 79 | modFile := android.PathForModuleOut(ctx, "mod_"+stem+".rs") |
| 80 | // mod_<stem>.rs is the main/first output file to be included/compiled |
| 81 | outputs := android.WritablePaths{modFile, stemFile} |
| 82 | depFile := android.PathForModuleOut(ctx, "mod_"+stem+".d") |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 83 | |
| 84 | rule := android.NewRuleBuilder() |
| 85 | android.ProtoRule(ctx, rule, protoFile.Path(), protoFlags, protoFlags.Deps, outDir, depFile, outputs) |
Chih-Hung Hsieh | c49649c | 2020-10-01 21:25:05 -0700 | [diff] [blame] | 86 | rule.Command().Text("printf '// @generated\\npub mod %s;\\n' '" + stem + "' >").Output(modFile) |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 87 | rule.Build(pctx, ctx, "protoc_"+protoFile.Path().Rel(), "protoc "+protoFile.Path().Rel()) |
| 88 | |
Chih-Hung Hsieh | c49649c | 2020-10-01 21:25:05 -0700 | [diff] [blame] | 89 | proto.BaseSourceProvider.OutputFiles = android.Paths{modFile, stemFile} |
| 90 | return modFile |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Matthew Maurer | 5819e58 | 2020-11-06 01:40:41 +0000 | [diff] [blame] | 93 | func (proto *protobufDecorator) setupPlugin(ctx ModuleContext, protoFlags android.ProtoFlags, outDir android.ModuleOutPath) (android.Paths, android.ProtoFlags) { |
| 94 | pluginPaths := []android.Path{} |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 95 | |
| 96 | if proto.plugin == Protobuf { |
Matthew Maurer | 5819e58 | 2020-11-06 01:40:41 +0000 | [diff] [blame] | 97 | pluginPath := ctx.Config().HostToolPath(ctx, "protoc-gen-rust") |
| 98 | pluginPaths = append(pluginPaths, pluginPath) |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 99 | protoFlags.Flags = append(protoFlags.Flags, "--plugin="+pluginPath.String()) |
| 100 | } else if proto.plugin == Grpc { |
Matthew Maurer | 5819e58 | 2020-11-06 01:40:41 +0000 | [diff] [blame] | 101 | grpcPath := ctx.Config().HostToolPath(ctx, "grpc_rust_plugin") |
| 102 | protobufPath := ctx.Config().HostToolPath(ctx, "protoc-gen-rust") |
| 103 | pluginPaths = append(pluginPaths, grpcPath, protobufPath) |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 104 | protoFlags.Flags = append(protoFlags.Flags, "--grpc_out="+outDir.String()) |
Matthew Maurer | 5819e58 | 2020-11-06 01:40:41 +0000 | [diff] [blame] | 105 | protoFlags.Flags = append(protoFlags.Flags, "--plugin=protoc-gen-grpc="+grpcPath.String()) |
| 106 | protoFlags.Flags = append(protoFlags.Flags, "--plugin=protoc-gen-rust="+protobufPath.String()) |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 107 | } else { |
| 108 | ctx.ModuleErrorf("Unknown protobuf plugin type requested") |
| 109 | } |
| 110 | |
Matthew Maurer | 5819e58 | 2020-11-06 01:40:41 +0000 | [diff] [blame] | 111 | return pluginPaths, protoFlags |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 114 | func (proto *protobufDecorator) SourceProviderProps() []interface{} { |
| 115 | return append(proto.BaseSourceProvider.SourceProviderProps(), &proto.Properties) |
| 116 | } |
| 117 | |
| 118 | func (proto *protobufDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps { |
| 119 | deps = proto.BaseSourceProvider.SourceProviderDeps(ctx, deps) |
| 120 | deps.Rustlibs = append(deps.Rustlibs, "libprotobuf") |
| 121 | return deps |
| 122 | } |
| 123 | |
| 124 | // rust_protobuf generates protobuf rust code from the provided proto file. This uses the protoc-gen-rust plugin for |
| 125 | // protoc. Additional flags to the protoc command can be passed via the proto_flags property. This module type will |
| 126 | // create library variants that can be used as a crate dependency by adding it to the rlibs, dylibs, and rustlibs |
| 127 | // properties of other modules. |
| 128 | func RustProtobufFactory() android.Module { |
| 129 | module, _ := NewRustProtobuf(android.HostAndDeviceSupported) |
| 130 | return module.Init() |
| 131 | } |
| 132 | |
| 133 | // A host-only variant of rust_protobuf. Refer to rust_protobuf for more details. |
| 134 | func RustProtobufHostFactory() android.Module { |
| 135 | module, _ := NewRustProtobuf(android.HostSupported) |
| 136 | return module.Init() |
| 137 | } |
| 138 | |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 139 | func RustGrpcioFactory() android.Module { |
| 140 | module, _ := NewRustGrpcio(android.HostAndDeviceSupported) |
| 141 | return module.Init() |
| 142 | } |
| 143 | |
| 144 | // A host-only variant of rust_protobuf. Refer to rust_protobuf for more details. |
| 145 | func RustGrpcioHostFactory() android.Module { |
| 146 | module, _ := NewRustGrpcio(android.HostSupported) |
| 147 | return module.Init() |
| 148 | } |
| 149 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 150 | func NewRustProtobuf(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) { |
| 151 | protobuf := &protobufDecorator{ |
| 152 | BaseSourceProvider: NewSourceProvider(), |
| 153 | Properties: ProtobufProperties{}, |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 154 | plugin: Protobuf, |
| 155 | } |
| 156 | |
| 157 | module := NewSourceProviderModule(hod, protobuf, false) |
| 158 | |
| 159 | return module, protobuf |
| 160 | } |
| 161 | |
| 162 | func NewRustGrpcio(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) { |
| 163 | protobuf := &protobufDecorator{ |
| 164 | BaseSourceProvider: NewSourceProvider(), |
| 165 | Properties: ProtobufProperties{}, |
| 166 | plugin: Grpc, |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | module := NewSourceProviderModule(hod, protobuf, false) |
| 170 | |
| 171 | return module, protobuf |
| 172 | } |