2020-08-21 12:01:58 +02:00
|
|
|
// Copyright 2020 The Android Open Source Project
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package rust
|
|
|
|
|
|
|
|
import (
|
2020-11-06 20:56:27 +01:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-08-21 12:01:58 +02:00
|
|
|
"android/soong/android"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultProtobufFlags = []string{""}
|
|
|
|
)
|
|
|
|
|
2020-11-06 20:56:27 +01:00
|
|
|
const (
|
|
|
|
grpcSuffix = "_grpc"
|
|
|
|
)
|
|
|
|
|
2020-11-02 21:06:26 +01:00
|
|
|
type PluginType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Protobuf PluginType = iota
|
|
|
|
Grpc
|
|
|
|
)
|
|
|
|
|
2020-08-21 12:01:58 +02:00
|
|
|
func init() {
|
|
|
|
android.RegisterModuleType("rust_protobuf", RustProtobufFactory)
|
|
|
|
android.RegisterModuleType("rust_protobuf_host", RustProtobufHostFactory)
|
2020-11-02 21:06:26 +01:00
|
|
|
android.RegisterModuleType("rust_grpcio", RustGrpcioFactory)
|
|
|
|
android.RegisterModuleType("rust_grpcio_host", RustGrpcioHostFactory)
|
2020-08-21 12:01:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ SourceProvider = (*protobufDecorator)(nil)
|
|
|
|
|
|
|
|
type ProtobufProperties struct {
|
2020-12-01 15:25:22 +01:00
|
|
|
// List of relative paths to proto files that will be used to generate the source
|
2020-10-28 14:32:10 +01:00
|
|
|
Protos []string `android:"path,arch_variant"`
|
2020-08-21 12:01:58 +02:00
|
|
|
|
|
|
|
// List of additional flags to pass to aprotoc
|
|
|
|
Proto_flags []string `android:"arch_variant"`
|
2020-11-06 20:56:27 +01:00
|
|
|
|
|
|
|
// List of libraries which export include paths required for this module
|
2020-11-17 19:39:30 +01:00
|
|
|
Header_libs []string `android:"arch_variant,variant_prepend"`
|
2020-08-21 12:01:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type protobufDecorator struct {
|
|
|
|
*BaseSourceProvider
|
|
|
|
|
|
|
|
Properties ProtobufProperties
|
2020-11-02 21:06:26 +01:00
|
|
|
plugin PluginType
|
2020-08-21 12:01:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (proto *protobufDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path {
|
|
|
|
var protoFlags android.ProtoFlags
|
2020-11-06 02:40:41 +01:00
|
|
|
var pluginPaths android.Paths
|
2020-10-28 14:32:10 +01:00
|
|
|
var protoNames []string
|
2020-08-21 12:01:58 +02:00
|
|
|
|
|
|
|
protoFlags.OutTypeFlag = "--rust_out"
|
2020-11-02 21:06:26 +01:00
|
|
|
outDir := android.PathForModuleOut(ctx)
|
|
|
|
|
2020-11-06 02:40:41 +01:00
|
|
|
pluginPaths, protoFlags = proto.setupPlugin(ctx, protoFlags, outDir)
|
2020-08-21 12:01:58 +02:00
|
|
|
|
|
|
|
protoFlags.Flags = append(protoFlags.Flags, defaultProtobufFlags...)
|
|
|
|
protoFlags.Flags = append(protoFlags.Flags, proto.Properties.Proto_flags...)
|
|
|
|
|
2020-11-06 02:40:41 +01:00
|
|
|
protoFlags.Deps = append(protoFlags.Deps, pluginPaths...)
|
2020-08-21 12:01:58 +02:00
|
|
|
|
2020-10-28 14:32:10 +01:00
|
|
|
protoFiles := android.PathsForModuleSrc(ctx, proto.Properties.Protos)
|
2020-08-21 12:01:58 +02:00
|
|
|
|
2020-12-01 15:25:22 +01:00
|
|
|
if len(protoFiles) == 0 {
|
|
|
|
ctx.PropertyErrorf("protos", "at least one protobuf must be defined.")
|
|
|
|
}
|
|
|
|
|
2020-11-06 20:56:27 +01:00
|
|
|
// Add exported dependency include paths
|
|
|
|
for _, include := range deps.depIncludePaths {
|
|
|
|
protoFlags.Flags = append(protoFlags.Flags, "-I"+include.String())
|
|
|
|
}
|
|
|
|
|
2020-10-02 06:25:05 +02:00
|
|
|
stem := proto.BaseSourceProvider.getStem(ctx)
|
2020-10-28 14:32:10 +01:00
|
|
|
|
|
|
|
// The mod_stem.rs file is used to avoid collisions if this is not included as a crate.
|
|
|
|
stemFile := android.PathForModuleOut(ctx, "mod_"+stem+".rs")
|
|
|
|
|
|
|
|
// stemFile must be first here as the first path in BaseSourceProvider.OutputFiles is the library entry-point.
|
2020-12-01 15:25:22 +01:00
|
|
|
var outputs android.WritablePaths
|
2020-08-21 12:01:58 +02:00
|
|
|
|
2020-11-17 02:32:30 +01:00
|
|
|
rule := android.NewRuleBuilder(pctx, ctx)
|
2020-10-28 14:32:10 +01:00
|
|
|
for _, protoFile := range protoFiles {
|
|
|
|
protoName := strings.TrimSuffix(protoFile.Base(), ".proto")
|
|
|
|
protoNames = append(protoNames, protoName)
|
|
|
|
|
|
|
|
protoOut := android.PathForModuleOut(ctx, protoName+".rs")
|
|
|
|
ruleOutputs := android.WritablePaths{android.WritablePath(protoOut)}
|
|
|
|
|
|
|
|
if proto.plugin == Grpc {
|
|
|
|
grpcOut := android.PathForModuleOut(ctx, protoName+grpcSuffix+".rs")
|
|
|
|
ruleOutputs = append(ruleOutputs, android.WritablePath(grpcOut))
|
|
|
|
}
|
|
|
|
|
|
|
|
depFile := android.PathForModuleOut(ctx, protoName+".d")
|
2020-08-21 12:01:58 +02:00
|
|
|
|
2020-11-17 02:32:30 +01:00
|
|
|
android.ProtoRule(rule, protoFile, protoFlags, protoFlags.Deps, outDir, depFile, ruleOutputs)
|
2020-10-28 14:32:10 +01:00
|
|
|
outputs = append(outputs, ruleOutputs...)
|
|
|
|
}
|
|
|
|
|
2020-12-01 15:25:22 +01:00
|
|
|
android.WriteFileRule(ctx, stemFile, proto.genModFileContents(ctx, protoNames))
|
2020-10-28 14:32:10 +01:00
|
|
|
|
2020-11-17 02:32:30 +01:00
|
|
|
rule.Build("protoc_"+ctx.ModuleName(), "protoc "+ctx.ModuleName())
|
2020-10-28 14:32:10 +01:00
|
|
|
|
2020-12-01 15:25:22 +01:00
|
|
|
// stemFile must be first here as the first path in BaseSourceProvider.OutputFiles is the library entry-point.
|
|
|
|
proto.BaseSourceProvider.OutputFiles = append(android.Paths{stemFile}, outputs.Paths()...)
|
2020-10-28 14:32:10 +01:00
|
|
|
|
|
|
|
// mod_stem.rs is the entry-point for our library modules, so this is what we return.
|
|
|
|
return stemFile
|
2020-08-21 12:01:58 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 14:32:10 +01:00
|
|
|
func (proto *protobufDecorator) genModFileContents(ctx ModuleContext, protoNames []string) string {
|
2020-11-06 20:56:27 +01:00
|
|
|
lines := []string{
|
2020-10-28 14:32:10 +01:00
|
|
|
"// @Soong generated Source",
|
|
|
|
}
|
|
|
|
for _, protoName := range protoNames {
|
|
|
|
lines = append(lines, fmt.Sprintf("pub mod %s;", protoName))
|
|
|
|
|
|
|
|
if proto.plugin == Grpc {
|
|
|
|
lines = append(lines, fmt.Sprintf("pub mod %s%s;", protoName, grpcSuffix))
|
|
|
|
}
|
2020-11-06 20:56:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if proto.plugin == Grpc {
|
|
|
|
lines = append(
|
|
|
|
lines,
|
|
|
|
"pub mod empty {",
|
|
|
|
" pub use protobuf::well_known_types::Empty;",
|
|
|
|
"}")
|
|
|
|
}
|
|
|
|
|
2020-12-01 15:25:22 +01:00
|
|
|
return strings.Join(lines, "\n")
|
2020-11-06 20:56:27 +01:00
|
|
|
}
|
|
|
|
|
2020-11-06 02:40:41 +01:00
|
|
|
func (proto *protobufDecorator) setupPlugin(ctx ModuleContext, protoFlags android.ProtoFlags, outDir android.ModuleOutPath) (android.Paths, android.ProtoFlags) {
|
|
|
|
pluginPaths := []android.Path{}
|
2020-11-02 21:06:26 +01:00
|
|
|
|
|
|
|
if proto.plugin == Protobuf {
|
2020-11-06 02:40:41 +01:00
|
|
|
pluginPath := ctx.Config().HostToolPath(ctx, "protoc-gen-rust")
|
|
|
|
pluginPaths = append(pluginPaths, pluginPath)
|
2020-11-02 21:06:26 +01:00
|
|
|
protoFlags.Flags = append(protoFlags.Flags, "--plugin="+pluginPath.String())
|
|
|
|
} else if proto.plugin == Grpc {
|
2020-11-06 02:40:41 +01:00
|
|
|
grpcPath := ctx.Config().HostToolPath(ctx, "grpc_rust_plugin")
|
|
|
|
protobufPath := ctx.Config().HostToolPath(ctx, "protoc-gen-rust")
|
|
|
|
pluginPaths = append(pluginPaths, grpcPath, protobufPath)
|
2020-11-02 21:06:26 +01:00
|
|
|
protoFlags.Flags = append(protoFlags.Flags, "--grpc_out="+outDir.String())
|
2020-11-06 02:40:41 +01:00
|
|
|
protoFlags.Flags = append(protoFlags.Flags, "--plugin=protoc-gen-grpc="+grpcPath.String())
|
|
|
|
protoFlags.Flags = append(protoFlags.Flags, "--plugin=protoc-gen-rust="+protobufPath.String())
|
2020-11-02 21:06:26 +01:00
|
|
|
} else {
|
|
|
|
ctx.ModuleErrorf("Unknown protobuf plugin type requested")
|
|
|
|
}
|
|
|
|
|
2020-11-06 02:40:41 +01:00
|
|
|
return pluginPaths, protoFlags
|
2020-11-02 21:06:26 +01:00
|
|
|
}
|
|
|
|
|
2020-08-21 12:01:58 +02:00
|
|
|
func (proto *protobufDecorator) SourceProviderProps() []interface{} {
|
|
|
|
return append(proto.BaseSourceProvider.SourceProviderProps(), &proto.Properties)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (proto *protobufDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps {
|
|
|
|
deps = proto.BaseSourceProvider.SourceProviderDeps(ctx, deps)
|
|
|
|
deps.Rustlibs = append(deps.Rustlibs, "libprotobuf")
|
2020-11-06 20:56:27 +01:00
|
|
|
deps.HeaderLibs = append(deps.SharedLibs, proto.Properties.Header_libs...)
|
|
|
|
|
|
|
|
if proto.plugin == Grpc {
|
|
|
|
deps.Rustlibs = append(deps.Rustlibs, "libgrpcio", "libfutures")
|
|
|
|
deps.HeaderLibs = append(deps.HeaderLibs, "libprotobuf-cpp-full")
|
|
|
|
}
|
|
|
|
|
2020-08-21 12:01:58 +02:00
|
|
|
return deps
|
|
|
|
}
|
|
|
|
|
|
|
|
// rust_protobuf generates protobuf rust code from the provided proto file. This uses the protoc-gen-rust plugin for
|
|
|
|
// protoc. Additional flags to the protoc command can be passed via the proto_flags property. This module type will
|
|
|
|
// create library variants that can be used as a crate dependency by adding it to the rlibs, dylibs, and rustlibs
|
|
|
|
// properties of other modules.
|
|
|
|
func RustProtobufFactory() android.Module {
|
|
|
|
module, _ := NewRustProtobuf(android.HostAndDeviceSupported)
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
// A host-only variant of rust_protobuf. Refer to rust_protobuf for more details.
|
|
|
|
func RustProtobufHostFactory() android.Module {
|
|
|
|
module, _ := NewRustProtobuf(android.HostSupported)
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
2020-11-02 21:06:26 +01:00
|
|
|
func RustGrpcioFactory() android.Module {
|
|
|
|
module, _ := NewRustGrpcio(android.HostAndDeviceSupported)
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
// A host-only variant of rust_protobuf. Refer to rust_protobuf for more details.
|
|
|
|
func RustGrpcioHostFactory() android.Module {
|
|
|
|
module, _ := NewRustGrpcio(android.HostSupported)
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
2020-08-21 12:01:58 +02:00
|
|
|
func NewRustProtobuf(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) {
|
|
|
|
protobuf := &protobufDecorator{
|
|
|
|
BaseSourceProvider: NewSourceProvider(),
|
|
|
|
Properties: ProtobufProperties{},
|
2020-11-02 21:06:26 +01:00
|
|
|
plugin: Protobuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
module := NewSourceProviderModule(hod, protobuf, false)
|
|
|
|
|
|
|
|
return module, protobuf
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRustGrpcio(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) {
|
|
|
|
protobuf := &protobufDecorator{
|
|
|
|
BaseSourceProvider: NewSourceProvider(),
|
|
|
|
Properties: ProtobufProperties{},
|
|
|
|
plugin: Grpc,
|
2020-08-21 12:01:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module := NewSourceProviderModule(hod, protobuf, false)
|
|
|
|
|
|
|
|
return module, protobuf
|
|
|
|
}
|