protobuf.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. package rust
  15. import (
  16. "fmt"
  17. "strings"
  18. "android/soong/android"
  19. )
  20. var (
  21. defaultProtobufFlags = []string{""}
  22. )
  23. const (
  24. grpcSuffix = "_grpc"
  25. )
  26. type PluginType int
  27. func init() {
  28. android.RegisterModuleType("rust_protobuf", RustProtobufFactory)
  29. android.RegisterModuleType("rust_protobuf_host", RustProtobufHostFactory)
  30. }
  31. var _ SourceProvider = (*protobufDecorator)(nil)
  32. type ProtobufProperties struct {
  33. // List of relative paths to proto files that will be used to generate the source.
  34. // Either this or grpc_protos must be defined.
  35. Protos []string `android:"path,arch_variant"`
  36. // List of relative paths to GRPC-containing proto files that will be used to generate the source.
  37. // Either this or protos must be defined.
  38. Grpc_protos []string `android:"path,arch_variant"`
  39. // List of additional flags to pass to aprotoc
  40. Proto_flags []string `android:"arch_variant"`
  41. // List of libraries which export include paths required for this module
  42. Header_libs []string `android:"arch_variant,variant_prepend"`
  43. // Use protobuf version 3.x. This will be deleted once we migrate all current users
  44. // of protobuf off of 2.x.
  45. Use_protobuf3 *bool
  46. }
  47. type protobufDecorator struct {
  48. *BaseSourceProvider
  49. Properties ProtobufProperties
  50. protoNames []string
  51. grpcNames []string
  52. grpcProtoFlags android.ProtoFlags
  53. protoFlags android.ProtoFlags
  54. }
  55. func (proto *protobufDecorator) useProtobuf3() bool {
  56. return Bool(proto.Properties.Use_protobuf3)
  57. }
  58. func (proto *protobufDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path {
  59. var protoFlags android.ProtoFlags
  60. var grpcProtoFlags android.ProtoFlags
  61. var commonProtoFlags []string
  62. outDir := android.PathForModuleOut(ctx)
  63. protoFiles := android.PathsForModuleSrc(ctx, proto.Properties.Protos)
  64. grpcFiles := android.PathsForModuleSrc(ctx, proto.Properties.Grpc_protos)
  65. // For now protobuf2 (the deprecated version) remains the default. This will change in the
  66. // future as we update the various users.
  67. protoPluginPath := ctx.Config().HostToolPath(ctx, "protoc-gen-rust-deprecated")
  68. if proto.useProtobuf3() == true {
  69. protoPluginPath = ctx.Config().HostToolPath(ctx, "protoc-gen-rust")
  70. }
  71. commonProtoFlags = append(commonProtoFlags, defaultProtobufFlags...)
  72. commonProtoFlags = append(commonProtoFlags, proto.Properties.Proto_flags...)
  73. commonProtoFlags = append(commonProtoFlags, "--plugin=protoc-gen-rust="+protoPluginPath.String())
  74. if len(protoFiles) > 0 {
  75. protoFlags.OutTypeFlag = "--rust_out"
  76. protoFlags.Flags = append(protoFlags.Flags, commonProtoFlags...)
  77. protoFlags.Deps = append(protoFlags.Deps, protoPluginPath)
  78. }
  79. if len(grpcFiles) > 0 {
  80. grpcPath := ctx.Config().HostToolPath(ctx, "grpc_rust_plugin")
  81. grpcProtoFlags.OutTypeFlag = "--rust_out"
  82. grpcProtoFlags.Flags = append(grpcProtoFlags.Flags, "--grpc_out="+outDir.String())
  83. grpcProtoFlags.Flags = append(grpcProtoFlags.Flags, "--plugin=protoc-gen-grpc="+grpcPath.String())
  84. grpcProtoFlags.Flags = append(grpcProtoFlags.Flags, commonProtoFlags...)
  85. grpcProtoFlags.Deps = append(grpcProtoFlags.Deps, grpcPath, protoPluginPath)
  86. }
  87. if len(protoFiles) == 0 && len(grpcFiles) == 0 {
  88. ctx.PropertyErrorf("protos",
  89. "at least one protobuf must be defined in either protos or grpc_protos.")
  90. }
  91. // Add exported dependency include paths
  92. for _, include := range deps.depIncludePaths {
  93. protoFlags.Flags = append(protoFlags.Flags, "-I"+include.String())
  94. grpcProtoFlags.Flags = append(grpcProtoFlags.Flags, "-I"+include.String())
  95. }
  96. stem := proto.BaseSourceProvider.getStem(ctx)
  97. // The mod_stem.rs file is used to avoid collisions if this is not included as a crate.
  98. stemFile := android.PathForModuleOut(ctx, "mod_"+stem+".rs")
  99. // stemFile must be first here as the first path in BaseSourceProvider.OutputFiles is the library entry-point.
  100. var outputs android.WritablePaths
  101. rule := android.NewRuleBuilder(pctx, ctx)
  102. for _, protoFile := range protoFiles {
  103. // Since we're iterating over the protoFiles already, make sure they're not redeclared in grpcFiles
  104. if android.InList(protoFile.String(), grpcFiles.Strings()) {
  105. ctx.PropertyErrorf("protos",
  106. "A proto can only be added once to either grpc_protos or protos. %q is declared in both properties",
  107. protoFile.String())
  108. }
  109. protoName := strings.TrimSuffix(protoFile.Base(), ".proto")
  110. proto.protoNames = append(proto.protoNames, protoName)
  111. protoOut := android.PathForModuleOut(ctx, protoName+".rs")
  112. depFile := android.PathForModuleOut(ctx, protoName+".d")
  113. ruleOutputs := android.WritablePaths{protoOut, depFile}
  114. android.ProtoRule(rule, protoFile, protoFlags, protoFlags.Deps, outDir, depFile, ruleOutputs)
  115. outputs = append(outputs, ruleOutputs...)
  116. }
  117. for _, grpcFile := range grpcFiles {
  118. grpcName := strings.TrimSuffix(grpcFile.Base(), ".proto")
  119. proto.grpcNames = append(proto.grpcNames, grpcName)
  120. // GRPC protos produce two files, a proto.rs and a proto_grpc.rs
  121. protoOut := android.WritablePath(android.PathForModuleOut(ctx, grpcName+".rs"))
  122. grpcOut := android.WritablePath(android.PathForModuleOut(ctx, grpcName+grpcSuffix+".rs"))
  123. depFile := android.PathForModuleOut(ctx, grpcName+".d")
  124. ruleOutputs := android.WritablePaths{protoOut, grpcOut, depFile}
  125. android.ProtoRule(rule, grpcFile, grpcProtoFlags, grpcProtoFlags.Deps, outDir, depFile, ruleOutputs)
  126. outputs = append(outputs, ruleOutputs...)
  127. }
  128. // Check that all proto base filenames are unique as outputs are written to the same directory.
  129. baseFilenames := append(proto.protoNames, proto.grpcNames...)
  130. if len(baseFilenames) != len(android.FirstUniqueStrings(baseFilenames)) {
  131. ctx.PropertyErrorf("protos", "proto filenames must be unique across 'protos' and 'grpc_protos' "+
  132. "to be used in the same rust_protobuf module. For example, foo.proto and src/foo.proto will conflict.")
  133. }
  134. android.WriteFileRule(ctx, stemFile, proto.genModFileContents())
  135. rule.Build("protoc_"+ctx.ModuleName(), "protoc "+ctx.ModuleName())
  136. // stemFile must be first here as the first path in BaseSourceProvider.OutputFiles is the library entry-point.
  137. proto.BaseSourceProvider.OutputFiles = append(android.Paths{stemFile}, outputs.Paths()...)
  138. // mod_stem.rs is the entry-point for our library modules, so this is what we return.
  139. return stemFile
  140. }
  141. func (proto *protobufDecorator) genModFileContents() string {
  142. lines := []string{
  143. "// @Soong generated Source",
  144. }
  145. for _, protoName := range proto.protoNames {
  146. lines = append(lines, fmt.Sprintf("pub mod %s;", protoName))
  147. }
  148. for _, grpcName := range proto.grpcNames {
  149. lines = append(lines, fmt.Sprintf("pub mod %s;", grpcName))
  150. lines = append(lines, fmt.Sprintf("pub mod %s%s;", grpcName, grpcSuffix))
  151. }
  152. if len(proto.grpcNames) > 0 {
  153. lines = append(
  154. lines,
  155. "pub mod empty {",
  156. " pub use protobuf::well_known_types::Empty;",
  157. "}",
  158. "pub mod wrappers {",
  159. " pub use protobuf::well_known_types::{",
  160. " DoubleValue, FloatValue, Int64Value, UInt64Value, Int32Value, UInt32Value,",
  161. " BoolValue, StringValue, BytesValue",
  162. " };",
  163. "}")
  164. }
  165. return strings.Join(lines, "\n")
  166. }
  167. func (proto *protobufDecorator) SourceProviderProps() []interface{} {
  168. return append(proto.BaseSourceProvider.SourceProviderProps(), &proto.Properties)
  169. }
  170. func (proto *protobufDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps {
  171. deps = proto.BaseSourceProvider.SourceProviderDeps(ctx, deps)
  172. useProtobuf3 := proto.useProtobuf3()
  173. if useProtobuf3 == true {
  174. deps.Rustlibs = append(deps.Rustlibs, "libprotobuf")
  175. } else {
  176. deps.Rustlibs = append(deps.Rustlibs, "libprotobuf_deprecated")
  177. }
  178. deps.HeaderLibs = append(deps.SharedLibs, proto.Properties.Header_libs...)
  179. if len(proto.Properties.Grpc_protos) > 0 {
  180. if useProtobuf3 == true {
  181. ctx.PropertyErrorf("protos", "rust_protobuf with grpc_protos defined must currently use "+
  182. "`use_protobuf3: false,` in the Android.bp file. This is temporary until the "+
  183. "grpcio crate is updated to use the current version of the protobuf crate.")
  184. }
  185. deps.Rustlibs = append(deps.Rustlibs, "libgrpcio", "libfutures")
  186. deps.HeaderLibs = append(deps.HeaderLibs, "libprotobuf-cpp-full")
  187. }
  188. return deps
  189. }
  190. // rust_protobuf generates protobuf rust code from the provided proto file. This uses the protoc-gen-rust plugin for
  191. // protoc. Additional flags to the protoc command can be passed via the proto_flags property. This module type will
  192. // create library variants that can be used as a crate dependency by adding it to the rlibs, dylibs, and rustlibs
  193. // properties of other modules.
  194. func RustProtobufFactory() android.Module {
  195. module, _ := NewRustProtobuf(android.HostAndDeviceSupported)
  196. return module.Init()
  197. }
  198. // A host-only variant of rust_protobuf. Refer to rust_protobuf for more details.
  199. func RustProtobufHostFactory() android.Module {
  200. module, _ := NewRustProtobuf(android.HostSupported)
  201. return module.Init()
  202. }
  203. func NewRustProtobuf(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) {
  204. protobuf := &protobufDecorator{
  205. BaseSourceProvider: NewSourceProvider(),
  206. Properties: ProtobufProperties{},
  207. }
  208. module := NewSourceProviderModule(hod, protobuf, false, false)
  209. return module, protobuf
  210. }