blob: e27ef2c4115e30cb0d3d7c12297ab3777f98a666 [file] [log] [blame]
Colin Cross6af17aa2017-09-20 12:59:05 -07001// 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
15package java
16
17import (
Colin Cross9516a6c2019-06-14 18:51:12 -070018 "path/filepath"
19 "strconv"
20
Colin Cross6af17aa2017-09-20 12:59:05 -070021 "android/soong/android"
Sam Delmericoc7681022022-02-04 21:01:20 +000022)
23
24const (
25 protoTypeDefault = "lite"
Colin Cross6af17aa2017-09-20 12:59:05 -070026)
27
Colin Cross9516a6c2019-06-14 18:51:12 -070028func genProto(ctx android.ModuleContext, protoFiles android.Paths, flags android.ProtoFlags) android.Paths {
29 // Shard proto files into groups of 100 to avoid having to recompile all of them if one changes and to avoid
30 // hitting command line length limits.
Colin Cross93fa5ef2021-10-18 10:19:03 -070031 shards := android.ShardPaths(protoFiles, 50)
Dan Willemsenab9f4262018-02-14 13:58:34 -080032
Colin Cross9516a6c2019-06-14 18:51:12 -070033 srcJarFiles := make(android.Paths, 0, len(shards))
Colin Cross6af17aa2017-09-20 12:59:05 -070034
Colin Cross9516a6c2019-06-14 18:51:12 -070035 for i, shard := range shards {
36 srcJarFile := android.PathForModuleGen(ctx, "proto", "proto"+strconv.Itoa(i)+".srcjar")
37 srcJarFiles = append(srcJarFiles, srcJarFile)
Colin Cross19878da2019-03-28 14:45:07 -070038
Colin Cross9516a6c2019-06-14 18:51:12 -070039 outDir := srcJarFile.ReplaceExtension(ctx, "tmp")
Colin Cross19878da2019-03-28 14:45:07 -070040
Colin Crossf1a035e2020-11-16 17:32:30 -080041 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross19878da2019-03-28 14:45:07 -070042
Colin Cross9516a6c2019-06-14 18:51:12 -070043 rule.Command().Text("rm -rf").Flag(outDir.String())
44 rule.Command().Text("mkdir -p").Flag(outDir.String())
Colin Cross19878da2019-03-28 14:45:07 -070045
Colin Cross9516a6c2019-06-14 18:51:12 -070046 for _, protoFile := range shard {
47 depFile := srcJarFile.InSameDir(ctx, protoFile.String()+".d")
48 rule.Command().Text("mkdir -p").Flag(filepath.Dir(depFile.String()))
Colin Crossf1a035e2020-11-16 17:32:30 -080049 android.ProtoRule(rule, protoFile, flags, flags.Deps, outDir, depFile, nil)
Colin Cross9516a6c2019-06-14 18:51:12 -070050 }
Colin Cross19878da2019-03-28 14:45:07 -070051
Colin Cross9516a6c2019-06-14 18:51:12 -070052 // Proto generated java files have an unknown package name in the path, so package the entire output directory
53 // into a srcjar.
54 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -080055 BuiltTool("soong_zip").
Colin Crosscf02ec82020-12-23 17:13:16 -080056 Flag("-srcjar").
Colin Cross9516a6c2019-06-14 18:51:12 -070057 Flag("-write_if_changed").
58 FlagWithOutput("-o ", srcJarFile).
59 FlagWithArg("-C ", outDir.String()).
60 FlagWithArg("-D ", outDir.String())
Dan Willemsenab9f4262018-02-14 13:58:34 -080061
Colin Cross9516a6c2019-06-14 18:51:12 -070062 rule.Command().Text("rm -rf").Flag(outDir.String())
63
64 rule.Restat()
65
66 ruleName := "protoc"
67 ruleDesc := "protoc"
68 if len(shards) > 1 {
69 ruleName += "_" + strconv.Itoa(i)
70 ruleDesc += " " + strconv.Itoa(i)
71 }
72
Colin Crossf1a035e2020-11-16 17:32:30 -080073 rule.Build(ruleName, ruleDesc)
Colin Cross9516a6c2019-06-14 18:51:12 -070074 }
75
76 return srcJarFiles
Colin Cross6af17aa2017-09-20 12:59:05 -070077}
78
79func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) {
Sam Delmerico9f047d92022-02-03 20:27:31 +000080 const unspecifiedProtobufPluginType = ""
Colin Crossfe17f6f2019-03-28 19:30:56 -070081 if String(p.Proto.Plugin) == "" {
82 switch String(p.Proto.Type) {
Sam Delmerico9f047d92022-02-03 20:27:31 +000083 case "stream": // does not require additional dependencies
Colin Crossfe17f6f2019-03-28 19:30:56 -070084 case "micro":
85 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-micro")
86 case "nano":
87 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-nano")
Sam Delmerico9f047d92022-02-03 20:27:31 +000088 case "lite", unspecifiedProtobufPluginType:
Colin Crossfe17f6f2019-03-28 19:30:56 -070089 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-lite")
90 case "full":
Liz Kammer3bf97bd2022-04-26 09:38:20 -040091 if ctx.Host() {
Colin Crossfe17f6f2019-03-28 19:30:56 -070092 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-full")
93 } else {
94 ctx.PropertyErrorf("proto.type", "full java protos only supported on the host")
95 }
96 default:
97 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
98 String(p.Proto.Type))
Colin Cross6af17aa2017-09-20 12:59:05 -070099 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700100 }
101}
102
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -0800103func protoFlags(ctx android.ModuleContext, j *CommonProperties, p *android.ProtoProperties,
Colin Cross0f2ee152017-12-14 15:22:43 -0800104 flags javaBuilderFlags) javaBuilderFlags {
105
Colin Cross19878da2019-03-28 14:45:07 -0700106 flags.proto = android.GetProtoFlags(ctx, p)
107
Colin Crossfe17f6f2019-03-28 19:30:56 -0700108 if String(p.Proto.Plugin) == "" {
Colin Cross6f205092019-08-13 16:53:19 -0700109 var typeToPlugin string
Colin Crossfe17f6f2019-03-28 19:30:56 -0700110 switch String(p.Proto.Type) {
Joe Onorato83fdc942021-10-28 15:05:59 -0700111 case "stream":
112 flags.proto.OutTypeFlag = "--javastream_out"
113 typeToPlugin = "javastream"
Colin Crossfe17f6f2019-03-28 19:30:56 -0700114 case "micro":
115 flags.proto.OutTypeFlag = "--javamicro_out"
Colin Cross6f205092019-08-13 16:53:19 -0700116 typeToPlugin = "javamicro"
Colin Crossfe17f6f2019-03-28 19:30:56 -0700117 case "nano":
118 flags.proto.OutTypeFlag = "--javanano_out"
Colin Cross6ac04602019-08-13 16:54:20 -0700119 typeToPlugin = "javanano"
Colin Cross0f97ada2019-10-25 18:24:13 -0700120 case "lite", "":
Colin Crossfe17f6f2019-03-28 19:30:56 -0700121 flags.proto.OutTypeFlag = "--java_out"
122 flags.proto.OutParams = append(flags.proto.OutParams, "lite")
Colin Cross0f97ada2019-10-25 18:24:13 -0700123 case "full":
Colin Crossfe17f6f2019-03-28 19:30:56 -0700124 flags.proto.OutTypeFlag = "--java_out"
125 default:
126 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
127 String(p.Proto.Type))
128 }
Colin Cross6f205092019-08-13 16:53:19 -0700129
130 if typeToPlugin != "" {
131 hostTool := ctx.Config().HostToolPath(ctx, "protoc-gen-"+typeToPlugin)
132 flags.proto.Deps = append(flags.proto.Deps, hostTool)
133 flags.proto.Flags = append(flags.proto.Flags, "--plugin=protoc-gen-"+typeToPlugin+"="+hostTool.String())
134 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700135 }
Colin Crossd5dbfb72017-11-14 13:11:23 -0800136
Colin Cross19878da2019-03-28 14:45:07 -0700137 flags.proto.OutParams = append(flags.proto.OutParams, j.Proto.Output_params...)
Colin Crossd5dbfb72017-11-14 13:11:23 -0800138
Colin Cross6af17aa2017-09-20 12:59:05 -0700139 return flags
140}