blob: 28a4597f9db39193ccf914c9ffbd8710eb4f3d27 [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
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050058 var pluginPath android.Path
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
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050063 pluginPath, protoFlags = proto.setupPlugin(ctx, protoFlags, outDir)
64
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
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 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
Ivan Lozano6a3d1e92020-11-02 15:06:26 -050093func (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 Robot588aae72020-08-21 10:01:58 +0000110func (proto *protobufDecorator) SourceProviderProps() []interface{} {
111 return append(proto.BaseSourceProvider.SourceProviderProps(), &proto.Properties)
112}
113
114func (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.
124func 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.
130func RustProtobufHostFactory() android.Module {
131 module, _ := NewRustProtobuf(android.HostSupported)
132 return module.Init()
133}
134
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500135func 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.
141func RustGrpcioHostFactory() android.Module {
142 module, _ := NewRustGrpcio(android.HostSupported)
143 return module.Init()
144}
145
Treehugger Robot588aae72020-08-21 10:01:58 +0000146func NewRustProtobuf(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) {
147 protobuf := &protobufDecorator{
148 BaseSourceProvider: NewSourceProvider(),
149 Properties: ProtobufProperties{},
Ivan Lozano6a3d1e92020-11-02 15:06:26 -0500150 plugin: Protobuf,
151 }
152
153 module := NewSourceProviderModule(hod, protobuf, false)
154
155 return module, protobuf
156}
157
158func NewRustGrpcio(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) {
159 protobuf := &protobufDecorator{
160 BaseSourceProvider: NewSourceProvider(),
161 Properties: ProtobufProperties{},
162 plugin: Grpc,
Treehugger Robot588aae72020-08-21 10:01:58 +0000163 }
164
165 module := NewSourceProviderModule(hod, protobuf, false)
166
167 return module, protobuf
168}