blob: ca4015402e304f19a259dfa7ff776a79255048f4 [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 (
18 "android/soong/android"
19)
20
21var (
22 defaultProtobufFlags = []string{""}
23)
24
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050025type PluginType int
26
27const (
28 Protobuf PluginType = iota
29 Grpc
30)
31
Treehugger Robot588aae72020-08-21 10:01:58 +000032func init() {
33 android.RegisterModuleType("rust_protobuf", RustProtobufFactory)
34 android.RegisterModuleType("rust_protobuf_host", RustProtobufHostFactory)
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050035 android.RegisterModuleType("rust_grpcio", RustGrpcioFactory)
36 android.RegisterModuleType("rust_grpcio_host", RustGrpcioHostFactory)
Treehugger Robot588aae72020-08-21 10:01:58 +000037}
38
39var _ SourceProvider = (*protobufDecorator)(nil)
40
41type 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
49type protobufDecorator struct {
50 *BaseSourceProvider
51
52 Properties ProtobufProperties
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050053 plugin PluginType
Treehugger Robot588aae72020-08-21 10:01:58 +000054}
55
56func (proto *protobufDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path {
57 var protoFlags android.ProtoFlags
Matthew Maurer5819e582020-11-06 01:40:41 +000058 var pluginPaths android.Paths
Treehugger Robot588aae72020-08-21 10:01:58 +000059
60 protoFlags.OutTypeFlag = "--rust_out"
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050061 outDir := android.PathForModuleOut(ctx)
Treehugger Robot588aae72020-08-21 10:01:58 +000062
Matthew Maurer5819e582020-11-06 01:40:41 +000063 pluginPaths, protoFlags = proto.setupPlugin(ctx, protoFlags, outDir)
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050064
Treehugger Robot588aae72020-08-21 10:01:58 +000065 protoFlags.Flags = append(protoFlags.Flags, defaultProtobufFlags...)
66 protoFlags.Flags = append(protoFlags.Flags, proto.Properties.Proto_flags...)
67
Matthew Maurer5819e582020-11-06 01:40:41 +000068 protoFlags.Deps = append(protoFlags.Deps, pluginPaths...)
Treehugger Robot588aae72020-08-21 10:01:58 +000069
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 Hsiehc49649c2020-10-01 21:25:05 -070075 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 Robot588aae72020-08-21 10:01:58 +000083
84 rule := android.NewRuleBuilder()
85 android.ProtoRule(ctx, rule, protoFile.Path(), protoFlags, protoFlags.Deps, outDir, depFile, outputs)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -070086 rule.Command().Text("printf '// @generated\\npub mod %s;\\n' '" + stem + "' >").Output(modFile)
Treehugger Robot588aae72020-08-21 10:01:58 +000087 rule.Build(pctx, ctx, "protoc_"+protoFile.Path().Rel(), "protoc "+protoFile.Path().Rel())
88
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -070089 proto.BaseSourceProvider.OutputFiles = android.Paths{modFile, stemFile}
90 return modFile
Treehugger Robot588aae72020-08-21 10:01:58 +000091}
92
Matthew Maurer5819e582020-11-06 01:40:41 +000093func (proto *protobufDecorator) setupPlugin(ctx ModuleContext, protoFlags android.ProtoFlags, outDir android.ModuleOutPath) (android.Paths, android.ProtoFlags) {
94 pluginPaths := []android.Path{}
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050095
96 if proto.plugin == Protobuf {
Matthew Maurer5819e582020-11-06 01:40:41 +000097 pluginPath := ctx.Config().HostToolPath(ctx, "protoc-gen-rust")
98 pluginPaths = append(pluginPaths, pluginPath)
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050099 protoFlags.Flags = append(protoFlags.Flags, "--plugin="+pluginPath.String())
100 } else if proto.plugin == Grpc {
Matthew Maurer5819e582020-11-06 01:40:41 +0000101 grpcPath := ctx.Config().HostToolPath(ctx, "grpc_rust_plugin")
102 protobufPath := ctx.Config().HostToolPath(ctx, "protoc-gen-rust")
103 pluginPaths = append(pluginPaths, grpcPath, protobufPath)
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500104 protoFlags.Flags = append(protoFlags.Flags, "--grpc_out="+outDir.String())
Matthew Maurer5819e582020-11-06 01:40:41 +0000105 protoFlags.Flags = append(protoFlags.Flags, "--plugin=protoc-gen-grpc="+grpcPath.String())
106 protoFlags.Flags = append(protoFlags.Flags, "--plugin=protoc-gen-rust="+protobufPath.String())
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500107 } else {
108 ctx.ModuleErrorf("Unknown protobuf plugin type requested")
109 }
110
Matthew Maurer5819e582020-11-06 01:40:41 +0000111 return pluginPaths, protoFlags
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500112}
113
Treehugger Robot588aae72020-08-21 10:01:58 +0000114func (proto *protobufDecorator) SourceProviderProps() []interface{} {
115 return append(proto.BaseSourceProvider.SourceProviderProps(), &proto.Properties)
116}
117
118func (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.
128func 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.
134func RustProtobufHostFactory() android.Module {
135 module, _ := NewRustProtobuf(android.HostSupported)
136 return module.Init()
137}
138
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500139func 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.
145func RustGrpcioHostFactory() android.Module {
146 module, _ := NewRustGrpcio(android.HostSupported)
147 return module.Init()
148}
149
Treehugger Robot588aae72020-08-21 10:01:58 +0000150func NewRustProtobuf(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) {
151 protobuf := &protobufDecorator{
152 BaseSourceProvider: NewSourceProvider(),
153 Properties: ProtobufProperties{},
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500154 plugin: Protobuf,
155 }
156
157 module := NewSourceProviderModule(hod, protobuf, false)
158
159 return module, protobuf
160}
161
162func NewRustGrpcio(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) {
163 protobuf := &protobufDecorator{
164 BaseSourceProvider: NewSourceProvider(),
165 Properties: ProtobufProperties{},
166 plugin: Grpc,
Treehugger Robot588aae72020-08-21 10:01:58 +0000167 }
168
169 module := NewSourceProviderModule(hod, protobuf, false)
170
171 return module, protobuf
172}