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 | |
| 25 | func init() { |
| 26 | android.RegisterModuleType("rust_protobuf", RustProtobufFactory) |
| 27 | android.RegisterModuleType("rust_protobuf_host", RustProtobufHostFactory) |
| 28 | } |
| 29 | |
| 30 | var _ SourceProvider = (*protobufDecorator)(nil) |
| 31 | |
| 32 | type ProtobufProperties struct { |
| 33 | // Path to the proto file that will be used to generate the source |
| 34 | Proto *string `android:"path,arch_variant"` |
| 35 | |
| 36 | // List of additional flags to pass to aprotoc |
| 37 | Proto_flags []string `android:"arch_variant"` |
| 38 | } |
| 39 | |
| 40 | type protobufDecorator struct { |
| 41 | *BaseSourceProvider |
| 42 | |
| 43 | Properties ProtobufProperties |
| 44 | } |
| 45 | |
| 46 | func (proto *protobufDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path { |
| 47 | var protoFlags android.ProtoFlags |
| 48 | pluginPath := ctx.Config().HostToolPath(ctx, "protoc-gen-rust") |
| 49 | |
| 50 | protoFlags.OutTypeFlag = "--rust_out" |
| 51 | |
| 52 | protoFlags.Flags = append(protoFlags.Flags, " --plugin="+pluginPath.String()) |
| 53 | protoFlags.Flags = append(protoFlags.Flags, defaultProtobufFlags...) |
| 54 | protoFlags.Flags = append(protoFlags.Flags, proto.Properties.Proto_flags...) |
| 55 | |
| 56 | protoFlags.Deps = append(protoFlags.Deps, pluginPath) |
| 57 | |
| 58 | protoFile := android.OptionalPathForModuleSrc(ctx, proto.Properties.Proto) |
| 59 | if !protoFile.Valid() { |
| 60 | ctx.PropertyErrorf("proto", "invalid path to proto file") |
| 61 | } |
| 62 | |
| 63 | outDir := android.PathForModuleOut(ctx) |
Chih-Hung Hsieh | c49649c | 2020-10-01 21:25:05 -0700 | [diff] [blame] | 64 | stem := proto.BaseSourceProvider.getStem(ctx) |
| 65 | // rust protobuf-codegen output <stem>.rs |
| 66 | stemFile := android.PathForModuleOut(ctx, stem+".rs") |
| 67 | // add mod_<stem>.rs to import <stem>.rs |
| 68 | modFile := android.PathForModuleOut(ctx, "mod_"+stem+".rs") |
| 69 | // mod_<stem>.rs is the main/first output file to be included/compiled |
| 70 | outputs := android.WritablePaths{modFile, stemFile} |
| 71 | depFile := android.PathForModuleOut(ctx, "mod_"+stem+".d") |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 72 | |
| 73 | rule := android.NewRuleBuilder() |
| 74 | 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] | 75 | rule.Command().Text("printf '// @generated\\npub mod %s;\\n' '" + stem + "' >").Output(modFile) |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 76 | rule.Build(pctx, ctx, "protoc_"+protoFile.Path().Rel(), "protoc "+protoFile.Path().Rel()) |
| 77 | |
Chih-Hung Hsieh | c49649c | 2020-10-01 21:25:05 -0700 | [diff] [blame] | 78 | proto.BaseSourceProvider.OutputFiles = android.Paths{modFile, stemFile} |
| 79 | return modFile |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | func (proto *protobufDecorator) SourceProviderProps() []interface{} { |
| 83 | return append(proto.BaseSourceProvider.SourceProviderProps(), &proto.Properties) |
| 84 | } |
| 85 | |
| 86 | func (proto *protobufDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps { |
| 87 | deps = proto.BaseSourceProvider.SourceProviderDeps(ctx, deps) |
| 88 | deps.Rustlibs = append(deps.Rustlibs, "libprotobuf") |
| 89 | return deps |
| 90 | } |
| 91 | |
| 92 | // rust_protobuf generates protobuf rust code from the provided proto file. This uses the protoc-gen-rust plugin for |
| 93 | // protoc. Additional flags to the protoc command can be passed via the proto_flags property. This module type will |
| 94 | // create library variants that can be used as a crate dependency by adding it to the rlibs, dylibs, and rustlibs |
| 95 | // properties of other modules. |
| 96 | func RustProtobufFactory() android.Module { |
| 97 | module, _ := NewRustProtobuf(android.HostAndDeviceSupported) |
| 98 | return module.Init() |
| 99 | } |
| 100 | |
| 101 | // A host-only variant of rust_protobuf. Refer to rust_protobuf for more details. |
| 102 | func RustProtobufHostFactory() android.Module { |
| 103 | module, _ := NewRustProtobuf(android.HostSupported) |
| 104 | return module.Init() |
| 105 | } |
| 106 | |
| 107 | func NewRustProtobuf(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) { |
| 108 | protobuf := &protobufDecorator{ |
| 109 | BaseSourceProvider: NewSourceProvider(), |
| 110 | Properties: ProtobufProperties{}, |
| 111 | } |
| 112 | |
| 113 | module := NewSourceProviderModule(hod, protobuf, false) |
| 114 | |
| 115 | return module, protobuf |
| 116 | } |