blob: 897300f195ca4a4e28d5d26de90c7c48bc40288a [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
25func init() {
26 android.RegisterModuleType("rust_protobuf", RustProtobufFactory)
27 android.RegisterModuleType("rust_protobuf_host", RustProtobufHostFactory)
28}
29
30var _ SourceProvider = (*protobufDecorator)(nil)
31
32type 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
40type protobufDecorator struct {
41 *BaseSourceProvider
42
43 Properties ProtobufProperties
44}
45
46func (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)
64 depFile := android.PathForModuleOut(ctx, proto.BaseSourceProvider.getStem(ctx)+".d")
65 outputs := android.WritablePaths{android.PathForModuleOut(ctx, proto.BaseSourceProvider.getStem(ctx)+".rs")}
66
67 rule := android.NewRuleBuilder()
68 android.ProtoRule(ctx, rule, protoFile.Path(), protoFlags, protoFlags.Deps, outDir, depFile, outputs)
69 rule.Build(pctx, ctx, "protoc_"+protoFile.Path().Rel(), "protoc "+protoFile.Path().Rel())
70
71 proto.BaseSourceProvider.OutputFile = outputs[0]
72 return outputs[0]
73}
74
75func (proto *protobufDecorator) SourceProviderProps() []interface{} {
76 return append(proto.BaseSourceProvider.SourceProviderProps(), &proto.Properties)
77}
78
79func (proto *protobufDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps {
80 deps = proto.BaseSourceProvider.SourceProviderDeps(ctx, deps)
81 deps.Rustlibs = append(deps.Rustlibs, "libprotobuf")
82 return deps
83}
84
85// rust_protobuf generates protobuf rust code from the provided proto file. This uses the protoc-gen-rust plugin for
86// protoc. Additional flags to the protoc command can be passed via the proto_flags property. This module type will
87// create library variants that can be used as a crate dependency by adding it to the rlibs, dylibs, and rustlibs
88// properties of other modules.
89func RustProtobufFactory() android.Module {
90 module, _ := NewRustProtobuf(android.HostAndDeviceSupported)
91 return module.Init()
92}
93
94// A host-only variant of rust_protobuf. Refer to rust_protobuf for more details.
95func RustProtobufHostFactory() android.Module {
96 module, _ := NewRustProtobuf(android.HostSupported)
97 return module.Init()
98}
99
100func NewRustProtobuf(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) {
101 protobuf := &protobufDecorator{
102 BaseSourceProvider: NewSourceProvider(),
103 Properties: ProtobufProperties{},
104 }
105
106 module := NewSourceProviderModule(hod, protobuf, false)
107
108 return module, protobuf
109}