proto.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. )
  20. func genProto(ctx android.ModuleContext, protoFiles android.Paths, flags android.ProtoFlags) android.Paths {
  21. // Shard proto files into groups of 100 to avoid having to recompile all of them if one changes and to avoid
  22. // hitting command line length limits.
  23. shards := android.ShardPaths(protoFiles, 50)
  24. srcJarFiles := make(android.Paths, 0, len(shards))
  25. for i, shard := range shards {
  26. srcJarFile := android.PathForModuleGen(ctx, "proto", "proto"+strconv.Itoa(i)+".srcjar")
  27. srcJarFiles = append(srcJarFiles, srcJarFile)
  28. outDir := srcJarFile.ReplaceExtension(ctx, "tmp")
  29. rule := android.NewRuleBuilder(pctx, ctx)
  30. rule.Command().Text("rm -rf").Flag(outDir.String())
  31. rule.Command().Text("mkdir -p").Flag(outDir.String())
  32. for _, protoFile := range shard {
  33. depFile := srcJarFile.InSameDir(ctx, protoFile.String()+".d")
  34. rule.Command().Text("mkdir -p").Flag(filepath.Dir(depFile.String()))
  35. android.ProtoRule(rule, protoFile, flags, flags.Deps, outDir, depFile, nil)
  36. }
  37. // Proto generated java files have an unknown package name in the path, so package the entire output directory
  38. // into a srcjar.
  39. rule.Command().
  40. BuiltTool("soong_zip").
  41. Flag("-srcjar").
  42. Flag("-write_if_changed").
  43. FlagWithOutput("-o ", srcJarFile).
  44. FlagWithArg("-C ", outDir.String()).
  45. FlagWithArg("-D ", outDir.String())
  46. rule.Command().Text("rm -rf").Flag(outDir.String())
  47. rule.Restat()
  48. ruleName := "protoc"
  49. ruleDesc := "protoc"
  50. if len(shards) > 1 {
  51. ruleName += "_" + strconv.Itoa(i)
  52. ruleDesc += " " + strconv.Itoa(i)
  53. }
  54. rule.Build(ruleName, ruleDesc)
  55. }
  56. return srcJarFiles
  57. }
  58. func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) {
  59. const unspecifiedProtobufPluginType = ""
  60. if String(p.Proto.Plugin) == "" {
  61. switch String(p.Proto.Type) {
  62. case "stream": // does not require additional dependencies
  63. case "micro":
  64. ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-micro")
  65. case "nano":
  66. ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-nano")
  67. case "lite", unspecifiedProtobufPluginType:
  68. ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-lite")
  69. case "full":
  70. if ctx.Host() || ctx.BazelConversionMode() {
  71. ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-full")
  72. } else {
  73. ctx.PropertyErrorf("proto.type", "full java protos only supported on the host")
  74. }
  75. default:
  76. ctx.PropertyErrorf("proto.type", "unknown proto type %q",
  77. String(p.Proto.Type))
  78. }
  79. }
  80. }
  81. func protoFlags(ctx android.ModuleContext, j *CommonProperties, p *android.ProtoProperties,
  82. flags javaBuilderFlags) javaBuilderFlags {
  83. flags.proto = android.GetProtoFlags(ctx, p)
  84. if String(p.Proto.Plugin) == "" {
  85. var typeToPlugin string
  86. switch String(p.Proto.Type) {
  87. case "stream":
  88. flags.proto.OutTypeFlag = "--javastream_out"
  89. typeToPlugin = "javastream"
  90. case "micro":
  91. flags.proto.OutTypeFlag = "--javamicro_out"
  92. typeToPlugin = "javamicro"
  93. case "nano":
  94. flags.proto.OutTypeFlag = "--javanano_out"
  95. typeToPlugin = "javanano"
  96. case "lite", "":
  97. flags.proto.OutTypeFlag = "--java_out"
  98. flags.proto.OutParams = append(flags.proto.OutParams, "lite")
  99. case "full":
  100. flags.proto.OutTypeFlag = "--java_out"
  101. default:
  102. ctx.PropertyErrorf("proto.type", "unknown proto type %q",
  103. String(p.Proto.Type))
  104. }
  105. if typeToPlugin != "" {
  106. hostTool := ctx.Config().HostToolPath(ctx, "protoc-gen-"+typeToPlugin)
  107. flags.proto.Deps = append(flags.proto.Deps, hostTool)
  108. flags.proto.Flags = append(flags.proto.Flags, "--plugin=protoc-gen-"+typeToPlugin+"="+hostTool.String())
  109. }
  110. }
  111. flags.proto.OutParams = append(flags.proto.OutParams, j.Proto.Output_params...)
  112. return flags
  113. }