blob: 8d2380322f3f7a62d3792d25779b84fa15c03123 [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"
22)
23
Colin Cross9516a6c2019-06-14 18:51:12 -070024func genProto(ctx android.ModuleContext, protoFiles android.Paths, flags android.ProtoFlags) android.Paths {
25 // Shard proto files into groups of 100 to avoid having to recompile all of them if one changes and to avoid
26 // hitting command line length limits.
Colin Cross93fa5ef2021-10-18 10:19:03 -070027 shards := android.ShardPaths(protoFiles, 50)
Dan Willemsenab9f4262018-02-14 13:58:34 -080028
Colin Cross9516a6c2019-06-14 18:51:12 -070029 srcJarFiles := make(android.Paths, 0, len(shards))
Colin Cross6af17aa2017-09-20 12:59:05 -070030
Colin Cross9516a6c2019-06-14 18:51:12 -070031 for i, shard := range shards {
32 srcJarFile := android.PathForModuleGen(ctx, "proto", "proto"+strconv.Itoa(i)+".srcjar")
33 srcJarFiles = append(srcJarFiles, srcJarFile)
Colin Cross19878da2019-03-28 14:45:07 -070034
Colin Cross9516a6c2019-06-14 18:51:12 -070035 outDir := srcJarFile.ReplaceExtension(ctx, "tmp")
Colin Cross19878da2019-03-28 14:45:07 -070036
Colin Crossf1a035e2020-11-16 17:32:30 -080037 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross19878da2019-03-28 14:45:07 -070038
Colin Cross9516a6c2019-06-14 18:51:12 -070039 rule.Command().Text("rm -rf").Flag(outDir.String())
40 rule.Command().Text("mkdir -p").Flag(outDir.String())
Colin Cross19878da2019-03-28 14:45:07 -070041
Colin Cross9516a6c2019-06-14 18:51:12 -070042 for _, protoFile := range shard {
43 depFile := srcJarFile.InSameDir(ctx, protoFile.String()+".d")
44 rule.Command().Text("mkdir -p").Flag(filepath.Dir(depFile.String()))
Colin Crossf1a035e2020-11-16 17:32:30 -080045 android.ProtoRule(rule, protoFile, flags, flags.Deps, outDir, depFile, nil)
Colin Cross9516a6c2019-06-14 18:51:12 -070046 }
Colin Cross19878da2019-03-28 14:45:07 -070047
Colin Cross9516a6c2019-06-14 18:51:12 -070048 // Proto generated java files have an unknown package name in the path, so package the entire output directory
49 // into a srcjar.
50 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -080051 BuiltTool("soong_zip").
Colin Crosscf02ec82020-12-23 17:13:16 -080052 Flag("-srcjar").
Colin Cross9516a6c2019-06-14 18:51:12 -070053 Flag("-write_if_changed").
54 FlagWithOutput("-o ", srcJarFile).
55 FlagWithArg("-C ", outDir.String()).
56 FlagWithArg("-D ", outDir.String())
Dan Willemsenab9f4262018-02-14 13:58:34 -080057
Colin Cross9516a6c2019-06-14 18:51:12 -070058 rule.Command().Text("rm -rf").Flag(outDir.String())
59
60 rule.Restat()
61
62 ruleName := "protoc"
63 ruleDesc := "protoc"
64 if len(shards) > 1 {
65 ruleName += "_" + strconv.Itoa(i)
66 ruleDesc += " " + strconv.Itoa(i)
67 }
68
Colin Crossf1a035e2020-11-16 17:32:30 -080069 rule.Build(ruleName, ruleDesc)
Colin Cross9516a6c2019-06-14 18:51:12 -070070 }
71
72 return srcJarFiles
Colin Cross6af17aa2017-09-20 12:59:05 -070073}
74
75func protoDeps(ctx android.BottomUpMutatorContext, p *android.ProtoProperties) {
Sam Delmerico9f047d92022-02-03 20:27:31 +000076 const unspecifiedProtobufPluginType = ""
Colin Crossfe17f6f2019-03-28 19:30:56 -070077 if String(p.Proto.Plugin) == "" {
78 switch String(p.Proto.Type) {
Sam Delmerico9f047d92022-02-03 20:27:31 +000079 case "stream": // does not require additional dependencies
Colin Crossfe17f6f2019-03-28 19:30:56 -070080 case "micro":
81 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-micro")
82 case "nano":
83 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-nano")
Sam Delmerico9f047d92022-02-03 20:27:31 +000084 case "lite", unspecifiedProtobufPluginType:
Colin Crossfe17f6f2019-03-28 19:30:56 -070085 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-lite")
86 case "full":
Liz Kammer356f7d42021-01-26 09:18:53 -050087 if ctx.Host() || ctx.BazelConversionMode() {
Colin Crossfe17f6f2019-03-28 19:30:56 -070088 ctx.AddVariationDependencies(nil, staticLibTag, "libprotobuf-java-full")
89 } else {
90 ctx.PropertyErrorf("proto.type", "full java protos only supported on the host")
91 }
92 default:
93 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
94 String(p.Proto.Type))
Colin Cross6af17aa2017-09-20 12:59:05 -070095 }
Colin Cross6af17aa2017-09-20 12:59:05 -070096 }
97}
98
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080099func protoFlags(ctx android.ModuleContext, j *CommonProperties, p *android.ProtoProperties,
Colin Cross0f2ee152017-12-14 15:22:43 -0800100 flags javaBuilderFlags) javaBuilderFlags {
101
Colin Cross19878da2019-03-28 14:45:07 -0700102 flags.proto = android.GetProtoFlags(ctx, p)
103
Colin Crossfe17f6f2019-03-28 19:30:56 -0700104 if String(p.Proto.Plugin) == "" {
Colin Cross6f205092019-08-13 16:53:19 -0700105 var typeToPlugin string
Colin Crossfe17f6f2019-03-28 19:30:56 -0700106 switch String(p.Proto.Type) {
Joe Onorato83fdc942021-10-28 15:05:59 -0700107 case "stream":
108 flags.proto.OutTypeFlag = "--javastream_out"
109 typeToPlugin = "javastream"
Colin Crossfe17f6f2019-03-28 19:30:56 -0700110 case "micro":
111 flags.proto.OutTypeFlag = "--javamicro_out"
Colin Cross6f205092019-08-13 16:53:19 -0700112 typeToPlugin = "javamicro"
Colin Crossfe17f6f2019-03-28 19:30:56 -0700113 case "nano":
114 flags.proto.OutTypeFlag = "--javanano_out"
Colin Cross6ac04602019-08-13 16:54:20 -0700115 typeToPlugin = "javanano"
Colin Cross0f97ada2019-10-25 18:24:13 -0700116 case "lite", "":
Colin Crossfe17f6f2019-03-28 19:30:56 -0700117 flags.proto.OutTypeFlag = "--java_out"
118 flags.proto.OutParams = append(flags.proto.OutParams, "lite")
Colin Cross0f97ada2019-10-25 18:24:13 -0700119 case "full":
Colin Crossfe17f6f2019-03-28 19:30:56 -0700120 flags.proto.OutTypeFlag = "--java_out"
121 default:
122 ctx.PropertyErrorf("proto.type", "unknown proto type %q",
123 String(p.Proto.Type))
124 }
Colin Cross6f205092019-08-13 16:53:19 -0700125
126 if typeToPlugin != "" {
127 hostTool := ctx.Config().HostToolPath(ctx, "protoc-gen-"+typeToPlugin)
128 flags.proto.Deps = append(flags.proto.Deps, hostTool)
129 flags.proto.Flags = append(flags.proto.Flags, "--plugin=protoc-gen-"+typeToPlugin+"="+hostTool.String())
130 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700131 }
Colin Crossd5dbfb72017-11-14 13:11:23 -0800132
Colin Cross19878da2019-03-28 14:45:07 -0700133 flags.proto.OutParams = append(flags.proto.OutParams, j.Proto.Output_params...)
Colin Crossd5dbfb72017-11-14 13:11:23 -0800134
Colin Cross6af17aa2017-09-20 12:59:05 -0700135 return flags
136}