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 |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame^] | 58 | var pluginPath android.Path |
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 | |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame^] | 63 | pluginPath, protoFlags = proto.setupPlugin(ctx, protoFlags, outDir) |
| 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 | |
| 68 | protoFlags.Deps = append(protoFlags.Deps, pluginPath) |
| 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 | |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame^] | 93 | func (proto *protobufDecorator) setupPlugin(ctx ModuleContext, protoFlags android.ProtoFlags, outDir android.ModuleOutPath) (android.Path, android.ProtoFlags) { |
| 94 | var pluginPath android.Path |
| 95 | |
| 96 | if proto.plugin == Protobuf { |
| 97 | pluginPath = ctx.Config().HostToolPath(ctx, "protoc-gen-rust") |
| 98 | protoFlags.Flags = append(protoFlags.Flags, "--plugin="+pluginPath.String()) |
| 99 | } else if proto.plugin == Grpc { |
| 100 | pluginPath = ctx.Config().HostToolPath(ctx, "grpc_rust_plugin") |
| 101 | protoFlags.Flags = append(protoFlags.Flags, "--grpc_out="+outDir.String()) |
| 102 | protoFlags.Flags = append(protoFlags.Flags, "--plugin=protoc-gen-grpc="+pluginPath.String()) |
| 103 | } else { |
| 104 | ctx.ModuleErrorf("Unknown protobuf plugin type requested") |
| 105 | } |
| 106 | |
| 107 | return pluginPath, protoFlags |
| 108 | } |
| 109 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 110 | func (proto *protobufDecorator) SourceProviderProps() []interface{} { |
| 111 | return append(proto.BaseSourceProvider.SourceProviderProps(), &proto.Properties) |
| 112 | } |
| 113 | |
| 114 | func (proto *protobufDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps { |
| 115 | deps = proto.BaseSourceProvider.SourceProviderDeps(ctx, deps) |
| 116 | deps.Rustlibs = append(deps.Rustlibs, "libprotobuf") |
| 117 | return deps |
| 118 | } |
| 119 | |
| 120 | // rust_protobuf generates protobuf rust code from the provided proto file. This uses the protoc-gen-rust plugin for |
| 121 | // protoc. Additional flags to the protoc command can be passed via the proto_flags property. This module type will |
| 122 | // create library variants that can be used as a crate dependency by adding it to the rlibs, dylibs, and rustlibs |
| 123 | // properties of other modules. |
| 124 | func RustProtobufFactory() android.Module { |
| 125 | module, _ := NewRustProtobuf(android.HostAndDeviceSupported) |
| 126 | return module.Init() |
| 127 | } |
| 128 | |
| 129 | // A host-only variant of rust_protobuf. Refer to rust_protobuf for more details. |
| 130 | func RustProtobufHostFactory() android.Module { |
| 131 | module, _ := NewRustProtobuf(android.HostSupported) |
| 132 | return module.Init() |
| 133 | } |
| 134 | |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame^] | 135 | func RustGrpcioFactory() android.Module { |
| 136 | module, _ := NewRustGrpcio(android.HostAndDeviceSupported) |
| 137 | return module.Init() |
| 138 | } |
| 139 | |
| 140 | // A host-only variant of rust_protobuf. Refer to rust_protobuf for more details. |
| 141 | func RustGrpcioHostFactory() android.Module { |
| 142 | module, _ := NewRustGrpcio(android.HostSupported) |
| 143 | return module.Init() |
| 144 | } |
| 145 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 146 | func NewRustProtobuf(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) { |
| 147 | protobuf := &protobufDecorator{ |
| 148 | BaseSourceProvider: NewSourceProvider(), |
| 149 | Properties: ProtobufProperties{}, |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame^] | 150 | plugin: Protobuf, |
| 151 | } |
| 152 | |
| 153 | module := NewSourceProviderModule(hod, protobuf, false) |
| 154 | |
| 155 | return module, protobuf |
| 156 | } |
| 157 | |
| 158 | func NewRustGrpcio(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) { |
| 159 | protobuf := &protobufDecorator{ |
| 160 | BaseSourceProvider: NewSourceProvider(), |
| 161 | Properties: ProtobufProperties{}, |
| 162 | plugin: Grpc, |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | module := NewSourceProviderModule(hod, protobuf, false) |
| 166 | |
| 167 | return module, protobuf |
| 168 | } |