proto.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2017 Google Inc. All rights reserved.
  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 java
  15. import (
  16. "path/filepath"
  17. "strconv"
  18. "android/soong/android"
  19. "android/soong/bazel"
  20. "github.com/google/blueprint/proptools"
  21. )
  22. const (
  23. protoTypeDefault = "lite"
  24. )
  25. func genProto(ctx android.ModuleContext, protoFiles android.Paths, flags android.ProtoFlags) android.Paths {
  26. // Shard proto files into groups of 100 to avoid having to recompile all of them if one changes and to avoid
  27. // hitting command line length limits.
  28. shards := android.ShardPaths(protoFiles, 50)
  29. srcJarFiles := make(android.Paths, 0, len(shards))
  30. for i, shard := range shards {
  31. srcJarFile := android.PathForModuleGen(ctx, "proto", "proto"+strconv.Itoa(i)+".srcjar")
  32. srcJarFiles = append(srcJarFiles, srcJarFile)
  33. outDir := srcJarFile.ReplaceExtension(ctx, "tmp")
  34. rule := android.NewRuleBuilder(pctx, ctx)
  35. rule.Command().Text("rm -rf").Flag(outDir.String())
  36. rule.Command().Text("mkdir -p").Flag(outDir.String())
  37. for _, protoFile := range shard {
  38. depFile := srcJarFile.InSameDir(ctx, protoFile.String()+".d")
  39. rule.Command().Text("mkdir -p").Flag(filepath.Dir(depFile.String()))
  40. android.ProtoRule(rule, protoFile, flags, flags.Deps, outDir, depFile, nil)
  41. }
  42. // Proto generated java files have an unknown package name in the path, so package the entire output directory
  43. // into a srcjar.
  44. rule.Command().
  45. BuiltTool("soong_zip").
  46. Flag("-srcjar").
  47. Flag("-write_if_changed").
  48. FlagWithOutput("-o ", srcJarFile).
  49. FlagWithArg("-C ", outDir.String()).
  50. FlagWithArg("-D ", outDir.String())
  51. rule.Command().Text("rm -rf").Flag(outDir.String())
  52. rule.Restat()
  53. ruleName := "protoc"
  54. ruleDesc := "protoc"
  55. if len(shards) > 1 {
  56. ruleName += "_" + strconv.Itoa(i)
  57. ruleDesc += " " + strconv.Itoa(i)
  58. }
  59. rule.Build(ruleName, ruleDesc)
  60. }
  61. return srcJarFiles
  62. }
  63. func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) {
  64. const unspecifiedProtobufPluginType = ""
  65. if String(p.Proto.Plugin) == "" {
  66. switch String(p.Proto.Type) {
  67. case "stream": // does not require additional dependencies
  68. case "micro":
  69. ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-micro")
  70. case "nano":
  71. ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-nano")
  72. case "lite", unspecifiedProtobufPluginType:
  73. ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-lite")
  74. case "full":
  75. if ctx.Host() {
  76. ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-full")
  77. } else {
  78. ctx.PropertyErrorf("proto.type", "full java protos only supported on the host")
  79. }
  80. default:
  81. ctx.PropertyErrorf("proto.type", "unknown proto type %q",
  82. String(p.Proto.Type))
  83. }
  84. }
  85. }
  86. func protoFlags(ctx android.ModuleContext, j *CommonProperties, p *android.ProtoProperties,
  87. flags javaBuilderFlags) javaBuilderFlags {
  88. flags.proto = android.GetProtoFlags(ctx, p)
  89. if String(p.Proto.Plugin) == "" {
  90. var typeToPlugin string
  91. switch String(p.Proto.Type) {
  92. case "stream":
  93. flags.proto.OutTypeFlag = "--javastream_out"
  94. typeToPlugin = "javastream"
  95. case "micro":
  96. flags.proto.OutTypeFlag = "--javamicro_out"
  97. typeToPlugin = "javamicro"
  98. case "nano":
  99. flags.proto.OutTypeFlag = "--javanano_out"
  100. typeToPlugin = "javanano"
  101. case "lite", "":
  102. flags.proto.OutTypeFlag = "--java_out"
  103. flags.proto.OutParams = append(flags.proto.OutParams, "lite")
  104. case "full":
  105. flags.proto.OutTypeFlag = "--java_out"
  106. default:
  107. ctx.PropertyErrorf("proto.type", "unknown proto type %q",
  108. String(p.Proto.Type))
  109. }
  110. if typeToPlugin != "" {
  111. hostTool := ctx.Config().HostToolPath(ctx, "protoc-gen-"+typeToPlugin)
  112. flags.proto.Deps = append(flags.proto.Deps, hostTool)
  113. flags.proto.Flags = append(flags.proto.Flags, "--plugin=protoc-gen-"+typeToPlugin+"="+hostTool.String())
  114. }
  115. }
  116. flags.proto.OutParams = append(flags.proto.OutParams, j.Proto.Output_params...)
  117. return flags
  118. }
  119. type protoAttributes struct {
  120. Deps bazel.LabelListAttribute
  121. Sdk_version bazel.StringAttribute
  122. Java_version bazel.StringAttribute
  123. }
  124. func bp2buildProto(ctx android.Bp2buildMutatorContext, m *Module, protoSrcs bazel.LabelListAttribute) *bazel.Label {
  125. protoInfo, ok := android.Bp2buildProtoProperties(ctx, &m.ModuleBase, protoSrcs)
  126. if !ok {
  127. return nil
  128. }
  129. typ := proptools.StringDefault(protoInfo.Type, protoTypeDefault)
  130. var rule_class string
  131. suffix := "_java_proto"
  132. switch typ {
  133. case "nano":
  134. suffix += "_nano"
  135. rule_class = "java_nano_proto_library"
  136. case "micro":
  137. suffix += "_micro"
  138. rule_class = "java_micro_proto_library"
  139. case "lite":
  140. suffix += "_lite"
  141. rule_class = "java_lite_proto_library"
  142. case "stream":
  143. suffix += "_stream"
  144. rule_class = "java_stream_proto_library"
  145. case "full":
  146. rule_class = "java_proto_library"
  147. default:
  148. ctx.PropertyErrorf("proto.type", "cannot handle conversion at this time: %q", typ)
  149. }
  150. protoLabel := bazel.Label{Label: ":" + m.Name() + "_proto"}
  151. protoAttrs := &protoAttributes{
  152. Deps: bazel.MakeSingleLabelListAttribute(protoLabel),
  153. Java_version: bazel.StringAttribute{Value: m.properties.Java_version},
  154. Sdk_version: bazel.StringAttribute{Value: m.deviceProperties.Sdk_version},
  155. }
  156. name := m.Name() + suffix
  157. ctx.CreateBazelTargetModule(
  158. bazel.BazelTargetModuleProperties{
  159. Rule_class: rule_class,
  160. Bzl_load_location: "//build/bazel/rules/java:proto.bzl",
  161. },
  162. android.CommonAttributes{Name: name},
  163. protoAttrs)
  164. return &bazel.Label{Label: ":" + name}
  165. }